用python构建一个vgg模型
时间: 2023-06-07 18:07:15 浏览: 95
好的,下面是用Python构建VGG模型的示例代码:
```python
import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense
# 定义VGG模型的各层参数
conv_layers = [
(64, 3), (64, 3), 'max',
(128, 3), (128, 3), 'max',
(256, 3), (256, 3), (256, 3), 'max',
(512, 3), (512, 3), (512, 3), 'max',
(512, 3), (512, 3), (512, 3), 'max',
]
# 构建VGG模型
def build_vgg():
input_layer = Input(shape=(224, 224, 3))
x = input_layer
for layer in conv_layers:
if layer == 'max':
x = MaxPooling2D(pool_size=(2, 2))(x)
else:
filters, kernel_size = layer
x = Conv2D(filters, kernel_size, padding='same', activation='relu')(x)
x = Flatten()(x)
x = Dense(4096, activation='relu')(x)
x = Dense(4096, activation='relu')(x)
output_layer = Dense(1000, activation='softmax')(x)
model = tf.keras.Model(inputs=input_layer, outputs=output_layer)
return model
# 打印VGG模型结构
model = build_vgg()
model.summary()
```
这段代码将构建一个包含13个卷积层和3个全连接层的VGG模型。你可以根据需要修改卷积、全连接层的参数以及模型的输出大小。
阅读全文