由于matlab在列化a(:)以及reshape(a)等操作中是列优先的,所以要重构出新的高维度矩阵,通常要把reshape和permute结合起来使用。

matlab列优先与高维矩阵重构 及 CNN 逐层可视化 on Matlab-LMLPHP

先到 http://caffe.berkeleyvision.org/ 下载 训练好的model bvlc_reference_caffenet.caffemodel;

更多caffe使用也请参看上面的网址。

  clear
close all addpath ./matlab model= './models/bvlc_reference_caffenet/deploy.prototxt';
weights= './models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'; net = caffe.Net(model, weights, 'test'); % create net and load weights %% obtain params in diff layers and show
pdata = net.params('conv1',).get_data(); vis_square(pdata,,0.5); net.blobs('data').reshape([ ]);
net.reshape(); %% prepare the image
im_data = caffe.io.load_image('./examples/images/cat.jpg');
mean = load('./matlab/+caffe/imagenet/ilsvrc_2012_mean.mat'); %% subtract mean_data (already in W x H x C, BGR)
mean_data = mean.mean_data;
im_data = im_data - mean_data; width = ; height = ;
im_data = imresize(im_data, [width, height]); % resize using Matlab's imresize
res = net.forward({im_data}); prob = res{}; %% obtain features and show
ddata = net.blobs('conv2').get_data();
vis_square(ddata,,);
 function vis_square(data,padsize,padval)

  data=net_data_normalize(data);

 if ~ exist('padsize', 'var')
padsize=;
end
if ~ exist('padval', 'var')
padval=;
end
ndim=ndims(data);
% w*num*h*chanel
if ndim==
fprintf('visualize params\n');
data=permute(data,[,,,]);
else ndim==
fprintf('visualize maps\n');
data=permute(data,[,,]);
end n = (ceil(sqrt(size(data,))));
data=padarray(data,[padsize n^-size(data,) padsize ],'post');
data=reshape(data,size(data,),n,n,size(data,),size(data,));
data=permute(data,[,,,,]);
data=reshape(data,[size(data,)*n,size(data,)*n,size(data,)]); figure
if ndim==
;
else ndim==
data=imrotate(data,-);
end imshow(imresize(data,[,],'nearest')) end
04-30 01:26