Caffe CNN特征可视化
生活随笔
收集整理的這篇文章主要介紹了
Caffe CNN特征可视化
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Caffe CNN特征可視化
轉(zhuǎn)載請(qǐng)注明出處,樓燚(yì)航的blog,http://www.cnblogs.com/louyihang-loves-baiyan/
以下部分代碼是根據(jù)caffe的python接口,從一次forword中取出param和blob里面的卷積核 和響應(yīng)的卷積圖。
import numpy as np import matplotlib.pyplot as plt import os import caffe import sys import pickle import cv2caffe_root = '../' deployPrototxt = '/home/chenjie/louyihang/caffe/models/bvlc_reference_caffenet/deploy_louyihang.prototxt' modelFile = '/home/chenjie/louyihang/caffe/models/bvlc_reference_caffenet/caffenet_carmodel_louyihang_iter_50000.caffemodel' meanFile = 'python/caffe/imagenet/ilsvrc_2012_mean.npy' imageListFile = '/home/chenjie/DataSet/CompCars/data/train_test_split/classification/test_model431_label_start0.txt' imageBasePath = '/home/chenjie/DataSet/CompCars/data/cropped_image' resultFile = 'PredictResult.txt'#網(wǎng)絡(luò)初始化 def initilize():print 'initilize ... 'sys.path.insert(0, caffe_root + 'python')caffe.set_mode_gpu()caffe.set_device(4)net = caffe.Net(deployPrototxt, modelFile,caffe.TEST)return net#取出網(wǎng)絡(luò)中的params和net.blobs的中的數(shù)據(jù) def getNetDetails(image, net):# input preprocessing: 'data' is the name of the input blob == net.inputs[0]transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})transformer.set_transpose('data', (2,0,1))transformer.set_mean('data', np.load(caffe_root + meanFile ).mean(1).mean(1)) # mean pixeltransformer.set_raw_scale('data', 255) # the reference model operates on images in [0,255] range instead of [0,1]transformer.set_channel_swap('data', (2,1,0)) # the reference model has channels in BGR order instead of RGB# set net to batch size of 50net.blobs['data'].reshape(1,3,227,227)net.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image(image))out = net.forward()#網(wǎng)絡(luò)提取conv1的卷積核filters = net.params['conv1'][0].datawith open('FirstLayerFilter.pickle','wb') as f:pickle.dump(filters,f)vis_square(filters.transpose(0, 2, 3, 1))#conv1的特征圖feat = net.blobs['conv1'].data[0, :36]with open('FirstLayerOutput.pickle','wb') as f:pickle.dump(feat,f)vis_square(feat,padval=1)pool = net.blobs['pool1'].data[0,:36]with open('pool1.pickle','wb') as f:pickle.dump(pool,f)vis_square(pool,padval=1)# 此處將卷積圖和進(jìn)行顯示, def vis_square(data, padsize=1, padval=0 ):data -= data.min()data /= data.max()#讓合成圖為方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))#合并卷積圖到一個(gè)圖像中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:])print data.shapeplt.imshow(data)if __name__ == "__main__":net = initilize()testimage = '../data/MyTest/visualize_test.jpg'getNetDetails(testimage, net) 輸入的測試圖像
第一層的卷積核和卷積圖,可以看到一些明顯的邊緣輪廓,左側(cè)是相應(yīng)的卷積核
第一個(gè)Pooling層的特征圖
第二層卷積特征圖
第二層pooling的特征圖,可以看到pooling之后,對(duì)conv的特征有部分強(qiáng)化,我網(wǎng)絡(luò)中使用的max-pooling,但是到了pooling2已經(jīng)出現(xiàn)一些離散的塊了,已經(jīng)有些抽象了,難以看出什么東西
總結(jié)
以上是生活随笔為你收集整理的Caffe CNN特征可视化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV特征点检测匹配图像-----
- 下一篇: 网格分割算法(Random Walks)