使用TPU的二进制分类与多类分类

我正在使用EfficientNetB7和EfficientNetB0模型来训练我的数据集,并且正面临一个重大异常。 EfficientNetB7的40个纪元,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)
    .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',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',metrics=['accuracy'])
#model3.compile(optimizer='adam',metrics=['accuracy'])
#model4.compile(optimizer='adam',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%。谁能告诉我我在哪里犯错。

iCMS 回答:使用TPU的二进制分类与多类分类

要预测2个以上的类,请使用损耗作为“ categorical_crossentropy”或“ sparse_categorical_crossentropy”,并激活为softmax

本文链接:https://www.f2er.com/2295659.html

大家都在问