【caffe-windows】 caffe-master 之 卷積核可視化(利用matlab)

來源:互聯網
上載者:User

標籤:iss   examples   .net   imshow   sso   讀取   view   term   ring   

此篇是利用matlab對caffemodel的卷積核進行可視化。只介紹了卷積核的可視化,不涉及特徵圖的可視化。 
是參考此部落格: http://blog.csdn.net/zb1165048017/article/details/52643188

前期準備,需要兩個東西 
1. 模型的描述檔案 deploy.prototxt 
2. 模型本身lenet_iter_10000.caffemodel (此處用的examples中的mnist裡的)

第一步:

在建立D:\caffe-master\matlab\demo 下建立 visualizing.m

clcclearaddpath(‘..‘) % 加入+caffe路徑  caffe.set_mode_cpu() ;% 設定CPU模式  model = ‘D:/caffe-master/examples/mnist/lenet.prototxt‘; % 模型描述  weights = ‘D:/caffe-master/examples/mnist/lenet_iter_10000.caffemodel‘; % 參數  net = caffe.Net(model,‘test‘); % 讀取netweight_partvisual( net, 1,1)  % 調用部分顯示函數 weight_partvisual( net,layer_num ,channels_num )                                 %  layer_num是第幾個卷積層, channels_num 表示                               %  顯示第幾個通道的卷積核,取值範圍為 (0,上一層的特徵圖數)
第二步:

在建立D:\caffe-master\matlab\demo 下建立weight_partvisual.m

function [  ] = weight_partvisual( net,layer_num ,channels_num )  layers=net.layer_names;  convlayer=[];  for i=1:length(layers)      if strcmp(layers{i}(1:3),‘con‘)          convlayer=[convlayer;layers{i}];      end  end  w=net.layers(convlayer(layer_num,:)).params(1).get_data();  b=net.layers(convlayer(layer_num,:)).params(2).get_data();  w=w-min(w(:));  w=w/max(w(:))*255;  weight=w(:,:,channels_num,:);%四維,核長*核寬*核左邊輸入*核右邊輸出(核個數)  [kernel_r,kernel_c,input_num,kernel_num]=size(w);  map_row=ceil(sqrt(kernel_num));%行數  map_col=map_row;%列數  weight_map=zeros(kernel_r*map_row,kernel_c*map_col);  kernelcout_map=1;  for i=0:map_row-1      for j=0:map_col-1          if kernelcout_map<=kernel_num              weight_map(i*kernel_r+1+i:(i+1)*kernel_r+i,j*kernel_c+1+j:(j+1)*kernel_c+j)=weight(:,:,:,kernelcout_map);              kernelcout_map=kernelcout_map+1;          end      end  end  figure  hAxe=axes(‘Parent‘,gcf,... % 設定新的axe, 將‘parent‘ 屬性設定為當前視窗gcf    ‘Units‘,‘pixels‘,...     %設定單位為pixels    ‘Position‘,[500 0 605 705]);  % 指定axe的位置 left和bottom設定了axe的左下角座標,width和height設定了視窗的寬度和高度axes(hAxe);imshow(uint8(weight_map))  str1=strcat(‘weight num:‘,num2str(kernelcout_map-1));  title(str1)  end  

運行 visualizing.m

結果 

感覺看不出什麼規律來,是否因為mnist映像太小? 而像訓練imagenet時模型輸入是 256*256,因此訓練得到的卷積核看起來有一些規律(類似邊緣)。

PS: 這裡用的是將 權值(w -min(w) / max(w) ) *255 
這個原理沒搞明白,如果有清楚的同學告訴我吧,THX~

【caffe-windows】 caffe-master 之 卷積核可視化(利用matlab)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.