Caffe 的可视化 (三) caffe model 的可视化

来源:互联网 发布:淘宝福袋真的不能退吗 编辑:程序博客网 时间:2024/06/05 06:59

Caffe 的可视化 (三) caffe model 的可视化

以 cifar10 quick net 为例子,

首先下载cifar10 data并且训练得到model:

#cd to the caffe root

mark@ubuntu:~$ cd caffe 

#download the cifar10 data

mark@ubuntu:~/caffe$ ./data/cifar10/get_cifar10.sh

#convert to LMDB

mark@ubuntu:~/caffe$ ./examples/cifar10/create_cifar10.sh

#train the data

mark@ubuntu:~/caffe$ ./examples/cifar10/train_quick.sh

训练完后,会看到生成的model 文件 cifar10_quick_iter_4000.caffemodel 在($CAFFE_ROOT/examples/cifar10/ 里)

修改 $CAFFE_ROOT/examples/cifar10/ 里的文件 cifar10_quick_train_test.prototxt, 生成一个deploy 文件  cifar10_deploy.prototxt,内容如下:

name: "CIFAR10_quick"input: "data"input_dim: 1  # batchsizeinput_dim: 3  # number of channels - rgbinput_dim: 32 # widthinput_dim: 32 # heightlayer {  name: "conv1"  type: "Convolution"  bottom: "data"  top: "conv1"  param {    lr_mult: 1  }  param {    lr_mult: 2  }  convolution_param {    num_output: 32    pad: 2    kernel_size: 5    stride: 1    weight_filler {      type: "gaussian"      std: 0.0001    }    bias_filler {      type: "constant"    }  }}layer {  name: "pool1"  type: "Pooling"  bottom: "conv1"  top: "pool1"  pooling_param {    pool: MAX    kernel_size: 3    stride: 2  }}layer {  name: "relu1"  type: "ReLU"  bottom: "pool1"  top: "pool1"}layer {  name: "conv2"  type: "Convolution"  bottom: "pool1"  top: "conv2"  param {    lr_mult: 1  }  param {    lr_mult: 2  }  convolution_param {    num_output: 32    pad: 2    kernel_size: 5    stride: 1    weight_filler {      type: "gaussian"      std: 0.01    }    bias_filler {      type: "constant"    }  }}layer {  name: "relu2"  type: "ReLU"  bottom: "conv2"  top: "conv2"}layer {  name: "pool2"  type: "Pooling"  bottom: "conv2"  top: "pool2"  pooling_param {    pool: AVE    kernel_size: 3    stride: 2  }}layer {  name: "conv3"  type: "Convolution"  bottom: "pool2"  top: "conv3"  param {    lr_mult: 1  }  param {    lr_mult: 2  }  convolution_param {    num_output: 64    pad: 2    kernel_size: 5    stride: 1    weight_filler {      type: "gaussian"      std: 0.01    }    bias_filler {      type: "constant"    }  }}layer {  name: "relu3"  type: "ReLU"  bottom: "conv3"  top: "conv3"}layer {  name: "pool3"  type: "Pooling"  bottom: "conv3"  top: "pool3"  pooling_param {    pool: AVE    kernel_size: 3    stride: 2  }}layer {  name: "ip1"  type: "InnerProduct"  bottom: "pool3"  top: "ip1"  param {    lr_mult: 1  }  param {    lr_mult: 2  }  inner_product_param {    num_output: 64    weight_filler {      type: "gaussian"      std: 0.1    }    bias_filler {      type: "constant"    }  }}layer {  name: "ip2"  type: "InnerProduct"  bottom: "ip1"  top: "ip2"  param {    lr_mult: 1  }  param {    lr_mult: 2  }  inner_product_param {    num_output: 10    weight_filler {      type: "gaussian"      std: 0.1    }    bias_filler {      type: "constant"    }  }}layer {  name: "prob"  type: "Softmax"  bottom: "ip2"  top: "prob"}

下面编写代码 extract_weights.py,将model 里的第一个卷积层和第二个卷积层的权值可视化
import numpy as npimport matplotlib.pyplot as pltimport osimport sysimport caffeCAFFE_ROOT = '/home/mark/caffe'deploy_file_name = 'cifar10_deploy.prototxt'model_file_name  = 'cifar10_quick_iter_4000.caffemodel'#编写一个函数,用于显示各层的参数,padsize用于设置图片间隔空隙,padval用于调整亮度 def show_weight(data, padsize=1, padval=0, name="conv.jpg"):    #归一化    data -= data.min()    data /= data.max()    print data.ndim    #根据data中图片数量data.shape[0],计算最后输出时每行每列图片数n    n = int(np.ceil(np.sqrt(data.shape[0])))    # padding = ((图片个数维度的padding),(图片高的padding), (图片宽的padding), ....)    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))        # 先将padding后的data分成n*n张图像    data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))        # 再将(n, W, n, H)变换成(n*w, n*H)    data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])    print data.shape    plt.set_cmap('gray')    plt.imshow(data)    plt.imsave(name,data)    plt.axis('off')if __name__ == '__main__':    deploy_file = CAFFE_ROOT + '/examples/cifar10/' + deploy_file_name    model_file  = CAFFE_ROOT + '/examples/cifar10/' + model_file_name    #初始化caffe    net = caffe.Net(deploy_file, model_file, caffe.TEST)    print [(k, v[0].data.shape) for k, v in net.params.items()]        #第一个卷积层,参数规模为(32,3,5,5),即32个5*5的3通道filter    weight = net.params["conv1"][0].data    print weight.shape    show_weight(weight.reshape(32*3,5,5), padsize=2, padval=0, name="conv1-cifar10.jpg")    #第二个卷积层,参数规模为(32,32,5,5),即32个5*5的32通道filter    weight = net.params["conv2"][0].data    print weight.shape    show_weight(weight.reshape(32*32,5,5), padsize=2, padval=0, name="conv2-cifar10.jpg") 

执行

mark@ubuntu:~/caffe$ python extract_weights.py

生成的可视化图如下:

conv1-cifar10.jpg


conv2-cifar10.jpg



原创粉丝点击