Noah McDermott
Noah McDermott

Reputation: 9

Neural net has high accuracy but all predictions are wrong

The objective of this neural net is to classify images into 7 categories. Here's the Code:

model = Sequential()
model.add(Conv2D(60, kernel_size=(3, 3), activation='relu', input_shape=(225,225,3)))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())

model.add(Dense(128, activation='relu'))
model.add(Dense(7, activation='softmax'))

model.compile(optimizer="rmsprop", loss="binary_crossentropy", metrics=["accuracy"])


h = model.fit(train_generator, epochs=4, steps_per_epoch=28, validation_data=validation_generator, validation_steps=11)

The accuracy is supposedly really good. Test accuracy is also about 77%

   Epoch 1/4
    28/28 [==============================] - 91s 3s/step - loss: 3.4790 - accuracy: 0.7696 - val_loss: 2.6292 - val_accuracy: 0.7720
    Epoch 2/4
    28/28 [==============================] - 87s 3s/step - loss: 3.5448 - accuracy: 0.7696 - val_loss: 3.9438 - val_accuracy: 0.7698
    Epoch 3/4
    28/28 [==============================] - 76s 3s/step - loss: 3.5431 - accuracy: 0.7696 - val_loss: 3.5056 - val_accuracy: 0.7698
    Epoch 4/4
    28/28 [==============================] - 76s 3s/step - loss: 3.5415 - accuracy: 0.7696 - val_loss: 3.0674 - val_accuracy: 0.7857

This is the code used to predict

prediction = model.predict(test_generator)

print(prediction)

The output is only in one class, however.

[[0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0.]
 ....
 [0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0.]]

Can someone explain why this is? I'm pretty new to neural nets

Upvotes: 0

Views: 73

Answers (1)

Nicolas Gervais
Nicolas Gervais

Reputation: 36704

Change binary_crossentropy to categorical_crossentropy (or sparse_categorical_crossentropy if your targets are not one-hot encoded).

As you can guess, binary_crossentropy is used only when there are two categories.

I suspect your accuracy is high because your data is highly imbalanced.

Upvotes: 1

Related Questions