win7 caffe使用笔记——权值可视化

1.编译matcaffe,用到matlab接口

microsoft caffe版本配置matlab2014,有个无法打开包括文件 "gpu/mxGPUArray.h"问题。

在matcaffe工程属性增加包含目录G:\Matlab2014\toolbox\distcomp\gpu\extern\include即可通过编译。


2.在matlab中使用caffe还需配置路径。

matlab主页设置路径,增加文件夹G:\caffe-master\Build\x64\Release\matcaffe


3.Matlab主函数代码置于caffe根目录下

caffenet_weights_vis.m

clear;
clc;
close all;
addpath('matlab')
caffe.set_mode_cpu();
fprintf(['Caffe Version = ', caffe.version(), '\n']);

net = caffe.Net('models/bvlc_reference_caffenet/deploy.prototxt', 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel', 'test');

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

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

% Conv1 Weight Visualization
conv1_layer = net.layer_vec(2);
blob1 = conv1_layer.params(1);
w = blob1.get_data();
fprintf('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();
fprintf('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();
fprintf('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();
fprintf('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();
fprintf('Conv5 Weight shape: ');
size(w5)
visualize_weights(w5, 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));
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);

对应net.layer_names中的卷积层,输出各层的权值图。自己写的网络如果无法通过,建议在deploy.prototxt上修改。


conv1权值图


conv2权值图




你可能感兴趣的:(caffe)