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

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  # batchsize
input_dim: 3  # number of channels - rgb
input_dim: 32 # width
input_dim: 32 # height

layer {
  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 np
import matplotlib.pyplot as plt
import os
import sys
import caffe

CAFFE_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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值