sha_hla
sha_hla

Reputation: 344

Keras Error when checking input: expected input_4 to have shape (299, 299, 3) but got array with shape (64, 64, 3)

I have large pickle data that train, test, validation are like to following shape:

(n_samples, 64, 64, 3)


[array([[[26, 16, 24],
         [36, 20, 31],
         [47, 28, 42],
         ...,
         [15,  8, 15],
         [ 8,  5, 10],
         [ 3,  2,  6]],
         ...,
        [[41, 27, 38],
         [54, 37, 51],
         [68, 47, 61],
         ...,
         [22, 14, 21],
         [16,  9, 16],
         [11,  6, 12]]], dtype=uint8),
 array([[[209, 126, 116],
         [212, 125, 117],
         [215, 135, 127],
         ...,

I changed it to:

a=[l.tolist() for l in train_images]
   #x = np.expand_dims(a, axis=0)
train_x =np.array(a)


train_x:
array([[[[ 26,  16,  24],
         [ 36,  20,  31],
         [ 47,  28,  42],
         ...,
         [ 15,   8,  15],
         [  8,   5,  10],
         [  3,   2,   6]],

train_x= preprocess_input(train_x)

and labels are similar to:

from keras.utils.np_utils import to_categorical
train_y = to_categorical(labels, 2)
train_y :
array([[0., 1.],
       [0., 1.],
       [0., 1.],
       ...,
       [0., 1.],
       [1., 0.],
       [0., 1.]], dtype=float32)

I want fit this data to a keras model like to inception v3:

from keras.applications.inception_v3 import InceptionV3
from keras import optimizers

base_model = InceptionV3(weights='imagenet', include_top = True)
model.compile(optimizer = optimizers.SGD(lr=1e-3, momentum=0.9),
                  loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(train_x, train_y , batch_size=128, nb_epoch=1,verbose=0)

but I got this error:

Error when checking input:expected input_4 to have the shape (299, 299, 3) but got array with shape (64, 64, 3)

I know this error is for dimensions. how can I modify the code that it be run? perhaps with freeze layers or fine-tuning or changing input dimensions (I don't want loss features and important data). please rewrite the correct code, if you know it.

Upvotes: 1

Views: 345

Answers (1)

Balraj Ashwath
Balraj Ashwath

Reputation: 1427

Include input_tensor=Input(shape=(64, 64, 3)) in the line base_model = InceptionV3(weights='imagenet', include_top = True) as follows:

base_model = InceptionV3(weights='imagenet', include_top = True, input_tensor=Input(shape=(64, 64, 3)))

If you need to use a pre-trained network for transfer learning but if the original model is trained on inputs of a different shape than the task at hand, you need to use the above method.

Note: The input shape cannot be of any dimension because of the structure of the model we could be using like transpose-convolution, skip connections, etc, which require inputs to be of certain dimensions to concatenate or to perform element wise multiplication later and so on.

References:

Hope this helps!

Upvotes: 1

Related Questions