0

i am working on tensorflow 2.x. I want to accept an Integer as one of the Multi-Inputs, and in some stage of the model, i need to do tf.reshape with shape set by the Integer of Input. It is more like:

...
x_h = Input((shape=(), dtype=tf.int32)
...
x=...
x = tf.reshape(x, [x_h, x_h*2, x_h*2, 64/x_h])
...
return Model(inputs=[..., x_h], outputs=x)

All of the model runs on tensorflowlite. Tensorflow reports me that shape can not be set by tensor. I also tried tensor.numpy, still got failure.

Below is my test code:

x_in = Input(shape=(None, None, 3))
x_h = Input(shape=(), dtype=tf.int32)
x = Conv2D(32, 3, padding='same')(x_in)
x = Reshape((x_h*2, x_h, 16))(x)
model = Model(inputs=[x_in, x_h], outputs=x)
model.compile(optimizer="Adam", loss="mse", metrics=["mae"])
x = np.random.random((3, 2, 2, 3))
x_h = 2
y = np.random.random((3, 4, 2, 16))
model.fit([x, x_h], y)

And,tensorflow reports:

Traceback (most recent call last):
  File "test.py", line 12, in <module>
    x = Reshape((x_h*2, x_h, 16))(x)
  File "/home/walter/.local/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/walter/.local/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
ValueError: Exception encountered when calling layer "tf.reshape" (type TFOpLambda).

Tried to convert 'shape' to a tensor and failed. Error: Shapes must be equal rank, but are 1 and 0
        From merging shape 2 with other shapes. for '{{node tf.reshape/Reshape/packed}} = Pack[N=4, T=DT_INT32, axis=0](tf.reshape/Reshape/shape/reshape/strided_slice, Placeholder, Placeholder_1, tf.reshape/Reshape/packed/3)' with input shapes: [], [?], [?], [].

Call arguments received:
  • tensor=tf.Tensor(shape=(None, None, None, 32), dtype=float32)
  • shape=('tf.Tensor(shape=(), dtype=int32)', 'tf.Tensor(shape=(None,), dtype=int32)', 'tf.Tensor(shape=(None,), dtype=int32)', '16')
  • name=None

Could anyone give me a hand? Appreciate so much!

Best Regards

Walter

walter
  • 11
  • 2
  • You should provide a code that reproduces your error so people understand your situation better. That said, you may need to use the Reshape layer instead of reshape function. https://www.tensorflow.org/api_docs/python/tf/keras/layers/Reshape – Kota Mori Jul 05 '22 at 12:47
  • Hi, Kota, thanks very much! I updated the question, and tf.karas.Reshape does not works too. Best Regards – walter Jul 06 '22 at 02:35
  • You will need predetermined input shapes for your model. `None` is allowed only in the batch dimension. – Shubham Panchal Jul 06 '22 at 05:40

1 Answers1

0

You can not increase the size of a tensor with Reshape. You can reshape it but the total dimension will be same. Like (4,3,2) -> (2,12).

There is tf.keras.layers.Resizing function but you cannot change the channel size with this method.

x_in = Input(shape=(5, 10, 3))

x = Conv2D(32, (3,3), padding='same')(x_in)
x = Resizing(10, 10)(x)
 
model = Model(inputs=x_in, outputs=x)
model.compile(optimizer="Adam", loss="mse", metrics=["mae"])
 
x = np.random.random((3, 5, 10, 3))
y = np.random.random((3, 10, 10, 32))
model.fit(x,y)

It looks like you want to create a network that accepts any size of images. I suggest you to take a look tf.keras.layers.GlobalAveragePooling2D, which may be used just before the fully connected layers. I hope this helps.

ai-py
  • 177
  • 1
  • 7
  • Hi, quasimodo, thank you very much for help! Yes, i know reshape can not change the total size of input, and , in fact, what i want is accpeting an input integer, and apply it on reshape during model running. But tensorflow sees do not support this :( Best Regards – walter Jul 14 '22 at 03:08