Caffe学习系列(九):特征可视化

Caffe学习系列(九):特征可视化

caffe特征可视化—python实现
YOLO训练可视化

1. 源码

说明:参看博主的模型为 caffe/models/bvlc_reference_caffenet
本文对SSD进行特征可视化,修改源码如下 SSD_visual.py

# -*- coding:utf-8 -*-
 
import numpy as np
import matplotlib.pyplot as plt
import os
import caffe
import sys
import pickle
import cv2
 
caffe_root = '/home/hitwh/workspace/Mobilenet-ssd/caffe/'   # Your caffe diretory path
 
deployPrototxt = '/home/hitwh/workspace/Mobilenet-ssd/caffe/models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt'
modelFile = '/home/hitwh/workspace/Mobilenet-ssd/caffe/models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_16000.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(0)
    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,300,300)
 
    net.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image(image))
    out = net.forward()
    
    #网络提取conv1的卷积核
    filters = net.params['conv1_1'][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_1'].data[0,:36]  # 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)
    plt.show()
 
if __name__ == "__main__":
   net = initilize()
   testimage = '/home/hitwh/workspace/Mobilenet-ssd/caffe/1.jpg'     # Your test picture path
   getNetDetails(testimage, net)
2.源码修改解读

1.Caffe路径及模型权重路径

caffe_root = '/home/hitwh/workspace/Mobilenet-ssd/caffe/'   # Your caffe diretory path
 
deployPrototxt = '/home/hitwh/workspace/Mobilenet-ssd/caffe/models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt'
modelFile = '/home/hitwh/workspace/Mobilenet-ssd/caffe/models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_16000.caffemodel'

2.输入图像尺寸
与deploy.prototxt内数据层一致,SSD为(1,3,300,300)

    net.blobs['data'].reshape(1,3,300,300)

3.卷积特征图

   #网络提取conv1的卷积核
    filters = net.params['conv1_1'][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_1'].data[0,:36]  # 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)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

la_fe_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值