windows-caffe 訓練和測試自己的數據集

對應博客地址
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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章