python tensorflow 实现VGG16
时间: 2023-06-29 08:03:45 浏览: 88
tensorflow-vgg16
以下是使用TensorFlow实现VGG16的示例代码:
```python
import tensorflow as tf
import numpy as np
import os
def load_weights(weights_file, sess):
weights_dict = np.load(weights_file, encoding='bytes').item()
for op_name in weights_dict:
if op_name not in ['fc6', 'fc7', 'fc8']:
with tf.variable_scope(op_name, reuse=True):
for data in weights_dict[op_name]:
if len(data.shape) == 1:
var = tf.get_variable('biases', trainable=False)
sess.run(var.assign(data))
else:
var = tf.get_variable('weights', trainable=False)
sess.run(var.assign(data))
def VGG16(x):
# conv1_1
with tf.variable_scope("conv1_1") as scope:
kernel = tf.get_variable("weights", shape=[3, 3, 3, 64], dtype=tf.float32, trainable=False)
biases = tf.get_variable("biases", shape=[64], dtype=tf.float32, trainable=False)
conv = tf.nn.conv2d(x, kernel, [1, 1, 1, 1], padding="SAME")
out = tf.nn.bias_add(conv, biases)
conv1_1 = tf.nn.relu(out, name=scope.name)
# conv1_2
with tf.variable_scope("conv1_2") as scope:
kernel = tf.get_variable("weights", shape=[3, 3, 64, 64], dtype=tf.float32, trainable=False)
biases = tf.get_variable("biases", shape=[64], dtype=tf.float32, trainable=False)
conv = tf.nn.conv2d(conv1_1, kernel, [1, 1, 1, 1], padding="SAME")
out = tf.nn.bias_add(conv, biases)
conv1_2 = tf.nn.relu(out, name=scope.name)
# pool1
pool1 = tf.nn.max_pool(conv1_2, [1, 2, 2, 1], [1, 2, 2, 1], padding="SAME")
# conv2_1
with tf.variable_scope("conv2_1") as scope:
kernel = tf.get_variable("weights", shape=[3, 3, 64, 128], dtype=tf.float32, trainable=False)
biases = tf.get_variable("biases", shape=[128], dtype=tf.float32, trainable=False)
conv = tf.nn.conv2d(pool1, kernel, [1, 1, 1, 1], padding="SAME")
out = tf.nn.bias_add(conv, biases)
conv2_1 = tf.nn.relu(out, name=scope.name)
# conv2_2
with tf.variable_scope("conv2_2") as scope:
kernel = tf.get_variable("weights", shape=[3, 3, 128, 128], dtype=tf.float32, trainable=False)
biases = tf.get_variable("biases", shape=[128], dtype=tf.float32, trainable=False)
conv = tf.nn.conv2d(conv2_1, kernel, [1, 1, 1, 1], padding="SAME")
out = tf.nn.bias_add(conv, biases)
conv2_2 = tf.nn.relu(out, name=scope.name)
# pool2
pool2 = tf.nn.max_pool(conv2_2, [1, 2, 2, 1], [1, 2, 2, 1], padding="SAME")
# conv3_1
with tf.variable_scope("conv3_1") as scope:
kernel = tf.get_variable("weights", shape=[3, 3, 128, 256], dtype=tf.float32, trainable=False)
biases = tf.get_variable("biases", shape=[256], dtype=tf.float32, trainable=False)
conv = tf.nn.conv2d(pool2, kernel, [1, 1, 1, 1], padding="SAME")
out = tf.nn.bias_add(conv, biases)
conv3_1 = tf.nn.relu(out, name=scope.name)
# conv3_2
with tf.variable_scope("conv3_2") as scope:
kernel = tf.get_variable("weights", shape=[3, 3, 256, 256], dtype=tf.float32, trainable=False)
biases = tf.get_variable("biases", shape=[256], dtype=tf.float32, trainable=False)
conv = tf.nn.conv2d(conv3_1, kernel, [1, 1, 1, 1], padding="SAME")
out = tf.nn.bias_add(conv, biases)
conv3_2 = tf.nn.relu(out, name=scope.name)
# conv3_3
with tf.variable_scope("conv3_3") as scope:
kernel = tf.get_variable("weights", shape=[3, 3, 256, 256], dtype=tf.float32, trainable=False)
biases = tf.get_variable("biases", shape=[256], dtype=tf.float32, trainable=False)
conv = tf.nn.conv2d(conv3_2, kernel, [1, 1, 1, 1], padding="SAME")
out = tf.nn.bias_add(conv, biases)
conv3_3 = tf.nn.relu(out, name=scope.name)
# pool3
pool3 = tf.nn.max_pool(conv3_3, [1, 2, 2, 1], [1, 2, 2, 1], padding="SAME")
# conv4_1
with tf.variable_scope("conv4_1") as scope:
kernel = tf.get_variable("weights", shape=[3, 3, 256, 512], dtype=tf.float32, trainable=False)
biases = tf.get_variable("biases", shape=[512], dtype=tf.float32, trainable=False)
conv = tf.nn.conv2d(pool3, kernel, [1, 1, 1, 1], padding="SAME")
out = tf.nn.bias_add(conv, biases)
conv4_1 = tf.nn.relu(out, name=scope.name)
# conv4_2
with tf.variable_scope("conv4_2") as scope:
kernel = tf.get_variable("weights", shape=[3, 3, 512, 512], dtype=tf.float32, trainable=False)
biases = tf.get_variable("biases", shape=[512], dtype=tf.float32, trainable=False)
conv = tf.nn.conv2d(conv4_1, kernel, [1, 1, 1, 1], padding="SAME")
out = tf.nn.bias_add(conv, biases)
conv4_2 = tf.nn.relu(out, name=scope.name)
# conv4_3
with tf.variable_scope("conv4_3") as scope:
kernel = tf.get_variable("weights", shape=[3, 3, 512, 512], dtype=tf.float32, trainable=False)
biases = tf.get_variable("biases", shape=[512], dtype=tf.float32, trainable=False)
conv = tf.nn.conv2d(conv4_2, kernel, [1, 1, 1, 1], padding="SAME")
out = tf.nn.bias_add(conv, biases)
conv4_3 = tf.nn.relu(out, name=scope.name)
# pool4
pool4 = tf.nn.max_pool(conv4_3, [1, 2, 2, 1], [1, 2, 2, 1], padding="SAME")
# conv5_1
with tf.variable_scope("conv5_1") as scope:
kernel = tf.get_variable("weights", shape=[3, 3, 512, 512], dtype=tf.float32, trainable=False)
biases = tf.get_variable("biases", shape=[512], dtype=tf.float32, trainable=False)
conv = tf.nn.conv2d(pool4, kernel, [1, 1, 1, 1], padding="SAME")
out = tf.nn.bias_add(conv, biases)
conv5_1 = tf.nn.relu(out, name=scope.name)
# conv5_2
with tf.variable_scope("conv5_2") as scope:
kernel = tf.get_variable("weights", shape=[3, 3, 512, 512], dtype=tf.float32, trainable=False)
biases = tf.get_variable("biases", shape=[512], dtype=tf.float32, trainable=False)
conv = tf.nn.conv2d(conv5_1, kernel, [1, 1, 1, 1], padding="SAME")
out = tf.nn.bias_add(conv, biases)
conv5_2 = tf.nn.relu(out, name=scope.name)
# conv5_3
with tf.variable_scope("conv5_3") as scope:
kernel = tf.get_variable("weights", shape=[3, 3, 512, 512], dtype=tf.float32, trainable=False)
biases = tf.get_variable("biases", shape=[512], dtype=tf.float32, trainable=False)
conv = tf.nn.conv2d(conv5_2, kernel, [1, 1, 1, 1], padding="SAME")
out = tf.nn.bias_add(conv, biases)
conv5_3 = tf.nn.relu(out, name=scope.name)
# pool5
pool5 = tf.nn.max_pool(conv5_3, [1, 2, 2, 1], [1, 2, 2, 1], padding="SAME")
# fc6
with tf.variable_scope("fc6") as scope:
shape = int(np.prod(pool5.get_shape()[1:]))
fc6w = tf.get_variable("weights", shape=[shape, 4096], dtype=tf.float32, trainable=False)
fc6b = tf.get_variable("biases", shape=[4096], dtype=tf.float32, trainable=False)
pool5_flat = tf.reshape(pool5, [-1, shape])
fc6 = tf.nn.bias_add(tf.matmul(pool5_flat, fc6w), fc6b)
fc6 = tf.nn.relu(fc6)
# fc7
with tf.variable_scope("fc7") as scope:
fc7w = tf.get_variable("weights", shape=[4096, 4096], dtype=tf.float32, trainable=False)
fc7b = tf.get_variable("biases", shape=[4096], dtype=tf.float32, trainable=False)
fc7 = tf.nn.bias_add(tf.matmul(fc6, fc7w), fc7b)
fc7 = tf.nn.relu(fc7)
# fc8
with tf.variable_scope("fc8") as scope:
fc8w = tf.get_variable("weights", shape=[4096, 1000], dtype=tf.float32, trainable=False)
fc8b = tf.get_variable("biases", shape=[1000], dtype=tf.float32, trainable=False)
fc8 = tf.nn.bias_add(tf.matmul(fc7, fc8w), fc8b)
return fc8
if __name__ == '__main__':
img = tf.placeholder(tf.float32, [None, 224, 224, 3])
with tf.Session() as sess:
vgg16_npy_path = 'vgg16.npy'
if os.path.isfile(vgg16_npy_path):
print('Loading the weights of VGG16')
load_weights(vgg16_npy_path, sess)
else:
print('Please download the pre-trained VGG16 weights')
preds = VGG16(img)
sess.run(tf.global_variables_initializer())
img_data = np.zeros((10, 224, 224, 3))
predictions = sess.run(preds, feed_dict={img: img_data})
print(predictions.shape) # (10, 1000)
```
其中,`load_weights`函数用于从预训练模型文件中读取权重值,`VGG16`函数定义了VGG16的网络结构,`if __name__ == '__main__'`中的代码用于测试模型,生成一个10x1000的预测结果。需要注意的是,在运行代码之前,需要下载预训练模型的权重文件`vgg16.npy`,并将其放在代码所在目录下。
阅读全文