Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WSL fix & missing dependencies #130

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,25 @@ add_subdirectory(TMOTai08/)
add_subdirectory(TMONguy15/) #Matej Valek


INSTALL(FILES tmolib/libtmo.so DESTINATION lib)
INSTALL(FILES TMOgui/tmogui PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE DESTINATION . )
INSTALL(FILES TMOCmd/tmocmd PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE DESTINATION . )
INSTALL(FILES TMOCmd/ward94.tml DESTINATION . )
#this should fix the problem with installation on WSL
IF(UNIX)
execute_process(COMMAND "wsl.exe" "echo" "test" RESULT_VARIABLE WSL_RESULT)
if (WSL_RESULT EQUAL 0)
message(STATUS "WSL detected")
INSTALL(FILES build/tmolib/libtmo.so DESTINATION lib)
INSTALL(FILES build/TMOgui/tmogui PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE DESTINATION . )
INSTALL(FILES build/TMOCmd/tmocmd PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE DESTINATION . )
INSTALL(FILES build/TMOCmd/ward94.tml DESTINATION . )
else()
message(STATUS "WSL not detected")
INSTALL(FILES tmolib/libtmo.so DESTINATION lib)
INSTALL(FILES TMOgui/tmogui PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE DESTINATION . )
INSTALL(FILES TMOCmd/tmocmd PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE DESTINATION . )
INSTALL(FILES TMOCmd/ward94.tml DESTINATION . )
endif()
ELSE()
INSTALL(FILES tmolib/libtmo.so DESTINATION lib)
INSTALL(FILES TMOgui/tmogui PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE DESTINATION . )
INSTALL(FILES TMOCmd/tmocmd PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE DESTINATION . )
INSTALL(FILES TMOCmd/ward94.tml DESTINATION . )
ENDIF(UNIX)
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,28 @@ You need at least these libraries:
- CGAL http://www.cgal.org/
- OpenCV https://opencv.org/
- NLopt https://nlopt.readthedocs.io/
- Eigen https://eigen.tuxfamily.org/

and for GUI:
- Qt5 https://www.qt.io/offline-installers


On Debian/Ubuntu these are provided by packages:
- libopenexr-dev
- libtiff4-dev
- libtiff5-dev
- libeigen3-dev
- libjpeg-dev
- qt5-default
- qt5-doc
- qttools5-dev-tools
- libfftw3-3
- libboost-all-dev
- libopencv-dev


On Ubuntu, you should install the following packages as follows:
```
sudo apt-get install libopenexr-dev libtiff5-dev libeigen3-dev libjpeg-dev qt5-default qt5-doc qttools5-dev-tools libfftw3-3 libboost-all-dev libopencv-dev
```

On Fedora, you should install the following packages as follows:

Expand Down
58 changes: 58 additions & 0 deletions TMOJah06/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.4.8)
SET(CMAKE_COLOR_MAKEFILE ON)
SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(CMAKE_INCLUDE_CURRENT_DIR TRUE)

IF(UNIX AND NOT LINUX_SET)
ADD_DEFINITIONS(-D LINUX)
ENDIF(UNIX AND NOT LINUX_SET)

# Find OpenCV
find_package(OpenCV REQUIRED)

cmake_policy(SET CMP0012 NEW)
cmake_policy(SET CMP0079 NEW)

# Find Torch
find_package(Torch 2.0.0 REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
target_link_libraries(tmocmd "${TORCH_LIBRARIES}")
set_property(TARGET tmocmd PROPERTY CXX_STANDARD 14)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

INCLUDE_DIRECTORIES(
${OpenCV_INCLUDE_DIRS}
${TORCH_INCLUDE_DIRS}
../tmolib/
.
)

LINK_LIBRARIES (
${OpenCV_LIBS}
${TORCH_LIBRARIES}
tmo
)

SET(TMO_SOURCES
TMOJah06.cpp
TMOPlugin.cpp
)


SET(TMOWARD_HEADERS
TMOJah06.h
TMOPlugin.h
)


add_library( jah06 SHARED ${TMO_SOURCES} )
add_custom_command( TARGET jah06 POST_BUILD
COMMAND cp -f libjah06.so ../TMOCmd/jah06.tml
COMMAND cp -f libjah06.so ../TMOgui/jah06.tml
COMMAND cp -f libjah06.so ../jah06.tml
COMMAND cp -f ../../TMOJah06/model.pt ../model.pt
# COMMAND rm -f libjah06.so
)


152 changes: 152 additions & 0 deletions TMOJah06/TMOJah06.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/* --------------------------------------------------------------------------- *
* TMOJah06.cpp: implementation of the TMOJah06 class. *
* --------------------------------------------------------------------------- */

#include "TMOJah06.h"

/* --------------------------------------------------------------------------- *
* Constructor serves for describing a technique and input parameters *
* --------------------------------------------------------------------------- */
TMOJah06::TMOJah06()
{
SetName(L"Jah06");
SetDescription(L"apply style transfer using neural network");

/*
dPath.SetName(L"p");
dPath.SetDescription(L"Path to torchscript model");
dPath.SetDefault("model.pt");
this->Register(dPath);
*/
//cv::useOptimized();
}

TMOJah06::~TMOJah06()
{
}

cv::Mat TMOImage2Mat(TMOImage* pSrc)
{
double* pSourceData;
int rowsCnt, colsCnt;

pSourceData = pSrc->GetData();
rowsCnt = pSrc->GetHeight();
colsCnt = pSrc->GetWidth();

cv::Mat srcConvMat(rowsCnt, colsCnt, CV_64FC3);

if (srcConvMat.isContinuous())
{
colsCnt *= rowsCnt;
rowsCnt = 1;
}
int chCnt = 3;
for (int y = 0; y<rowsCnt; y++)
{
for (int x = 0; x<colsCnt; x++)
{
for (int c = 0; c<chCnt; c++)
{
srcConvMat.at<double>(y, x*chCnt+c) = pSourceData[chCnt-c-1];
}

/* Add count of channels (RGB) to pointer */
pSourceData += chCnt;
}
}

return srcConvMat;
}

// Function to load the PyTorch model
torch::jit::script::Module loadModel(const std::string& modelPath) {
torch::jit::script::Module module;
try {
// Load the model
module = torch::jit::load(modelPath);
}
catch (const c10::Error& e) {
std::cerr<<"Error loading the model: "<<e.what()<<std::endl;
exit(1);
}
return module;
}

// Function to preprocess the input image
torch::Tensor preprocessImage(const cv::Mat& img) {
cv::Mat floatImage;
img.convertTo(floatImage, CV_32FC3, 1.0/255.0);

//convert to tensor
torch::Tensor tensor = torch::from_blob(floatImage.data, { 1, floatImage.rows, floatImage.cols, 3 }, torch::kFloat32);
tensor = tensor.permute({ 0, 3, 1, 2 }); // Transpose dimensions from [1, H, W, C] to [1, C, H, W]

return tensor;
}

cv::Mat tensorToImage(const torch::Tensor& tensor) {
// Remove the batch_size dimension
torch::Tensor squeezed = tensor.squeeze();

// Convert tensor to CPU and extract data as a pointer
torch::Tensor cpuTensor = squeezed.to(torch::kCPU);
float* dataPtr = cpuTensor.data_ptr<float>();

// Get the dimensions of the tensor
int64_t channels = cpuTensor.size(0);
int64_t height = cpuTensor.size(1);
int64_t width = cpuTensor.size(2);

// Create an OpenCV Mat with the same dimensions
cv::Mat image(height, width, CV_32FC(channels));

// Copy the data from the tensor to the Mat
std::memcpy(image.data, dataPtr, channels*height*width*sizeof(float));

// Transpose from [C, H, W] -> [H, W, C]
cv::Mat transposedImage = image.t();

// Convert the image to the appropriate data type and scale
cv::Mat scaledImage;
transposedImage.convertTo(scaledImage, CV_8UC(channels));

// Return the final image
return scaledImage;
}
/* --------------------------------------------------------------------------- *
* This overloaded function is an implementation of your tone mapping operator *
* --------------------------------------------------------------------------- */
int TMOJah06::Transform()
{
// model loading
torch::jit::script::Module model = loadModel("model.pt");

// image load + preprocess
cv::Mat inputImage = TMOImage2Mat(pSrc);
torch::Tensor inputTensor = preprocessImage(inputImage);
inputTensor = inputTensor.to(torch::kCPU);

torch::Tensor generatedTensor = model.forward({ inputTensor }).toTensor();
cv::Mat generatedImage = tensorToImage(generatedTensor);


//image save
double* pDestinationData = pDst->GetData();

int chCnt = 3;
int y = 0;
for (y = 0; y<pSrc->GetHeight(); y++)
{
pSrc->ProgressBar(y, pSrc->GetHeight());
for (int x = 0; x<pSrc->GetWidth(); x++)
{
// Convert the pixel values to the appropriate data type
for (int c = 0; c<chCnt; c++) {
*pDestinationData++ = static_cast<double>(generatedImage.at<cv::Vec3f>(y, x)[chCnt-c-1]);
}
}
}
pSrc->ProgressBar(y, pSrc->GetHeight());
return 0;
}
15 changes: 15 additions & 0 deletions TMOJah06/TMOJah06.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "TMO.h"
#include <torch/torch.h>
#include <torch/script.h>
#include <opencv2/opencv.hpp>

class TMOJah06 : public TMO
{
public:
TMOJah06();
virtual ~TMOJah06();
virtual int Transform();

protected:
TMODouble dParameter;
};
80 changes: 80 additions & 0 deletions TMOJah06/TMOPlugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* -------------------------------------------------------------------- *
* TMOPlugin.cpp : Template for tone mapping operator plugin *
* in Tone Mapping Studio 2004 *
* -------------------------------------------------------------------- *
* *
* Put this file into a DLL project with your plugin functions and *
* replace commented sections below. *
* *
* Add this preprocesor definition to the project setings : *
* *
* TMOPLUGIN_EXPORTS *
* *
* -------------------------------------------------------------------- */
#include "./TMOPlugin.h"

/* -------------------------------------------------------------------- *
* Insert your operator header below *
* -------------------------------------------------------------------- */
#include "./TMOJah06.h"

/* -------------------------------------------------------------------- *
* Insert a number of implemented operators *
* -------------------------------------------------------------------- */
int iOperatorCount = 1;

/* -------------------------------------------------------------------- *
* DLL Entry point; no changes necessary *
* -------------------------------------------------------------------- */
/*
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
*/
/* -------------------------------------------------------------------- *
* Returns a number of implemented operators; no changes necessary *
* -------------------------------------------------------------------- */
int TMOPLUGIN_API OperatorCount()
{
return iOperatorCount;
}

/* -------------------------------------------------------------------- *
* For each implemented operator create a new object in field operators,*
* then return number of created operators *
* For exemple : *
* *
* operators[0] = new TMOOperator1; *
* operators[1] = new TMOOperator2; *
* . *
* . *
* . *
* -------------------------------------------------------------------- */
int TMOPLUGIN_API EnumOperators(TMO **operators)
{
operators[0] = new TMOJah06;
return iOperatorCount;
}

/* -------------------------------------------------------------------- *
* Deletes operators; no changes necessary *
* -------------------------------------------------------------------- */
int TMOPLUGIN_API DeleteOperators(TMO **operators)
{
int i;
for (i = 0; i < iOperatorCount; i++)
delete operators[i];
return iOperatorCount;
}
15 changes: 15 additions & 0 deletions TMOJah06/TMOPlugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "TMO.h"
/*
#ifdef TMOPLUGIN_EXPORTS
#define TMOPLUGIN_API __declspec(dllexport)
#else
#define TMOPLUGIN_API __declspec(dllimport)
#endif
#pragma warning (disable: 4251)
*/

#define TMOPLUGIN_API

extern "C" TMOPLUGIN_API int EnumOperators(TMO **operators);
extern "C" TMOPLUGIN_API int DeleteOperators(TMO **operators);
extern "C" TMOPLUGIN_API int OperatorCount();
Binary file added TMOJah06/model.pt
Binary file not shown.