import scipy.io
import numpy as np
import os
#import scipy.misc
import imageio
import matplotlib.pyplot as plt
import tensorflow as tf
网络结构
def _conv_layer(input, weights, bias):
#VGG网络结构已经确定,不可以更改,将w转换为常数,strides=1
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")
数据准备
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)
#对所有的输入进行减均值操作
#在RGB三个通道上分别减去三个通道的均值
mean = data['normalization'][0][0][0]
mean_pixel = np.mean(mean, axis=(0, 1))
#打印三个通道的均值
#在训练VGG的时候有减去均值,所以测试也需要
print (mean_pixel)#[123.68 116.779 103.939]
weights = data['layers'][0]
#第一个数表示第几层,如0表示conv1_1,最后一个数表示w还是b
#conv_1 第一层w为3个通道filter为3x3,输出64个features
print(weights[0][0][0][0][0][0].shape)#(3, 3, 3, 64)
#conv_1 b直接等于64
print(weights[0][0][0][0][0][1].shape)#(1, 64)
#定义net字典结构,不断的保存前向传播结果
#如有了输入和conv1_1的w和b,就可以计算conv1_1卷积结果
net = {}
current = input_image #加载输入图像
for i, name in enumerate(layers):
#因为conv层与relu或pool层不一样
#这里用name[:4]取前4个字母,如果等于conv说明是卷积层,执行卷积层操作
kind = name[:4]
if kind == 'conv':
kernels, bias = weights[i][0][0][0][0]#第i层的w和b的元组
#.mat与tensorflow格式不太一样
# matconvnet: weights are [width, height, in_channels, out_channels]
# tensorflow: weights are [height, width, in_channels, out_channels]
#所以需要先transpose一下,将.mat转换为tensorflow格式
kernels = np.transpose(kernels, (1, 0, 2, 3))
#conv层bias为1x64的,转换为64
bias = bias.reshape(-1)
#初始化完成,将input,w,b计算卷积
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 = cwd + "/data/imagenet-vgg-verydeep-19.mat"
IMG_PATH = cwd + "/data/cat.jpg"
#input_image = imread(IMG_PATH)
input_image = imageio.imread(IMG_PATH)
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)
nets, mean_pixel, all_layers = net(VGG_PATH, image)
#对输入做预处理操作(图形减均值)
input_image_pre = np.array([preprocess(input_image, mean_pixel)])
layers = all_layers # For all layers
# layers = ('relu2_1', 'relu3_1', 'relu4_1')
for i, layer in enumerate(layers):
print ("[%d/%d] %s" % (i+1, len(layers), layer))
#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
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()
VGG模型下载
链接:https://pan.baidu.com/s/1hn40kZ2bNT-MNuhI8Uywjg
提取码:2nxp
复制这段内容后打开百度网盘手机App,操作更方便哦