Reputation: 9
I am a beginner and I am trying to classify some images in 20 classes
This is the code I am trying to use:
from tensorflow.python.keras import Sequential
from tensorflow.python.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
x=x/255.0
model=Sequential()
model.add(Conv2D(64,(3,3), input_shape=x.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64,(3,3), input_shape=x.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Dense(20))
model.add(Activation('softmax'))
loss = tf.keras.losses.CategoricalCrossentropy()
lr = 1e-3
optimizer = tf.keras.optimizers.Adam(learning_rate=lr)
metrics = ['accuracy']
model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
Y= np.asarray(y)
model.fit(x,Y,batch_size=32,validation_split=0.1)
But i receive this error:
ValueError: You are passing a target array of shape (1554, 1) while using as loss `categorical_crossentropy`. `categorical_crossentropy` expects targets to be binary matrices (1s and 0s) of shape (samples, classes). If your targets are integer classes, you can convert them to the expected format via:
x.shape returns (1554, 50, 50, 1)
and Y.shape returns
Thanks for the help! (1554,)
Upvotes: 0
Views: 45
Reputation: 159
You probably forgot to one-hot encode your label data. You can use to_categorical in keras.utils to convert your labels accordingly if they're integers from 0 to 19 (representing the different classes)
Upvotes: 1