我剛剛開始與keras本週,並已經通過文檔來弄清楚爲什麼我得到一個錯誤。但我認爲如果我得到一些幫助,速度會更快,而且感覺就像我沒有看到的細節。Keras錯誤使用Lambda
Error when checking model input: expected lambda_input_1 to have 4 dimensions,
but got array with shape (0, 1)
這是整個代碼,看看是否可以幫助你看到我做錯了什麼。
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()
我試圖找出如果我需要調整我的輸入數據設置該model.fit(...)
功能正常工作。它之前工作沒有問題,直到我添加裁剪和更多的數據。
謝謝,任何方向都會有幫助,即使它讓我更接近理解這個尺寸誤差。
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)
你說得對!我不知道這是怎麼搞砸的......我在之前的模型上做過。謝謝! –