Here is the error in full:
Exception: Error when checking model input: expected convolution2d_input_1 to have shape (None, 3, 224, 224) but got array with shape (20, 3, 244, 244)
Everything works until the final model.fit_generator(...)
chunk of code. I am using a theano backend.
I'm pretty new to keras, so I'm not sure exactly how to proceed. Checking the documentation I can see that the None
in layers.convolutional.Convolution2D
corresponds to the number of batches (or samples)? Substituting input_shape=(20,3,244,244)
yielded the following error Exception: Input 0 is incompatible with layer conv1_1: expected ndim=4, found ndim=5
. Using 23000 instead of 20 yielded the same error.
Any help is appreciated.
Below is my code:
# ======================
# load data
# ======================
# Set relevant paths for dir structure
current_dir = "/home/ubuntu/nbs/"
DATA_HOME_DIR = current_dir + 'lesson1/data/redux'
path = DATA_HOME_DIR + '/'
train_path = DATA_HOME_DIR + '/train/'
valid_path = DATA_HOME_DIR + '/valid/'
test_path = DATA_HOME_DIR + '/test/'
nb_train_samples = 23000
nb_validation_samples = 2000
nb_epoch = 4
# ======================
# import stuff
# ======================
import numpy as np
from keras.utils.data_utils import get_file
from keras import backend as K
from keras.layers.normalization import BatchNormalization
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout, Lambda
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.layers.pooling import GlobalAveragePooling2D
from keras.optimizers import SGD, RMSprop, Adam
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
# ======================
# define model
# ======================
def vgg():
model = Sequential()
model.add(Convolution2D(64, 3, 3,input_shape=(3,224,224), activation='relu', name='conv1_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))
return model
model = vgg()
print model.summary()
#### load weights
fname = 'vgg16.h5'
model.load_weights(get_file(fname, 'http://www.platform.ai/models/'+fname, cache_subdir='models'))
print "successfully created model and loaded weights"
#### Finetune model
model.pop()
for layer in model.layers: layer.trainable=False
model.add(Dense(batches.nb_class, activation='softmax'))
#### Compile model
model.compile(optimizer=Adam(lr=0.01),
loss='categorical_crossentropy', metrics=['accuracy'])
train_datagen = ImageDataGenerator(
rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_path,
target_size=(244,244),
batch_size = 20,
class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(
valid_path,
target_size=(244,244),
batch_size=20,
class_mode='categorical')
model.fit_generator(
train_generator,
samples_per_epoch=nb_train_samples,
nb_epoch=nb_epoch,
validation_data=validation_generator,
nb_val_samples=nb_validation_samples)
There is a mismatch between the expected size of the images and the actual one. Your model expects images of size 224 x 224
and according to the attached error message actual size is 244 x 244
.