1、概述
VGG是5个group的卷积、2层全连接层用于提取图像特征、1层全连接层用于分类特征。根据前5个卷积层组每个组中的不同配置,卷积层数从8到16递增,网络结构如下图所示。
2、实验部分
采用预加载模式,利用训练好的模型参数进行加载。
权重集:vgg16_weights.npz
2.1 模块加载
import numpy as np
import tensorflow.compat.v1 as tf
import matlab
import cv2
#from imagenet_classes import class_names
2.2 vgg16网络结构
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
第一个参数input:指需要做卷积的输入图像,它要求是一个Tensor,具有[batch, in_height, in_width, in_channels]这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求类型为float32和float64其中之一
第二个参数filter:相当于CNN中的卷积核,它要求是一个Tensor,具有[filter_height, filter_width, in_channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维
第三个参数strides:卷积时在图像每一维的步长,这是一个一维的向量,长度4
第四个参数padding:string类型的量,只能是"SAME","VALID"其中之一,这个值决定了不同的卷积方式
第五个参数:use_cudnn_on_gpu:bool类型,是否使用cudnn加速,默认为true
结果返回一个Tensor,这个输出,就是我们常说的feature map,shape仍然是[batch, height, width, channels]这种形式。
class vgg16:
def __init__(self,imgs,weights=None,sess=None):
self.imgs = imgs;
self.convlayers();
self.fc_layers();
self.probs = tf.nn.softmax(self.fc3l)
if weights is not None and sess is not None:
self.load_weights(weights,sess)
#vgg16网络层
def convlayers(self):
self.parameters=[]
#输入
with tf.name_scope('preprocess') as scope:
mean = tf.constant([123.68,116.79,103.939],dtype=tf.float32,shape=[1,1,1,3],name='img_mean')
images = self.imgs;
#conv1_1
with tf.name_scope('conv1_1') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,3,64],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(images,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[64],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
self.conv1_1 = tf.nn.relu(out,name=scope)
self.parameters+=[kernel,biases]
#conv1_2
with tf.name_scope('conv1_2') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,64,64],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(self.conv1_1,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[64],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
self.conv1_2 = tf.nn.relu(out,name=scope)
self.parameters+=[kernel,biases]
#pool
self.pool1 = tf.nn.max_pool(self.conv1_2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME',name='pool1')
#conv2_1
with tf.name_scope('conv2_1') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,64,128],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(self.pool1,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[128],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
self.conv2_1 = tf.nn.relu(out,name=scope)
self.parameters+=[kernel,biases]
#conv2_2
with tf.name_scope('conv2_2') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,128,128],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(self.conv2_1,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[128],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
self.conv2_2 = tf.nn.relu(out,name=scope)
self.parameters+=[kernel,biases]
#poo2
self.pool2 = tf.nn.max_pool(self.conv2_2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME',name='pool2')
#conv3_1
with tf.name_scope('conv3_1') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,128,256],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(self.pool2,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[256],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
self.conv3_1 = tf.nn.relu(out,name=scope)
self.parameters+=[kernel,biases]
#conv3_2
with tf.name_scope('conv3_2') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,256,256],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(self.conv3_1,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[256],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
self.conv3_2 = tf.nn.relu(out,name=scope)
self.parameters+=[kernel,biases]
#conv3_3
with tf.name_scope('conv3_3') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,256,256],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(self.conv3_2,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[256],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
self.conv3_3 = tf.nn.relu(out,name=scope)
self.parameters+=[kernel,biases]
#pool3
self.pool3 = tf.nn.max_pool(self.conv3_3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME',name='pool3')
#conv4_1
with tf.name_scope('conv4_1') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,256,512],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(self.pool3,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[512],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
self.conv4_1 = tf.nn.relu(out,name=scope)
self.parameters+=[kernel,biases]
#conv4_2
with tf.name_scope('conv4_2') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,512,512],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(self.conv4_1,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[512],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
self.conv4_2 = tf.nn.relu(out,name=scope)
self.parameters+=[kernel,biases]
#conv4_3
with tf.name_scope('conv4_3') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,512,512],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(self.conv4_2,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[512],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
self.conv4_3 = tf.nn.relu(out,name=scope)
self.parameters+=[kernel,biases]
#pool4
self.pool4 = tf.nn.max_pool(self.conv4_3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME',name='pool4')
#conv5_1
with tf.name_scope('conv5_1') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,512,512],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(self.pool4,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[512],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
self.conv5_1 = tf.nn.relu(out,name=scope)
self.parameters+=[kernel,biases]
#conv5_2
with tf.name_scope('conv5_2') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,512,512],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(self.conv5_1,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[512],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
self.conv5_2 = tf.nn.relu(out,name=scope)
self.parameters+=[kernel,biases]
#conv5_3
with tf.name_scope('conv4_3') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,512,512],dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(self.conv5_2,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.0,shape=[512],dtype=tf.float32),trainable=True,name='biases')
out = tf.nn.bias_add(conv,biases)
self.conv5_3 = tf.nn.relu(out,name=scope)
self.parameters+=[kernel,biases]
#pool5
self.pool5 = tf.nn.max_pool(self.conv5_3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME',name='pool5')
def fc_layers(self):
#fc1
with tf.name_scope('fc1') as scope:
shape = int(np.prod(self.pool5.get_shape()[1:]))
fc1w = tf.Variable(tf.truncated_normal([shape,4096],dtype=tf.float32,stddev=1e-1),name='weights')
fc1b = tf.Variable(tf.constant(1.0,shape=[4096],dtype=tf.float32),trainable=True,name='bias')
pool5_flat = tf.reshape(self.pool5,[-1,shape])
fc1l = tf.nn.bias_add(tf.matmul(pool5_flat,fc1w),fc1b)
self.fc1 = tf.nn.relu(fc1l)
self.parameters +=[fc1w,fc1b]
#fc2
with tf.name_scope('fc2') as scope:
fc2w = tf.Variable(tf.truncated_normal([4096,4096],dtype=tf.float32,stddev=1e-1),name='weights')
fc2b = tf.Variable(tf.constant(1.0,shape=[4096],dtype=tf.float32),trainable=True,name='bias')
fc2l = tf.nn.bias_add(tf.matmul(self.fc1,fc2w),fc2b)
self.fc2 = tf.nn.relu(fc2l)
self.parameters +=[fc2w,fc2b]
#fc3
with tf.name_scope('fc3') as scope:
fc3w = tf.Variable(tf.truncated_normal([4096,1000],dtype=tf.float32,stddev=1e-1),name='weights')
fc3b = tf.Variable(tf.constant(1.0,shape=[1000],dtype=tf.float32),trainable=True,name='bias')
self.fc3l = tf.nn.bias_add(tf.matmul(self.fc2,fc3w),fc3b)
self.parameters +=[fc3w,fc3b]
def load_weights(self,weight_file,sess):
weights = np.load(weight_file)
print(weights)
keys = sorted(weights.keys())
for i,k in enumerate(keys):
print(i,k,np.shape(weights[k]))
sess.run(self.parameters[i].assign(weights[k]))
2.3 运行模型
tf.disable_eager_execution()
sess = tf.Session()
imgs = tf.placeholder(tf.float32,[None,224,224,3])
vgg = vgg16(imgs,'./vgg16_weights.npz',sess)
img1 = cv2.imread('./laska.jpg')
#img1 = imresize(img1,(224,224))
prob = sess.run(vgg.probs,feed_dict={vgg.imgs:[img1]})[0]
preds = (np.argsort(prob)[::-1])[0:5]
for p in preds:
#print(class_names[p],prob[p])
print(p,prob[p])
notebook代码下载地址: