參考鏈接:
http://www.cnblogs.com/yixuan-xu/p/5858595.html
http://www.cnblogs.com/zf-blog/p/6139044.html
http://blog.csdn.net/zb1165048017/article/details/51355143?locationNum=6&fps=1
http://blog.csdn.net/zb1165048017/article/details/52980102
搭建一個完整的運行環境,不出錯是很難的。尤其是對於我這樣的小白。上面是我為搭建此環境參考的前輩博客鏈接。
這里做下總結,Windows7 64下搭建Caffe+python接口環境。(有些資源是前輩博客里的)
1.軟件
Caffe下載鏈接(caffe-master):
https://github.com/Microsoft/caffe
百度雲下載:http://pan.baidu.com/s/1hs8ngpA 密碼:ith0
微軟的Windows三方包(Nuget程序包):http://pan.baidu.com/s/1pKQKJJP 密碼:2vzy
VS2013安裝包鏈接:http://pan.baidu.com/s/1dF5OTWH 密碼:16fn
anaconda2下載: 官網下載 https://repo.continuum.io/archive/Anaconda2-4.2.0-Windows-x86_64.exe
(千萬要下載支持python2.7的anaconda2,不然后面會遇到找不到python27.lib,折騰了我一下午時間)
2.軟件下載完成之后,開始一步一步來搭建環境。
(1)首先解壓caffe-master.zip壓縮包,打開caffe-master文件夾。在windows文件夾下復制CommonSettings.props.example文件,
重命名為CommonSettings.props文件。
(2)在windows文件夾下找到Caffe.sln,並用VS2013打開->項目/解決方案。
在CommonSettings.props文件中修改相應內容:(下面Python路徑一定要改為anaconda2的路徑,圖片中是anaconda3路徑)

由於PC只有CPU沒有GPU,修改第7、8行;想要支持Python接口,修改第13行,並添加Python路徑,修改第48行 打開Caffe.sln出現了libcaffe和test_all加載失敗。出現這個情況,原因可能和更改配置有關系,就將項目/解決方案關閉,重新打開就好了。

(3)配置文件修改好后,可以編譯Caffe了。(有兩種Debug和Release活動配置,都可以編譯)

這時,可能會彈出一個對話框,表示正在下載第三方包。
也可以這么做,在編譯之前直接將NugetPackages文件夾(由於我們已經下載好NugetPackages),放在與Caffe-Master並列文件夾中。下載完成后,如圖。
(4)設置一下運行項。
(5)運行caffe.cpp,直接雙擊打開caffe.cpp,然后ctrl+F5直接編譯。出現如下命令窗口說明編譯成功。


3.用Caffe來測試mnist數據集
(1)去官網http://yann.lecun.com/exdb/mnist/下載數據集。將圖里的數據都下載下來,備用。
下載后解壓到.\caffe-master\data\mnist放在兩個文件夾下。

(2)在caffe-master目錄下,新建一個create_mnist.bat文件,寫入下面代碼:
.\Build\x64\Release\convert_mnist_data.exe .\data\mnist\mnist_train_lmdb\train-images.idx3-ubyte .\data\mnist\mnist_train_lmdb\train-labels.idx1-ubyte .\examples\mnist\mnist_train_lmdb echo. .\Build\x64\Release\convert_mnist_data.exe .\data\mnist\mnist_test_lmdb\t10k-images.idx3-ubyte .\data\mnist\mnist_test_lmdb\t10k-labels.idx1-ubyte .\examples\mnist\mnist_test_lmdb pause
雙擊該腳本運行,會在.\caffe-master\examples\mnist下生成相應的lmdb數據文件。


可能會遇到這個問題:
convert_mnist_data.cpp:103] Check failed: mdb_env_open(mdb_env, db_path, 0, 0664) == 0 (112 vs. 0) mdb_env_open failed
解決:在convert_mnist_data.cpp第103行代碼中
CHECK_EQ(mdb_env_set_mapsize(mdb_env, 1099511627776), MDB_SUCCESS) //1TB
將數字改為107374182(100M),再重新生成一下convert_mnist_data.cpp
再次運行腳本,又出現這個問題:
convert_mnist_data.cpp:98] Check failed: _mkdir(db_path) == 0 (-1 vs. 0) mkdir .\examples\mnist\mnist_test_lmdbfailed
解決:前面雖然出現了錯誤,但數據文件夾同樣生成了。在convert_mnist_data.cpp第98行代碼中mkdir(db_path, 0744)表示為數據庫創建文件夾。
如果文件夾已經存在,程序會報錯退出。程序不會覆蓋已有的數據庫。已有的數據庫如果不要了,需要手動刪除。
(3)修改.\caffe-master\examples\mnist\lenet_solver.prototxt 將最后一行改為solver_mode:CPU
修改.\caffe-master\examples\mnist\lenet_train_test.prototxt,如下圖。

(4)在.\caffe-master目錄下,新建train_mnist.bat,然后寫入下面代碼:
.\Build\x64\Release\caffe.exe train --solver=.\examples\mnist\lenet_solver.prototxt
pause
然后雙擊運行,就會開始訓練,訓練完畢后會得到相應的准確率和損失率。(用Debug配置運行時,大概要一個小時左右才能訓練完)

4.配置Python接口
(1)編譯caffe接口,確認配置文件CommonSettings.props的參數中第13、48行已修改,如下。
<PythonSupport>true</PythonSupport>
<PythonDir>C:\Anaconda2\</PythonDir>
保存之后,去編譯pycaffe,配置模式改為Release
編譯完成以后,會出現.\caffe-master\Build\x64\Release\pycaffe文件夾
將caffe文件夾復制到Anaconda2安裝目錄下C:\Anaconda2\Lib\site-packages
(2)import caffe
由於我PC裝了很多版本的Python,在cmd里指定下目錄,調用C:\Anaconda2\里的Python.exe
再import caffe
這時會出現 ImportError: No module named google.protobuf.internal 這個錯誤。
原因是沒有protobuf這個模塊。使用pip install protobuf(這個安裝也需要注意,在cmd里指定下目錄,
調用C:\Anaconda2\Scripts\里的pip.exe 即pip.exe install protobuf).

模塊安裝完成以后,再import caffe,就OK了。

(3)看一個實例
實現的是利用噪聲生成一張真實圖片,使用的實例是Texture Synthesis Using Convolutional Neural Networks
原文地址: http://blog.csdn.net/zb1165048017/article/details/52980102
github地址:https://github.com/leongatys/DeepTextures
下載github的程序,新建一個DeepTextures-master.py文件。
import glob
import sys
import os
from collections import OrderedDict
import caffe
import numpy as np
from matplotlib import pyplot as plt
import qtpy
base_dir1 = os.getcwd()
print base_dir1
sys.path.append(r'C:\Anaconda2\Lib\site-packages\PyQt5')
print sys.path
#How to call custom moudles(DeepTextures-master)?
#1.find the site-packages folder in the python installation directory(.\Lib\site-packages)
#2.create a path file in this directory,such as myPython.pth
#3.open the myPython.pth,write the folder path for the user module(xxx.\DeepTextures-master)
#4.restart python container(IDLE/command line)
#5.from DeepImageSynthesis import *
base_dir = r"C:\baidu\spider\caffe\PDF\example\DeepTextures-master"
from DeepImageSynthesis import *
VGGweights = os.path.join(base_dir,r'Models\vgg_normalised.caffemodel')
VGGmodel = os.path.join(base_dir,r'Models\VGG_ave_pool_deploy.prototxt')
imagenet_mean = np.array([ 0.40760392, 0.45795686, 0.48501961]) #mean for color channels (bgr)
im_dir = os.path.join(base_dir, 'Images/')
caffe.set_mode_cpu() #for cpu mode do 'caffe.set_mode_cpu()'
#if cpu mode we should not call,below 3 line of code is just for using GPU mode.
#gpu = 0
#caffe.set_mode_gpu()
#caffe.set_device(gpu)
#load source image
source_img_name = glob.glob1(im_dir,'pebbles.jpg')[0]
print source_img_name
source_img_org = caffe.io.load_image(im_dir + source_img_name)
im_size = 256.
[source_img,net] = load_image(im_dir + source_img_name, im_size,
VGGmodel, VGGweights, imagenet_mean,
show_img=True)
im_size = np.asarray(source_img.shape[-2:])
#l-bfgs parameters optimisation
maxiter = 2000
m = 20
#define layers to include in the texture model and weights w_l
tex_layers = ['pool4', 'pool3', 'pool2', 'pool1', 'conv1_1']
tex_weights = [1e9,1e9,1e9,1e9,1e9]
#pass image through the network and save the constraints on each layer
constraints = OrderedDict()
net.forward(data = source_img)
for l,layer in enumerate(tex_layers):
constraints[layer] = constraint([LossFunctions.gram_mse_loss],
[{'target_gram_matrix': gram_matrix(net.blobs[layer].data),
'weight': tex_weights[l]}])
#get optimisation bounds
bounds = get_bounds([source_img],im_size)
#generate new texture
result = ImageSyn(net, constraints, bounds=bounds,
callback=lambda x: show_progress(x,net),
minimize_options={'maxiter': maxiter,
'maxcor': m,
'ftol': 0, 'gtol': 0})
#match histogram of new texture with that of the source texture and show both images
new_texture = result['x'].reshape(*source_img.shape[1:]).transpose(1,2,0)[:,:,::-1]
new_texture = histogram_matching(new_texture, source_img_org)
plt.imshow(new_texture)
pltfigure()
plt.imshow(source_img_org)
調試可能遇到下面幾個問題:
(1). F0122 16:28:32.843454 14000 common.cpp:75] Cannot use GPU in CPU-only Caffe
因為在前面caffe配置過程中,只配置了CPU模式。無法使用GPU,代碼中已更改。
(2). This application failed to start because it could not find or load the Qt platform plugin "windows" in "".
由於沒有配置系統的環境變量QT_QPA_PLATFORM_PLUGIN_PATH,找到pyqt的plugin目錄
C:\Anaconda2\Library\plugins(可能每個人安裝的路徑不同)

不知道是什么問題。結果卻不敬人意,待續解決。
