简体   繁体   中英

Binary vs Multiclass Classification using TPU

I am using an EfficientNetB7 and EfficientNetB0 model for training my dataset, and am facing a major anomaly. EfficientNetB7 gave 96.4 percent accuracy with 40 epochs, lr_callback,4 nb_classes,imagenet weights.

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

So, I tried improving the accuracy by using 4 EfficientNetB0 models to predict the 4 classes independently, but the accuracy got stuck at 50 per cent. I tried varying the learning rate to see if it is stuck in a local minimum, but the accuracy is the same.

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

I also tried other Neural Networks like ResNet50, but the accuracy remained stuck at 50 per cent. Can anyone please tell me where I am committing the mistake.

TO predict more than 2 classes use loss as 'categorical_crossentropy' or 'sparse_categorical_crossentropy' with activation as softmax

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM