Windows7 64下搭建Caffe+python接口环境
参考链接:
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(可能每个人安装的路径不同)

不知道是什么问题。结果却不敬人意,待续解决。
Windows7 64下搭建Caffe+python接口环境的更多相关文章
- Windows7下搭建Eclipse+Python开发环境
机器: Windows7_x86_64 前提: 机器已成功安装Python2.7,并配置好环境变量. 步骤: 一.Eclipse的安装 Eclipse是基于java的一个应用程序,因此需要一个java ...
- windows下搭建spark+python 开发环境
有时候我们会在windows 下开发spark程序,测试程序运行情况,再部署到真实服务器中运行. 那么本文介绍如何在windows 环境中搭建简单的基于hadoop 的spark 环境. 我的wind ...
- ubuntu 下搭建apache+python的运行环境
ubuntu下怎么搭建apache+python运行环境,可以参考http://www.01happy.com/ubuntu-apache-mod-python/ ,这里只是简单的记录下步骤,本文主要 ...
- 【转】Eclipse和PyDev搭建完美Python开发环境(Ubuntu篇)
原文网址:http://www.cnblogs.com/Realh/archive/2010/10/10/1847251.html 前两天在Windows下成功地搭好了一个Python开发环境,这次转 ...
- 使用Vagrant搭建本地python开发环境
使用Vagrant搭建本地python开发环境 关于vagrant:Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境,它使用Oracle的开源VirtualBox虚拟化系统也可以使 ...
- Windows下搭建Spark+Hadoop开发环境
Windows下搭建Spark+Hadoop开发环境需要一些工具支持. 只需要确保您的电脑已装好Java环境,那么就可以开始了. 一. 准备工作 1. 下载Hadoop2.7.1版本(写Spark和H ...
- 在Linux(Ubuntu)下搭建ASP.NET Core环境并运行 继续跨平台
最新教程:http://www.cnblogs.com/linezero/p/aspnetcoreubuntu.html 无需安装mono,在Linux(Ubuntu)下搭建ASP.NET Core环 ...
- 如何在Ubuntu下搭建Android NDK开发环境
1 搭建Android SDK开发环境 参考在在Ubuntu下搭建Android SDK开发环境(图文)首先在Ubuntu下搭建Android SDK开发环境. 2 下载NDK开发包 打开官网: ht ...
- Windows下搭建objective C开发环境
摘自:http://blog.csdn.net/zhanghefu/article/details/18320827 最近打算针对iPhone.iPod touch和iPad开发一些应用,所以,需要开 ...
随机推荐
- 软件工程M1/M2总结
也不分M1/M2了,就从头到尾的梳理一下这学期的软工课吧. 第一节课,老师就稀里哗啦说了一下这学期要怎么搞,什么个人项目啦,结对项目啦,团队项目一二啦,还要组队啊什么的,然后风风火火的组队. 个人项目 ...
- [2019BUAA软件工程]第1次阅读作业
[2019BUAA软件工程]第1次阅读作业 Tips Link 作业连接 [2019BUAA软件工程]第1次阅读作业 读<构建之法>的疑惑 个人开发流程(Personal Software ...
- 20135323符运锦----第七周:Linux内核如何装载和启动一个可执行程序
可执行程序的装载 一.预处理.编译.链接和目标文件的格式 1.可执行程序是怎么得来的 ①编译器预处理 gcc -E -o XX.cpp XX.c (-m32)// 注:把include的文件包含进来, ...
- 我的github地址 https://github.com/1010de/Test.git
构建之法老师叫交下任务学习github,经过一段时间的学习和了解,看介绍.看视频.看博客.初步认识到github的方便与好处. 自己试着去注册和使用github,已经慢慢学会了一些基本操作. ...
- app推广及主要代码
app推广: 一.基本情况 我们把推广和调研都放在了一起,主要是调研,主要通过调查问卷和直接访问的方式,让调查的人能够看到我们app的主要功能, 然后做出评价和对此改善的意见.调 ...
- Chrome 启动参数列表
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --type=gpu-process --channel ...
- Oracle 数据库启动过程
一 启动数据库 Oracle启动过程涉及几种模式,这些模式涉及不同的文件,每个状态下数据库做不同的事情,同时这些模式适用于不同的维护需求,主要的模式有三种:NOMOUNT.MOUNT.OPEN. 1 ...
- ionic创建项目失败
× Downloading and extracting blank starter - failed![ERROR] Network connectivity error occurred, are ...
- SVN上线步骤笔记
项目代码位置: /data/svn/play_out 项目代码目录名称: test SVN创建位置:/data/svn/repos_Websvn线上地址:svn://192.168.1.1/repos ...
- D3.js v5 Tutorials
D3.js v5 Tutorials D3.js v5 教程 https://github.com/d3/d3/blob/master/API.md CHANGES https://github.co ...