0

I see that similar questions have been answered and this has helped me to realize that the input is not what the model expects, but nowhere have I been able to find how to correct this.

My question is why is it expecting 2 dimensions and what can I do to my code to make this work to classify into 10 different classes?

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Import Libraries Section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
from keras.datasets import fashion_mnist
from keras import models
from keras import layers
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Load Data Section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() 

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Parameters Section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
train_number_images = train_images.shape[0]
test_number_images = test_images.shape[0]
train_x_image_size = train_images.shape[1]
train_y_image_size = train_images.shape[2]
test_x_image_size = test_images.shape[1]
test_y_image_size = test_images.shape[2]

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Pretreat Data Section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
train_images = train_images.reshape((train_number_images, train_x_image_size , train_y_image_size, 1))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((test_number_images, test_x_image_size , test_y_image_size, 1))
test_images = test_images.astype('float32') / 255

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

gen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Define Model Section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model2 = models.Sequential()
model2.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)))
model2.add(layers.MaxPooling2D((2,2)))
model2.add(layers.Conv2D(64, (3,3), activation='relu'))
model2.add(layers.MaxPooling2D((2,2)))
model2.add(layers.Conv2D(64, (3,3), activation='relu'))
model2.add(layers.MaxPooling2D((2,2)))
model2.add(layers.Flatten())
model2.add(layers.Dense(64, activation='relu'))
model2.add(layers.Dense(10, activation='sigmoid'))
model2.compile(optimizer='adam', 
               loss='categorical_crossentropy',
               metrics=['accuracy'])

history2 = model2.fit_generator(gen.flow(train_images, train_labels, 64), epochs=15,
                                steps_per_epoch=len(train_images) // 64)
Jacob Myer
  • 479
  • 5
  • 22

1 Answers1

1

This is a multi-class classification problem, hence your output layer should using softmax rather than sigmoid. Try to changeactivation='sigmoid' on your output layers to activation='softmax'.

Have a look here for more information about multi-class vs multi-label classification, and their corresponding activation- and loss-functions.

You are also scaling your data two times by both dividing by 255, as well as setting rescale=1./255 in ImageDataGenerator. Your should probably only rescale ones.

The reshape of the data is not right. something like this

train_images.reshape(-1,img_width, img_height,img_channels) where img_width and img_height is 28, and img_channels is 1 should work.

KrisR89
  • 1,483
  • 5
  • 11