我是深度学习的初学者,我正在尝试训练一个深度学习模型来使用 Mobilenet_v2 和 Inception 对不同的 ASL 手势进行分类。
这是我的代码创建一个 ImageDataGenerator 来创建训练和验证集。
# Reformat Images and Create Batches
IMAGE_RES = 224
BATCH_SIZE = 32
datagen = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1./255,
validation_split = 0.4
)
train_generator = datagen.flow_from_directory(
base_dir,
target_size = (IMAGE_RES,IMAGE_RES),
batch_size = BATCH_SIZE,
subset = 'training'
)
val_generator = datagen.flow_from_directory(
base_dir,
target_size= (IMAGE_RES, IMAGE_RES),
batch_size = BATCH_SIZE,
subset = 'validation'
)
以下是训练模型的代码:
# Do transfer learning with Tensorflow Hub
URL = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
feature_extractor = hub.KerasLayer(URL,
input_shape=(IMAGE_RES, IMAGE_RES, 3))
# Freeze pre-trained model
feature_extractor.trainable = False
# Attach a classification head
model = tf.keras.Sequential([
feature_extractor,
layers.Dense(5, activation='softmax')
])
model.summary()
# Train the model
model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
EPOCHS = 5
history = model.fit(train_generator,
steps_per_epoch=len(train_generator),
epochs=EPOCHS,
validation_data = val_generator,
validation_steps=len(val_generator)
)
纪元 1/5 94/94 [===============================] - 19 秒 199 毫秒/步 - 损失:0.7333 - 准确度:0.7730 - val_loss:0.6276 - val_accuracy:0.7705
纪元 2/5 94/94 [===============================] - 18 秒 190 毫秒/步 - 损失:0.1574 - 准确度:0.9893 - val_loss:0.5118 - val_accuracy:0.8145
纪元 3/5 94/94 [===============================] - 18 秒 191 毫秒/步 - 损失:0.0783 - 准确度:0.9980 - val_loss:0.4850 - val_accuracy:0.8235
纪元 4/5 94/94 [===============================] - 18 秒 196 毫秒/步 - 损失:0.0492 - 准确度:0.9997 - val_loss:0.4541 - val_accuracy:0.8395
纪元 5/5 94/94 [===============================] - 18 秒 193 毫秒/步 - 损失:0.0349 - 准确度:0.9997 - val_loss:0.4590 - val_accuracy:0.8365
我尝试过使用数据增强,但模型仍然过拟合,所以我想知道我的代码是否做错了什么。