2

I am new to Keras and I am trying to make a Neuronal Network to recognize 38 cases. I created such a model, but it just does not work. There is some problem with last layer I think. I checked summary and it looks like output of last layers is 38 as it should. Can someone help me with making it work?

My code is:

model = Sequential()
model.add(Convolution2D(16, 5, 5, border_mode='valid', input_shape=(168, 192, 3)) )
model.add( Activation('relu') )
model.add( MaxPooling2D(2,2) )
model.add( Convolution2D(16, 5, 5) )
model.add( Activation('relu') )
model.add( MaxPooling2D(2,2) )
model.add( Flatten() ) 
model.add( Dense(512, activation='relu'))
model.add(Dense(38, activation='softmax'))

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


train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)


train_data_dir = 'data/train'
validation_data_dir = 'data/validation'

train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        'data/train',
        target_size=(168, 192),
        batch_size=38,
        class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
        'data/validation',
         target_size=(168, 192),
        batch_size=38,
        class_mode='binary')


model.fit_generator(
        train_generator,
        steps_per_epoch=2000,
        epochs=10,
        validation_data=validation_generator,
        validation_steps=800)

and the error looks like:

ValueError: Error when checking target: expected dense_129 to have shape (None, 38) but got array with shape (38, 1)
Wiciaq123
  • 486
  • 4
  • 13

1 Answers1

1

According to Keras documentation of from_from_directory, the specified directory ('data/train' in your case) should contain one subdirectory per class.

Since the error is saying the model is getting an array of shape (38, 1), this means you do not have 38 folders with data/train. (Note do not confuse that the first 38 dimension is the batch size, which coincidentally you have set it to same as number of classes, but does not have to be).

So you should either reaarange your subfolders into one class per subfolder, or load data manually, and flow from memory.

Gerges
  • 6,269
  • 2
  • 22
  • 44
  • I wanted to check this information about batch size and changed its value to 32 in both places. As a result I received problem like: ValueError: Error when checking target: expected dense_131 to have shape (None, 38) but got array with shape (32, 1) Also, I checked structure of files in data/train and there are 38 folders with around 44 files in each. By rearranging it You meant creating one folder or what exactly should I do? – Wiciaq123 Jan 26 '18 at 23:19
  • I think this will help you: https://gist.github.com/fchollet/0830affa1f7f19fd47b06d4cf89ed44d – Gerges Jan 26 '18 at 23:28
  • I checked it out, but their last layer contains 1 output node while I want 38 and while I change it to 38 then problems appears again – Wiciaq123 Jan 27 '18 at 09:59
  • change `class_mode='categorical'` for the arguments to your generator. That should help. – Gerges Jan 27 '18 at 11:15