Reputation: 113
I have a Keras model designed to turn a (600, 600, 3) image into 5 copies of a (199,199,3) image.
X has a shape of (samples, 1, 600, 600, 3) and Y has a shape of (samples, 5, 199, 199, 3).
My model is:
inputs = Input(shape=(1, 600, 600, 3))
output1 = Conv3D(20, kernel_size=(1, 2, 2), activation='relu')(inputs)
output2 = Conv3D(40, kernel_size=(1, 3, 3), activation='relu')(output1)
output3 = MaxPooling3D(pool_size=(1, 3, 3))(output2)
output4 = Conv3D(120, kernel_size=(1, 4, 4), activation='relu')(output3)
output5 = Conv3DTranspose(3, kernel_size=(1, 4,4), activation='relu')(output4)
listOutput = concatenate(axis=0, inputs=[output5]*5)
model = Model(inputs=inputs, outputs=listOutput)
model.compile(loss='mse',
optimizer='adam',
metrics=['mae'])
When I compile my model, I get:
ValueError: Error when checking target: expected concatenate_1 to have shape (1, 199, 199, 3) but got array with shape (5, 199, 199, 3)
It appears that Keras is reading my target shape incorrectly, based on the shape of X. For example, if I change X to input a shape of (2, 600, 600, 3), I get:
ValueError: Error when checking target: expected concatenate_1 to have shape (2, 199, 199, 3) but got array with shape (5, 199, 199, 3).
Keras is interpreting my target shape based on X, with it being equal to (X.shape[1], 199, 199, 3) when I just want to have an output shape of (5, 199, 199, 3), and I defined Y as an array with shape (samples, 5, 199, 199, 3). I have no idea how Keras is actually reading my target data.
Is there any way to manually override this, or manually define an output shape in Keras? Thanks.
Upvotes: 1
Views: 67
Reputation: 33450
Short answer: use axis=1
in concatenation layer.
Long answer: The output of Conv3DTranspose
layer (i.e. output5
tensor) has a shape of (None, 1, 199, 199, 3)
. Now, you want to concatenate 5 copies of this tensor (which sounds a bit odd to me since I don't know the application, but surely you know what you are doing!) on the second axis. Therefore, you need to use axis=1
(and not 0 which would be the batch/samples axis) as the concatenation axis to get an output of shape (None, 5, 199, 199, 3)
, i.e. 5 copies concatenated together.
Upvotes: 2