Skip to content Skip to sidebar Skip to footer

Keras Python Multi Image Input Shape Error

I am trying to teach myself to build a CNN that takes more than one image as an input. Since the dataset I created to test this is large and in the long run I hope to solve a probl

Solution 1:

You are creating a list of arrays in your generator with the wrong dimensions.

If you want the correct shape, reshape individual images to have the 4 dimensions: (n_samples, x_size, y_size, n_bands) your model will work. In your case you should reshape your images to (1, 100, 100, 1).

At the end stack them with np.vstack. The generator will yield an array of shape (4, 100, 100, 1).

Check if this adapted code works

def input_generator(folder, directories):

    Streams = []
    for i in range(len(directories)):
        Streams.append(os.listdir(folder + "/" + directories[i]))
        for j in range(len(Streams[i])):
            Streams[i][j] = "Stream" + str(i + 1) + "/" + Streams[i][j]   
        Streams[i].sort()


    length = len(Streams[0])
    index = 0
    while True:
        X = []
        y = np.zeros(4)
        for Stream in Streams:
            image = load_img(folder + '/' + Stream[index], grayscale = True)
            array = img_to_array(image).reshape((1,100,100,1))
            X.append(array)
        y[int(Stream[index][15]) - 1] = 1
        index += 1
        index = index % length
        yield np.vstack(X), y

Post a Comment for "Keras Python Multi Image Input Shape Error"