有人可以看看我的 Keras CNN 模型并帮助我确定提高训练和验证准确性的方法吗?
Can someone look at my Keras CNN model and help me identify ways to improve the training and validation accuracies?
my_model = models.Sequential()
#first convolutional block
my_model.add(Conv2D(16, (3, 3), input_shape = (178, 218, 3), activation="relu", padding="same"))
#add dropout
my_model.add(Dropout(0.5))
my_model.add(MaxPooling2D((2, 2), padding="same"))
#second block
my_model.add(Conv2D(32, (3, 3), activation="relu", padding="same"))
#add dropout
my_model.add(Dropout(0.5))
my_model.add(MaxPooling2D((2, 2), padding="same"))
#third block
my_model.add(Conv2D(64, (3, 3), activation="relu", padding="same"))
#add dropout
my_model.add(Dropout(0.5))
my_model.add(MaxPooling2D((2, 2), padding="same"))
#fourth block
my_model.add(Conv2D(128, (3, 3), activation="relu", padding="same"))
#add dropout
my_model.add(Dropout(0.5))
my_model.add(MaxPooling2D((2, 2), padding="same"))
#global average pooling
my_model.add(GlobalAveragePooling2D())
#fully connected layer
my_model.add(Dense(64, activation='relu'))
my_model.add(BatchNormalization())
#make predictions
my_model.add(Dense(18, activation="softmax"))
from tensorflow.python.keras.callbacks import EarlyStopping, ModelCheckpoint
es = EarlyStopping(monitor="val_loss", mode="min",verbose=1, patience=5)
mc = ModelCheckpoint('/content/model.h5', monitor="val_loss", mode="min", verbose=1, save_best_only=True)
cb_list=[es,mc]
# compile model
from keras.optimizers import Adam
my_model.compile(optimizer=Adam(learning_rate=0.00005),loss="categorical_crossentropy", metrics=["accuracy"])
from tensorflow.python.keras.applications.vgg16 import preprocess_input
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
#set up data generator
data_generator = ImageDataGenerator(preprocessing_function=preprocess_input)
#get batches of training images from the directory
train_generator = data_generator.flow_from_directory(
'/content/output7/train',
target_size=(224, 224),
batch_size=32,
class_mode='categorical')
validation_generator = data_generator.flow_from_directory(
'/content/output7/val',
target_size=(224, 224),
batch_size=32,
class_mode='categorical')
history = my_model.fit(train_generator, epochs = 100, steps_per_epoch=350, validation_data = validation_generator, validation_steps = 100, callbacks = cb_list)
这是我在 Google Colab 和 Python 中使用 keras 进行图像分类的模型代码。现在我坚持大约 30% 的训练准确率和 10% 的验证准确率。我可以查看的任何资源或对我可以进行的特定代码修改的建议都会有所帮助!
一些背景信息:我选择添加 dropout 层来帮助减少任何过度拟合的情况。图像存储在 18 个不同的 类 中。大约有 11000 张训练图像和 2800 张验证图像。
好吧,你有很多辍学的方法。移除除最后一层之外的所有丢弃层,并将速率设置为大约 0.3。在您的早期停止检查点中添加参数
restore_best_weights=正确。这将导致您的模型结束训练,并将权重设置为验证损失最低的时期的权重。那么你就不需要减慢训练速度的检查点回调。还使用 Keras 回调 ReduceLROnPlateau 添加可调整学习。将其设置为监控验证损失,如下面的代码所示:
rlronp=tf.keras.callbacks.ReduceLROnPlateau( monitor="val_loss",
factor=0.5,patience=10, verbose=1)
将 rlronp 添加到回调列表。您正在对过度拟合做出假设。您需要在模型训练时监控训练损失和验证损失。过度拟合是指训练损失继续减少而验证损失开始呈上升趋势。如果这种情况在早期阶段开始发生,则增加 dropout 的数量。如果它在后面的时期开始发生,提前停止回调应该触发并且 return 模型的最佳权重。
my_model = models.Sequential()
#first convolutional block
my_model.add(Conv2D(16, (3, 3), input_shape = (178, 218, 3), activation="relu", padding="same"))
#add dropout
my_model.add(Dropout(0.5))
my_model.add(MaxPooling2D((2, 2), padding="same"))
#second block
my_model.add(Conv2D(32, (3, 3), activation="relu", padding="same"))
#add dropout
my_model.add(Dropout(0.5))
my_model.add(MaxPooling2D((2, 2), padding="same"))
#third block
my_model.add(Conv2D(64, (3, 3), activation="relu", padding="same"))
#add dropout
my_model.add(Dropout(0.5))
my_model.add(MaxPooling2D((2, 2), padding="same"))
#fourth block
my_model.add(Conv2D(128, (3, 3), activation="relu", padding="same"))
#add dropout
my_model.add(Dropout(0.5))
my_model.add(MaxPooling2D((2, 2), padding="same"))
#global average pooling
my_model.add(GlobalAveragePooling2D())
#fully connected layer
my_model.add(Dense(64, activation='relu'))
my_model.add(BatchNormalization())
#make predictions
my_model.add(Dense(18, activation="softmax"))
from tensorflow.python.keras.callbacks import EarlyStopping, ModelCheckpoint
es = EarlyStopping(monitor="val_loss", mode="min",verbose=1, patience=5)
mc = ModelCheckpoint('/content/model.h5', monitor="val_loss", mode="min", verbose=1, save_best_only=True)
cb_list=[es,mc]
# compile model
from keras.optimizers import Adam
my_model.compile(optimizer=Adam(learning_rate=0.00005),loss="categorical_crossentropy", metrics=["accuracy"])
from tensorflow.python.keras.applications.vgg16 import preprocess_input
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
#set up data generator
data_generator = ImageDataGenerator(preprocessing_function=preprocess_input)
#get batches of training images from the directory
train_generator = data_generator.flow_from_directory(
'/content/output7/train',
target_size=(224, 224),
batch_size=32,
class_mode='categorical')
validation_generator = data_generator.flow_from_directory(
'/content/output7/val',
target_size=(224, 224),
batch_size=32,
class_mode='categorical')
history = my_model.fit(train_generator, epochs = 100, steps_per_epoch=350, validation_data = validation_generator, validation_steps = 100, callbacks = cb_list)
这是我在 Google Colab 和 Python 中使用 keras 进行图像分类的模型代码。现在我坚持大约 30% 的训练准确率和 10% 的验证准确率。我可以查看的任何资源或对我可以进行的特定代码修改的建议都会有所帮助!
一些背景信息:我选择添加 dropout 层来帮助减少任何过度拟合的情况。图像存储在 18 个不同的 类 中。大约有 11000 张训练图像和 2800 张验证图像。
好吧,你有很多辍学的方法。移除除最后一层之外的所有丢弃层,并将速率设置为大约 0.3。在您的早期停止检查点中添加参数 restore_best_weights=正确。这将导致您的模型结束训练,并将权重设置为验证损失最低的时期的权重。那么你就不需要减慢训练速度的检查点回调。还使用 Keras 回调 ReduceLROnPlateau 添加可调整学习。将其设置为监控验证损失,如下面的代码所示:
rlronp=tf.keras.callbacks.ReduceLROnPlateau( monitor="val_loss",
factor=0.5,patience=10, verbose=1)
将 rlronp 添加到回调列表。您正在对过度拟合做出假设。您需要在模型训练时监控训练损失和验证损失。过度拟合是指训练损失继续减少而验证损失开始呈上升趋势。如果这种情况在早期阶段开始发生,则增加 dropout 的数量。如果它在后面的时期开始发生,提前停止回调应该触发并且 return 模型的最佳权重。