还是一样用tensorflow实现了一下VGG16的网络结构。
import tensorflow as tf
learn_rate=1e-3
batch_size=50
train_epoch=10000
x=tf.placeholder(tf.float32,shape=[None,224*224],name='x')
y=tf.placeholder(tf.float32,shape=[None,10],name='y')
def get_variable(name,shape=None,dtype=tf.float32,initialiezr=tf.truncated_normal_initializer(mean=0,stddev=0.1)):
return tf.get_variable(name,shape,dtype,initialiezr)
def VGG16(x,y):
with tf.variable_scope('input'):
net=tf.reshape(x,[-1,224,224,3])
with tf.variable_scope('conv1'):
net=tf.nn.conv2d(input=net,filter=get_variable('w',[3,3,3,64]),strides=[1,1,1,1],padding='SAME')
net=tf.nn.bias_add(net,get_variable('b',[64]))
net=tf.nn.relu(net)
net=tf.nn.conv2d(input=net,filter=get_variable('w',[3,3,64,64]),strides=[1,1,1,1],padding='SAME')
net=tf.nn.bias_add(net,get_variable('b',[64]))
net=tf.nn.relu(net)
with tf.variable_scope('pool1'):
net=tf.nn.max_pool(value=net,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
with tf.variable_scope('conv2'):
net=tf.nn.conv2d(input=net,filter=get_variable('w',[3,3,64,128]),strides=[1,1,1,1],padding='SAME')
net=tf.nn.bias_add(net,get_variable('b',[128]))
net=tf.nn.relu(net)
net=tf.nn.conv2d(input=net,filter=get_variable('w',[3,3,128,128]),strides=[1,1,1,1],padding='SAME')
net=tf.nn.bias_add(net,get_variable('b',[128]))
net=tf.nn.relu(net)
with tf.variable_scope('pool2'):
net=tf.nn.max_pool(value=net,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
with tf.variable_scope('conv3'):
net=tf.nn.conv2d(input=net,filter=get_variable('w',[3,3,128,256]),strides=[1,1,1,1],padding='SAME')
net=tf.nn.bias_add(net,get_variable('b',[256]))
net=tf.nn.relu(net)
net = tf.nn.conv2d(input=net, filter=get_variable('w', [3, 3, 256, 256]), strides=[1, 1, 1, 1], padding='SAME')
net = tf.nn.bias_add(net, get_variable('b', [256]))
net = tf.nn.relu(net)
net = tf.nn.conv2d(input=net, filter=get_variable('w', [3, 3, 256, 256]), strides=[1, 1, 1, 1], padding='SAME')
net = tf.nn.bias_add(net, get_variable('b', [256]))
net = tf.nn.relu(net)
with tf.variable_scope('pool3'):
net=tf.nn.max_pool(value=net,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
with tf.variable_scope('conv4'):
net=tf.nn.conv2d(input=net,filter=get_variable('w',[3,3,256,512]),strides=[1,1,1,1],padding='SAME')
net=tf.nn.bias_add(net,get_variable('b',[512]))
net=tf.nn.relu(net)
net = tf.nn.conv2d(input=net, filter=get_variable('w', [3, 3, 512, 512]), strides=[1, 1, 1, 1], padding='SAME')
net = tf.nn.bias_add(net, get_variable('b', [512]))
net = tf.nn.relu(net)
net = tf.nn.conv2d(input=net, filter=get_variable('w', [3, 3, 512, 512]), strides=[1, 1, 1, 1], padding='SAME')
net = tf.nn.bias_add(net, get_variable('b', [512]))
net = tf.nn.relu(net)
with tf.variable_scope('pool4'):
net=tf.nn.max_pool(value=net,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
with tf.variable_scope('conv5'):
net=tf.nn.conv2d(input=net,filter=get_variable('w',[3,3,512,512]),strides=[1,1,1,1],padding='SAME')
net=tf.nn.bias_add(net,get_variable('b',[512]))
net=tf.nn.relu(net)
net = tf.nn.conv2d(input=net, filter=get_variable('w', [3, 3, 512, 512]), strides=[1, 1, 1, 1], padding='SAME')
net = tf.nn.bias_add(net, get_variable('b', [512]))
net = tf.nn.relu(net)
net = tf.nn.conv2d(input=net, filter=get_variable('w', [3, 3, 512, 512]), strides=[1, 1, 1, 1], padding='SAME')
net = tf.nn.bias_add(net, get_variable('b', [512]))
net = tf.nn.relu(net)
with tf.variable_scope('pool5'):
net=tf.nn.max_pool(value=net,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
with tf.variable_scope('fc1'):
net=tf.reshape(net,[-1,7*7*512])
net=tf.add(tf.matmul(net,get_variable('w',[7*7*512,4096])),get_variable('b',[4096]))
net=tf.nn.relu(net)
net=tf.nn.dropout(net,keep_prob=0.75)
with tf.variable_scope('fc2'):
net=tf.add(tf.matmul(net,get_variable('w',[4096,4096])),get_variable('b',[4096]))
net=tf.nn.relu(net)
net=tf.nn.dropout(net,keep_prob=0.75)
with tf.variable_scope('fc3'):
net=tf.add(tf.matmul(net,get_variable('w',[4096,1000])),get_variable('b',[1000]))
net=tf.nn.relu(net)
net=tf.nn.dropout(net,keep_prob=0.75)
with tf.variable_scope('softmax'):
act=tf.nn.softmax(net)
return act
act=VGG16(x,y)
可以自己导入数据集去训练VGG16。