matlab列优先与高维矩阵重构

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

matlab列优先与高维矩阵重构_第1张图片

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

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

 1  clear
 2  close all
 3 
 4 
 5 addpath ./matlab
 6 
 7 model= './models/bvlc_reference_caffenet/deploy.prototxt';
 8 weights= './models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel';
 9 
10  
11  net = caffe.Net(model, weights, 'test'); % create net and load weights
12 
13 %% obtain params in diff layers and show
14 pdata = net.params('conv1',1).get_data();
15 
16 vis_square(pdata,2,0.5);
17  
18   
19 net.blobs('data').reshape([227 227 3 1]);
20 net.reshape();
21 
22 %% prepare the image
23 im_data = caffe.io.load_image('./examples/images/cat.jpg');
24  mean = load('./matlab/+caffe/imagenet/ilsvrc_2012_mean.mat');
25 
26 %% subtract mean_data (already in W x H x C, BGR)
27  mean_data = mean.mean_data;
28  im_data = im_data - mean_data;  
29 
30  width = 227; height = 227;
31  im_data = imresize(im_data, [width, height]); % resize using Matlab's imresize
32  res = net.forward({im_data});
33 
34  prob = res{1};
35 
36 %% obtain features and show
37  ddata = net.blobs('conv2').get_data();
38  vis_square(ddata,2,0);

MPCA可用于高维数据进行降维可与LDA结合 

 1 function vis_square(data,padsize,padval)
 2 
 3  data=net_data_normalize(data);
 4 
 5 if ~ exist('padsize', 'var') 
 6   padsize=1;
 7 end
 8 if ~ exist('padval', 'var') 
 9   padval=0;
10 end
11 ndim=ndims(data);
12 % w*num*h*chanel
13 if ndim==4
14     fprintf('visualize params\n');
15     data=permute(data,[1,4,2,3]);
16 else ndim==3
17     fprintf('visualize maps\n');
18     data=permute(data,[1,3,2]);
19 end
20     
21     n = (ceil(sqrt(size(data,2))));
22     data=padarray(data,[padsize n^2-size(data,2) padsize 0],'post');
23     data=reshape(data,size(data,1),n,n,size(data,3),size(data,4));
24     data=permute(data,[1,3,4,2,5]);
25     data=reshape(data,[size(data,1)*n,size(data,3)*n,size(data,5)]);
26    
27 figure
28 if ndim==4
29     ;
30 else ndim==3
31     data=imrotate(data,-90);
32 end   
33 
34 imshow(imresize(data,[500,500],'nearest'))
35 
36 end

你可能感兴趣的:(matlab,矩阵,重构)