3

I am trying to classify CIFAR10 images using pre-trained imagenet weights for the Inception v3. I am using the following code.

from keras.applications.inception_v3 import InceptionV3

(xtrain, ytrain), (xtest, ytest) = cifar10.load_data()

input_cifar = Input(shape=(32, 32, 3))

base_model = InceptionV3(weights='imagenet', 
                         include_top=False, 
                         input_tensor=input_cifar)

But it gives me an error like 'Negative dimension' at an intermediate conv layer.

This does not happen when I use VGG16 network.

I am using keras with tensorflow backend and tf dim ordernig.

saurabh kumar
  • 131
  • 1
  • 8

3 Answers3

4

Inception network is trained on 224x224 sized images and their down sampling path goes down to something below 10x10. Therefore for 32,32,3 images the downsampling leads to negative dimension sizes. Now you can do multiple things. First you could resize every image in the cifar10 dataset to 224x224 and pass this tensor into the inception model. You could remove some downsampling filters of the network. Then it would still work. Third you could do Zero padding to increase the image size without changing the resolution.

Thomas Pinetz
  • 6,948
  • 2
  • 27
  • 46
  • Actually - (150, 150, 3) is the smallest possible size. – Marcin Możejko Mar 01 '17 at 09:48
  • could you please post some code to enlighten me how to do it? here is my question https://stackoverflow.com/questions/59222403/keras-use-trained-inceptionv3-model-cifar10-got-error-about-batch-size – Franva Dec 07 '19 at 02:11
2

In documentation of this layer one may found that minimal shape of the input is (150, 150, 3) (with tf dim ordering). Your input is much smaller. This minimal size comes from multiple pooling and valid border mode which makes the output of each layers smaller - and if it is smaller than a certain size - it's impossible to perform neither pooling nor convolution.

Marcin Możejko
  • 39,542
  • 10
  • 109
  • 120
  • thanks Marcin, I think it's the cause of my problem. Could you please provide some code to enlighten me about how to make it work? my code is here: https://stackoverflow.com/questions/59222403/keras-use-trained-inceptionv3-model-cifar10-got-error-about-batch-size – Franva Dec 07 '19 at 02:11
0

You need to upsample the original images from CIFAR10 (32, 32, 3) to at least 75, 75.

base_model2 = InceptionV3(include_top=False, weights='imagenet', input_shape=(128, 128, 3)) # shape of images after upsampling that inception will accept

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

#input are original (32, 32, 3) images
inputs = tf.keras.layers.Input(shape=(32, 32, 3))
#upsampling twice (ie. 32*2*2 = 128) big enough for inception
upsamp1 = tf.keras.layers.UpSampling2D((2,2))(inputs)
upsamp2 = tf.keras.layers.UpSampling2D((2,2))(upsamp1)
pre_trained_model = base_model2(upsamp2)
dense4 = tf.keras.layers.Dense(128, activation='relu'). 
    (pre_trained_model)
predictions = tf.keras.layers.Dense(10, activation='softmax')(dense4)

model = Model(inputs = inputs, outputs = predictions)
model.compile(optimizer=Adam(learning_rate=0.01), loss='categorical_crossentropy', metrics=['accuracy']) 
model.fit(.......)

'

Kamil
  • 275
  • 3
  • 8
  • 1
    I think a `Flatten` layer should be added right after the pre-trained model (as mentioned [here](https://stackoverflow.com/questions/56301426/tensorflow-keras-logits-and-labels-must-have-the-same-first-dimension-how-to)) given that the imported CIFAR10 the OP used is of shape `(50000,32,32,3)` – gbi1977 Jun 26 '21 at 17:14