我的任务是根据缺陷对种子进行分类。我在7个班级中有大约14k图像(它们大小不相等,有些班级的照片更多,有些班级的照片更少)。我尝试从头开始训练Inception V3,我的准确率大约为90%。然后,我尝试使用带有ImageNet权重的预训练模型进行迁移学习。我从inception_v3
导入了applications
,但没有顶层fc层,然后在文档中添加了自己的内容。我以以下代码结束:
# Setting dimensions
img_width = 454
img_height = 227
###########################
# PART 1 - Creating Model #
###########################
# Creating InceptionV3 model without Fully-Connected layers
base_model = InceptionV3(weights='imagenet', include_top=False, input_shape = (img_height, img_width, 3))
# Adding layers which will be fine-tunned
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(7, activation='softmax')(x)
# Creating final model
model = Model(inputs=base_model.input, outputs=predictions)
# Plotting model
plot_model(model, to_file='inceptionV3.png')
# Freezing Convolutional layers
for layer in base_model.layers:
layer.trainable = False
# Summarizing layers
print(model.summary())
# Compiling the CNN
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
##############################################
# PART 2 - Images Preproccessing and Fitting #
##############################################
# Fitting the CNN to the images
train_datagen = ImageDataGenerator(rescale = 1./255,
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
preprocessing_function=preprocess_input,)
valid_datagen = ImageDataGenerator(rescale = 1./255,
preprocessing_function=preprocess_input,)
train_generator = train_datagen.flow_from_directory("dataset/training_set",
target_size=(img_height, img_width),
batch_size = 4,
class_mode = "categorical",
shuffle = True,
seed = 42)
valid_generator = valid_datagen.flow_from_directory("dataset/validation_set",
target_size=(img_height, img_width),
batch_size = 4,
class_mode = "categorical",
shuffle = True,
seed = 42)
STEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size
STEP_SIZE_VALID = valid_generator.n//valid_generator.batch_size
# Save the model according to the conditions
checkpoint = ModelCheckpoint("inception_v3_1.h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)
early = EarlyStopping(monitor='val_acc', min_delta=0, patience=10, verbose=1, mode='auto')
#Training the model
history = model.fit_generator(generator=train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
epochs=25,
callbacks = [checkpoint, early])
但是我得到了可怕的结果:45%的准确性。我认为应该会更好。我有一些假设可能会出问题:
preprocessing_function=preprocess_input
(网络上的文章非常重要,因此我决定添加它)。rotation_range=30
,width_shift_range=0.2
,height_shift_range=0.2
和horizontal_flip = True
。还是我没有其他失败?
编辑:我发布了一份训练历史图。也许其中包含有价值的信息:
EDIT2:,其中InceptionV3的参数已更改:
VGG16进行比较:
答案 0 :(得分:1)
如果要使用Keras中的preprocess_input
方法对输入进行预处理,请删除rescale=1./255
参数。否则,保留rescale
参数并删除preprocessing_function
参数。另外,如果损失没有减少,请尝试降低学习率,例如1e-4或3e-5或1e-5(Adam优化器的默认学习率是1e-3):
from keras.optimizers import Adam
model.compile(optimizer = Adam(lr=learning_rate), ...)
编辑:添加训练图后,您会发现它过度适合训练集。您可以:
答案 1 :(得分:1)
@今天,我发现了一个问题。这是由于“批归一化”层及其冻结时的行为发生了一些变化。 Chollet先生给出了一种解决方法,但是我使用了datumbox制造的Keras叉,它解决了我的问题。主要问题描述如下:
https://github.com/keras-team/keras/pull/9965
现在,我的准确率达到了〜85%,并试图提高它。