Windows下用Matlab加载caffemodel做图像分类
2016-03-19 11:16
726 查看
1.编译caffe的matlab接口
用到了happynear提供的caffe-windows-master,编译caffe和matlab接口的过程看这里。编译好之后,caffe-windows-master\matlab\+caffe\private内的文件如下:如果嫌麻烦,可以直接跳过1,下载我编译好的matlab接口,以及happynear提供的第三方库的dll(在bin文件夹):
下载地址:
matlab接口;
bin文件夹;
注意:记得将该bin文件夹加入系环境变量;
2.修改matlab\demo\classification_demo.m
将bin文件夹加入环境变量后,修改matlab\demo\classification_demo.m,可以参考我修改的代码:function classification_demo() close all; clc; clear mex; clear is_valid_handle; caffe.reset_all(); if isdir('log') rmdir('log','s'); end net_model = 'deploy.prototxt'; net_weights = 'Net_iter_150000.caffemodel'; label_file = 'car_words.txt'; im_path = 'car\'; phase = 'test'; % run with phase test (so that dropout isn't applied) use_gpu=true; gpu_id=0; if exist('../+caffe', 'dir') addpath('..') else error('error'); end % Set caffe mode if use_gpu caffe.set_mode_gpu(); caffe.set_device(gpu_id); else caffe.set_mode_cpu(); end if ~exist(net_weights, 'file') error('模型文件不存在!'); end % Initialize a network net = caffe.Net(net_model, net_weights, phase); Files = dir(im_path); im_names = cell(1,length(Files)-2); for j = 3:length(Files) im_names{j-2} = Files(j).name; end for j = 1:length(im_names) im = imread([im_path, im_names{j}]); 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 [maxscores, maxlabel] = max(scores); fid=fopen(label_file,'r','n','UTF-8'); for ii = 1:maxlabel tline=fgetl(fid); end fclose(fid); label=tline; label=strcat(label,':',num2str(maxscores)); imshow(im); text(50,50,label,'fontsize',20,'color','r'); pause(); % call caffe.reset_all() to reset caffe if j == length(im_names) caffe.reset_all(); end end % ------------------------------------------------------------------------ function crops_data = prepare_image(im) d = load('+caffe/imagenet/ilsvrc_2012_mean.mat'); mean_data = d.mean_data; IMAGE_DIM = 256; CROPPED_DIM = 224; % 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);
其中,net_model是模型定义prototxt文件;net_weights是训练的到的caffemodel;label_file是标签文件。
CROPPED_DIM = 224;修改成你的模型输入大小。
(因为不知道怎么用matlab读取binaryproto文件,所以这里均值文件用的是ilsvrc2012的均值文件。之前试过用c++读取binaryproto文件并保存为jpg,但是感觉效果不是很好。)
原来它是测试一张图片的,这里改为测试整个文件夹的图片;
3.结果
相关文章推荐
- matlab练习程序(TV模型图像修复)
- numpy和matlab计算协方差矩阵的不同(matlab是标准的,numpy相当于转置后计算)
- MATLAB与C混合编程
- GMM的EM算法实现
- 【Matlab】图像裁剪函数imcrop的原点、长度、宽度问题
- 【Matlab】函数imread的返回值
- 【matlab】:matlab如何实现切分图片并保存?
- Matlab从图像中选取矩形区域
- 数据挖掘-全面的MAC上matlab安装libsvm
- Matlab中K-means聚类算法的使用(K-均值聚类)
- 【MATLAB】用MATLAB绘制奥运五环
- 【MATLAB】用MATLAB绘制奥运五环
- 利用cuda加速MATLAB程序
- MATLAB读写Excel
- Matlab读取CSV文件
- 数据挖掘-SVM(支持向量机)的matlab基础DEMO详细注解
- matlab基础语法笔记(二)
- Matlab的regionprops详解
- kd树简介 在matlab下VLFeat中的kd-tree使用
- 局部敏感哈希matlab代码解读