深度学习21天实战实战caffe学习笔记《14:Caffe可视化方法》

1、数据可视化

Matlab:

数据可视化不依赖Caffe环境,可以在任意位置运行代码,建议安装Matlab R2014以上版本;

安装:

cd MATHWORKS_R2014A
sudo ./install -javadir=/opt/jvm/jdk1.8.0_77
注意:

(1).安装过程中使用破解文件夹Crack内Readme.txt中的序列号;安装完成后使用破解文件夹Crack内 license_405329_R2014a.lic进行激活;并将破解文件夹Crack内Linux文件夹内libmwservices.so copy到 /opt/MATLAB/R2014A/bin/glnxa64;完成安装,命令行下使用sudo matlab即可启动使用;

(2).参数:-javadir=/opt/jvm/jdk1.8.0_77是指定jdk的安装路径,因为Matlab的安装程序是用java编写的,jdk安装参考:Ubuntu 16.04配置JDK1.8.0_77教程

python:

Eg:Mnist可视化可以放在$CAFFE_ROOT/data/mnist/下

clc;
clear;
close all;
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');

image_data=fread(fid1,'uint8');
index_data=fread(fid2,'uint8');

fclose(fid1);
fclose(fid2);

image_data=image_data(17:end);
index_data=index_data(9:end);
image_buffer=zeros(28,28);

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

CIFAR10可视化可以放在$CAFFE_ROOT/data/cifar10/下

clear;
clc;
close all;
strings={
    'airplane'
    'automobile'
    'bird'
    'cat'
    'deer'
    'dog'
    'frog'
    'horse'
    'ship'
    'truck'
    };
image_file_name='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));
        figure;
        imshow(uint8(image_buffer));
     %   title(strings{image_idx(k+t-1)+1});
    end
    input('press enter to next picture:');
    pause;
end

ImageNet可以使用Barnes-Hut t-SNE得到与L2距离相关的二维前如图;

例子:mnist的t-SNE可视化

from time import time
from tsne import bh_sne
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
from matplotlib import offsetbox
from sklearn import (manifold, datasets, decomposition, ensemble,
                     discriminant_analysis, random_projection)
from sklearn import decomposition


mnist = input_data.read_data_sets('./input_data', one_hot=False)
sub_sample = 5000
y = mnist.train.labels[0:sub_sample]
X = mnist.train.images[0:sub_sample]

n_samples, n_features = X.shape
n_neighbors = 30


#----------------------------------------------------------------------
# Scale and visualize the embedding vectors
def plot_embedding(X_emb, title=None):
    x_min, x_max = np.min(X_emb, 0), np.max(X_emb, 0)
    X_emb = (X_emb - x_min) / (x_max - x_min)

    plt.figure()
    ax = plt.subplot(111)
    for i in range(X_emb.shape[0]):
        plt.text(X_emb[i, 0], X_emb[i, 1], str(y[i]),
                 color=plt.cm.Set1(y[i] / 10.),
                 fontdict={'weight': 'bold', 'size': 9})

    if hasattr(offsetbox, 'AnnotationBbox'):
        # only print thumbnails with matplotlib > 1.0
        shown_images = np.array([[1., 1.]])  # just something big
        for i in range(sub_sample):
            dist = np.sum((X_emb[i] - shown_images) ** 2, 1)
            if np.min(dist) < 8e-3:
                # don't show points that are too close
                continue
            shown_images = np.r_[shown_images, [X_emb[i]]]
            imagebox = offsetbox.AnnotationBbox(
                offsetbox.OffsetImage(X[i].reshape(28,28)[::2,::2], cmap=plt.cm.gray_r),
                X_emb[i])
            ax.add_artist(imagebox)
    plt.xticks([]), plt.yticks([])
    if title is not None:
        plt.title(title)


#----------------------------------------------------------------------
# Plot images of the digits
n_img_per_row = 20
img = np.zeros((30 * n_img_per_row, 30 * n_img_per_row))
for i in range(n_img_per_row):
    ix = 30 * i + 1
    for j in range(n_img_per_row):
        iy = 30 * j + 1
        img[ix:ix + 28, iy:iy + 28] = X[i * n_img_per_row + j].reshape((28, 28))

plt.imshow(img, cmap=plt.cm.binary)
plt.xticks([])
plt.yticks([])
plt.title('A selection from the 64-dimensional digits dataset')

# t-SNE embedding of the digits dataset
print("Computing t-SNE embedding")
t0 = time()
X_pca = decomposition.TruncatedSVD(n_components=50).fit_transform(X)
# data =X.astype('float64')
X_tsne  = bh_sne(X_pca)

plot_embedding(X_tsne,
               "t-SNE embedding of the digits (time %.2fs)" %
               (time() - t0))

plt.show()
参考: http://scikit-learn.org/stable/auto_examples/manifold/plot_lle_digits.html#sphx-glr-auto-examples-manifold-plot-lle-digits-py

2、模型可视化

2.1、网络结构可视化

可视化网站:http://ethereon.github.io/netscope/quickstart.html

#1、准备python环境
 sudo apt update
 sudo apt install python-pip python-dev python-numpy
 sudo apt install gfortran
 sudo apt install -r ${CAFFE_ROOT}/python/requirements.txt
 sudo apt install pydot
#2、编译pycaffe
 
cd ${CAFFE_ROOT}
sudo make clean
sudo make -j
sudo pycaffe
#绘制网络结构图
 
cd ${CAFFE_ROOT}/python
python draw_net.py ../models/bvlc_reference_caffenet/train_val.prototxt caffenet.png


2.2、网络权值可视化

判断模型优劣的第二种方法:队训练后的模型权值进行可视化;

好的网络权值表现:美观、光滑的滤波器;

不好的网络权值表现:噪声图样(训练时间不够长,或者正则化强度过小导致的过拟合,或者图案相关性太高,或者缺乏结构性,或有较多‘死’区域。

1、编译matcaffe:

  • 安装matlab后,将matlab目录更新至caffe的Makefile.config :  MATLAB_DIR:=/your/path/to/matlab/r2015b/
  • 编译 : make matcaffe
2、相关代码:均放在Caffe根目录下
visualiz_weights.m
function visualize_weight(net,param_name,space)
w=net.params(param_name,1).get_data();
size(w)
nums=size(w,4);
channels=size(w,3);
width=size(w,2);
count=nums*channels;
n=ceil(sqrt(count));
weight_map=zeros(n*(width+space),n*(width+space),'uint8');
w=w-min(w(:));
w=w/max(w(:))*255;
w=uint8(w);
for i=0:count-1
    c=mod(i,n);
    r=floor(i/n);
    j=mod(i,channels)+1;
    k=floor(i/channels)+1;
    weight_map(r*(width+space)+(1:width),c*(width+space)+(1:width))=w(:,:,j,k);
end

figure;
imshow(weight_map);
title(param_name);
主程序 : caffenet_weights_vis.m
clear;
clc;
close all;
addpath('matlab');

caffe.set_mode_cpu();
model_dir = 'models/bvlc_reference_caffenet/';
net_model = [model_dir 'deploy.prototxt'];
net_weights = [model_dir 'bvlc_reference_caffenet.caffemodel'];
phase = 'test'; % run with phase test (so that dropout isn't applied)

% Initialize a network
net = caffe.Net(net_model, net_weights, phase);

param_names={'conv1','conv2','conv3','conv4','conv5'};
for i=1:length(param_names)
    visualize_weight(net,param_names{i},1);
end

3、特征图可视化(均放在caffe根目录下)

预先编译matcaffe;

visualize_feature_maps.m

clear;
clc;
close all;

addpath('matlab');
caffe.set_mode_cpu();

model_dir = 'models/bvlc_reference_caffenet/';
net_model = [model_dir 'deploy.prototxt'];
net_weights = [model_dir 'bvlc_reference_caffenet.caffemodel'];
phase = 'test'; % run with phase test (so that dropout isn't applied)

% Initialize a network
net = caffe.Net(net_model, net_weights, phase);

im=imread('examples/images/cat.jpg');
figure();imshow(im);title('Original Image');

input_data={prepare_image(im)};

scores=net.forward(input_data);

blob_names={'data','conv1','conv2','conv3','conv4','conv5'};
for i=1:length(blob_names)
    visualize_feature_maps(net,blob_names{i},1);
end

主函数代码:fm_visual.m

clear;
clc;
close all;

addpath('matlab');
caffe.set_mode_cpu();

model_dir = 'models/bvlc_reference_caffenet/';
net_model = [model_dir 'deploy.prototxt'];
net_weights = [model_dir 'bvlc_reference_caffenet.caffemodel'];
phase = 'test'; % run with phase test (so that dropout isn't applied)

% Initialize a network
net = caffe.Net(net_model, net_weights, phase);

im=imread('examples/images/cat.jpg');
figure();imshow(im);title('Original Image');

input_data={prepare_image(im)};

scores=net.forward(input_data);

blob_names={'data','conv1','conv2','conv3','conv4','conv5'};
for i=1:length(blob_names)
    visualize_feature_maps(net,blob_names{i},1);
end

4、学习曲线

利用caffe运行log绘制训练过程中的学习曲线。

1、把caffe运行产生的log重定向到文件:

$./examples/cifar/train_quick.sh >&cifar.log &

注:

>&表示所有标准输出和标准错误输出都将被重定向

cifar.log为重定向后log保存的文件;

最后的&表示将命令放入后台执行。

补充:可以使用tail -f cifar.log 连续观测log文件的更新,退出使用Ctrl+C

2、使用Shell命令提取log文件中的loss值;

$cat cifar.log | grep "Train net output" | awk '{print $11}'

注:

 | 为管道命令,即将前一条命令的输出直接送入后一条命令作为输入。

通过Matlab工具结合命令行提取例子中的loss数值,并绘制曲线。

代码 : show_loss_curve.m

clear;
clc;
close all;
% 这个参数用来指定 Caffe 运行 log 文件
% 生成 log 文件方法:./examples/cifar10/train_quick.sh  >& cifar.log&
train_log_file = 'trainmnist.log';
% 这个参数相当于 solver.prototxt 中的 display 值
train_interval = 100;
% 这个参数相当于 solver.prototxt 中的test_interval 值
test_interval = 500;


[~, string_output] = dos(['cat ', train_log_file, ' | grep ''Train net output #0'' | awk ''{print $11}''']);
% 第11个空格后面的提出取出
% fid = fopen('matlab_train_loss', 'r');
% train_loss = fscanf(fid, '%f\n');
% fclose(fid);

train_loss = str2num(string_output);
n = 1:length(train_loss);
idx_train = (n - 1) * train_interval;

% fid = fopen('matlab_test_loss', 'r');
% test_loss = fscanf(fid, '%f\n');
% fclose(fid);
[~, string_output] = dos(['cat ', train_log_file, ' | grep ''Test net output #1'' | awk ''{print $11}''']);
% 第11个空格后面的提出取出
test_loss = str2num(string_output);
m = 1:length(test_loss);
idx_test = (m - 1) * test_interval;
figure;
plot(idx_train, train_loss,'--r');
hold on;
plot(idx_test, test_loss,'g');

grid on;
legend('Train Loss', 'Test Loss');
xlabel('iterations');
ylabel('loss');
title(' Train & Test Loss Curve');

注:

通过学习曲线,可以评估当前训练状态:

train loss不断下降,test loss不断下降,说明网络仍然在学习;

train loss不断下降,test loss趋于不变,说明网络过拟合;

train loss趋于不变,test loss趋于不变,说明学习遇到瓶颈,需要减小学习速率或者批量数据尺寸;

train loss趋于不变,test loss不断下降,说明数据集100%有问题;

train loss不断上升,test loss不断上升(最终变为NaN),说明可能网络设计不当、训练超参数设置不当、程序bug等某个问题 引起的;


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

女王の专属领地

您的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值