【caffe-Windows】微軟官方caffe之 Python介面配置及圖片生成例項
阿新 • • 發佈:2019-02-07
前言
發現許多程式碼還是用python寫的,所以還是配置一下介面吧,雖然博主不會Python,咳咳。在這裡使用的python安裝包是anaconda2,注意使用Python2.7版本的那個安裝包。
百度雲:連結:http://pan.baidu.com/s/1nvrrfQx 密碼:1jg5
測試的例項是2015年一篇利用噪聲生成一張真實圖片的程式碼,使用的caffe+python時下
注意一下,我第一安裝的時候竟然沒有jupyter notebook,所以解除安裝了一下,又重新安裝了一次anaconda,安裝過程直接各種下一步就行了。
測試方法是直接在dos輸入jupyter notebook,看是否有一個網頁彈出來。
C:\Users\Bingo>jupyter notebook [I 10:16:40.512 NotebookApp] [nb_conda_kernels] enabled, 2 kernels found [I 10:16:41.680 NotebookApp] 鉁?nbpresent HTML export ENABLED [W 10:16:41.680 NotebookApp] 鉁?nbpresent PDF export DISABLED: No module named n bbrowserpdf.exporters.pdf [I 10:16:41.690 NotebookApp] [nb_conda] enabled [I 10:16:41.812 NotebookApp] [nb_anacondacloud] enabled [I 10:16:42.140 NotebookApp] Serving notebooks from local directory: C:\Users\Bi ngo [I 10:16:42.140 NotebookApp] 0 active kernels [I 10:16:42.141 NotebookApp] The Jupyter Notebook is running at: http://localhos t:8888/ [I 10:16:42.141 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
第一步
編譯caffe介面,修改配置檔案CommonSettings.props:
第13行:
<PythonSupport>true</PythonSupport>
第48行:
<PythonDir>C:\Program Files\Anaconda2\</PythonDir>
然後儲存,去編譯Release版本的pycaffe,好像如果編譯Debug版本會出現python27_d.lib找不到什麼的。
等編譯完成以後,有如下資料夾:
直接將caffe這個資料夾拷貝到C:\Program Files\Anaconda2\Lib\site-packages即可。
第二步
試試import caffe
Microsoft Windows [版本 6.1.7601]
版權所有 (c) 2009 Microsoft Corporation。保留所有權利。
C:\Users\Bingo>python
Python 2.7.12 |Anaconda 4.2.0 (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.
1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import caffe
這一步應該會出現不同的問題,我出現的是ImportError: No module named google.protobuf.internal
這個錯誤,反正只要跟protobuf相關的錯誤,你就直接在dos下安裝,方法是:C:\windows\system32>conda install protobuf
【PS】此處有的讀者使用這個命令安裝可能出現問題,如果不行的話就用pip install protobuf 安裝吧,因為我當時用pip安裝出問題了,所以只寫了conda安裝方法,兩個都試試吧
然後就會慢慢安裝,安裝過程如下:
Microsoft Windows [版本 6.1.7601]
版權所有 (c) 2009 Microsoft Corporation。保留所有權利。
C:\windows\system32>conda install protobuf
Fetching package metadata ..........Could not connect to https://repo.continuum
io/pkgs/free/win-64/
........
Solving package specifications: ..........
Package plan for installation in environment C:\Program Files\Anaconda2:
The following packages will be downloaded:
package | build
---------------------------|-----------------
conda-env-2.5.2 | py27_0 66 KB conda-forge
conda-4.1.12 | py27_0 263 KB conda-forge
protobuf-3.0.0 | py27_vc9_0 11.1 MB conda-forge
------------------------------------------------------------
Total: 11.4 MB
The following NEW packages will be INSTALLED:
conda-env: 2.5.2-py27_0 conda-forge
protobuf: 3.0.0-py27_vc9_0 conda-forge [vc9]
The following packages will be SUPERCEDED by a higher-priority channel:
conda: 4.2.9-py27_0 --> 4.1.12-py27_0 conda-forge
Proceed ([y]/n)? y
Fetching packages ...
conda-env-2.5. 100% |###############################| Time: 0:00:01 35.70 kB/s
conda-4.1.12-p 100% |###############################| Time: 0:00:03 71.46 kB/s
protobuf-3.0.0 100% |###############################| Time: 0:02:02 94.90 kB/s
Extracting packages ...
[ COMPLETE ]|##################################################| 100%
Unlinking packages ...
[ COMPLETE ]|##################################################| 100%
Linking packages ...
[ COMPLETE ]|##################################################| 100%
C:\windows\system32>
安裝完畢,再去試試能否匯入caffe:
Microsoft Windows [版本 6.1.7601]
版權所有 (c) 2009 Microsoft Corporation。保留所有權利。
C:\Users\Bingo>python
Python 2.7.12 |Anaconda 4.2.0 (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.
1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import caffe
C:\Program Files\Anaconda2\lib\site-packages\caffe\pycaffe.py:13: RuntimeWarning
: to-Python converter for class boost::shared_ptr<class caffe::Net<float> > alre
ady registered; second conversion method ignored.
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
C:\Program Files\Anaconda2\lib\site-packages\caffe\pycaffe.py:13: RuntimeWarning
: to-Python converter for class boost::shared_ptr<class caffe::Blob<float> > alr
eady registered; second conversion method ignored.
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
C:\Program Files\Anaconda2\lib\site-packages\caffe\pycaffe.py:13: RuntimeWarning
: to-Python converter for class boost::shared_ptr<class caffe::Solver<float> > a
lready registered; second conversion method ignored.
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
>>>
我這裡有三個warning,看網上說不用管即可,如果有輕微強迫症,不想看到warning,可以去google上搜一下,有解決方案,需要修改cpp檔案的東西,接下來我們引入一下例項看看效果。
第三步:
百度雲地址:連結:http://pan.baidu.com/s/1qYaqcZ6 密碼:5cm9
使用方法是先開啟jupyter notebook
【注意】此處開啟的路徑是你的程式碼解壓以後的位置
Microsoft Windows [版本 6.1.7601]
版權所有 (c) 2009 Microsoft Corporation。保留所有權利。
C:\Users\Bingo>E:
E:\>cd E:\caffe-code\DeepTextures-master
E:\caffe-code\DeepTextures-master>jupyter notebook
[I 10:38:58.658 NotebookApp] [nb_conda_kernels] enabled, 2 kernels found
[I 10:38:59.819 NotebookApp] 鉁?nbpresent HTML export ENABLED
[W 10:38:59.819 NotebookApp] 鉁?nbpresent PDF export DISABLED: No module named n
bbrowserpdf.exporters.pdf
[I 10:38:59.829 NotebookApp] [nb_conda] enabled
[I 10:38:59.951 NotebookApp] [nb_anacondacloud] enabled
[I 10:39:00.283 NotebookApp] Serving notebooks from local directory: E:\caffe-co
de\DeepTextures-master
[I 10:39:00.283 NotebookApp] 0 active kernels
[I 10:39:00.283 NotebookApp] The Jupyter Notebook is running at: http://localhos
t:8888/
[I 10:39:00.285 NotebookApp] Use Control-C to stop this server and shut down all
kernels (twice to skip confirmation).
然後瀏覽器彈出的頁面會有當前目錄的檔案:
然後點選使用快捷鍵shift+Enter逐步執行,最後會出現一個迭代生成過程,就是一張圖片一閃一閃的,但是我看其它人的都是由一個iteration數字在上面,不過不管怎麼樣,這樣就是在生成圖片了: