簡體   English   中英

Keras Tensorflow 的准確度高但預測不佳

[英]High accuracy but bad predictions on Keras Tensorflow

我有一個包含 7000 張圖像的 9 class 數據集,我使用 MobilenetV2 來訓練我的集合和 ImageGenerator,從而獲得 82% 的 val 准確率。 但是當我預測我的測試圖像時,它總是預測錯誤的 class。 我不知道它有什么問題。這是我的代碼;

我的圖像生成器:

image_gen = ImageDataGenerator(rotation_range = 20,
                               width_shift_range=0.12,
                               height_shift_range=0.12,
                               shear_range=0.1,
                               zoom_range = 0.06,
                               horizontal_flip=True,
                               fill_mode='nearest',
                               rescale=1./255)

我的 model:

Model = Sequential()

Model.add(Conv2D(filters=32,kernel_size=(3,3),input_shape=image_shape,activation='relu'))
Model.add(MaxPool2D(pool_size=(2,2)))

Model.add(Conv2D(filters=64,kernel_size=(3,3),input_shape=image_shape,activation='relu'))
Model.add(MaxPool2D(pool_size=(2,2)))

Model.add(Conv2D(filters=64,kernel_size=(3,3),input_shape=image_shape,activation='relu'))
Model.add(MaxPool2D(pool_size=(2,2)))

Model.add(Conv2D(filters=64,kernel_size=(3,3),input_shape=image_shape,activation='relu'))
Model.add(MaxPool2D(pool_size=(2,2)))

Model.add(Flatten())

Model.add(Dense(256,activation='relu'))

Model.add(Dense(9,activation='softmax'))

移動網絡V2:

height=224
width=224
img_shape=(height, width, 3)
dropout=.3
lr=.001
class_count=9 # number of classes
img_shape=(height, width, 3)
base_model=tf.keras.applications.MobileNetV2( include_top=False, input_shape=img_shape, pooling='max', weights='imagenet') 
x=base_model.output
x=keras.layers.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001 )(x)
x = Dense(512, kernel_regularizer = regularizers.l2(l = 0.016),activity_regularizer=regularizers.l1(0.006),
                bias_regularizer=regularizers.l1(0.006) ,activation='relu', kernel_initializer= tf.keras.initializers.GlorotUniform(seed=123))(x)
x=Dropout(rate=dropout, seed=123)(x)        
output=Dense(class_count, activation='softmax',kernel_initializer=tf.keras.initializers.GlorotUniform(seed=123))(x)
Model = keras.models.Model(inputs=base_model.input, outputs=output)
Model.compile( loss='categorical_crossentropy', metrics=['accuracy'],optimizer='Adamax') 

我的 Rlronp:

rlronp=tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=1, verbose=1, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0)

我的 train_image_gen:

train_image_gen = image_gen.flow_from_directory(train_path,
                                                target_size=image_shape[:2],
                                                color_mode='rgb',
                                                batch_size=batch_size,
                                                class_mode='categorical')

我的 test_image_gen:

test_image_gen = image_gen.flow_from_directory(test_path,
                                                target_size=image_shape[:2],
                                                color_mode='rgb',
                                                batch_size=batch_size,
                                            class_mode='categorical',shuffle=False)

我的早期停留:

early_stop = EarlyStopping(monitor='val_loss',patience=4)

我的 Model 適合:

results = Model.fit(train_image_gen,epochs=20,
                              validation_data=test_image_gen,callbacks=[rlronp,early_stop],class_weight=class_weight
                              )

訓練和准確性:

Epoch 20/20 200/200 [==============================] - 529s 3s/step -
 loss: 0.3995 - accuracy: 0.9925 - val_loss: 0.8637 - val_accuracy: 0.8258

我的問題是,當我從測試集中預測圖像時,它預測錯誤的 class,90% 的時間。

例如這里,它必須是第 3 個 class,但最大值是第 2 個 class。

array([[0.08064549, 0.04599327, 0.27055973, 0.05219262, 0.055945  ,
        0.25723988, 0.07608379, 0.10404343, 0.05729679]], dtype=float32)

我嘗試用 156 個 class 和 2.5k 圖像收集我自己的數據集,但情況更糟。

我在 20 個 epoch 上的損失: 失利

Model 過度擬合...使用 dropout 層.. 我認為這會有所幫助

Model.add(Dropout(0.2))

准確度: 0.9925 val_accuracy: 0.8258

顯然 model 過擬合,

  1. 嘗試使用 L2、L1 或 Dropout 等正則化技術,它們會起作用。
  2. 嘗試收集更多數據(或使用數據增強)
  3. 或搜索其他神經網絡架構
  4. 最好的方法是 plot val_loss v/s loss
r = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=15)

import matplotlib.pyplot as plt
plt.plot(r.history['loss'], label='loss')
plt.plot(r.history['val_loss'], label='val_loss')
plt.legend()

陰謀

並檢查lossval_loss相交的點,然后在交叉點查看 epoch 的數量(比如 x),並僅訓練 x epoch 的 model。

希望你會發現這很有用。

暫無
暫無

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

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