caffe 17 win10 caffe 数据可视化

本练习代码源自《深度学习21天实战caffe》

01 mnist 数据可视化

*本练习上接 [caffe 04 win10 运行examples的mnist测试用例]
(http://blog.youkuaiyun.com/longji/article/details/61446478)*

一定要注意需要为matlab设置环境,在path环境变量中加入:D:\git\DeepLearning\caffe\build\x64\install\bin
这里写图片描述

01.01 编写matlab代码

代码文件:D:\git\DeepLearning\caffe\data\mnist\show_mnist_data.m

% D:\git\DeepLearning\caffe\data\mnist\show_mnist_data.m
clear;
clc;
close all;

% 在 D:\git\DeepLearning\caffe\data\mnist\ 目录下的数据文件
image_file_name = 't10k-images.idx3-ubyte';
index_file_name = 't10k-labels.idx1-ubyte';

fid1 = fopen(image_file_name, 'rb');
fid2 = fopen(index_file_name, 'rb');

images_data = fread(fid1, 'uint8');
indexs_data = fread(fid2, 'uint8');

fclose(fid1);
fclose(fid2);

images_data = images_data(17:end);
indexs_data = indexs_data(9:end);
image_buffer = zeros(28, 28);

for k = 1 : 100: length(images_data)/28/28
  figure(100);
  for t = 1 : 100
    image_buffer = reshape(images_data((k + t - 2) * 28 * 28 + 1 : (k + t - 1) * 28 * 28), 28, 28);
    subplot(10, 10, t);
    imshow(uint8(image_buffer)');
    title(num2str(indexs_data(k + t -1)));
  end
  pause;
end

01.02在matlab中运行mnist可视化代码

这里写图片描述

02 cifar10数据可视化

02.01 写matlab可视化代码

matlab代码文件:D:\git\DeepLearning\caffe\data\cifar10\show_cifar10_data.m

% data/cifar10/show_cifar10_data.m
clear;
clc;
close all;
strings = {
  'airplane'
  'automobile'
  'bird'
  'cat'
  'deer'
  'dog'
  'frog'
  'horse'
  'ship'
  'truck'
};
image_file_name = 'cifar-10-batches-bin/data_batch_1.bin';
fid1 = fopen(image_file_name, 'rb');
images_data = fread(fid1, 'uint8');
fclose(fid1);

images_data = reshape(images_data, 3073, [])';
image_idx = images_data(:, 1);

for k = 1: 100 : size(images_data, 1)
  figure(100);
  for t = 1 : 100
    image_r = reshape(images_data(k + t - 1, 2: 1025), 32, [])';
    image_g = reshape(images_data(k + t - 1, 1026 : 2049), 32, [])';
    image_b = reshape(images_data(k + t - 1, 2050 : 3073), 32, [])';
    image_buffer = cat(3, image_r, image_g, image_b);
    subplot(10, 10, t);
    imshow(uint8(image_buffer));
    title(strings{image_idx(k + t - 1) + 1});
  end
  input('Press Enter to next picture :');
  pause;
end

02.02 在matlab中运行结果

这里写图片描述

03 权值可视化

03.01 Caffenet Conv1 权值可视化

matlat代码:D:\git\DeepLearning\caffe\conv1_weights_vis.m

% 网络权值可视化,D:\git\DeepLearning\caffe\conv_weights_vis.m
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);
w = blob1.get_data();
size(w)
W = zeros(11*3, 11*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;
W = uint8(W);
W = [W, zeros(size(W, 1), 4 * 11)];
WW = cat(3, W(1:11, :), W(12:22, :), W(23:33, :));
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), :);
  end
end
W = uint8(W);
figure;imshow(W);

运行结果:
这里写图片描述

03.02 Conv2-Conv5权值可视化

代码文件:D:\git\DeepLearning\caffe\visualize_weights.m

% D:\git\DeepLearning\caffe\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);

代码文件:D:\git\DeepLearning\caffe\caffenet_weights_vis.m

% 网络权值可视化,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 Visualizetion
conv1_layer = net.layer_vec(2);
blob1 = conv1_layer.params(1);
w1 = blob1.get_data();
fprintf('Conv1 Weight shape: ');
size(w1)
visualize_weights(w1, 1);

%Conv2 Weight Visualizetion
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 Visualizetion
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);

%Conv3 Weight Visualizetion
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 Visualizetion
conv4_layer = net.layer_vec(10);
blob4 = conv4_layer.params(1);
w4 = blob4.get_data();
fprintf('Conv4 Weight shape: ');
size(w4)
visualize_weights(w4, 1);

%Conv5 Weight Visualizetion
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);

运行结果:
这里写图片描述

04 特征图可视化

04.01 matlab源码

D:\git\DeepLearning\caffe\visualize_feature_maps.m

% 特征图可视化 D:\git\DeepLearning\caffe\visualize_feature_maps.m
function [] = visualize_feature_maps(w, s)
h = max(size(w, 1), size(w, 2)); % Feature map size
g = h + s;
c = size(w, 3);
cv = ceil(sqrt(c));
W = zeros(g * cv, g * cv);

for u = 1: cv
  for v = 1: cv
    tw = zeros(h, h);
    if (((u - 1) * cv + v) <= c)
      tw = w(:, :, (u - 1) * cv + v, 1)';
      tw = tw - min(min(tw));
      tw = tw / max(max(tw)) * 255;
    end
    W(g * (u - 1) + (1: h), g * (v -1) + (1:h)) = tw;
  end
end
W = uint8(W);
figure;imshow(W);

D:\git\DeepLearning\caffe\fm_visual.m

% 特征图可视化,放在caffe根目录下,D:\git\DeepLearning\caffe\fm_visual.m
clear;
clc;
close all;
% 设置接口路径(D:\git\DeepLearning\caffe\下面的matlab)
mat_fullname = mfilename('fullpath');
i = strfind(mat_fullname,'\');
work_path=mat_fullname(1: i(end));
userpath(work_path);
cd(work_path);
addpath('matlab');
% 设置设备类型为cpu
caffe.set_mode_cpu();
% 打印caffe版本号
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

fprintf('Now preparing data...\n');
im = imread('examples/images/cat.jpg');
figure;imshow(im);title('Original Image');
d = load('matlab/+caffe/imagenet/ilsvrc_2012_mean.mat');
mean_data = d.mean_data;
IMAGE_DIM = 256;
CROPPED_DIM = 227;

% Convert an image returned by Matlab's imread to im_data in caffe's data
% format: W X H X C with BGR channels
im_data = im(:, :, [3, 2, 1]); % permute channels from RGB to BGR
im_data = permute(im_data, [2, 1, 3]); % flip width and height
im_data = single(im_data); % convert from uint8 to single
im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear'); % resize im_data
im_data = im_data - mean_data; % subtract mean_data (already in W X H X C, BGR)
im = imresize(im_data, [CROPPED_DIM CROPPED_DIM], 'bilinear'); % resize im_data
km = cat(4, im, im, im, im, im);
pm = cat(4, km, km);
input_data = {pm};

scores = net.forward(input_data);

scores = scores{1};
scores = mean(scores, 2); % take average scores over 10 crops

[~, maxlabel] = max(scores);

maxlabel
figure;plot(scores);

fm_data = net.blob_vec(1);
dl = fm_data.get_data();
fprintf('Data size = ');
size(dl)
visualize_feature_maps(dl, 1);

fm_conv1 = net.blob_vec(2);
f1 = fm_conv1.get_data();
fprintf('Feature map conv1 size = ');
size(f1)
visualize_feature_maps(f1,1);

fm_conv2 = net.blob_vec(5);
f2 = fm_conv2.get_data();
fprintf('Feature map conv2 size = ');
size(f2)
visualize_feature_maps(f2,1);

fm_conv3 = net.blob_vec(8);
f3 = fm_conv3.get_data();
fprintf('Feature map conv3 size = ');
size(f3)
visualize_feature_maps(f3,1);

fm_conv4 = net.blob_vec(9);
f4 = fm_conv4.get_data();
fprintf('Feature map conv4 size = ');
size(f4)
visualize_feature_maps(f4,1);

fm_conv5 = net.blob_vec(10);
f5 = fm_conv5.get_data();
fprintf('Feature map conv5 size = ');
size(f5)
visualize_feature_maps(f5,1);

这里写图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值