Dimension mismatch with dense_12

Hi Friends , am trying to use Lesson 1 code on a different dataset. I was able to use it fine on the dogs vs cats dataset. But when I run it on a different data set, I get the following error. Am new to ML and not able to figure out what is causing it. Can someone pls guide me on how to approach to fix this?

Pls note that am using Phyton 3.5.x and Keras 2.x and I used the codebase from the github repo - https://github.com/roebius/deeplearning1_keras2 , shared in the forum by @Robi.

Error:

Code where error is:

vgg = Vgg16()
# Grab a few images at a time for training and validation.
# NB: They must be in subdirectories named based on their category
batches = vgg.get_batches(path+'train', batch_size=batch_size)
val_batches = vgg.get_batches(path+'valid', batch_size=batch_size*2)
vgg.finetune(batches)
vgg.fit(batches, val_batches, batch_size, nb_epoch=1)

Error Detail:

Found 65 images belonging to 7 classes.
Found 21 images belonging to 2 classes.
Epoch 1/1
4/5 [=======================>…] - ETA: 6s - loss: 3.0385 - acc: 0.2969

ValueError Traceback (most recent call last)
in ()
5 val_batches = vgg.get_batches(path+‘valid’, batch_size=batch_size*2)
6 vgg.finetune(batches)
----> 7 vgg.fit(batches, val_batches, batch_size, nb_epoch=1)

/MachineLearning/JeremyTutorials/lesson1a/vgg16.py in fit(self, batches, val_batches, batch_size, nb_epoch)
116 def fit(self, batches, val_batches, batch_size, nb_epoch=1):
117 self.model.fit_generator(batches, steps_per_epoch=int(np.ceil(batches.samples/batch_size)), epochs=nb_epoch,
–> 118 validation_data=val_batches, validation_steps=int(np.ceil(val_batches.samples/batch_size)))
119
120

/anaconda/lib/python3.6/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

/anaconda/lib/python3.6/site-packages/keras/models.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)
1108 workers=workers,
1109 pickle_safe=pickle_safe,
-> 1110 initial_epoch=initial_epoch)
1111
1112 @interfaces.legacy_generator_methods_support

/anaconda/lib/python3.6/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

/anaconda/lib/python3.6/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)
1910 max_q_size=max_q_size,
1911 workers=workers,
-> 1912 pickle_safe=pickle_safe)
1913 else:
1914 # No need for try/except because

/anaconda/lib/python3.6/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

/anaconda/lib/python3.6/site-packages/keras/engine/training.py in evaluate_generator(self, generator, steps, max_q_size, workers, pickle_safe)
2007 'or (x, y). Found: ’ +
2008 str(generator_output))
-> 2009 outs = self.test_on_batch(x, y, sample_weight=sample_weight)
2010
2011 if isinstance(x, list):

/anaconda/lib/python3.6/site-packages/keras/engine/training.py in test_on_batch(self, x, y, sample_weight)
1667 x, y,
1668 sample_weight=sample_weight,
-> 1669 check_batch_axis=True)
1670 if self.uses_learning_phase and not isinstance(K.learning_phase(), int):
1671 ins = x + y + sample_weights + [0.]

/anaconda/lib/python3.6/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)

/anaconda/lib/python3.6/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, 7) but got array with shape (21, 2)

From the error detail above it appears that your validation data falls in only 2 classes while your train data belongs to 7 classes, so there is a mismatch because you have probably changed your last dense layer to manage 7 classes. I guess your validation data should belong to 7 classes too.

Hi @Robi - Thank you so much. Yes, that is indeed the problem. I was assuming that I can have a subset of training classes, for validation, and hence put only 2 of the 7 classes for validation.

Thanks again. Have a good day!!