My first image classification model has an error

I was following steps from a Nich Renotte youtube where he was classifying happy/sad face pictures. I tried modifying the model to recognize pictures from 10 classes of litter rather than 2. What would cause my training results to be so poor? I only have 175 training images, I know its low but thought it would kinda work?

3. CREATE A DATASET

Load the image Data into a Tensorflow Pipeline tf.data.Dataset?? to display help.

data = tf.keras.utils.image_dataset_from_directory(‘Train’)

data_iterator = data.as_numpy_iterator()
batch = data_iterator.next()

4. SCALE THE DATA

Preprocess the data convert to grayscale x are the images, y are the labels

data = data.map(lambda x,y: (x/255, y))
data.as_numpy_iterator().next()

5. SPLIT THE DATA INTO TRAINING AND TESTING

len(data)

train_size = int(len(data).7)
val_size = int(len(data)
.2)
test_size = int(len(data)*.1)+1
train_size+val_size+test_size

train = data.take(train_size)
val = data.skip(train_size).take(val_size)
test = data.skip(train_size+val_size).take(test_size)

6. BUILD THE DEEPLEARNING NETWORK

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

model = Sequential()
model.add(Conv2D(16, (3,3), 1, activation = ‘relu’, input_shape=(256,256,3)))
model.add(MaxPooling2D())
model.add(Conv2D(32, (3,3), 1, activation = ‘relu’))
model.add(MaxPooling2D())
model.add(Conv2D(16, (3,3), 1, activation = ‘relu’))
model.add(MaxPooling2D())
model.add(Flatten())
model.add(Dense(256, activation=‘relu’))
model.add(Dense(1, activation = ‘sigmoid’))

model.compile(‘adam’, loss=tf.losses.BinaryCrossentropy(), metrics=[‘accuracy’])
model.summary()

7. TRAIN THE MODEL

logdir = ‘logs’
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=logdir)
hist = model.fit(train, epochs=10, validation_data=val, callbacks=[tensorboard_callback])

Thanks for any advice.

Hi @Frank_Ogiamien, welcome to the Forum.

Can you try to change the final output layer of your network

From:
model.add(Dense(1, activation = ‘sigmoid’))
To:
model.add(Dense(2, activation = ‘softmax’))

Since you’ll need 2 classes (happy/sad), your last output layer should have a size of 2 as well.

Verified - Khushboo Gupta

Thanks Dennis, I gave that a try but it didn’t like it. I got:
ValueError: logits and labels must have the same shape, received ((None, 2) vs (None, 1)).
I was hoping it could do 10 labels for this project based on the images I’m using.
I’ll try and find some tutorials that train on many classes. Thanks

If you have several classes, you should either apply one-hot encoding to your labels and use tf.keras.losses.CategoricalCrossentropy or leave the labels as they are and use tf.keras.losses.SparseCategoricalCrossentropy.

1 Like

Thanks Ekaterina, I will research these methods and give it a try.