DarkForte / LowPoly

15-618 Final Project: Transforming an image to low poly style
14 stars 3 forks source link

Setup guide for OSX 10.14 #1

Open Ariel-Rodriguez opened 5 years ago

Ariel-Rodriguez commented 5 years ago

Hello! First of all thank you so much for sharing!

I'd like to share my experience about running this project in my Macbook OSX 10.14. As JS developer it was a nice challenge to compile and run :D so i am here to share my experience. So hope of utility for others

Cuda Toolkit 8.0 (even if you don't own NVIDIA in your system)

Add CUDA PATH to your .bashrc or .zshrc

export PATH=/Developer/NVIDIA/CUDA-8.0/bin${PATH:+:${PATH}}
export DYLD_LIBRARY_PATH=/Developer/NVIDIA/CUDA-8.0/lib\
 ${DYLD_LIBRARY_PATH:+:${DYLD_LIBRARY_PATH}}

Downgrade OSX Command Line Tools

Make sure you have xcode basics installed

xcode-select install

Download CMT Mount DMG, Install the PKG

Get CMT from apple old archives: Download CLT for XCode 8.2 Dec 2016

Activate it (downgrade)
 sudo xcode-select --switch /Library/Developer/CommandLineTools

Later you may want to revert this change, if you want/can/must restore back to defaults once you finish with this project sudo xcode-select --reset

Install Opencv@3

 brew install opencv@3

Add paths to your bash profile

export PKG_CONFIG_PATH=/usr/local/opt/opencv@3/lib/pkgconfig

Configure opencv3:

pkg-config --cflags --libs opencv

Clone repository and edit CMakeLists.txt

I removed -gencode arch=compute_61,code=sm_61 NVCC_FLAG and included opencv directory
cmake_minimum_required (VERSION 2.8)

set (OPENCV_PATH "/usr/local/opt/opencv@3")

project ("LowPoly")

FIND_PACKAGE(CUDA REQUIRED)

set(CUDA_SEPARABLE_COMPILATION ON)

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -std=c++11")
SET (CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};-O3;--std=c++11;")

message(${CUDA_NVCC_FLAGS})

include_directories(${OPENCV_PATH}/include)

# Include sub-projects.
add_subdirectory ("src")

Edit src/CMakeLists.txt

Find line with LINK_DIRECTORIES and change to follow

LINK_DIRECTORIES(/usr/local/opt/opencv@3/lib)

PATCH CUDA TOOLKIT LIBRARY

I had compilation errors at cuda library, you can fix and patch manually by your self: Edit as sudo following file

/Developer/NVIDIA/CUDA-8.0/include/thrust/system/cuda/detail/cub/util_ptx.cuh

Change lines

Build with CLion

I found very useful IDE CLion for OSX, as JS dev it was easy to load CMakeLists.txt and rebuild.

Run

./cmake-build-debug/LowPoly -c -i test.jpg -t 30.0 -b 0.2 -e 10e-3 -n 10e-4

Questions:

@DarkForte @FredHuangBia can you give an example how to save Edge Detection and Selected vertices output as image?

Troubleshooting

This is how my env looks like

/Library/Developer/CommandLineTools/usr/bin/c++ --version
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
/usr/bin/make --version                                  
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for i386-apple-darwin11.3.0
nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Tue_Jan_10_13:22:46_CST_2017
Cuda compilation tools, release 8.0, V8.0.61
FredHuangBia commented 5 years ago

Hey, thanks for sharing your experience. To save intermediate results, please look into "cvutil.h", there are helper functions to draw intermediate results. Such as "drawTriangleLineOnImg", "drawTriangle", and "drawVoronoi". They all return a cv::Mat type and you can then call "cv::imwrite" to save it as an image. For example:

cv::Mat triLine = drawTriangleLineOnImg(triangles, voronoi);
cv::imwrite("triangle_lines.png", triLine);

As for edge detection and selected points, I didn't write helper functions for them. You can refer to the following code. Here the "vertices" and "grad" are the just these two variables in LowPoly.cpp.

// write selected points to img
cv::Mat pts = cv::Mat(rows, cols, CV_32F, 0.0);
for (int i = 0; i < numVertices; i++)
{
    int row = vertices[i].y;
    int col = vertices[i].x;
    pts.at<float>(row, col) = 255.0;
}
cv::imwrite("points.png", pts);

// write edge detection result to img
cv::Mat edges = grad * 255.0;
cv::imwrite("edges.png", edges);
DarkForte commented 5 years ago

@Ariel-Rodriguez Thanks a lot for sharing your experience!

As you see, this is actually a class project. We were working on a Linux machine in our lab that had CUDA and everything installed, and the README is basically a project report, so we did not mention how to compile and run it. It is actually strange to me that CUDA 9.0 and 10.0 do not work on it. I will check the CUDA version on our machine later on.

Besides, the reason that we linked opencv rudely is that we could not install it on our lab machine without root privilege. So we had to copy the binaries to a specific folder and link to them.

Your comments will be extremely helpful for those who want to try it out!

Ariel-Rodriguez commented 5 years ago

Hey @DarkForte you welcome!

Thanks for thee feedback. There is no rush to check CUDA version. Just in case, if you are interested, here is the output when i use CUDA 10.1

/Developer/NVIDIA/CUDA-10.1/include/thrust/system/cuda/detail/cub/block/specializations/../../warp/specializations/warp_reduce_shfl.cuh(528): error: calling a __host__ function("std::__1::__clz") from a __device__ function("thrust::cuda_cub::cub::TilePrefixCallbackOp<int,  ::thrust::plus<int> ,  ::thrust::cuda_cub::cub::ScanTileState<int, (bool)1> , (int)300> ::operator ()") is not allowed

/Developer/NVIDIA/CUDA-10.1/include/thrust/system/cuda/detail/cub/block/specializations/../../warp/specializations/warp_reduce_shfl.cuh(528): error: identifier "std::__1::__clz" is undefined in device code

/Developer/NVIDIA/CUDA-10.1/include/thrust/system/cuda/detail/cub/block/specializations/../../warp/specializations/warp_reduce_shfl.cuh(528): error: calling a __host__ function("std::__1::__clz") from a __device__ function("thrust::cuda_cub::cub::TilePrefixCallbackOp<int,  ::thrust::plus<int> ,  ::thrust::cuda_cub::cub::ScanTileState<int, (bool)1> , (int)300> ::operator ()") is not allowed

/Developer/NVIDIA/CUDA-10.1/include/thrust/system/cuda/detail/cub/block/specializations/../../warp/specializations/warp_reduce_shfl.cuh(528): error: identifier "std::__1::__clz" is undefined in device code

4 errors detected in the compilation of "/var/folders/qt/_kpg344d58b6mzh8whj37hl40000gp/T//tmpxft_00007a40_00000000-6_delauneyGPU.cpp1.ii".
CMake Error at LowPoly_generated_delauneyGPU.cu.o.Debug.cmake:279 (message):
  Error generating file
  /Users/arielrodriguez/LowPoly/cmake-build-debug/src/CMakeFiles/LowPoly.dir//./LowPoly_generated_delauneyGPU.cu.o

nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Fri_Feb__8_19:08:19_PST_2019
Cuda compilation tools, release 10.1, V10.1.105

BTW: Do you have any other demo using GPU similar to this sample technique?https://www.learnopencv.com/non-photorealistic-rendering-using-opencv-python-c/

Note: I had to add this CMake option variable -DCUDA_CUDART_LIBRARY=/Developer/NVIDIA/CUDA-10.1/lib/libcudart.10.1.dylib