gpt4 book ai didi

python - 迁移学习准确性差

转载 作者:行者123 更新时间:2023-11-30 08:52:01 26 4
gpt4 key购买 nike

我的任务是根据缺陷对种子进行分类。我有 7 个类的大约 14k 图像(它们的大小不相等,有些类有更多照片,有些类有更少)。我尝试从头开始训练 Inception V3,准确率约为 90%。然后我尝试使用带有 ImageNet 权重的预训练模型进行迁移学习。我从没有顶级 fc 层的 applications 导入了 inception_v3,然后在文档中添加了我自己的内容。我以以下代码结束:

# 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% 的准确率。我想应该会更好。我有一些假设可能会出现问题:

  • 我从头开始对缩放图像 (299x299) 和非缩放图像进行迁移学习 (227x454) 训练,但它失败了(或者可能是我失败了尺寸顺序)。
  • 在迁移学习时,我使用了 preprocessing_function=preprocess_input(在网上发现一篇文章,它非常重要,所以我决定添加它)。
  • 在传输时添加了 rotation_range=30width_shift_range=0.2height_shift_range=0.2horizo​​ntal_flip = True学习更多地扩充数据。
  • 也许 Adam 优化器是个坏主意?例如,我应该尝试 RMSprop 吗?
  • 我是否也应该使用小学习率的 SGD 微调一些卷积层?

或者我在其他方面失败了?

编辑:我发布了训练历史图。也许它包含有值(value)的信息:

History training plot

编辑2:随着InceptionV3参数的变化:

InceptionV3 with changed parameters

VGG16 进行比较:

VGG16 for comparison

最佳答案

@今天,我发现了一个问题。这是因为批量归一化层及其卡住时的行为发生了一些变化。 Chollet先生给出了一个解决方法,但我使用了datumbox制作的Keras fork,这解决了我的问题。主要问题描述如下:

https://github.com/keras-team/keras/pull/9965

现在我的准确率约为 85%,并正在努力提高它。

关于python - 迁移学习准确性差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51994344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com