Nicholas Johnson
Nicholas Johnson

Reputation: 13

Keras Error using Lambda

I've just started out with keras this week and have been going through the documentation to figure out why I'm getting an error. But I figured it would be faster if I got some help, and it feels like a detail that I'm just not seeing.

Error when checking model input: expected lambda_input_1 to have 4 dimensions, 
but got array with shape (0, 1)

Here is the Entire Code to look over if that helps you see what I'm doing wrong.

import csv
import cv2
import numpy as np

lines = []
with open('../data2/driving_log.csv') as csvfile:
        reader = csv.reader(csvfile)
        for line in reader:
                lines.append(line)
        for row in reader:
            steering_center = float(row[3])
                # create adjusted steering measurements for the side camera images
            correction = 0.2 # this is a parameter to tune
            steering_left = steering_center + correction
            steering_right = steering_center - correction
                 # read in images from center, left and right cameras
            directory = "..." # fill in the path to your training IMG directory
            img_center = process_image(np.asarray(Image.open(path + row[0])))
            img_left = process_image(np.asarray(Image.open(path + row[1])))
            img_right = process_image(np.asarray(Image.open(path + row[2])))
                # add images and angles to data set
            car_images.extend(img_center, img_left, img_right)
            steering_angles.extend(steering_center, steering_left, steering_right)

images = []
measurements =[]
for line in lines:
        source_path = line[0]
        filename = source_path.split('/')[-1]
        current_path = '../data2/IMG/' + filename
        image = cv2.imread(current_path)
        measurement = float(line[3])
        measurements.append(measurement)

augmented_images, augmented_measuremnets = [], []
for image, measurement in zip(image, measurements):
        augmented_images.append(image)
        augmented_measuremnets.append(measurement)
        augmented_images.append(cv2.flip(image,1))
        augmented_measuremnets.append(measurement*-1)

X_train = np.array(images)
y_train = np.array(measurements)

from keras.models import Sequential
from keras.layers import Flatten, Dense, Lambda, Cropping2D
from keras.layers.convolutional import Convolution2D
from keras.layers.pooling import MaxPooling2D

model = Sequential()
model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160,320,3)))
model.add(Cropping2D(cropping=((70,25), (0,0))))
model.add(Convolution2D(24,5,5, subsample=(2,2), activation="relu"))
model.add(Convolution2D(36,5,5, subsample=(2,2), activation="relu"))
model.add(Convolution2D(48,5,5, subsample=(2,2), activation="relu"))
model.add(Convolution2D(64,3,3, activation="relu"))
model.add(Convolution2D(64,3,3, activation="relu"))
model.add(Flatten())
model.add(Dense(120))
model.add(Dense(84))
model.add(Dense(1))

model.compile(loss='mse', optimizer='adam')
model.fit(X_train, y_train, validation_split=0.2, shuffle=True, nb_epoch=5)


model.save('model.h5')
exit()

I'm trying to figure out if I need to resize my input data set for this model.fit(...) function to work properly. It worked before without issues until I added the cropping and more data.

Thanks, any direction would help, even if it just gets me closer to understanding this dimensional error.

File "nvidia.py", line 64, in <module>
        model.fit(X_train, y_train, validation_split=0.2, shuffle=True, nb_epoch=5)
      File "/home/carnd/anaconda3/envs/carnd-term1/lib/python3.5/site-packages/keras/models.py", line 672, in fit
        initial_epoch=initial_epoch)
      File "/home/carnd/anaconda3/envs/carnd-term1/lib/python3.5/site-packages/keras/engine/training.py", line 1117, in fit
        batch_size=batch_size)
      File "/home/carnd/anaconda3/envs/carnd-term1/lib/python3.5/site-packages/keras/engine/training.py", line 1030, in _standardize_user_data
        exception_prefix='model input')
      File "/home/carnd/anaconda3/envs/carnd-term1/lib/python3.5/site-packages/keras/engine/training.py", line 112, in standardize_input_data
        str(array.shape))
    ValueError: Error when checking model input: expected lambda_input_1 to have 4 dimensions, but got array with shape (0, 1)

Upvotes: 1

Views: 157

Answers (2)

Nicholas Johnson
Nicholas Johnson

Reputation: 13

The solutions was not the Lambda or in Keras it was me being foolish, somehow this model didn't get the line images.append(image) so the X_train array was not being populated. Problem solved.

Upvotes: 0

Yu-Yang
Yu-Yang

Reputation: 14619

You forgot to insert image into images. So X_train is basically empty.

Upvotes: 1

Related Questions