{"id":617,"date":"2016-03-12T00:49:36","date_gmt":"2016-03-12T05:49:36","guid":{"rendered":"http:\/\/sunapi386.ca\/wordpress\/?p=617"},"modified":"2016-03-12T10:03:25","modified_gmt":"2016-03-12T15:03:25","slug":"neural-style","status":"publish","type":"post","link":"https:\/\/sunapi386.ca\/wordpress\/neural-style\/","title":{"rendered":"Neural Style"},"content":{"rendered":"<p>Inspiration comes from <a href=\"https:\/\/github.com\/jcjohnson\/neural-style\">https:\/\/github.com\/jcjohnson\/neural-style<\/a>.<br \/>\nBecause installing all the required toolchains on OS X 10.11.3 is a bit challenging, I here are my installation steps.<\/p>\n<pre> cd workspace\/\r\n git clone git@github.com:jcjohnson\/neural-style.git\r\n git clone https:\/\/github.com\/torch\/distro.git ~\/torch --recursive\r\n cd ~\/torch; bash install-deps;\r\n<\/pre>\n<p>This will fail a few times if you have already installed them (but different versions). I needed fiddle around unlinking things.<\/p>\n<pre> brew unlink qt\r\n brew linkapps qt\r\n brew link --overwrite wget\r\n bash install-deps;\r\n brew unlink cmake\r\n bash install-deps;\r\n brew unlink imagemagick\r\n brew unlink brew-cask\r\n bash install-deps;\r\n<\/pre>\n<p>Anyhow, make sure the install-deps script doesn&#8217;t error out, otherwise you&#8217;ll be missing dependencies.<\/p>\n<pre> .\/install.sh\r\n<\/pre>\n<p>This succeeds. It tells you to activate, but I&#8217;m using non-standard shell (fish shell), so I mess with the fish config.<\/p>\n<pre> . \/Users\/jason\/torch\/install\/bin\/torch-activate\r\n th #checking this exists in path, and it doesn't\r\n luarocks install image\r\n source ~\/.profile\r\n . ~\/.profile\r\n vim ~\/.bashrc\r\n subl \/Users\/jason\/torch\/install\/bin\/torch-activate\r\n subl ~\/.config\/fish\/config.fish\r\n th #now it exists\r\n<\/pre>\n<p>The change into fish.config that was necessary (for my user, my paths) was:<\/p>\n<pre># . \/Users\/jason\/torch\/install\/bin\/torch-activate\r\nset LUA_PATH '\/Users\/jason\/.luarocks\/share\/lua\/5.1\/?.lua;\/Users\/jason\/.luarocks\/share\/lua\/5.1\/?\/init.lua;\/Users\/jason\/torch\/install\/share\/lua\/5.1\/?.lua;\/Users\/jason\/torch\/install\/share\/lua\/5.1\/?\/init.lua;.\/?.lua;\/Users\/jason\/torch\/install\/share\/luajit-2.1.0-beta1\/?.lua;\/usr\/local\/share\/lua\/5.1\/?.lua;\/usr\/local\/share\/lua\/5.1\/?\/init.lua'\r\nset LUA_CPATH '\/Users\/jason\/.luarocks\/lib\/lua\/5.1\/?.so;\/Users\/jason\/torch\/install\/lib\/lua\/5.1\/?.so;.\/?.so;\/usr\/local\/lib\/lua\/5.1\/?.so;\/usr\/local\/lib\/lua\/5.1\/loadall.so'\r\nset PATH \/Users\/jason\/torch\/install\/bin $PATH\r\nset LD_LIBRARY_PATH \/Users\/jason\/torch\/install\/lib $LD_LIBRARY_PATH\r\nset DYLD_LIBRARY_PATH \/Users\/jason\/torch\/install\/lib $DYLD_LIBRARY_PATH\r\nset LUA_CPATH '\/Users\/jason\/torch\/install\/lib\/?.dylib;'$LUA_CPATH\r\n<\/pre>\n<p>Convert the bash syntax to fish syntax by replacing &#8220;export&#8221; with &#8220;set&#8221; and &#8220;:&#8221; with &#8221; &#8220;.<br \/>\nI have a Nvidia graphics card, so I download and install <a href=\"http:\/\/www.nvidia.com\/object\/cuda_home_new.html\">CUDA<\/a>.<\/p>\n<p><a href=\"https:\/\/sunapi386.ca\/wordpress\/wp-content\/uploads\/2016\/03\/Screen-Shot-2016-03-12-at-6.22.17-AM.png\" rel=\"attachment wp-att-625\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-625\" src=\"https:\/\/sunapi386.ca\/wordpress\/wp-content\/uploads\/2016\/03\/Screen-Shot-2016-03-12-at-6.22.17-AM-300x159.png\" alt=\"Cuda Preferences\" width=\"300\" height=\"159\" srcset=\"https:\/\/sunapi386.ca\/wordpress\/wp-content\/uploads\/2016\/03\/Screen-Shot-2016-03-12-at-6.22.17-AM-300x159.png 300w, https:\/\/sunapi386.ca\/wordpress\/wp-content\/uploads\/2016\/03\/Screen-Shot-2016-03-12-at-6.22.17-AM-768x407.png 768w, https:\/\/sunapi386.ca\/wordpress\/wp-content\/uploads\/2016\/03\/Screen-Shot-2016-03-12-at-6.22.17-AM-1024x542.png 1024w, https:\/\/sunapi386.ca\/wordpress\/wp-content\/uploads\/2016\/03\/Screen-Shot-2016-03-12-at-6.22.17-AM-624x330.png 624w, https:\/\/sunapi386.ca\/wordpress\/wp-content\/uploads\/2016\/03\/Screen-Shot-2016-03-12-at-6.22.17-AM.png 1560w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Then we can continue installing dependencies.<\/p>\n<pre> brew install protobuf\r\n luarocks install loadcaffe\r\n luarocks install torch\r\n luarocks install nn\r\n<\/pre>\n<p>I found that having Xcode 7 means the clang compiler is too new and not supported by cutorch and cunn. The error you would see is this:<\/p>\n<pre>nvcc fatal   : The version ('70002') of the host compiler ('Apple clang') is not supported\r\n<\/pre>\n<p>Sometimes the error messages are garbled. Concurrency build, I presume. I downloaded Xcode 6.4, and replaced my Xcode 7:<\/p>\n<pre>cd \/Applications\r\nsudo mv Xcode.app\/ Xcode7.app\r\nsudo mv Xcode\\ 2.app\/ Xcode.app # this is Xcode 6.4 when you install it\r\nsudo xcode-select -s \/Applications\/Xcode.app\/Contents\/Developer\r\nclang -v \r\n#Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)\r\n#Target: x86_64-apple-darwin15.3.0\r\n#Thread model: posix\r\n<\/pre>\n<p>But now I get another fatal issue:<\/p>\n<pre>\/usr\/local\/cuda\/include\/common_functions.h:65:10: fatal error: 'string.h' file not found\r\n#include \r\n<\/pre>\n<p>Seems like this is an issue people have, <a href=\"https:\/\/github.com\/torch\/cutorch\/issues\/241\">cutorch issue 241<\/a>. Can be resolved by doing<\/p>\n<pre>xcode-select --install\r\n<\/pre>\n<p>This gets me a little further, now the issue seems related to torch.<\/p>\n<pre>make[2]: *** No rule to make target `\/Applications\/Xcode.app\/Contents\/Developer\/Platforms\/MacOSX.platform\/Developer\/SDKs\/MacOSX10.11.sdk\/System\/Library\/Frameworks\/Accelerate.framework', needed by `lib\/THC\/libTHC.dylib'.  Stop.\r\nmake[2]: *** Waiting for unfinished jobs....\r\n[ 64%] Building C object lib\/THC\/CMakeFiles\/THC.dir\/THCGeneral.c.o\r\n[ 69%] Building C object lib\/THC\/CMakeFiles\/THC.dir\/THCAllocator.c.o\r\n[ 71%] Building C object lib\/THC\/CMakeFiles\/THC.dir\/THCStorage.c.o\r\n[ 76%] Building C object lib\/THC\/CMakeFiles\/THC.dir\/THCTensorCopy.c.o\r\n[ 76%] Building C object lib\/THC\/CMakeFiles\/THC.dir\/THCStorageCopy.c.o\r\n[ 76%] Building C object lib\/THC\/CMakeFiles\/THC.dir\/THCTensor.c.o\r\n\/tmp\/luarocks_cutorch-scm-1-3748\/cutorch\/lib\/THC\/THCGeneral.c:633:7: warning: absolute value function 'abs' given an\r\n      argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Wabsolute-value]\r\n  if (abs(state-&gt;heapDelta) &lt; heapMaxDelta) { ^ \/tmp\/luarocks_cutorch-scm-1-3748\/cutorch\/lib\/THC\/THCGeneral.c:633:7: note: use function 'labs' instead if (abs(state-&gt;heapDelta) &lt; heapMaxDelta) {\r\n      ^~~\r\n      labs\r\n1 warning generated.\r\nmake[1]: *** [lib\/THC\/CMakeFiles\/THC.dir\/all] Error 2\r\nmake: *** [all] Error 2\r\n\r\nError: Build error: Failed building.\r\n<\/pre>\n<p>Since I just updated to OS X 10.11, I presume frameworks in 10.10 should be ok. So this hack should be ok as well, to make Accelerate.framework appear.<\/p>\n<pre>ln -s \"\/Applications\/Xcode.app\/Contents\/Developer\/Platforms\/MacOSX.platform\/Developer\/SDKs\/MacOSX10.10.sdk\" \"\/Applications\/Xcode.app\/Contents\/Developer\/Platforms\/MacOSX.platform\/Developer\/SDKs\/MacOSX10.11.sdk\"\r\n<\/pre>\n<p>Finally I try to install cutorch, with success.<\/p>\n<pre>luarocks install cutorch\r\nluarocks install cunn #this installs fine as well, it didn't before\r\n<\/pre>\n<p>Everything should be good to go. But nothing works smooth.<\/p>\n<pre>\r\njason@jmbp15-nvidia ~\/w\/neural-style (master)> \r\nth neural_style.lua -style_image IMG_2663.JPG -content_image IMG_2911.JPG \r\n[libprotobuf WARNING google\/protobuf\/io\/coded_stream.cc:537] Reading dangerously large protocol message.  If the message turns out to be larger than 1073741824 bytes, parsing will be halted for security reasons.  To increase the limit (or to disable these warnings), see CodedInputStream::SetTotalBytesLimit() in google\/protobuf\/io\/coded_stream.h.\r\n[libprotobuf WARNING google\/protobuf\/io\/coded_stream.cc:78] The total number of bytes read was 574671192\r\nSuccessfully loaded models\/VGG_ILSVRC_19_layers.caffemodel\r\nconv1_1: 64 3 3 3\r\nconv1_2: 64 64 3 3\r\nconv2_1: 128 64 3 3\r\nconv2_2: 128 128 3 3\r\nconv3_1: 256 128 3 3\r\nconv3_2: 256 256 3 3\r\nconv3_3: 256 256 3 3\r\nconv3_4: 256 256 3 3\r\nconv4_1: 512 256 3 3\r\nconv4_2: 512 512 3 3\r\nconv4_3: 512 512 3 3\r\nconv4_4: 512 512 3 3\r\nconv5_1: 512 512 3 3\r\nconv5_2: 512 512 3 3\r\nconv5_3: 512 512 3 3\r\nconv5_4: 512 512 3 3\r\nfc6: 1 1 25088 4096\r\nfc7: 1 1 4096 4096\r\nfc8: 1 1 4096 1000\r\nTHCudaCheck FAIL file=\/tmp\/luarocks_cutorch-scm-1-5715\/cutorch\/lib\/THC\/generic\/THCStorage.cu line=40 error=2 : out of memory\r\n\/Users\/jason\/torch\/install\/bin\/luajit: \/Users\/jason\/torch\/install\/share\/lua\/5.1\/nn\/utils.lua:11: cuda runtime error (2) : out of memory at \/tmp\/luarocks_cutorch-scm-1-5715\/cutorch\/lib\/THC\/generic\/THCStorage.cu:40\r\nstack traceback:\r\n\t[C]: in function 'resize'\r\n\t\/Users\/jason\/torch\/install\/share\/lua\/5.1\/nn\/utils.lua:11: in function 'torch_Storage_type'\r\n\t\/Users\/jason\/torch\/install\/share\/lua\/5.1\/nn\/utils.lua:57: in function 'recursiveType'\r\n\t\/Users\/jason\/torch\/install\/share\/lua\/5.1\/nn\/Module.lua:123: in function 'type'\r\n\t\/Users\/jason\/torch\/install\/share\/lua\/5.1\/nn\/utils.lua:45: in function 'recursiveType'\r\n\t\/Users\/jason\/torch\/install\/share\/lua\/5.1\/nn\/utils.lua:41: in function 'recursiveType'\r\n\t\/Users\/jason\/torch\/install\/share\/lua\/5.1\/nn\/Module.lua:123: in function 'cuda'\r\n\tneural_style.lua:76: in function 'main'\r\n\tneural_style.lua:500: in main chunk\r\n\t[C]: in function 'dofile'\r\n\t...ason\/torch\/install\/lib\/luarocks\/rocks\/trepl\/scm-1\/bin\/th:145: in main chunk\r\n\t[C]: at 0x0109a0dd50\r\n<\/pre>\n<p>I guess I ran out of GPU memory? Seems to be an issue here <a href=\"https:\/\/github.com\/jcjohnson\/neural-style\/issues\/150\">https:\/\/github.com\/jcjohnson\/neural-style\/issues\/150<\/a>. My pictures aren&#8217;t that small, I guess. I&#8217;ll just resize them.<\/p>\n<pre>\r\njason@jmbp15-nvidia ~\/w\/neural-style (master)> ls -lh\r\ntotal 5480\r\n-rw-r--r--@ 1 jason  staff   2.4M 11 Mar 21:39 IMG_2663.JPG\r\n-rw-r--r--@ 1 jason  staff   235K 11 Mar 21:38 IMG_2911.JPG\r\n-rw-r--r--  1 jason  staff   9.1K 11 Mar 20:34 INSTALL.md\r\n-rw-r--r--  1 jason  staff   1.1K 11 Mar 20:34 LICENSE\r\n-rw-r--r--  1 jason  staff    16K 11 Mar 20:34 README.md\r\ndrwxr-xr-x  4 jason  staff   136B 11 Mar 20:34 examples\r\ndrwxr-xr-x  8 jason  staff   272B 12 Mar 06:24 models\r\n-rw-r--r--  1 jason  staff    16K 11 Mar 20:34 neural_style.lua\r\njason@jmbp15-nvidia ~\/w\/neural-style (master)> sips IMG_2663.JPG -Z 680\r\n\/Users\/jason\/workspace\/neural-style\/IMG_2663.JPG\r\n<CGColor 0x7fc922508a60> [<CGColorSpace 0x7fc9225067d0> (kCGColorSpaceDeviceRGB)] ( 0 0 0 1 )\r\n  \/Users\/jason\/workspace\/neural-style\/IMG_2663.JPG\r\njason@jmbp15-nvidia ~\/w\/neural-style (master)> sips IMG_2911.JPG  -Z 680\r\n\/Users\/jason\/workspace\/neural-style\/IMG_2911.JPG\r\n<CGColor 0x7fc088e02c30> [<CGColorSpace 0x7fc088c0ab10> (kCGColorSpaceDeviceRGB)] ( 0 0 0 1 )\r\n  \/Users\/jason\/workspace\/neural-style\/IMG_2911.JPG\r\njason@jmbp15-nvidia ~\/w\/neural-style (master)> ls -lh\r\ntotal 2224\r\n-rw-r--r--  1 jason  staff   105K 12 Mar 06:27 IMG_2663.JPG\r\n-rw-r--r--  1 jason  staff   64K 12 Mar 06:28 IMG_2911.JPG\r\n-rw-r--r--  1 jason  staff   9.1K 11 Mar 20:34 INSTALL.md\r\n-rw-r--r--  1 jason  staff   1.1K 11 Mar 20:34 LICENSE\r\n-rw-r--r--  1 jason  staff    16K 11 Mar 20:34 README.md\r\ndrwxr-xr-x  4 jason  staff   136B 11 Mar 20:34 examples\r\ndrwxr-xr-x  8 jason  staff   272B 12 Mar 06:24 models\r\n-rw-r--r--  1 jason  staff    16K 11 Mar 20:34 neural_style.lua\r\njason@jmbp15-nvidia ~\/w\/neural-style (master)> \r\n<\/pre>\n<p>Well resizing didn&#8217;t work, still runs out of memory. Default `th` uses nn, so I will try using cudnn, but not working.<\/p>\n<pre>\r\njason@jmbp15-nvidia ~\/w\/neural-style (master)> \r\nth neural_style.lua -style_image IMG_2663.JPG -content_image IMG_2911.JPG  -backend cudnn\r\nnil\t\r\n\/Users\/jason\/torch\/install\/bin\/luajit: \/Users\/jason\/torch\/install\/share\/lua\/5.1\/trepl\/init.lua:384: \/Users\/jason\/torch\/install\/share\/lua\/5.1\/trepl\/init.lua:384: \/Users\/jason\/torch\/install\/share\/lua\/5.1\/cudnn\/ffi.lua:1279: 'libcudnn (R4) not found in library path.\r\nPlease install CuDNN from https:\/\/developer.nvidia.com\/cuDNN\r\nThen make sure files named as libcudnn.so.4 or libcudnn.4.dylib are placed in your library load path (for example \/usr\/local\/lib , or manually add a path to LD_LIBRARY_PATH)\r\n\r\nstack traceback:\r\n\t[C]: in function 'error'\r\n\t\/Users\/jason\/torch\/install\/share\/lua\/5.1\/trepl\/init.lua:384: in function 'require'\r\n\tneural_style.lua:64: in function 'main'\r\n\tneural_style.lua:500: in main chunk\r\n\t[C]: in function 'dofile'\r\n\t...ason\/torch\/install\/lib\/luarocks\/rocks\/trepl\/scm-1\/bin\/th:145: in main chunk\r\n\t[C]: at 0x0106769d50\r\n<\/pre>\n<p>So I go to <a href=\"https:\/\/developer.nvidia.com\/rdp\/cudnn-download\">https:\/\/developer.nvidia.com\/rdp\/cudnn-download<\/a> and download cudnn-7.0-osx-x64-v4.0-prod.tgz and follow their install guide:<\/p>\n<pre>\r\nPREREQUISITES\r\n\r\n    CUDA 7.0 and a GPU of compute capability 3.0 or higher are required.\r\n\r\nALL PLATFORMS\r\n\r\n    Extract the cuDNN archive to a directory of your choice, referred to below as <installpath>.\r\n    Then follow the platform-specific instructions as follows.\r\n\r\nLINUX\r\n\r\n    cd <installpath>\r\n    export LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH\r\n\r\n    Add <installpath> to your build and link process by adding -I<installpath> to your compile\r\n    line and -L<installpath> -lcudnn to your link line.\r\n\r\nOS X\r\n\r\n    cd <installpath>\r\n    export DYLD_LIBRARY_PATH=`pwd`:$DYLD_LIBRARY_PATH\r\n\r\n    Add <installpath> to your build and link process by adding -I<installpath> to your compile\r\n    line and -L<installpath> -lcudnn to your link line.\r\n\r\nWINDOWS\r\n\r\n    Add <installpath> to the PATH environment variable.\r\n\r\n    In your Visual Studio project properties, add <installpath> to the Include Directories \r\n    and Library Directories lists and add cudnn.lib to Linker->Input->Additional Dependencies.\r\n<\/pre>\n<p>Opening the tgz file gives me cuda folder. I just need to add this my path<\/p>\n<pre>\r\njason@jmbp15-nvidia ~\/workspace> cd cuda\/\r\njason@jmbp15-nvidia ~\/w\/cuda> set DYLD_LIBRARY_PATH (pwd) $DYLD_LIBRARY_PATH\r\njason@jmbp15-nvidia ~\/w\/cuda> echo $DYLD_LIBRARY_PATH\r\n\/Users\/jason\/workspace\/cuda \/Users\/jason\/torch\/install\/lib\r\njason@jmbp15-nvidia ~\/w\/cuda [127]> tree \r\n.\r\n\u251c\u2500\u2500 cd\r\n\u251c\u2500\u2500 include\r\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 cudnn.h\r\n\u2514\u2500\u2500 lib\r\n    \u251c\u2500\u2500 libcudnn.4.dylib\r\n    \u251c\u2500\u2500 libcudnn.dylib -> libcudnn.4.dylib\r\n    \u2514\u2500\u2500 libcudnn_static.a\r\n\r\n2 directories, 5 files\r\njason@jmbp15-nvidia ~\/w\/cuda> set LD_LIBRARY_PATH (pwd)\/lib\/ $LD_LIBRARY_PATH\r\njason@jmbp15-nvidia ~\/w\/cuda> echo $LD_LIBRARY_PATH\r\n\/Users\/jason\/workspace\/cuda\/lib\/ \/Users\/jason\/torch\/install\/lib\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Inspiration comes from https:\/\/github.com\/jcjohnson\/neural-style. Because installing all the required toolchains on OS X 10.11.3 is a bit challenging, I here are my installation steps. cd workspace\/ git clone git@github.com:jcjohnson\/neural-style.git git clone https:\/\/github.com\/torch\/distro.git ~\/torch &#8211;recursive cd ~\/torch; bash install-deps; This will fail a few times if you have already installed them (but different versions). I needed &hellip; <a href=\"https:\/\/sunapi386.ca\/wordpress\/neural-style\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Neural Style<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-617","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/comments?post=617"}],"version-history":[{"count":9,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/617\/revisions"}],"predecessor-version":[{"id":628,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/617\/revisions\/628"}],"wp:attachment":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/media?parent=617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/categories?post=617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/tags?post=617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}