tensorflow卷积层可视化的实现。基于VGG-19.

  1. #导入包

  2. import scipy.io

  3. import numpy as np

  4. import os

  5. import scipy.misc

  6. import matplotlib.pyplot as plt

  7. import tensorflow as tf

  8. #import seaborn as sns #用来画热度图

  9.  
  10. #定义网络结构,本文用的是VGG-19

  11. def _conv_layer(input, weights, bias):

  12. conv = tf.nn.conv2d(input, tf.constant(weights), strides=(1, 1, 1, 1),

  13. padding='SAME')

  14. return tf.nn.bias_add(conv, bias)

  15. def _pool_layer(input):

  16. return tf.nn.max_pool(input, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1),

  17. padding='SAME')

  18. def preprocess(image, mean_pixel):

  19. return image - mean_pixel

  20. def unprocess(image, mean_pixel):

  21. return image + mean_pixel

  22. def imread(path):

  23. return scipy.misc.imread(path).astype(np.float)

  24. def imsave(path, img):

  25. img = np.clip(img, 0, 255).astype(np.uint8)

  26. scipy.misc.imsave(path, img)

  27. print ("Functions for VGG ready")

  28.  
  29. def net(data_path, input_image):

  30. layers = (

  31. 'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1',

  32. 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',

  33. 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3',

  34. 'relu3_3', 'conv3_4', 'relu3_4', 'pool3',

  35. 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3',

  36. 'relu4_3', 'conv4_4', 'relu4_4', 'pool4',

  37. 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3',

  38. 'relu5_3', 'conv5_4', 'relu5_4'

  39. )

  40. data = scipy.io.loadmat(data_path)

  41. mean = data['normalization'][0][0][0]

  42. mean_pixel = np.mean(mean, axis=(0, 1))

  43. weights = data['layers'][0]

  44. net = {}

  45. current = input_image

  46. for i, name in enumerate(layers):

  47. kind = name[:4]

  48. if kind == 'conv':

  49. kernels, bias = weights[i][0][0][0][0]

  50. # matconvnet: weights are [width, height, in_channels, out_channels]

  51. # tensorflow: weights are [height, width, in_channels, out_channels]

  52. kernels = np.transpose(kernels, (1, 0, 2, 3))

  53. bias = bias.reshape(-1)

  54. current = _conv_layer(current, kernels, bias)

  55. elif kind == 'relu':

  56. current = tf.nn.relu(current)

  57. elif kind == 'pool':

  58. current = _pool_layer(current)

  59. net[name] = current

  60. assert len(net) == len(layers)

  61. return net, mean_pixel, layers

  62. print ("Network for VGG ready")

  63.  
  64. #展示VGG-19每一层的特征图。一共有36层,就有36张特征图。

  65. cwd = os.getcwd()

  66. VGG_PATH = cwd+"\\imagenet-vgg-verydeep-19.mat" #导入数据,初始化网络。

  67. IMG_PATH = cwd+"\\123.jpg" #导入图片

  68. input_image = imread(IMG_PATH)

  69. shape = (1,input_image.shape[0],input_image.shape[1],input_image.shape[2])

  70. with tf.Session() as sess:

  71. image = tf.placeholder('float', shape=shape)

  72. nets, mean_pixel, all_layers = net(VGG_PATH, image)

  73. input_image_pre = np.array([preprocess(input_image, mean_pixel)])

  74. layers = all_layers # For all layers

  75. # layers = ('conv1_1', 'conv2_2','conv3_2','conv5_2')

  76. for i, layer in enumerate(layers):

  77. print ("[%d/%d] %s" % (i+1, len(layers), layer))

  78. features = nets[layer].eval(feed_dict={image: input_image_pre})

  79.  
  80. print (" Type of 'features' is ", type(features))

  81. print (" Shape of 'features' is %s" % (features.shape,))

  82. # Plot response

  83. if 1:

  84. plt.figure(i+1, figsize=(10, 5))

  85. #heatmap = sns.heatmap(features[0, :, :, 0]) #此处显示的是热度图。

  86. plt.matshow(features[0, :, :, 0], fignum=i+1,cmap=plt.cm.gray) #显示灰度图

  87. plt.title("" + layer)

  88. plt.colorbar()

  89. plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值