突然接到任務,要實踐一下風格遷移文章《Deep Painterly Harmonization》,該文章的說明大家可以看博客,作爲GitHub小能手o(╥﹏╥)o,在GitHub上看到了該文章的實現,https://github.com/luanfujun/deep-painterly-harmonization。從star和fork來看,非常靠譜。但是實踐後發現,真的是坑集大成者,今天特地針對碰到的坑做記錄。
一、風格遷移
首先介紹一下風格遷移是什麼。簡單來說,我們給定一張風格圖A,內容圖B,通過深度學習的方法,生成具備圖A風格和圖B內容的圖片圖C,如下所示。
但是該文章創造性地提出了一種基於風格遷移的圖像融合方法,可以將一幅圖像毫無違和感地嵌入另一幅圖像當中。如下
二、實踐及問題解決
坑1:vgg模型下載
項目中提供了下載腳本,需要下載三個文件VGG_ILSVRC_19_layers_deploy.prototxt、vgg_normalised.caffemodel和VGG_ILSVRC_19_layers.caffemodel
sh models/download_models.sh
可惜,根本下不動。o(╥﹏╥)o。這裏提供VGG_ILSVRC_19_layers_deploy.prototxt和vgg_normalised.caffemodel的下載鏈接,但是VGG_ILSVRC_19_layers.caffemodel太大,大家還是直接下載http://www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_19_layers.caffemodel
坑2:torch,cuda
如果直接按照文檔中的步驟,運行make clean && make,會出現如下問題
find . -type f | xargs -n 5 touch
rm -f libcuda_utils.so
/usr/local/cuda-8.0/bin/nvcc -arch sm_35 -O3 -DNDEBUG -Xcompiler -fopenmp --compiler-options '-fPIC' -o libcuda_utils.so --shared cuda_utils.cu -I/home/ubuntu/torch/install/include/THC -I/home/ubuntu/torch/install/include/TH -I/home/ubuntu/torch/install/include -L/home/ubuntu/torch/install/lib -Xlinker -rpath,/home/ubuntu/torch/install/lib -lluaT -lTHC -lTH -lpng -lluajit -lgomp
make: /usr/local/cuda-8.0/bin/nvcc: Command not found
makefile:10: recipe for target 'libcuda_utils.so' failed
make: *** [libcuda_utils.so] Error 127
或者
cuda_utils.cu:2:18: fatal error: lua.h: No such file or directory
#include "lua.h"
^
compilation terminated.
首先使用英偉達GPU,就需要他的分佈式計算框架,cuda,由於我們公司已經安裝好了cuda,沒有碰到問題。安裝見cuda安裝。該項目並不使用常規的pytorch和tensorflow,而是使用torch,所以我們要安裝torch,torch使用的lua語言,torch和pytorch的區別。torch安裝如下
git clone https://github.com/torch/distro.git ~/torch --recursive
cd ~/torch
bash install-deps
./install.sh
#原因是cuda和torch的頭文件都提供了相同的重載運算符,編譯器不知道用哪一個。輸入下面shell命令禁止使用cuda的頭文件編譯torch
export TORCH_NVCC_FLAGS="-D__CUDA_NO_HALF_OPERATORS__"
./install.sh
source ~/.bashrc(這個需要根據具體文件來看,通常爲.bashrc)。輸入th,出現以下就表示成功。
坑3:loadcaffe安裝
loadcaffe的安裝需要提前安裝好protobuf,否則會出現以下問題
make[2]: *** No rule to make target ../PROTOBUF_PROTOC_EXECUTABLE-NOTFOUND', needed bycaffe.pb.cc'. Stop.
make[1]: *** [CMakeFiles/loadcaffe.dir/all] Error 2
make: *** [all] Error 2
Error: Build error: Failed building.
因此我們需要安裝protobuf,安裝步驟如下
sudo apt-get install autoconf automake libtool curl make g++ unzip
git clone https://github.com/google/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh
./configure
make
make check
sudo make install
sudo ldconfig
安裝完後,我們就可以安裝loadcaffe,同時要注意,要指定 luarocks的路徑,同時要使用sudo權限,否則會有以下問題
Error: No results matching query were found.
那麼loadcaffe的安裝步驟如下
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo ~/torch/install/bin/luarocks install loadcaffe
坑4:Octave安裝
安裝步驟
sudo apt-add-repository ppa:octave/stable
sudo apt-get update
sudo apt-get install octave
命令行輸入octave,得到錯誤
octave exited with signal 6
需要sudo
sudo octave
好了,解決了這些bug後,我們就可以開心地進行圖片風格轉換了,我們顯存有12G,然而並沒有什麼卵用,一張圖處理大致要七分鐘左右。
#To generate all results (in data/) using the provided scripts, simply run
python gen_all.py
#in Python and then
run('filt_cnn_artifact.m')
#in Matlab or Octave. The final output will be in results/
再接再厲。