0

我正在使用 EfficientNetB7 和 EfficientNetB0 模型来训练我的数据集,并且正面临一个重大异常。EfficientNetB7 在 40 个 epoch、lr_callback、4 个 nb_classes、imagenet 权重下给出了 96.4% 的准确率。

GCS_DS_PATH = KaggleDatasets().get_gcs_path('plant-pathology-2020-fgvc7')  
path='../input/plant-pathology-2020-fgvc7/'
train = pd.read_csv(path + 'train.csv')
test = pd.read_csv(path + 'test.csv')
sub = pd.read_csv(path + 'sample_submission.csv')


train_paths = train.image_id.apply(lambda x : GCS_DS_PATH + '/images/' + x + '.jpg').values
test_paths = test.image_id.apply(lambda x : GCS_DS_PATH + '/images/' + x + '.jpg').values

train_labels = train.loc[:,'healthy':].values.astype(int)
train_labels_healthy = train.loc[:,'healthy'].values.astype(int)
train_labels_multiple_diseases = train.loc[:,'multiple_diseases'].values.astype(int)
train_labels_rust = train.loc[:,'rust'].values.astype(int)
train_labels_scab = train.loc[:,'scab'].values.astype(int)


train_dataset = (
    tf.data.Dataset
    .from_tensor_slices((train_paths, train_labels))
    .map(decode_image, num_parallel_calls=AUTO)
    .map(data_augment, num_parallel_calls=AUTO)
    .repeat()
    .shuffle(512)
    .batch(BATCH_SIZE)
    .prefetch(AUTO)
    )



train_dataset1 = (
    tf.data.Dataset
    .from_tensor_slices((train_paths, train_labels_healthy_one_hot))
    .map(decode_image, num_parallel_calls=AUTO)
    .map(data_augment, num_parallel_calls=AUTO)
    .repeat()
    .shuffle(512)
    .batch(BATCH_SIZE)
    .prefetch(AUTO)
    )


nb_classes=4

def get_model():
    base_model =  efn.EfficientNetB7(weights='imagenet', include_top=False, pooling='avg', input_shape=(img_size, img_size, 3))
    x = base_model.output
    predictions = Dense(nb_classes, activation="softmax")(x)
    return Model(inputs=base_model.input, outputs=predictions)

with strategy.scope():
    model = get_model()
model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'])
model.summary()
model.fit(
    train_dataset, 
    steps_per_epoch=train_labels.shape[0] // BATCH_SIZE,
    epochs=10
)
Output: Train for 28 steps
Epoch 1/10
28/28 [==============================] - 253s 9s/step - loss: 0.2862 - accuracy: 0.8951
Epoch 2/10
28/28 [==============================] - 15s 535ms/step - loss: 0.1453 - accuracy: 0.9520
Epoch 3/10
28/28 [==============================] - 34s 1s/step - loss: 0.1450 - accuracy: 0.9554
Epoch 4/10
28/28 [==============================] - 35s 1s/step - loss: 0.1271 - accuracy: 0.9587
Epoch 5/10
28/28 [==============================] - 35s 1s/step - loss: 0.0935 - accuracy: 0.9621
Epoch 6/10
28/28 [==============================] - 35s 1s/step - loss: 0.0951 - accuracy: 0.9621
Epoch 7/10
28/28 [==============================] - 35s 1s/step - loss: 0.0615 - accuracy: 0.9721
Epoch 8/10
28/28 [==============================] - 35s 1s/step - loss: 0.0674 - accuracy: 0.9833
Epoch 9/10
28/28 [==============================] - 35s 1s/step - loss: 0.0654 - accuracy: 0.9743
Epoch 10/10
28/28 [==============================] - 35s 1s/step - loss: 0.0435 - accuracy: 0.9821

因此,我尝试通过使用 4 个 EfficientNetB0 模型来独立预测 4 个类别来提高准确度,但准确度停留在 50%。我尝试改变学习率,看看它是否停留在局部最小值,但准确度是一样的。

nb_classes=1
def get_model():
    base_model =  efn.EfficientNetB0(weights='imagenet', include_top=False, pooling='avg', input_shape=(img_size, img_size, 3))
    x = base_model.output
    predictions = Dense(nb_classes, activation="softmax")(x)
    return Model(inputs=base_model.input, outputs=predictions)

adam = Adam(learning_rate=0.05) #Tried 0.0001,0.001,0.01,0.05
with strategy.scope():
    model1 = get_model()
    #print('1')
   # model2 = get_model()
 #  print('2')
 #   model3 = get_model()
 #   print('3')
 #   model4 = get_model()
 #   print('4')

model1.compile(optimizer=adam, loss='binary_crossentropy',metrics=['accuracy'])
#model2.compile(optimizer='adam', loss='binary_crossentropy',metrics=['accuracy'])
#model3.compile(optimizer='adam', loss='binary_crossentropy',metrics=['accuracy'])
#model4.compile(optimizer='adam', loss='binary_crossentropy',metrics=['accuracy'])

model1.summary()
#model2.summary()
#model3.summary()
#model4.summary()

model1.fit(
    train_dataset1, 
    steps_per_epoch=train_labels_rust.shape[0] // BATCH_SIZE,
    epochs=10
)
Output: Train for 28 steps
Epoch 1/10
28/28 [==============================] - 77s 3s/step - loss: 7.6666 - accuracy: 0.5000
Epoch 2/10
28/28 [==============================] - 32s 1s/step - loss: 7.6666 - accuracy: 0.5000
Epoch 3/10
28/28 [==============================] - 33s 1s/step - loss: 7.6666 - accuracy: 0.5000
Epoch 4/10
28/28 [==============================] - 33s 1s/step - loss: 7.6666 - accuracy: 0.5000
Epoch 5/10
28/28 [==============================] - 33s 1s/step - loss: 7.6666 - accuracy: 0.5000
Epoch 6/10
28/28 [==============================] - 33s 1s/step - loss: 7.6666 - accuracy: 0.5000
Epoch 7/10
28/28 [==============================] - 33s 1s/step - loss: 7.6666 - accuracy: 0.5000
Epoch 8/10
28/28 [==============================] - 33s 1s/step - loss: 7.6666 - accuracy: 0.5000
Epoch 9/10
28/28 [==============================] - 33s 1s/step - loss: 7.6666 - accuracy: 0.5000
Epoch 10/10
28/28 [==============================] - 34s 1s/step - loss: 7.6666 - accuracy: 0.5000

我还尝试了其他神经网络,例如 ResNet50,但准确率仍然停留在 50%。谁能告诉我我在哪里犯了错误。

4

1 回答 1

0

要预测超过 2 个类,使用损失作为“categorical_crossentropy”或“sparse_categorical_crossentropy”,激活作为 softmax

于 2020-05-23T10:40:54.137 回答