我正在训练自动驾驶汽车的模型。输入是图像,标签是包含控件[Z,Q,D,M]的Multi-Hot编码器数组。 如果汽车向前行驶,则数组为[1,0,0,0],如果向左行驶,则数组为[0,1,0,0]
根据错误:
ValueError:输入数组的样本数应与 目标数组。找到40个输入样本和4个目标样本。
我可以假设模型将数组中的每个数字都视为一个标签,而整个数组必须是一个标签。
这是我的代码:
def train_model(model, save_best, lr, samples_per_epoch, epochs, data_dir, X_train, X_valid, y_train, y_valid):
checkpoint = ModelCheckpoint('model-{epoch:03d}.h5',
monitor='val_loss',
verbose=0,
save_best_only=save_best,
mode='auto')
model.compile(loss='mean_squared_error', optimizer=Adam(lr=lr))
model.fit_generator(batch_generator(data_dir, X_train, y_train, BATCH_SIZE, True),
samples_per_epoch,
epochs,
max_q_size=1,
validation_data=batch_generator(data_dir, X_valid, y_valid, BATCH_SIZE, False),
nb_val_samples=len(X_valid),
callbacks=[checkpoint],
verbose=1)
def main():
# get_data()
data = load_data(DATA_DIR, TEST_SIZE)
model = nvidia_model(0.5)
train_model(model, SAVE_BEST, LR, SAMPLES_PER_EPOCH, EPOCHS, DATA_DIR, *data)
if __name__ == "__main__":
main()
批处理生成器
的代码def batch_generator(data_dir, image_paths, controls, batch_size, is_training):
"""
Generate training image give image paths and associated control keys
"""
images = np.empty([batch_size, IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS])
steers = np.empty(batch_size)
while True:
i = 0
for index in np.random.permutation(image_paths.shape[0]):
image_path = image_paths[index]
control = controls[index]
image = load_image(data_dir, image_path)
# add the image and steering angle to the batch
images[i] = preprocess(image)
i += 1
if i == batch_size:
break
yield images, control
```