您的位置:首页 > Web前端

4000 windows-caffe 训练和测试自己的数据集

2017-01-02 11:43 531 查看
对应博客地址

http://www.jianshu.com/p/607f1e51e3ab

http://www.cnblogs.com/denny402/p/5083300.html

两个博客的混合体

一、准备数据

二、转换为lmdb格式

(以下命令的执行,都是在D:\caffe-windows-master\caffe-windows-master 路径下执行的 )

其他文件都是在该路径下 为基础进行的

(1)

bin\convert_imageset.exe --shuffle --resize_height=256 --resize_width=256  data\re\  examples\myfile\train.txt examples\myfile\img_train_lmdb


//1 运行程序名称(转化为lmdb格式)、路径 2 切割尺寸大小参数 3图片文件上层目录 4图片文件所在目录及名称(txt) 5 目的文件

(如果文件夹中已经存在img_train_lmdb,再次运行会报错,应该先删除该文件,再次运行 当为了验证是否成功配置windows-caffe的时候 )

(2)

bin\convert_imageset.exe --shuffle --resize_height=256 --resize_width=256  data\re\  examples\myfile\test.txt examples\myfile\img_test_lmdb


//1 运行程序名称(转化为lmdb格式)、路径 2 切割尺寸大小参数 3图片文件上层目录 4图片文件所在目录及名称(txt) 5 目的文件

三、计算均值并保存

(1)

bin\compute_image_mean.exe  examples\myfile\img_train_lmdb examples\myfile\mean.binaryproto


// 1、程序名称 2 源文件路径、名称 3 目标文件

四 创建模型并编写配置文件

1、修改其中的solver.prototxt

2、修改train_val.protxt

五 训练和测试

bin\caffe.exe train -solver examples\myfile\solver.prototxt  (需要用到的图片路径包含在train_val.protxt里,而train_val.protxt包含在solver.prototxt )


// 1、程序名称 2、功能 3、配置文件

注意:

一、

solver.prototxt文件中

#snapshot: 1000

#snapshot_prefix: “regression_test\example_ising”

这两行要注释掉 也就是要加上 # 符号,否则无法运行

二、solver.prototxt中指明的网络配置文件中,源文件 source 路径要正确

一般运行不了,都是因为路径配置的问题

七、matcaffe 接口在matlab中完成这一训练和测试过程

(1)将caffe-windows-master根目录下的所有文件夹都添加进路径中

caffe.set_mode_gpu();
gpu_id = 0
caffe.set_device(gpu_id);
solver =caffe.Solver('.\examples\myfile\solver.prototxt');
solver.solve();


日志文件还是在log文件夹当中,而训练所得到的网络权重在solver.prototxt文件

snapshot_prefix: "examples/myfile/caffenet_train_test"


指定的路径下

有的时候,运行solver.solve()语句一次并不能让训练完成(即满足设定的迭代次数),

可查看log日志信息,然后重复运行该命令即可。

八、matlab测试单幅图片

(1)测试之前,我们首先要修改训练时的网络结构,许多参数需要去掉,我的测试网络结构文件和caffe-windows-master中models\bvlc_reference_caffenet\deploy.prototxt相同,大家可以比较一下训练网络和测试网络的不同,主要是train网络结构的最后两层:Accuracy和SoftmaxWithLoss替换为测试网络的Softmax层。

(2)之前我们利用命令行生成了训练图片的均值文件 mean.binaryproto

在matlab中,需要转化为.matlab data格式。这里可以利用matlab/+caffe/private/io.m

中的read_mean函数,直接将mean.binaryproto转化为matlab格式,这一点十分便捷!!!

function mean_data = read_mean(mean_proto_file)

CHECK(ischar(mean_proto_file), 'mean_proto_file must be a string');
CHECK_FILE_EXIST(mean_proto_file);
mean_data = caffe_('read_mean', mean_proto_file);
end


这里我将CHECK语句注释掉啦,直接运行。

(3)加载图片,做分类

%读取一张测试图片
im = imread('\data\re\test\700.jpg');
%调用.m文件
[scores,maxlabel]=matcaffe_test(im,1)
%输出结果为8,标签类型是7(这里有一些疑惑,需要思考一下)
%其实是因为,caffe中标签是从0开始,(在caffe中,标签是数组的索引,而caffe是用C++实现,索引是从0开始),而matlab数组下标是从1开始,这就导致了,matlab输出结果比标签值大1


matcaffe_test.m文件内容如下:

function [scores, maxlabel] = matcaffe_test(im, use_gpu)
% [scores, maxlabel] = classification_demo(im, use_gpu)
%
% Image classification demo using BVLC CaffeNet.
%
% IMPORTANT: before you run this demo, you should download BVLC CaffeNet
% from Model Zoo (http://caffe.berkeleyvision.org/model_zoo.html)
%
% ****************************************************************************
% 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 HxWx4
%   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-4.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);
%  [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 = 'examples/myfile/';
net_model = [model_dir 'deploy.prototxt'];
net_weights = [model_dir 'caffenet_train_test_iter_100.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('../../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);
toc;

scores = scores{1};
scores = mean(scores, 2);  % take average scores over 10 crops

[~, 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('images_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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: