Charlie D.
Charlie D.

Reputation: 35

Keras loss: 0.0000e+00 and accuracy stays constant

I have 101 folders from 0-100 containing synthetic training images. This is my code:

dataset = tf.keras.utils.image_dataset_from_directory(
'Pictures/synthdataset5', labels='inferred', label_mode='int', class_names=None, color_mode='rgb', batch_size=32, image_size=(128,128), shuffle=True, seed=None, validation_split=None, subset=None,interpolation='bilinear', follow_links=False,crop_to_aspect_ratio=False
)

from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten

model = Sequential()

model.add(Conv2D(32, kernel_size=5, activation='relu', input_shape=(128,128,3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, kernel_size=5, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, kernel_size=3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, kernel_size=3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(dataset,epochs=75)

And I always get the same result for every epoch:

Epoch 1/75
469/469 [==============================] - 632s 1s/step - loss: 0.0000e+00 - accuracy: 0.0098

What's wrong???

Upvotes: 1

Views: 3359

Answers (2)

j-vasil
j-vasil

Reputation: 149

I had a similar problem and fixed it by changing the loss function to loss="binary_crossentropy" (instead of categorical_crossentropy). I think the same may apply to this problem since in both cases the output layer is a single node.

Upvotes: 0

Fabian
Fabian

Reputation: 802

So turns out your loss might be the problem after all. If you use SparseCategoricalCrossentropy instead as loss it should work.

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

After this you should adjust the last layer to:

model.add(Dense(101, activation='softmax'))

Also don't forget to import import tensorflow as tf Let me know if this solves the issue.

Upvotes: 1

Related Questions