Keras大法(8)——实现VGGNet19模型

Keras大法(8)——实现VGGNet19模型


在这里插入图片描述

(1)模型结构

模型结构如下:
在这里插入图片描述

(2)模型代码

import tensorflow as tf
from tensorflow import keras

# 使用tf.data进行数据馈送,此为参考示例
def parse_function(example_proto):
    features = {
        'label' : tf.FixedLenFeature([], tf.int64),
        'img_raw' : tf.FixedLenFeature([], tf.string)
    }
    parsed_features = tf.parse_single_example(example_proto, features)
    img = tf.decode_raw(parsed_features['img_raw'], tf.uint8)
    img = tf.reshape(img, [224, 224, 3])
    img = tf.cast(img, tf.float32) / 255
    label = tf.one_hot(parsed_features['label'], depth= 2)
    #label = to_categorical(parsed_features['label'], num_classes= 8)
    return img, label
dataset = tf.data.TFRecordDataset(r"C:\Users\12394\PycharmProjects\Chest X-Ray Automatic Identification\dataset\train.tfrecords")
dataset = dataset.map(parse_function)
dataset = dataset.repeat()
dataset = dataset.shuffle(buffer_size= 5).batch(4)

# Define the input layer
inputs = keras.Input(shape = [150, 150, 3])

# Define the converlutional layer 1
conv1_1 = keras.layers.Conv2D(64, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(inputs)
conv1_2 = keras.layers.Conv2D(64, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(conv1_1)
pooling1 = keras.layers.MaxPooling2D(pool_size= [2, 2], strides= [2, 2], padding= 'same')(conv1_2)

# Define the converlutional layer 2
conv2_1 = keras.layers.Conv2D(128, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(pooling1)
conv2_2 = keras.layers.Conv2D(128, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(conv2_1)
pooling2 = keras.layers.MaxPooling2D(pool_size= [2, 2], strides= [2, 2], padding= 'same')(conv2_2)

# Define the converlutional layer 3
conv3_1 = keras.layers.Conv2D(256, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(pooling2)
conv3_2 = keras.layers.Conv2D(256, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(conv3_1)
conv3_3 = keras.layers.Conv2D(256, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(conv3_2)
conv3_4 = keras.layers.Conv2D(256, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(conv3_3)
pooling3 = keras.layers.MaxPooling2D(pool_size= [2, 2], strides= [2, 2], padding= 'same')(conv3_4)

# Define the converlutional layer 4
conv4_1 = keras.layers.Conv2D(512, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(pooling3)
conv4_2 = keras.layers.Conv2D(512, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(conv4_1)
conv4_3 = keras.layers.Conv2D(512, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(conv4_2)
conv4_4 = keras.layers.Conv2D(512, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(conv4_3)
pooling4 = keras.layers.MaxPooling2D(pool_size= [2, 2], strides= [2, 2], padding= 'same')(conv4_4)

# Define the converlutional layer 5
conv5_1 = keras.layers.Conv2D(512, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(pooling4)
conv5_2 = keras.layers.Conv2D(512, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(conv5_1)
conv5_3 = keras.layers.Conv2D(512, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(conv5_2)
conv5_4 = keras.layers.Conv2D(512, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'same')(conv5_3)
pooling5 = keras.layers.MaxPooling2D(pool_size= [2, 2], strides= [2, 2], padding= 'same')(conv5_4)

flatten = keras.layers.Flatten()(pooling5)

# Defien the fully connected layer
fc1 = keras.layers.Dense(4096, activation= keras.activations.relu, use_bias= True)(flatten)

fc2 = keras.layers.Dense(4096, activation= keras.activations.relu, use_bias= True)(fc1)

fc3 = keras.layers.Dense(1000, activation= keras.activations.relu, use_bias= True)(fc2)

prediction = keras.layers.Dense(2, activation= keras.activations.softmax, use_bias= True)(fc3)
# 基于Model方法构建模型
model = keras.Model(inputs= inputs, outputs = prediction)

# 编译模型
model.compile(optimizer= tf.train.AdamOptimizer(0.01),
              loss= keras.losses.categorical_crossentropy,
              metrics= ['accuracy'])
# 训练配置,仅供参考
model.fit(dataset, epochs = 5, steps_per_epoch= 4442)
model.save('模型文件地址')

(3)总 结

在这一节中我们使用Keras实现了VGGNet19模型,有任何的疑问请在评论区留言,我会尽快回复,谢谢支持!

发布了80 篇原创文章 · 获赞 74 · 访问量 32万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览