Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)
recategorized by

I'm using model.fit_generator and it gives me an error that the input size does not match with expected size. But I reshaped it using image_datagen.flow_from_directory using target_size=(224, 224), I cannot set it to (1, 224, 224) or it gives me another error.

I am not sure how to check my input size when using the train_generator = image_datagen.flow_from_directory(target_size =(224,224))

train_generator = image_datagen.flow_from_directory(

'C:/output/train/',

    class_mode="categorical",

    seed=seed,

    batch_size=batch_size,

    target_size=(input_size, input_size),

    color_mode='grayscale',

    shuffle=True)

valid_generator = image_datagen.flow_from_directory(

    'C:/output/valid/',

    class_mode="categorical",

    seed=seed,

    batch_size=batch_size,

    target_size=(input_size, input_size),

    color_mode='grayscale',

    shuffle=True)

# https://github.com/keras-team/keras/blob/master/keras/callbacks.py

class MyCheckPoint(keras.callbacks.Callback):

    def on_epoch_end(self, epoch, logs=None):

        loss = logs["loss"]

        val_loss = logs["val_loss"]

        fileName = "model.%02d_%0.5f_%0.5f.h5" % (epoch, loss, val_loss)

        self.model.save(fileName)

#weight_saver = MyCheckPoint()

model = models.getVGGModel(num_classes)

#model = models. getStandardModel(input_size)

model.compile(optimizer=Adam(lr=1e-5, decay=1e-8), loss=keras.losses.categorical_crossentropy)

#model.load_weights("weights.26-1.48.h5")

weight_saver = ModelCheckpoint('weights.{epoch:02d}-{val_loss:.2f}.h5',save_best_only=True, save_weights_only=True)

hist = model.fit_generator(train_generator, validation_data=valid_generator, validation_steps=80, steps_per_epoch=400, epochs=200, callbacks=[weight_saver])

def getVGGModel(num_classes):

    model = Sequential()

    model.add(Dense(32, input_shape=(1, 224, 224)))

    # Reshape((784,), input_shape=(1, 224, 224))

    model.add(Conv2D(64, (3, 3), activation='relu', strides=(1,1), padding='same',input_shape=(1, 224, 224), data_format="channels_first"))

    model.add(Conv2D(64, (3, 3), activation='relu', strides=(1,1), padding='same',data_format = 'channels_first'))

    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2),data_format = 'channels_first'))

    model.add(Conv2D(128, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))

    model.add(Conv2D(128, (3, 3), activation='relu', padding='same', data_format = 'channels_first'))

    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2), padding='same',data_format = 'channels_first'))

    model.add(Conv2D(256, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))

    model.add(Conv2D(256, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))

    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2), padding='same',data_format = 'channels_first'))

    model.add(Conv2D(512, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))

    model.add(Conv2D(512, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))

    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2), padding='same',data_format = 'channels_first'))

    model.add(Conv2D(512, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))

    model.add(Conv2D(512, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))

    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2), padding='same',data_format = 'channels_first'))

    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(num_classes, activation="softmax"))

    return model

1 Answer

0 votes
by (25.1k points)

The problem is in this line of code.

model.add(Dense(32, input_shape=(1, 224, 224)))

You are passing in the channel (1) at the begging you need to pass it at the end of the argument list or not add it at all as 1 is default. Do it like this:

model.add(Dense(32, input_shape=(224, 224, 1)))

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...