Caffe CNN特征可视化
来源:互联网 发布:魔兽光荣套淘宝卖多少 编辑:程序博客网 时间:2024/06/09 17:28
Caffe CNN特征可视化
转载请注明出处,楼燚(yì)航的blog,http://www.cnblogs.com/louyihang-loves-baiyan/
以下部分代码是根据caffe的python接口,从一次forword中取出param和blob里面的卷积核 和响应的卷积图。
import numpy as npimport matplotlib.pyplot as pltimport osimport caffeimport sysimport pickleimport 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'#网络初始化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#取出网络中的params和net.blobs的中的数据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 pixel transformer.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 50 net.blobs['data'].reshape(1,3,227,227) net.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image(image)) out = net.forward() #网络提取conv1的卷积核 filters = net.params['conv1'][0].data with 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)# 此处将卷积图和进行显示,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)) #合并卷积图到一个图像中 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.shape plt.imshow(data)if __name__ == "__main__": net = initilize() testimage = '../data/MyTest/visualize_test.jpg' getNetDetails(testimage, net)
输入的测试图像
第一层的卷积核和卷积图,可以看到一些明显的边缘轮廓,左侧是相应的卷积核
第一个Pooling层的特征图
第二层卷积特征图
第二层pooling的特征图,可以看到pooling之后,对conv的特征有部分强化,我网络中使用的max-pooling,但是到了pooling2已经出现一些离散的块了,已经有些抽象了,难以看出什么东西
0 0
- Caffe CNN特征可视化
- Caffe CNN特征可视化
- Caffe CNN特征可视化
- Caffe CNN特征可视化
- Caffe 特征图可视化
- caffe特征可视化
- Caffe 特征图可视化
- caffe cnn提取各层特征并可视化结果(Python接口)
- 基于caffe的特征可视化
- CAFFE提取特征并可视化
- caffe提取特征并可视化
- caffe matlab接口特征可视化
- CAFFE提取特征并可视化
- caffe权值可视化,特征可视化,网络模型可视化
- Caffe 抽取CNN网络特征 Python
- caffe预测、特征可视化python接口调用
- caffe特征可视化的代码样例
- caffe预测、特征可视化python接口调用
- 数据库差异备份及还原
- 最新版chrome谷歌浏览器Ajax跨域调试问题
- 大端模式和小端模式
- table表单中的td内容两端对齐
- Git忽略规则及.gitignore规则不生效的解决办法
- Caffe CNN特征可视化
- android开发思路、一些想法
- LeetCode——010
- UITabBarController基础篇
- Android Button的隐藏与显示
- 迎接“物联云”,英特尔将推知识生成器工具套件
- 算数基本定理(唯一分解定理)
- 数据库事物隔离级别
- 将 Sublime 3 打造成 Python/Django IDE