saurabh kumar
saurabh kumar

Reputation: 131

How to input cifar10 into inceptionv3 in keras

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.

Upvotes: 3

Views: 3702

Answers (3)

Kamil
Kamil

Reputation: 345

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(.......)

'

Upvotes: 0

Marcin Możejko
Marcin Możejko

Reputation: 40516

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.

Upvotes: 2

Thomas Pinetz
Thomas Pinetz

Reputation: 7148

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.

Upvotes: 4

Related Questions