簡體   English   中英

轉移學習准確性差

[英]Transfer learning bad accuracy

我有一項任務是根據缺陷對種子進行分類。 我在7個班級中有大約14k圖像(它們的大小不同,有些班級有更多的照片,有些班級有更少的照片)。 我試圖從頭開始訓練初始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.2horizontal_flip = True同時傳輸學習更加增強數據。
  • 也許Adam優化器是個壞主意? 我應該嘗試RMSprop嗎?
  • 我應該用SGD以小學習率微調一些轉換層嗎?

或者我失敗了什么?

編輯:我發布了一段訓練歷史。 也許它包含有價值的信息:

歷史訓練情節

EDIT2:改變InceptionV3的參數:

具有更改參數的InceptionV3

VGG16進行比較:

VGG16進行比較

@today,我發現了一個問題。 這是因為Batch Normalization圖層中的一些更改及其凍結時的行為。 Chollet先生給出了一個解決方法,但我使用了由datumbox制作的Keras前叉,這解決了我的問題。 主要問題在這里描述:

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

現在我的准確率達到了85%,我正在努力提高它。

如果要使用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), ...)

編輯:添加訓練圖后,您可以看到它在訓練集上過度擬合。 您可以:

  • 添加某種正規化,如Dropout圖層,
  • 或者通過降低最后一層之前的Dense層中的單元數來減小網絡大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM