0

I have the following code for an image classification problem. And I keep running into this error:

ValueError: Error when checking input: expected flatten_1_input to have shape (4, 4, 512) but got array with shape (128, 128, 3)

I've seen a similar problem with someone who butchered their model loading process, but I'm not doing that. Here's my code:

def save_bottlebeck_features():`
    datagen = ImageDataGenerator(rescale=1. / 255)
    ​
    # build the VGG16 network
    model = applications.VGG16(include_top=False, weights='imagenet')
    ​
    generator = datagen.flow_from_directory(
            train_data_dir,
            target_size=(img_width, img_height),
            batch_size=batch_size,
            class_mode=None,
            shuffle=False)

   bottleneck_features_train = model.predict_generator(
            generator, nb_train_samples // batch_size)
   np.save('bottleneck_features_train.npy', bottleneck_features_train)
    ​
   generator = datagen.flow_from_directory(
            validation_data_dir,
            target_size=(img_width, img_height),
            batch_size=batch_size,
            class_mode=None,
            shuffle=False)

   bottleneck_features_validation = model.predict_generator(
            generator, nb_validation_samples // batch_size)
   np.save('bottleneck_features_validation.npy',bottleneck_features_validation)

def train_top_model():

  train_data = np.load('bottleneck_features_train.npy',"r+")
  train_labels = np.array([0] * (nb_train_samples // 2) + [1] * (nb_train_samples // 2))
  validation_data = np.load('bottleneck_features_validation.npy',"r+")
  validation_labels = np.array([0] * (nb_validation_samples // 2) + [1] * (nb_validation_samples // 2))

  model = Sequential()
  model.add(Flatten(input_shape=train_data.shape[1:]))
  model.add(Dense(256, activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(1, activation='sigmoid'))

  model.compile(optimizer='rmsprop',
                  loss='binary_crossentropy', metrics=['accuracy'])

  model.fit(train_data, train_labels,
              epochs=epochs,
              batch_size=batch_size,
              validation_data=(validation_data, validation_labels))
  model.save_weights(top_model_weights_path)

  model.save('my_model.model')

save_bottlebeck_features()
train_top_model()

from keras.preprocessing import image
import numpy as np
from keras.models import load_model
import os

resnet_50 = load_model("my_model.model")

TEST_DIR = 'test/'

with open('better_score.csv','w') as f:
    f.write('Id,Expected\n')
    for x in range(1,7091): 
        mystr = "test_" + str(x) +".jpg"
        path = os.path.join(TEST_DIR, mystr)

        if (os.path.exists(path)):
            img = image.load_img(path, target_size=(128, 128))
            img = image.img_to_array(img)
            img = np.expand_dims(img, axis=0)
            model_out  = resnet_50.predict(img/255)
            f.write('{},{}\n'.format(mystr, model_out[0][0]))

I have printed the train_data shape and that is (2000, 4, 4, 512) and validation_data shape is (800, 4, 4, 512). I am able to train my model, and save it. The problem occurs when I get to the line before last, when I'm trying to output my results to a csv file.

4
  • On which line do you have error? Commented Nov 28, 2018 at 8:06
  • the line before last model_out = resnet_50.predict(img/255) Commented Nov 28, 2018 at 8:07
  • Probably this solution can help: stackoverflow.com/questions/47143718/… Commented Nov 28, 2018 at 8:12
  • @KevinFang I saw that thread. It didn't workout for me Commented Nov 28, 2018 at 8:13

1 Answer 1

0

You first need to extract features of your input image using VGG model and then pass the features to resnet_50 model. So it would look like this:

model = applications.VGG16(include_top=False, weights='imagenet')
features = model.predict(img/255.0)
model_out = resnet_50.predict(features)

Don't forget that the input of your resent_50 model is the features of the image obtained by VGG model.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I missed that. I'm having to do this too for model_out = resnet_50.predict(features/255.0) Any idea why? Other wise the prediction gives me ridiculous numbers
@Joeski No, that should not be done at all. You have not done that during training of the model. What are those "ridiculous numbers" you get?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.