1
$\begingroup$

I have built a CNN with images of size 720 (height) and 1280 (width). I attempt to re scale images to 144x256. However, I receive this error:

ValueError: Error when checking input: expected conv2d_25_input to have shape (144, 256, 3) but got array with shape (256, 144, 3)

I do the obvious thing and swap the height and width, but the error still persists:

ValueError: Error when checking input: expected conv2d_33_input to have shape (256, 144, 3) but got array with shape (144, 256, 3)

The error is the same but reversed. Here is my code, ran on google colab. I have embedded the data from github into the project so you should have no problem running the entire program. Can someone explain where the error originates from? Thank you!

# Get data
! git clone https://github.com/finn-williams/cachedetector.git

# Import dependancies
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import cv2
import os
import random
%tensorflow_version 1.x
import tensorflow
#from keras.models import Sequential
from keras.layers import Conv2D,Dense,Flatten,Dropout,MaxPooling2D
#from keras.preprocessing.image import ImageDataGenerator
#from keras.callbacks import ReduceLROnPlateau
#from keras import *
from keras.preprocessing.image import ImageDataGenerator
#from keras.preprocessing import image
from keras.callbacks import ModelCheckpoint
from keras import *

# Get data paths
total_images_train_cache = os.listdir('/content/cachedetector/Data/train/Cache')
total_images_train_no_cache = os.listdir('/content/cachedetector/Data/train/NoCache')

# Image processing with generators
image_train_transformer = ImageDataGenerator(rescale = 1./255,
                                   rotation_range = 5,
                                   shear_range = 0.2,
                                   width_shift_range = 0.1,  # Shifts width of each image up to second parameter
                                   height_shift_range = 0.1, # Shifts width of each image up to second parameter
                                   zoom_range = 0.2,  # Zooms into image
                                   brightness_range = [0.2, 1.0], # Changes brightness between parameters
                                   channel_shift_range = 70 # Changes brightness
                                   )

image_test_transformer = ImageDataGenerator(rescale = 1./255)

# Hyperparameters
image_height = 144
image_width = 256
batch_size = 10 # High batch = poor generalization
no_of_epochs  = 200

# Reshape Images
training_set = image_train_transformer.flow_from_directory('/content/cachedetector/Data/train',
                                                 target_size = (image_width, image_height),
                                                 batch_size = batch_size,
                                                 class_mode = 'binary')

test_set = image_test_transformer.flow_from_directory('/content/cachedetector/Data/test',
                                            target_size = (image_width, image_height),
                                            batch_size = batch_size,
                                            class_mode = 'binary')

val_set = image_test_transformer.flow_from_directory('/content/cachedetector/Data/val',
                                            target_size = (image_width, image_height),
                                            batch_size = 1,
                                            shuffle = False,
                                            class_mode = 'binary')

# Build model
model = Sequential([Conv2D(32, (3, 3),input_shape = (image_height, image_width, 3), activation = 'relu'),
                    # Keep filters low at beginning/iamges close to output
                    Conv2D(32,(3,3),activation='relu'),
                    MaxPooling2D(pool_size=(2,2)),
                    Dropout(0.2),
                    Conv2D(64,(3,3),activation='relu'),
                    # Ramp up filters in hidden layer
                    Conv2D(64,(3,3),activation='relu'),
                    MaxPooling2D(pool_size=(2,2)),
                    # Max pooling to reduce dimenstions caused by filters
                    Dropout(0.2),
                    Conv2D(128,(3,3),activation='relu'),
                    Conv2D(128,(3,3),activation='relu'),
                    MaxPooling2D(pool_size=(2,2)),
                    Dropout(0.2),
                    Conv2D(256,(3,3),activation='relu'),
                    Conv2D(256,(3,3),activation='relu'),
                    MaxPooling2D(pool_size=(2,2)),
                    Flatten(),
                    Dense(units=256,activation='relu'),
                    Dropout(0.2),
                    Dense(units=1,activation='sigmoid')])

# Model settings
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Train
history = model.fit_generator(training_set,
                    steps_per_epoch=1319//batch_size, # When to conduct new epoch (calculated using ceil(num_samples / batch_size) )
                    epochs=no_of_epochs,  
                    validation_data=test_set,
                    validation_steps=145//batch_size,
                    callbacks=callbacks_list
                   )
$\endgroup$
2
$\begingroup$

Clearly, there is this inconsistency between the shape of input images:

target_size = (image_width, image_height)

and the input shape of the model:

input_shape = (image_height, image_width, 3)

These two should be the same. So, in order to save effort and make the smallest changes, just fix the input shape of the model as follow:

input_shape = (image_width, image_height, 3)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.