Keras - How do i train a multiple class model?

Viewed 1902

I'm new to machine learning, I followed some youtube guides and I'm able to make an image classifier from scratch using only 2 classes of images.

However right now i'm quite lost. I'm not exactly sure how to make a multi-class image classifier. I have collected some clues though, such as using "categorical_crossentrpy" and softmax. But my issue is how do i process the images before feeding it to train?

So I have 3 folders each with about 2000 images: Tree, Foilage & Stump

I'm able to execute model.fit by using binary_crossentropy and sigmoid. However, the loss & val_loss are in the negative values.

When i try to get model.fit to run using categorical_crossentropy and softmax, It throws me this error:

ValueError: You are passing a target array of shape (460, 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:
from keras.utils import to_categorical
y_binary = to_categorical(y_int)

Alternatively, you can use the loss function `sparse_categorical_crossentropy` instead, which does expect integer targets.

This is the code for processing: By the way, all my codes were written in Jupyter. Sorry if it's messy. I tried my best.

import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
from tqdm import tqdm

DATADIR = "assets"

CATEGORIES = ["Tree", "Stump", "Ground"]

for category in CATEGORIES:
    path = os.path.join(DATADIR,category)
    for img in os.listdir(path):
        img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE)
        plt.imshow(img_array, cmap='gray')
        plt.show()

        break
    break
print(img_array)
print(img_array.shape)
IMG_SIZE = 150

new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
plt.imshow(new_array, cmap='gray')
plt.show()

new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
plt.imshow(new_array, cmap='gray')
plt.show()

training_data = []

def create_training_data():
    for category in CATEGORIES:

        path = os.path.join(DATADIR,category)
        class_num = CATEGORIES.index(category)

        for img in tqdm(os.listdir(path)):
            try:
                img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE)
                new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
                training_data.append([new_array, class_num])
            except Exception as e:
                print(e)

create_training_data()

print(len(training_data))

import random

random.shuffle(training_data)

X = []
y = []

for features,label in training_data:
    X.append(features)
    y.append(label)


#print(X[0].reshape(-1, IMG_SIZE, IMG_SIZE, 1))

X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
# y_len = len(y)
# y = np.array(y).reshape((y_len, 1))
print(y)

import pickle

pickle_out = open("X.pickle","wb")
pickle.dump(X, pickle_out)
pickle_out.close()

pickle_out = open("y.pickle","wb")
pickle.dump(y, pickle_out)
pickle_out.close()

This is the code for making the model:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.callbacks import TensorBoard
import pickle
import time
import keras
pickle_in = open("X.pickle","rb")
X = pickle.load(pickle_in)

pickle_in = open("y.pickle","rb")
y = pickle.load(pickle_in)
# y = keras.utils.to_categorical(y, num_classes = 3)
# print(y)

X = X/255.0

dense_layers = [0, 1, 2]
layer_sizes = [32, 64, 128]
conv_layers = [1, 2, 3]

for dense_layer in dense_layers:
    for layer_size in layer_sizes:
        for conv_layer in conv_layers:
            NAME = "{}-conv-{}-nodes-{}-dense-{}".format(conv_layer, layer_size, dense_layer, int(time.time()))
            print(NAME)

            model = Sequential()

            model.add(Conv2D(layer_size, (3, 3), input_shape=X.shape[1:]))
            model.add(Activation('relu'))
            model.add(MaxPooling2D(pool_size=(2, 2)))

            for l in range(conv_layer-1):
                model.add(Conv2D(layer_size, (3, 3)))
                model.add(Activation('relu'))
                model.add(MaxPooling2D(pool_size=(2, 2)))

            model.add(Flatten())

            for _ in range(dense_layer):
                model.add(Dense(layer_size))
                model.add(Activation('relu'))

            model.add(Dense(1)) # this value no change ah
            model.add(Activation('softmax'))

            tensorboard = TensorBoard(log_dir="logs/{}".format(NAME))

model.compile(loss='categorical_crossentropy',
                          optimizer='adam',
                          metrics=['accuracy'],
                          )
model.fit(X, y,
                      batch_size=32,
                      epochs=1,
                      validation_split=0.3,
                      callbacks=[tensorboard])

I expect to be able to train a model with 3 classes.

1 Answers

There are a couple of error in your code. First the final dense layer must have the same size as the number of labels (3 in your case):

model.add(Dense(1)) # Change this to be 3
model.add(Activation('softmax'))

Using softmax on a single output has no meaning.

In addition, you need to turn your label vector (y) into a one-hot-encoding representation, so that instead of classes 0,1 and 2 , you will have: [1,0,0], [0,1,0], [0,0,1] (I assume you haven't done that based on the current shape of your output):

import numpy as np
num_classes = 3
y.reshape(-1) # your initial classes
Y = np.eye(num_classes )[y]
Related