win10下安装caffe新得

来源:互联网 发布:手机 dns劫持 数据网络 编辑:程序博客网 时间:2024/06/08 01:03

1、经过matcaffe编译生成的caffe_.mexw64的路径在D:\Software\Caffe\caffe-master\Build\x64\Release\matcaffe\+caffe\private;

2、使用微软的caffe版本(https://github.com/Microsoft/caffe)时遇到了"gpu/mxGPUArray.h" Not Found问题,排查了一下,发现是我装的matlab版本(2016)太新了,修改CommonSettings.props配置如下:

[html] view plain copy
 print?
  1. <PropertyGroup Condition="'$(MatlabSupport)'=='true'">  
  2.     <MatlabDir>E:\02_software\01_develop\matlab</MatlabDir>  
  3.     <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath>  
  4.     <IncludePath>$(MatlabDir)\extern\include;$(MatlabDir)\toolbox\distcomp\gpu\extern\include;$(IncludePath)</IncludePath>  <!-- 增加了一个include路径 -->  
  5. </PropertyGroup>  

具体排查过程,全局查找mxGPUArray.h,发现在$(MatlabDir)\toolbox\distcomp\gpu\extern\include路径下,遂在配置中加上。估计是matlab新版本中gpu文件夹已从\extern\include独立出来了。

3、=============================================================================================================

stu_why博客地址:http://blog.csdn.net/zpp1994,下面配置的环境是不带GPU的,之使用CPU的配置方法:

第一

在github上下载Microsoft为Windows用户提供的Caffe for Windows分支,下载链接:https://github.com/microsoft/caffe。下载后解压到你要安装的目录,本人安装目录为E:\Caffe。然后复制Windows下CommonSettings.props.example,后缀改为CommonSettings.props,如下图: 


这里写图片描述

第二

修改复制过来的CommonSettings.props配置文件如下:CpuOnlyBuild:true;UseCuDNN:false 


这里写图片描述

第53行修改MatlabDir为 C:\Program Files\MATLAB\R2016a,即你的matlab安装路径;

这里写图片描述

第三

下载Caffe依赖包NugetPackages,下载链接: https://pan.baidu.com/s/1jHWzike 密码: u3tb,下载解压放到与caffe-master并列文件夹即可,如图: 

这里写图片描述

第四

双击Windows下的caffe.sln,选中libcaffe项目,右键->属性->配置管理器->活动解决方案配置为Release,如下图: 


这里写图片描述

然后右键->属性->生成解决方案; 
为了使用caffe的matlab接口,需要添加matlab中的C++头文件mxGPUArray.h,:选中matcaffe项目,右键->属性->配置属性->C/C++->常规->附加包含目录,本人目录:C:\Program Files\MATLAB\R2016a\toolbox\distcomp\gpu\extern\include\gpu, 
读者照此修改为自己的matlab安装路径即可;最后编译caffe_.cpp,生成caffe_.mexw64,Note:生成的路径在D:\Software\Caffe\caffe-master\Build\x64\Release\matcaffe\+caffe\private;


最后,选中caffe.sln下的16个项目,右键->生成解决方案,如下图: 

这里写图片描述

这一过程在本人电脑上十分钟左右,结果如下: 


这里写图片描述

按照这个流程编译下来很少会出现无法解析外部符号问题,出现这种问题有很大可能是静态库没连接好,但是微软的所有库是自动下载的,除非没下载全。还有关于一些.h 头文件未找到,请自行核对此头文件的位置,然后再配置文件中随便找个IncludePath(此includePath必须在编译时候被使用),将路径加进去即可,同时也必须注意是否需要相关的lib文件。不过帮忙配置挺多电脑以后,基本没遇到太多问题。

第五 配置和运行matlab实例

配置好Caffe运行所需要的环境变量后(否则matcaffe会运行失败),将D:\Software\Caffe\caffe-master\Build\x64\Release添加进系统环境变量path中,否则matcaffe会运行失败; 如下所示:


首先将上一步生成的matcaffe加入matlab的路径中,直接在setpath里面设置,具体如下所示:


在matlab中打开classification_demo.m和test.m(test.m为本人自己编写), 需要的可联系我:


classification_demo.m

function [scores, maxlabel] = classification_demo(im, use_gpu)  % [scores, maxlabel] = classification_demo(im, use_gpu)  % 使用BVLC CaffeNet进行图像分类的示例  % 重要:运行前,应首先从Model Zoo(http://caffe.berkeleyvision.org/model_zoo.html) 下载BVLC CaffeNet训练好的权值  %  % ****************************************************************************  % For detailed documentation and usage on Caffe's Matlab interface, please  % refer to Caffe Interface Tutorial at  % http://caffe.berkeleyvision.org/tutorial/interfaces.html#matlab  % ****************************************************************************  %  % input  %   im       color image as uint8 HxWx3  %   use_gpu  1 to use the GPU, 0 to use the CPU  %  % output  %   scores   1000-dimensional ILSVRC score vector  %   maxlabel the label of the highest score  %  % You may need to do the following before you start matlab:  %  $ export LD_LIBRARY_PATH=/opt/intel/mkl/lib/intel64:/usr/local/cuda-5.5/lib64  %  $ export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6  % Or the equivalent based on where things are installed on your system  %  % Usage:  %  im = imread('../../examples/images/cat.jpg');  %  scores = classification_demo(im, 1);  %1 表示的是使用GPU,由于本利不带GPU,故设为 0.%  [score, class] = max(scores);  % Five things to be aware of:  %   caffe uses row-major order  %   matlab uses column-major order  %   caffe uses BGR color channel order  %   matlab uses RGB color channel order  %   images need to have the data mean subtracted  % Data coming in from matlab needs to be in the order  %   [width, height, channels, images]  % where width is the fastest dimension.  % Here is the rough matlab for putting image data into the correct  % format in W x H x C with BGR channels:  %   % permute channels from RGB to BGR  %   im_data = im(:, :, [3, 2, 1]);  %   % flip width and height to make width the fastest dimension  %   im_data = permute(im_data, [2, 1, 3]);  %   % convert from uint8 to single  %   im_data = single(im_data);  %   % reshape to a fixed size (e.g., 227x227).  %   im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear');  %   % subtract mean_data (already in W x H x C with BGR channels)  %   im_data = im_data - mean_data;  % If you have multiple images, cat them with cat(4, ...)  % Add caffe/matlab to you Matlab search PATH to use matcaffe  % if exist('../+caffe', 'dir')     %   addpath('..');  % else  %   error('Please run this demo from caffe/matlab/demo');  % end  % Set caffe mode  if exist('use_gpu', 'var') && use_gpu    caffe.set_mode_gpu();    gpu_id = 0;  % we will use the first gpu in this demo    caffe.set_device(gpu_id);  else    caffe.set_mode_cpu();  end  % Initialize the network using BVLC CaffeNet for image classification  % Weights (parameter) file needs to be downloaded from Model Zoo.  model_dir = 'E:/Caffe/caffe-master/models/bvlc_reference_caffenet/';    % 模型所在目录  net_model = [model_dir 'deploy.prototxt'];              % 模型描述文件,注意是deploy.prototxt,不包含data layers  net_weights = [model_dir 'bvlc_reference_caffenet.caffemodel'];   % 模型权值文件,需要预先下载到这里  phase = 'test'; % run with phase test (so that dropout isn't applied)   % 只进行分类,不做训练  if ~exist(net_weights, 'file')    error('Please download CaffeNet from Model Zoo before you run this demo');  end  % Initialize a network  net = caffe.Net(net_model, net_weights, phase);   % 初始化网络  if nargin < 1    % For demo purposes we will use the cat image    fprintf('using caffe/examples/images/cat.jpg as input image\n');    im = imread('E:/Caffe/caffe-master/examples/images/cat.jpg');    % 获取输入图像  end  % prepare oversampled input  % input_data is Height x Width x Channel x Num  tic;  input_data = {prepare_image(im)};         % 图像冗余处理  toc;  % do forward pass to get scores  % scores are now Channels x Num, where Channels == 1000  tic;  % The net forward function. It takes in a cell array of N-D arrays  % (where N == 4 here) containing data of input blob(s) and outputs a cell  % array containing data from output blob(s)  scores = net.forward(input_data);      %  分类,得到scores  toc;  scores = scores{1};  scores = mean(scores, 2);  % 取所有分类结果的平均值  [~, maxlabel] = max(scores);  % 找到最大概率对应的标签号  % call caffe.reset_all() to reset caffe  caffe.reset_all();  % ------------------------------------------------------------------------  function crops_data = prepare_image(im)  % ------------------------------------------------------------------------  % caffe/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat contains mean_data that  % is already in W x H x C with BGR channels  d = load('E:/Caffe/caffe-master/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat');  mean_data = d.mean_data;  IMAGE_DIM = 256;  CROPPED_DIM = 227;  % Convert an image returned by Matlab's imread to im_data in caffe's data  % format: W x H x C with BGR channels  im_data = im(:, :, [3, 2, 1]);  % permute channels from RGB to BGR  im_data = permute(im_data, [2, 1, 3]);  % flip width and height  im_data = single(im_data);  % convert from uint8 to single  im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear');  % resize im_data  im_data = im_data - mean_data;  % subtract mean_data (already in W x H x C, BGR)  % oversample (4 corners, center, and their x-axis flips)  crops_data = zeros(CROPPED_DIM, CROPPED_DIM, 3, 10, 'single');  indices = [0 IMAGE_DIM-CROPPED_DIM] + 1;  n = 1;  for i = indices    for j = indices      crops_data(:, :, :, n) = im_data(i:i+CROPPED_DIM-1, j:j+CROPPED_DIM-1, :);      crops_data(:, :, :, n+5) = crops_data(end:-1:1, :, :, n);      n = n + 1;    end  end  center = floor(indices(2) / 2) + 1;  crops_data(:,:,:,5) = ...    im_data(center:center+CROPPED_DIM-1,center:center+CROPPED_DIM-1,:);  crops_data(:,:,:,10) = crops_data(end:-1:1, :, :, 5);  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145

test.m

clearclc  im = imread('../../examples/images/cat.jpg');%读取图片  [scores, maxlabel] = classification_demo(im, 0);%获取得分第二个参数0为CPU,1为GPU  maxlabel;%查看最大标签是谁  figure;x=[1:1000];plot(x,scores);%画出得分情况  axis([1, 1000, -0.1, 0.5]);%坐标轴范围  grid on %有网格  hold onplot(maxlabel,max(scores),'r*');jieguo=strcat(num2str(maxlabel),',');jieguo=strcat(jieguo,num2str(max(scores)));text(im2double(maxlabel)+20,im2double(max(scores)),jieguo);% [maxv,maxl]=findpeaks(im2double(scores),'minpeakdistance',1);% plot(maxl,maxv,'*','color','R');    %绘制最大值点fid = fopen('D:/Software/Caffe/caffe-master/data/ilsvrc12/synset_words.txt', 'r');  %Note:若是没有这个路径,则会报错i=0;  while ~feof(fid)      i=i+1;      lin = fgetl(fid);      lin = strtrim(lin);      if(i==maxlabel)          fprintf('the label of %d is %s\n',i,lin)          break      end  end  figure;imshow(im);%显示图片  str=strcat('分类结果:',lin);title(str);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

设置路径后,运行test.m,结果如下: 



说明最大分类概率的标签号为282,对应的是n02123045 tabby, tabby cat,即这张图片被分到第282类的概率为0.2985。

4、=============================================================================================================

会存在如下的问题,


solution:59行修改为:addpath('../../Build/x64/Release/matcaffe');


solution:需要下载相应的模型到里面下面的路径下


原创粉丝点击