Caffe 特征图可视化

特征图即网络在前向传播阶段的各层输出。 http://blog.csdn.net/u012938704/article/details/52767695
这里使用一个训练过的CaffeNet模型对一张猫的图片进行分类,使用matlab绘制出卷积层的特征图。

今天在使用matcaffe时遇到了CUDNN_STATUS_ARCH_MISMATCH的错误,根据http://blog.csdn.net/songyu0120/article/details/43233907,无奈之下禁止了cudnn加速,重新编译了一下Caffe。

关于matcaffe接口的使用参考这篇博文http://blog.csdn.net/ws_20100/article/details/50525879
代码参照《21天实战Caffe》与Caffe官方示例classification_demo.m。

程序包含三个文件:featuremap_visualize.m;visualize_feature_maps.m;prepare_image.m。均放在Caffe根目录下。

featuremap_visualize.m

clear;
clc;
close all;

addpath('matlab');
caffe.set_mode_cpu();

model_dir = 'models/bvlc_reference_caffenet/';
net_model = [model_dir 'deploy.prototxt'];
net_weights = [model_dir 'bvlc_reference_caffenet.caffemodel'];
phase = 'test'; % run with phase test (so that dropout isn't applied)

% Initialize a network
net = caffe.Net(net_model, net_weights, phase);

im=imread('examples/images/cat.jpg');
figure();imshow(im);title('Original Image');

input_data={prepare_image(im)};

scores=net.forward(input_data);

blob_names={'data','conv1','conv2','conv3','conv4','conv5'};
for i=1:length(blob_names)
    visualize_feature_maps(net,blob_names{i},1);
end

prepare_image.m

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('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);

visualize_feature_maps.m

function []=visualize_feature_maps(net,blob_name,space)
%space 为 特征图之间的间距
blob=net.blobs(blob_name).get_data();
blob_width=max(size(blob,1),size(blob,2));
ceil_width=blob_width+space;
channels=size(blob,3);
ceil_num=ceil(sqrt(channels));%每行或每列特征图个数
Map=zeros(ceil_width*ceil_num,ceil_width*ceil_num);
for u=1:ceil_num
    for v=1:ceil_num
        w=zeros(blob_width,blob_width);
        if(((u-1)*ceil_num+v)<=channels)
            w=blob(:,:,(u-1)*ceil_num+v,1)';
            w=w-min(min(w));%保证为非负数
            w=w/max(max(w))*255;%归一化
        end
        Map(ceil_width*(u-1)+(1:blob_width),ceil_width*(v-1)+(1:blob_width))=w;
    end
end
Map=uint8(Map);
figure();
imshow(Map);
colormap(jet);caxis([0 255]);%伪彩色显示
colorbar;
title(blob_name);


展开阅读全文

没有更多推荐了,返回首页