Caffe学习系列(16):各层权值参数可视化
原文有更新:
Caffe学习系列(16):各层权值参数可视化 - denny402 - 博客园
http://www.cnblogs.com/denny402/p/5103425.html
通过前面的学习,我们已经能够正常训练各种model了。我们训练cifar10数据,迭代10000次,然后将训练好的 model保存起来,名称为my_iter_10000.caffemodel,然后使用jupyter notebook 来进行可视化。
首先,导入必要的库 In [1]:import numpy as np import matplotlib.pyplot as plt import os,sys,caffe %matplotlib inlineIn [2]:
caffe_root="/home/lee/caffe/" os.chdir(caffe_root) sys.path.insert(0,caffe_root+"python")In [3]:
plt.rcParams["figure.figsize"] = (8, 8) plt.rcParams["image.interpolation"] = "nearest" plt.rcParams["image.cmap"] = "gray"设置网络模型,并显示该模型中各层参数名称和规模 In [4]:
net = caffe.Net(caffe_root + "examples/cifar10/cifar10_full.prototxt", caffe_root + "examples/cifar10/my_iter_10000.caffemodel", caffe.TEST) [(k, v[0].data.shape) for k, v in net.params.items()]Out[4]:
[("conv1", (32, 3, 5, 5)), ("conv2", (32, 32, 5, 5)), ("conv3", (64, 32, 5, 5)), ("ip1", (10, 1024))]cifar10训练的模型配置在文件cifar10_full.prototxt里面,共有三个卷积层和一个全连接层,参数规模如上所示。 In [5]:
#编写一个函数,用于显示各层的参数In [6]:
def show_feature(data, padsize=1, padval=0): data -= data.min() data /= data.max() # force the number of filters to be square n = int(np.ceil(np.sqrt(data.shape[0]))) padding = ((0, n ** 2 - data.shape[0]), (0, padsize), (0, padsize)) + ((0, 0),) * (data.ndim - 3) data = np.pad(data, padding, mode="constant", constant_values=(padval, padval)) # tile the filters into an image data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1))) data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:]) plt.imshow(data) plt.axis("off")
# 第一个卷积层,规模为(32,3,5,5) weight = net.params["conv1"][0].data print weight.shape show_feature(weight.transpose(0, 2, 3, 1))
(32, 3, 5, 5)
参数有两种类型:权值参数和偏置项。分别用params["conv1"][0] 和params["conv1"][1] 表示 。
我们只显示权值参数,因此用params["conv1"][0]
# 第二个卷积层的权值,共有32*32个filter,每个filter大小为5*5 weight = net.params["conv2"][0].data print weight.shape show_feature(weight.reshape(32**2, 5, 5))
(32, 32, 5, 5)
# 第三个卷积层的权值,共有64*32个filter,每个filter大小为5*5,取其前1024个进行可视化
weight = net.params["conv3"][0].data print weight.shape show_feature(weight.reshape(64*32, 5, 5)[:1024])
(64, 32, 5, 5)
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了