Reputation: 196
I have an input of:
[batch_size, number_of_images, img_size_x, img_size_y]
e.g. [24, 51, 28,28]
Now I want to process each image of an item of the batch through a Conv2d-Layer and collect the outputs.
I would like to reshape the input using a layer
tf.keras.layer.Reshape(1,28,28)
to get something like [1224, 1, 28, 28]
which I can process.
This is a minimal example to reproduce the error
import numpy as np
import tensorflow as tf
tf.enable_eager_execution()
input_data = np.ones((24, 51, 28, 28))
input_label = np.ones((24, 51, 10))
output_data = np.ones((24, 10))
inp_layer = tf.keras.layers.Input(shape=(51, 28, 28))
input_batch_label = tf.keras.layers.Input(shape=(51, 10))
res1 = tf.keras.layers.Reshape((1, 28, 28), name="reshape1")(inp_layer)
perm1 = tf.keras.layers.Permute((2, 3, 1))(res1)
cnn1 = tf.keras.layers.Conv2D(64, 3, padding="same", activation='relu')(perm1)
max1 = tf.keras.layers.MaxPooling2D(16, 16, padding="valid")(cnn1)
res2 = tf.keras.layers.Reshape((51, 64))(max1)
combined_input = tf.keras.layers.concatenate([res2, input_batch_label], axis=-1, )
flat = tf.keras.layers.Flatten()(combined_input)
fc1 = tf.keras.layers.Dense(10)(flat)
model = tf.keras.Model(inputs=[inp_layer, input_batch_label], outputs=fc1)
model.compile(optimizer=tf.train.AdamOptimizer(0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit([input_data, input_label], output_data, batch_size=24, verbose=1)
I assume from the following error that this reshape layer requests the input in the form of [24, 1, 28, 28]
but I need to pass [24, 51, 1, 28, 28]
tensorflow.python.framework.errors_impl.InvalidArgumentError:
Input to reshape is a tensor with 959616 values, but the requested shape has 18816
[[{{node Reshape}}]] [Op:StatefulPartitionedCall]
Do you have any recommendations or see another possibility to structure my model?
If I use tf.reshape this works fine, but I get trouble using Keras functional API, as the output of tf.reshape is no output of a proper Layer.
Thanks in advance
Upvotes: 1
Views: 295
Reputation: 196
@Berriel Thank you very much for your answer. If I change the code to the following everything works great.
def reshape1():
def func(x):
ret = tf.reshape(x, [-1, 1, 28, 28])
return ret
return tf.keras.layers.Lambda(func)
def reshape2():
def func(x):
ret = tf.reshape(x, [-1, 51, 64])
return ret
return tf.keras.layers.Lambda(func)
res1 = reshape1()(inp_layer)
perm1 = tf.keras.layers.Permute((2, 3, 1))(res1)
cnn1 = tf.keras.layers.Conv2D(64, 3, padding="same", activation='relu')(perm1)
max1 = tf.keras.layers.MaxPooling2D(16, 16, padding="valid")(cnn1)
#res2 = tf.keras.layers.Reshape((51, 64))(max1)
res2 = reshape2()(max1)
combined_input = tf.keras.layers.concatenate([res2, input_batch_label], axis=-1, )
flat = tf.keras.layers.Flatten()(combined_input)
fc1 = tf.keras.layers.Dense(10)(flat)
Upvotes: 2