tingpan

生命不息,折腾不止!
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

bvlc_reference_caffenet网络权值可视化

Posted on 2018-07-20 22:17  tingpan  阅读(1044)  评论(0编辑  收藏  举报

一、网络结构

models/bvlc_reference_caffenet/deploy.prototxt

deploy

 

 

二、显示conv1的网络权值

clear;
clc;
close all;
addpath('matlab')
caffe.set_mode_cpu();
caffe.version()
net = caffe.Net('models/bvlc_reference_caffenet/deploy.prototxt',...
'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel', 'test');
net.layer_names
net.blob_names
conv1_layer = net.layer_vec(2);
blob1 = conv1_layer.params(1);%权重,params(2)为偏置
w = blob1.get_data();
size(w)%11 11 3 96
W = zeros(11*3, 11*96);%96张小图片,每张小图片11*11*3,输出的卷积个数为96
for u = 1:3
    for v = 1:96
        W(11*(u-1) + (1:11), 11*(v-1) + (1:11)) = w(:, :, u, v)';%矩阵块拷贝
    end
end

W = W - min(min(W));
W = W/(max(max(W)))*255;%归一化到[0,255]
W = uint8(W);
W = [W, zeros(size(W, 1), 4*11)];%扩展矩阵,大小为(11x3,4x11)
WW = cat(3, W(1:11, :), W(12:22, :), W(23:33, :));%将二维矩阵按通道拆分为3个二维矩阵
W = zeros(10*12, 10*12, 3);%用于显示的彩色图片,初始化为黑色
for u = 1:10
    for v = 1:10
        W((u-1)*12 + (1:11), (v-1)*12 + (1:11), :) = WW(:, (u-1)*11*10 + (v-1)*11 + (1:11), :);%将11行每11列的数据块复制到W,复制3个通道
    end
end
W = uint8(W);
figure; imshow(W);

输出:

ans =

1.0.0


ans =

  24×1 cell 数组

    'data'
    'conv1'
    'relu1'
    'pool1'
    'norm1'
    'conv2'
    'relu2'
    'pool2'
    'norm2'
    'conv3'
    'relu3'
    'conv4'
    'relu4'
    'conv5'
    'relu5'
    'pool5'
    'fc6'
    'relu6'
    'drop6'
    'fc7'
    'relu7'
    'drop7'
    'fc8'
    'prob'


ans =

  15×1 cell 数组

    'data'
    'conv1'
    'pool1'
    'norm1'
    'conv2'
    'pool2'
    'norm2'
    'conv3'
    'conv4'
    'conv5'
    'pool5'
    'fc6'
    'fc7'
    'fc8'
    'prob'


ans =

    11    11     3    96

 

image

 

三、其他卷积层网络权值可视化

1、visualize_weights.m

function[] = visualize_weights(w,s)
h = max(size(w, 1), size(w, 2)); %Kernel size
g = h + s; %Grid size, larger than Kernel size for better visual effects.

%Normalization for gray scale
w = w - min(min(min(min(w))));
w = w/max(max(max(max(w))))*255;
w = uint8(w);

W = zeros(g*size(w, 3), g*size(w, 4));%用于保存权值的数据,通道数*g 行, 卷积核数*g 列
for u = 1:size(w, 3)
    for v = 1:size(w, 4)
        W(g*(u-1) + (1:h), g*(v-1) + (1:h)) = w(:, :, u, v)';
    end
end
W = uint8(W);
figure; imshow(W);

2、caffenet_weights_vis.m

clear;
clc;
close all;
addpath('matlab')
caffe.set_mode_cpu();
sprintf(['Caffe Version = ', caffe.version(), '\n']);
net = caffe.Net('models/bvlc_reference_caffenet/deploy.prototxt',...
'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel', 'test');

sprintf('Load net done. Net layers: ');
net.layer_names

sprintf('Net blobs: ');
net.blob_names

%Conv1 Weight Visualization
conv1_layer = net.layer_vec(2);
blob1 = conv1_layer.params(1);
w = blob1.get_data();
sprintf('Conv1 Weight shape:');
size(w)
visualize_weights(w, 1);

%Conv2 Weight Visualization
conv2_layer = net.layer_vec(6);
blob2 = conv2_layer.params(1);
w2 = blob2.get_data();
sprintf('Conv2 Weight shape:');
size(w2)
visualize_weights(w2, 1);

%Conv3 Weight Visualization
conv3_layer = net.layer_vec(10);
blob3 = conv3_layer.params(1);
w3 = blob3.get_data();
sprintf('Conv3 Weight shape:');
size(w3)
visualize_weights(w3, 1);

%Conv4 Weight Visualization
conv4_layer = net.layer_vec(12);
blob4 = conv4_layer.params(1);
w4 = blob4.get_data();
sprintf('Conv4 Weight shape:');
size(w4)
visualize_weights(w4, 1);

%Conv5 Weight Visualization
conv5_layer = net.layer_vec(14);
blob5 = conv5_layer.params(1);
w5 = blob5.get_data();
sprintf('Conv5 Weight shape:');
size(w5)
visualize_weights(w5, 1);

3、输出

ans =

  24×1 cell 数组

    'data'
    'conv1'
    'relu1'
    'pool1'
    'norm1'
    'conv2'
    'relu2'
    'pool2'
    'norm2'
    'conv3'
    'relu3'
    'conv4'
    'relu4'
    'conv5'
    'relu5'
    'pool5'
    'fc6'
    'relu6'
    'drop6'
    'fc7'
    'relu7'
    'drop7'
    'fc8'
    'prob'


ans =

  15×1 cell 数组

    'data'
    'conv1'
    'pool1'
    'norm1'
    'conv2'
    'pool2'
    'norm2'
    'conv3'
    'conv4'
    'conv5'
    'pool5'
    'fc6'
    'fc7'
    'fc8'
    'prob'


ans =

    11    11     3    96


ans =

     5     5    48   256


ans =

     3     3   256   384

警告: 图像太大,无法在屏幕上显示;将以 67% 显示 
> In images.internal.initSize (line 71)
  In imshow (line 327)
  In visualize_weights (line 17)
  In caffenet_weights_vis (line 38) 

ans =

     3     3   192   384


ans =

     3     3   192   256

>>

 

 

 

end