问题
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 problem involving a very large dataset, I am using a generator to read images into arrays which I am passing to Keras Model's fit_generator
function.
When I run my generator in isolation it works fine, and produces outputs of the appropriate shape. It yields a tuple containing two entries, the first of which has shape (4, 100, 100, 1)
and the second of which has shape (4, )
.
Reading about multiple input Keras CNNs has given me the impression that this is the right format for a generator for a 4 input CNN that is identifying which of the 4 inputs contains an image.
However, when I run the code I get:
"ValueError: Error when checking input: expected input_121 to have 4 dimensions, but got array with shape (100, 100, 1)"
I've been searching for a solution for some time now and I suspect that the problem lies in getting my (100, 100, 1)
shape arrays to be sent to the Inputs as (None, 100, 100, 1)
shape arrays.
But when I tried to modify the output of my generator I get an error about having dimension 5
, which makes sense as an error because the output of the generator should have the form X, y = [X1, X2, X3, X4], [a, b, c, d]
, where Xn
has shape (100, 100, 1)
, and a/b/c/d are numbers.
Here is the code:
https://gist.github.com/anonymous/d283494aee982fbc30f3b52f2a6f422c
Thanks in advance!
回答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
来源:https://stackoverflow.com/questions/44399299/keras-python-multi-image-input-shape-error