Multiple images as CNN input - cannot reshape array

Viewed 161

I am trying to train CNN to drive a car using 4 images as one input.

This is how my code looks like

import numpy as np
from datetime import datetime
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dropout, Dense
from tensorflow_core.python.keras.layers import concatenate
from tensorflow_core.python.keras.models import Model


# Loading data
train_data = np.load("/mydrive/Mister_car/training_data-6.npy", allow_pickle=True)
WIDTH = 200
HEIGHT = 160
MODEL_DIR = "/mydrive/Mister_car/model/"

# Load new model
#model = alexnet((WIDTH, HEIGHT, 3), 9)

model1 = alexnet((WIDTH, HEIGHT, 3), 9)
model2 = alexnet((WIDTH, HEIGHT, 3), 9)
model3 = alexnet((WIDTH, HEIGHT, 3), 9)
model4 = alexnet((WIDTH, HEIGHT, 3), 9)

combined = concatenate([model1.output, model2.output, model3.output, model4.output])
out = Dense(9, activation="softmax")(combined)

model = Model(inputs=[model1.input, model2.input, model3.input, model4.input], outputs = out)

model.compile(optimizer=tf.keras.optimizers.Adam(0.01),
                 loss='categorical_crossentropy',
                 metrics=['accuracy'])

# Load existing model
# model = tf.keras.models.load_model(MODEL_DIR)

if len(train_data) > 10:
    print("git")
    # Splitting data into a training set and test set
    train = train_data[:]
    test = train_data[:]

    print(train[6][0][0].shape)

    X = np.array([i[0] for i in train]).reshape(-1, 4, WIDTH, HEIGHT, 3)
    Y = np.array([i[1] for i in train])

    x_test = np.array([i[0] for i in test])#.reshape(-1, 4, WIDTH, HEIGHT, 3)
    y_test = np.array([i[1] for i in test])

    # start training
    model.fit(x=X,
              y=Y,
              epochs=10,
              validation_data=(x_test, y_test))

    # save the whole model
    model.save(MODEL_DIR)

This lines causes problem

X = np.array([i[0] for i in train]).reshape(-1, 4, WIDTH, HEIGHT, 3)
Y = np.array([i[1] for i in train])

x_test = np.array([i[0] for i in test])#.reshape(-1, 4, WIDTH, HEIGHT, 3)
y_test = np.array([i[1] for i in test])

cannot reshape array of size 200 into shape (4,200,160,3)

Earlier with one image as input I used this:

X = np.array([i[0] for i in train]).reshape(-1, WIDTH, HEIGHT, 3)
Y = np.array([i[1] for i in train])

x_test = np.array([i[0] for i in test])#.reshape(-1, WIDTH, HEIGHT, 3)
y_test = np.array([i[1] for i in test])

where i[0] is the image and i[1] is a list with keyboard input.

Now i[0] is a list of 4 images and i[1] is a list of 4 lists. So I added this new dimension, which is for but I get this error. It's very strange for me. I would be really appreciated for your help.

0 Answers
Related