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 = keras.layers.Conv2D(filters= 96, kernel_size= [11, 11], strides= [4, 4], activation= keras.activations.relu, use_bias= True, padding= 'valid')(inputs)
# Define the standardization layer 1
stand1 = keras.layers.BatchNormalization(axis= 1)(conv1)
# Define the pooling layer 1
pooling1 = keras.layers.MaxPooling2D(pool_size= [3, 3], strides= [2, 2], padding= 'valid')(stand1)
# Define the converlutional layer 2
conv2 = keras.layers.Conv2D(filters= 256, kernel_size= [5, 5], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'valid')(pooling1)
# Define the standardization layer 2
stand2 = keras.layers.BatchNormalization(axis= 1)(conv2)
# Defien the pooling layer 2
pooling2 = keras.layers.MaxPooling2D(pool_size= [3, 3], strides= [2, 2], padding= 'valid')(stand2)
# Define the converlutional layer 3
conv3 = keras.layers.Conv2D(filters= 384, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'valid')(pooling2)
# Define the converlutional layer 4
conv4 = keras.layers.Conv2D(filters= 384, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'valid')(conv3)
# Define the converlutional layer 5
conv5 = keras.layers.Conv2D(filters= 256, kernel_size= [3, 3], strides= [1, 1], activation= keras.activations.relu, use_bias= True, padding= 'valid')(conv4)
# Defien the pooling layer 3
pooling3 = keras.layers.MaxPooling2D(pool_size= [3, 3], strides= [2, 2], padding= 'valid')(conv5)
# Define the fully connected layer
flatten = keras.layers.Flatten()(pooling3)
fc1 = keras.layers.Dense(4096, activation= keras.activations.relu, use_bias= True)(flatten)
drop1 = keras.layers.Dropout(0.5)(fc1)
fc2 = keras.layers.Dense(4096, activation= keras.activations.relu, use_bias= True)(drop1)
drop2 = keras.layers.Dropout(0.5)(fc2)
fc3 = keras.layers.Dense(1000, activation= keras.activations.softmax, use_bias= True)(drop2)
# 基于Model方法构建模型
model = keras.Model(inputs= inputs, outputs = fc3)
# 编译模型
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('模型文件地址')
在这一节中我们使用Keras实现了AlexNet模型,有任何的疑问请在评论区留言,我会尽快回复,谢谢支持!