3

I am attempting to train InceptionV3 on a novel set of images using transfer learning. I am running into this issue - which clearly relates to a mismatch of input and output dimension (I think) but I can't seem to identify the issue). All relevant previous posts on SO relate to VGG16 (which I have got working). Here is my code:

 from keras.applications.inception_v3 import InceptionV3
 from keras.models import Model
 from keras.layers import Dense, GlobalAveragePooling2D
 from keras.callbacks import ModelCheckpoint, TensorBoard, CSVLogger, Callback
 from keras.optimizers import SGD
 from keras.preprocessing.image import ImageDataGenerator

 base_model = InceptionV3(weights='imagenet', include_top=False)
 x = base_model.output
 x = GlobalAveragePooling2D()(x)
 x = Dense(1024, activation='relu')(x)
 predictions = Dense(3, activation='softmax')(x)
 model = Model(inputs=base_model.input, output=predictions)

 for layer in base_model.layers:
     layer.trainable = False

 model.compile(optimizer=SGD(lr=0.001, momentum=0.9), loss='sparse_categorical_crossentropy')

 train_dir = 'hrct_data/ExtractedHRCTs/Train'
 validation_dir = 'hrct_data/ExtractedHRCTs/Validation'
 nb_train_samples = 21903
 nb_validation_samples = 6000
 epochs = 30
 batch_size = 256

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

 validation_datagen = ImageDataGenerator(
    rescale=1./255)

 train_generator = train_datagen.flow_from_directory(
    train_dir, 
    target_size=(512, 512), 
    batch_size=batch_size,
    class_mode="categorical")

 validation_generator = validation_datagen.flow_from_directory(
    validation_dir,
    target_size=(512, 512), 
    batch_size=batch_size,
    class_mode="categorical")


 model.fit_generator(
    train_generator,
    steps_per_epoch=21903 // batch_size,
    epochs=30,
    validation_data=validation_generator,
    validation_steps=6000 // batch_size)

 model.save_weights('hrct_inception.h5')

And here is the error:

---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call last)
 <ipython-input-89-f79a107413cd> in <module>()
     4         epochs=30,
     5         validation_data=validation_generator,
     6         validation_steps=6000 // batch_size)
     7 model.save_weights('hrct_inception.h5')

 /Users/simonalice/anaconda/lib/python3.5/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     86                 warnings.warn('Update your `' + object_name +
     87                               '` call to the Keras 2 API: ' + signature, stacklevel=2)
     88             return func(*args, **kwargs)
     89         wrapper._legacy_support_signature = inspect.getargspec(func)
     90         return wrapper

 /Users/simonalice/anaconda/lib/python3.5/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_q_size, workers, pickle_safe, initial_epoch)
     1888                     outs = self.train_on_batch(x, y,
     1889                                                
     sample_weight=sample_weight,
     1890                                                class_weight=class_weight)
     1891 
     1892                     if not isinstance(outs, list):

 /Users/simonalice/anaconda/lib/python3.5/site-packages/keras/engine/training.py in train_on_batch(self, x, y, sample_weight, class_weight)
     1625             sample_weight=sample_weight,
     1626             class_weight=class_weight,
     1627             check_batch_axis=True)
     1628         if self.uses_learning_phase and not 
                isinstance(K.learning_phase(), int):
     1629             ins = x + y + sample_weights + [1.]

  /Users/simonalice/anaconda/lib/python3.5/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size)
     1307                                     output_shapes,
     1308                                     check_batch_axis=False,
     1309                                     exception_prefix='target')
     1310         sample_weights = _standardize_sample_weights(sample_weight,
     1311                                                      self._feed_output_names)

  /Users/simonalice/anaconda/lib/python3.5/site-packages/keras/engine/training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
      137                             ' to have shape ' + str(shapes[i]) +
      138                             ' but got array with shape ' +
      139                             str(array.shape))
      140     return arrays
      141 

      ValueError: Error when checking target: expected dense_12 to have shape (None, 1) but got array with shape (256, 3)

Any assistance - even to get me in the right direction, would help.

Nassim Ben
  • 11,473
  • 1
  • 34
  • 52
GhostRider
  • 2,109
  • 7
  • 35
  • 53

1 Answers1

2

I believe that the error comes from the fact that you use sparse_categorical_crossentropy.

That loss is encoding the targets that you feed during training (the 'y') as a one-hot encoded target automatically. So it is expecting a target of shape (256,1) where you only feed the indices.

What you feed with your data generator is already encoded classes. So you feed (256,3) as targets... Hence the error :

ValueError: Error when checking target: expected dense_12 to have shape (None, 1) but got array with shape (256, 3)

To fix it, try with 'categorical_crossentropy' as loss function. This one is expecting the one-hot encoded vectors that the generator is giving.

I hope this helps :-)

Nassim Ben
  • 11,473
  • 1
  • 34
  • 52