一、簡介
VGG網絡在2014年的 ILSVRC localization and classification 兩個問題上分別取得了第一名和第二名。VGG網絡非常深,通常有16-19層,如果自己訓練網絡模型的話很浪費時間和計算資源。因此這裏採用一種方法獲取VGG19模型的模型數據,從而能夠更快速的應用到自己的任務中來,
本文在加載模型數據的同時,還可視化圖片在網絡傳播過程中,每一層的輸出特徵圖。讓我們能夠更直接的觀察網絡傳播的狀況。
運行環境爲spyder,Python3.5,tensorflow1.2.1
模型名稱爲: imagenet-vgg-verydeep-19.mat 大家可以在網上下載。
二、VGG19模型結構
模型的每一層結構如下圖所示:
三、代碼
#加載VGG19模型並可視化一張圖片前向傳播的過程中每一層的輸出
#引入包
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import scipy.io
import scipy.misc
#定義一些函數
#卷積
def _conv_layer(input, weights, bias):
conv = tf.nn.conv2d(input, tf.constant(weights), strides=(1, 1, 1, 1),
padding='SAME')
return tf.nn.bias_add(conv, bias)
#池化
def _pool_layer(input):
return tf.nn.max_pool(input, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1),
padding='SAME')
#減像素均值操作
def preprocess(image, mean_pixel):
return image - mean_pixel
#加像素均值操作
def unprocess(image, mean_pixel):
return image + mean_pixel
#讀
def imread(path):
return scipy.misc.imread(path).astype(np.float)
#保存
def imsave(path, img):
img = np.clip(img, 0, 255).astype(np.uint8)
scipy.misc.imsave(path, img)
print ("Functions for VGG ready")
#定義VGG的網絡結構,用來存儲網絡的權重和偏置參數
def net(data_path, input_image):
#拿到每一層對應的參數
layers = (
'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1',
'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',
'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3',
'relu3_3', 'conv3_4', 'relu3_4', 'pool3',
'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3',
'relu4_3', 'conv4_4', 'relu4_4', 'pool4',
'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3',
'relu5_3', 'conv5_4', 'relu5_4'
)
data = scipy.io.loadmat(data_path)
#原網絡在訓練的過程中,對每張圖片三通道都執行了減均值的操作,這裏也要減去均值
mean = data['normalization'][0][0][0]
mean_pixel = np.mean(mean, axis=(0, 1))
#print(mean_pixel)
#取到權重參數W和b,這裏運氣好的話,可以查到VGG模型中每層的參數含義,查不到的
#話可以打印出weights,然後打印每一層的shape,推出其中每一層代表的含義
weights = data['layers'][0]
#print(weights)
net = {}
current = input_image
#取到w和b
for i, name in enumerate(layers):
#:4的含義是隻看每一層的前三個字母,從而進行判斷
kind = name[:4]
if kind == 'conv':
kernels, bias = weights[i][0][0][0][0]
# matconvnet: weights are [width, height, in_channels, out_channels]\n",
# tensorflow: weights are [height, width, in_channels, out_channels]\n",
#這裏width和height是顛倒的,所以要做一次轉置運算
kernels = np.transpose(kernels, (1, 0, 2, 3))
#將bias轉換爲一個維度
bias = bias.reshape(-1)
current = _conv_layer(current, kernels, bias)
elif kind == 'relu':
current = tf.nn.relu(current)
elif kind == 'pool':
current = _pool_layer(current)
net[name] = current
assert len(net) == len(layers)
return net, mean_pixel, layers
print ("Network for VGG ready")
#cwd = os.getcwd()
#這裏用的是絕對路徑
VGG_PATH = "F:/mnist/imagenet-vgg-verydeep-19.mat"
#需要可視化的圖片路徑,這裏是一隻小貓
IMG_PATH = "D:/VS2015Program/cat.jpg"
input_image = imread(IMG_PATH)
#獲取圖像shape
shape = (1,input_image.shape[0],input_image.shape[1],input_image.shape[2])
#開始會話
with tf.Session() as sess:
image = tf.placeholder('float', shape=shape)
#調用net函數
nets, mean_pixel, all_layers = net(VGG_PATH, image)
#減均值操作(由於VGG網絡圖片傳入前都做了減均值操作,所以這裏也用相同的預處理
input_image_pre = np.array([preprocess(input_image, mean_pixel)])
layers = all_layers # For all layers \n",
# layers = ('relu2_1', 'relu3_1', 'relu4_1')\n",
for i, layer in enumerate(layers):
print ("[%d/%d] %s" % (i+1, len(layers), layer))
features = nets[layer].eval(feed_dict={image: input_image_pre})
print (" Type of 'features' is ", type(features))
print (" Shape of 'features' is %s" % (features.shape,))
# Plot response \n",
#畫出每一層
if 1:
plt.figure(i+1, figsize=(10, 5))
plt.matshow(features[0, :, :, 0], cmap=plt.cm.gray, fignum=i+1)
plt.title("" + layer)
plt.colorbar()
plt.show()
四、程序運行結果
1、print(weights)的結果:
2、程序運行最終結果:
中間層數太多,這裏就不展示了。程序最後兩層的可視化結果: