caffe-windows-matcaffe-pycaffe

网上有很多版本

https://github.com/Microsoft/caffe 

https://github.com/happynear/caffe-windows

我使用的微软的版本-大厂的靠谱

参考博客:

Caffe:Windows(64位)+VS2013下的Caffe(CPU Only)安装配置

https://blog.csdn.net/wanz2/article/details/52770316


VS2013 - without - gpu

一、环境准备
windows 10 64位 专业版(非必须)
visual studio 2013(墙裂推荐此版本)
笔者使用的操作系统为win10 64位,在其他版本的64位windows系统上应该同样可行。
visual studio 2013(以下简称vs2013)中需要安装NuGet,安装方法如下: 
a. 打开vs2013,点击“工具”-“扩展和更新” 
b. 在弹出的对话框中点击“联机”,在右上角搜索框中搜索NuGet,在搜索结果中“NuGet Package Manager”项上面的“下载”,开始下载NuGet,下载完后进行安装 
c. 安装完后重启vs2013,重启后点击“工具”-“扩展和更新”,选择“已安装”,可以看到NuGet Package Manager已经安装好了。
二、配置步骤
下载代码 
从https://github.com/Microsoft/caffe 
得到文件“caffe-master.zip” 
或从https://github.com/BVLC/caffe/tree/windows 
得到文件“caffe-windows.zip” 
二者的配置步骤相同,均能编译成功,本文以源码“caffe-master.zip”为例

解压后进入如下路径:\caffe-master\windows 之后的操作均在该目录下进行,因此之后省略\caffe-master的路径

在\windows目录下复制文件 CommonSettings.props.example(应该会以副本形式出现CommonSettings.props - 副本.example),并将该副本改名为CommonSettings.props。(请确认显示文件扩展名这个选项已生效)

用vs(或其他编辑器)打开CommonSettings.props,将第7行CpuOnlyBuild标签中的值改为true,将第8行UseCuDNN标签中的值改为false,更改完毕后保存并退出。 
(这两步是将caffe的GPU版本关闭,仅使用CPU版本。由于打开GPU会出现其他错误,作为初学者,我们先从CPU版本开始,等熟悉caffe之后再深入研究GPU版本)

我的matlab版本是2016a,版本太高,需要修改一些东西 

  • 修改CommonSettings.props配置
  • <PropertyGroup Condition="'$(MatlabSupport)'=='true'">  
        <MatlabDir>E:\02_software\01_develop\matlab</MatlabDir>  
        <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath>  
        <IncludePath>$(MatlabDir)\extern\include;$(MatlabDir)\toolbox\distcomp\gpu\extern\include;$(IncludePath)</IncludePath>
        <!-- 增加一个$(MatlabDir)\toolbox\distcomp\gpu\extern\include; -->  
    </PropertyGroup> 

用vs打开\windows下的Caffe.sln,可以看到该解决方案中共有16个项目,请注意核对。(若下载的源码为BVLC的“caffe-windows.zip”,则该解决方案中只有15个项目,所缺少的项目为“caffe.managed”,但经过笔者测试,在编译时并没有影响)右击“解决方案‘Caffe’”,选择“属性”,将“配置属性”-“配置”修改成Release和x64,如下图所示 
 
(这一步是使用Release来进行编译,若用Debug,则之后每次都要打开vs,会不方便) 
注意:在上图顶部工具栏中的“解决方案配置”和“解决方案平台”框,若你的vs2013中将这两个框在工具栏中显示,则要在工具栏中将配置改成Release和x64,否则直接右击“解决方案Caffe”来更改配置是无效的。

右击解决方案中的libcaffe项目,选择“属性”,在打开的属性页中选择“C/C++”-“常规”,将“将警告视为错误”设为“否”,如下图所示: 
右击“libcaffe”项目,选择“生成”,之后是一段时间的等待。 
注意:点击“生成”后会出现如下图的窗口,此时vs正在使用NuGet对caffe的一些依赖文件进行自动还原 
 
还原成功后,会在caffe-master的同级目录生成文件夹NugetPackages。

右击“解决方案Caffe”,选择“生成解决方案”,之后又是一段时间的等待

等待过后生成成功,到此windows下的caffe配置完成,此时在\caffe-master目录下会生成Build文件夹,即为我们编译成功的文件夹,而\caffe-master\Build\x64\Release目录下则会有我们编译出的caffe.exe执行文件,到此caffe配置完成。

主要参考 
1. http://www.cnblogs.com/yixuan-xu/p/5858595.html 
2. http://m.blog.csdn.net/article/details?id=51355143 
3. http://blog.csdn.net/qq_14845119/article/details/52415090


配置pycaffe和matcaffe

我存放的目录是:G:\VS2013code\caffe-master

编译后可执行的文件在,caffe中的Bulid\x64\release下面(包括caffe.exe、convert_imageset.exe等经常要用到的文件)
添加环境变量,我的路径是

G:\VS2013code\caffe-master\Build\x64\Release
G:\VS2013code\caffe-master\Build\x64\Release\pycaffe

添加完毕环境变量后对系统注销或者重启

pycaffe

成功生成解决方案后,在caffe-windows\Build\x64\Release\pycaffe\caffe路径下会有_caffe.pyd文件。将_caffe.pyd文件所在的文件夹caffe复制到D:\ProgramData\Anaconda3\envs\caffe-python2.7\Lib\site-packages中。不做这一步会导致在python中import caffe提示NO Model named caffe

再拷贝一份到G:\VS2013code\caffe-master\python中,否则由于D:\caffe\python下有一个caffe文件夹,覆盖了PYTHONPATH中的caffe文件夹,当使用G:\VS2013code\caffe-master\python下面的draw_net.py等函数时,会报错ImportError: No module named _caffe。

# 打开python
# 没报错就说明python caffe接口成功
import caffe

错误:

RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa

在安装Python版openCV时安装完成后,包括其他第三方库时会有如下问题,这也是numpy版本的问题,可以看见上面的numpy版本是 1.13.1的,而最新的是1.15.4版本的。所以需要更新numpy:

pip install --upgrade numpy
或者可以尝试:

easy_install numpy

测试:

import numpy as np
import sys
caffe_root = 'G:/VS2013code/caffe-master/'  # this file should be run from {caffe_root}/examples (otherwise change this line)
sys.path.insert(0, caffe_root + 'python')
 
import caffe
 
caffe.set_mode_cpu()
 
model_def = 'G:/VS2013code/caffe-master/models/bvlc_reference_caffenet/deploy.prototxt'
model_weights = 'G:/VS2013code/caffe-master/models//bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'
 
net = caffe.Net(model_def,      # defines the structure of the model
                model_weights,  # contains the trained weights
                caffe.TEST)     # use test mode (e.g., don't perform dropout)
mu = np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy')
mu = mu.mean(1).mean(1)  # average over pixels to obtain the mean (BGR) pixel values
print 'mean-subtracted values:', zip('BGR', mu)
 
# create transformer for the input called 'data'
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
 
transformer.set_transpose('data', (2,0,1))  # move image channels to outermost dimension
transformer.set_mean('data', mu)            # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255)      # rescale from [0, 1] to [0, 255]
transformer.set_channel_swap('data', (2,1,0))  # swap channels from RGB to BGR
 
net.blobs['data'].reshape(1,        # batch size
                          3,         # 3-channel (BGR) images
                          227, 227)  # image size is 227x227
 
image = caffe.io.load_image(caffe_root + 'examples/images/cat.jpg')
transformed_image = transformer.preprocess('data', image)
 
# copy the image data into the memory allocated for the net
net.blobs['data'].data[...] = transformed_image
 
### perform classification
output = net.forward()
 
output_prob = output['prob'][0]  # the output probability vector for the first image in the batch
 
print 'predicted class is:', output_prob.argmax()

matcaffe

首先添加系统路径(path):路径深度到Release目录为止:

G:\VS2013code\caffe-master\Build\x64\Release  (前面pycaffe已经添加)

然后打开matlab2015a, 添加路径,路径深度到matcaffe目录为止:

G:\VS2013code\caffe-master\Build\x64\Release\matcaffe

保存后,重启matlab,不然测试时会报找不到caffe_mexw64模块的错误。

在控制台输入一个函数作为测试:

caffe.reset_all();

可以看到如下:

 

说明matlab接口可以使用了。

猜你喜欢

转载自blog.csdn.net/baidu_40840693/article/details/85115098
今日推荐