Caffe CNN特征可视化(详解)

5 篇文章 0 订阅

转载自 Caffe CNN特征可视化
http://www.cnblogs.com/louyihang-loves-baiyan/

先贴出来SqueezeNet网络对应的可视化代码

# -*- coding:utf-8 -*-
 
import numpy as np
import matplotlib.pyplot as plt
import os
import caffe
import sys
import pickle
import cv2

# SqueezeNet
caffe_root = 'J:\\ZenglaiGao\\EggImages\\OrigianlEgg_dataset\\A---SqueezeNet_2019-7-1\\'   # Your caffe diretory path
deployPrototxt = caffe_root + 'deploy.prototxt'
modelFile = caffe_root + 'SqueezeNet_iter_85000.caffemodel'
meanFile = 'J:\\ZenglaiGao\\EggImages\\OrigianlEgg_dataset\\ming_mean.npy'


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


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_mean('data', np.load(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, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # pool1的特征图
    pool = net.blobs['pool1'].data[0, :256]
    with open('pool1.pickle', 'wb') as f:
        pickle.dump(pool, f)
    vis_square(pool, padval=1)

    # fire2/squeeze1x1的特征图
    feat = net.blobs['fire2/squeeze1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire2/expand1x1的特征图
    feat = net.blobs['fire2/expand1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire2/expand3x3的特征图
    feat = net.blobs['fire2/expand3x3'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire2/concat的特征图
    feat = net.blobs['fire2/concat'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire3/squeeze1x1的特征图
    feat = net.blobs['fire3/squeeze1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire3/expand1x1的特征图
    feat = net.blobs['fire3/expand1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire3/expand3x3的特征图
    feat = net.blobs['fire3/expand3x3'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire3/concat的特征图
    feat = net.blobs['fire3/concat'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire4/squeeze1x1的特征图
    feat = net.blobs['fire4/squeeze1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire4/expand1x1的特征图
    feat = net.blobs['fire4/expand1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire4/expand3x3的特征图
    pool = net.blobs['fire4/expand3x3'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(pool, f)
    vis_square(pool, padval=1)

    # fire4/concat
    feat = net.blobs['fire4/concat'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire5/squeeze1x1的特征图
    feat = net.blobs['fire5/squeeze1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)
    # fire5/expand1x1的特征图
    feat = net.blobs['fire5/expand1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)
    # fire5/expand3x3的特征图
    feat = net.blobs['fire5/expand3x3'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire5/concat的特征图
    feat = net.blobs['fire5/concat'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire6/squeeze1x1的特征图
    feat = net.blobs['fire6/squeeze1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)
    # fire6/expand1x1的特征图
    feat = net.blobs['fire6/expand1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire6/expand3x3的特征图
    pool = net.blobs['fire6/expand3x3'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(pool, f)
    vis_square(pool, padval=1)

    # fire6/concat的特征图
    feat = net.blobs['fire6/concat'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire7/squeeze1x1的特征图
    feat = net.blobs['fire7/squeeze1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)
    # fire7/expand1x1的特征图
    feat = net.blobs['fire7/expand1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)
    # fire7/expand3x3的特征图
    feat = net.blobs['fire7/expand3x3'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire7/concat的特征图
    feat = net.blobs['fire7/concat'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire8/squeeze1x1的特征图
    feat = net.blobs['fire8/squeeze1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)
    # fire8/expand1x1的特征图
    feat = net.blobs['fire8/expand1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire8/expand3x3的特征图
    pool = net.blobs['fire8/expand3x3'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(pool, f)
    vis_square(pool, padval=1)

    # fire8/concat的特征图
    feat = net.blobs['fire8/concat'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire9/squeeze1x1的特征图
    feat = net.blobs['fire9/squeeze1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)
    # fire9/expand1x1的特征图
    feat = net.blobs['fire9/expand1x1'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)
    # fire9/expand3x3的特征图
    feat = net.blobs['fire9/expand3x3'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # fire9/concat的特征图
    feat = net.blobs['fire9/concat'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, padval=1)

    # conv10的特征图
    feat = net.blobs['conv10'].data[0, :64]
    with open('FirstLayerOutput.pickle', 'wb') as f:
        pickle.dump(feat, f)
    vis_square(feat, 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 = r'J:\ZenglaiGao\EggImages\OrigianlEgg_dataset\test\0\00093.jpg'     # Your test picture path
    getNetDetails(testimage, net)

再详解使用方法

1 配置caffe.exe、模型文件等路径,跟windows中的caffe测试源码中的classification.cpp中的测试需要设置的路径相同。
在这里插入图片描述
2 修改对应尺寸大小
在这里插入图片描述
三通道图像修改后边两个参数,即图像尺寸,一般为227 * 277或者224 * 244,对应网络文件。统测试的时候deploy.prototxt文件中的input_dim设置相同。
在这里插入图片描述
如果尺寸不对应的话则会报下边的错误
在这里插入图片描述
3 修改或添加你想看到的层的特征图的名称,conv1对应第一个卷积层。如果需要看其它层的特征图,则只需要对应网络结构中的name进行修改。上边的代码是将SqueezeNet所有层的特征图都查看了。这部分可以按需修改。
在这里插入图片描述
下面是部分SqueezeNet结构,辅助理解。
在这里插入图片描述

作者:GL3_24
来源:CSDN
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值