Closed micosacak closed 3 years ago
@micosacak Can you please share the error trace? Thanks
@AdityaKane2001 Here is the output.
>>>my_model = Unet()
2021-04-14 17:32:12.327108: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-04-14 17:32:12.327370: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 37, in Unet
File "<stdin>", line 3, in get_crop_shape
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'
@micosacak
This may be stupid on my part, but can you resize the inputs and use input_shape= (128,128,1)
? Resize layer is available in keras.
@micosacak,
On running the code, I am facing an error stating NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for model/variables/variables
. Please find the gist of it here.
In order to reproduce the issue reported, could you please provide the TensorFlow version, a minimal code snippet and the supporting files necessary to run the code.
Also, take a look at @AdityaKane2001's comment and check if it helps.
Thanks!
@amahendrakar
model1 = Unet(input_shape = (129,239,1))
model1.load_weights("model/variables/variables")
model1.load_weights("model/variables/variables")
will raise error of course. Because you do not have a trained model saved as "model" in the path. If you want any dataset you can download an example from here. But my question does not require an input data, I think. Because it is a more general question.
I think you did not get my question.
My question is:
How to generate a model that can get different input shapes, even after training? For instance, if I use (128,128,1) as input shape and save the model, the model requires to have always (128,128,1) input shape. If the image shape is different from (128,128,1), then it raises error. If I use (None, None,1) as input shape, then I can not initiate a model, as it raises this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 37, in Unet
File "<stdin>", line 3, in get_crop_shape
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'
@AdityaKane2001 what do you mean by resizing? Is this what you are suggesting?
BTW; the tensorflow 2.4.1 raises the error, if the input shape is (256,256,1) as below:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ilias/py37CPUtf241/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 998, in __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
File "/home/ilias/py37CPUtf241/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py", line 274, in assert_input_compatibility
', found shape=' + display_shape(x.shape))
ValueError: Input 0 is incompatible with layer model_1: expected shape=(None, 128, 128, 1), found shape=(1, 256, 256, 1)
Now, if I try the same with tensorflow 2.2
WARNING:tensorflow:Model was constructed with shape (None, 128, 128, 1) for input Tensor("input_2:0", shape=(None, 128, 128, 1), dtype=float32), but it was called on an input with incompatible shape (1, 256, 256, 1).
However, tensorflow 2.2 still perform image segmentation correctly!
In order to prevent above errors; I do the following (as I have shown above), if the image shape is (256,256,1):
import tensorflow as tf
model1 = Unet(input_shape = (256,256,1))
model1.load_weights("model/variables/variables")
then it does not show any error or warning message!
@micosacak
Yes, I was suggesting Resizing
layer. The thing is, when you pass some integer values in shape, you essentially create a graph of the model. All the functions used in the model are now traced by AutoGraph. This means that your input size is now fixed. Thus, using None
in your input shape means that you are not allowing the model to be traced. Also, when you compile the model, the model now fixes ambiguous shapes of the tensors that are inside the graph. Obviously, this cannot be done when you pass None
. Hence the function get_crop_shape
fails because while tracing a dummy tensor is passed in that function.
@amahendrakar please correct me if I am wrong.
Hope this helps.
@micosacak I think there are two options.
target_size
), orImageDataGenerator
class that reshapes your input images to target_size
as shown in this tutorial. In the above tutorial there are several models that were trained with different size. Later those models were used with flower_dataset
that is of different size when compared to the original images that were used for training those models. Hope it helps. Thanks!
Thanks for your help, I will check the tutorial as well.
I am trying to have a model that can get different input_shapes. I use a (128,128,1) input_shape for for training data as all my training images have the same size.
However, after training the image size will be variable (e.g.; 312x256, 550x2300, etc). How to create a model that can get variable input shapes. The model below raises error in
get_crop_shape
, if I use input_shape as [None, None,1]. I have to useget_crop_shape
to prevent problems during concatenations as tensor shape will be different if the shape is not multiple of 128.if I use any shape excepts None (e.g.; [128, 128, 1] or [129, 239, 1]), the model works!
I need the model accepts different input_shapes, so I can use it in tensorflow/java.
In Python, I create model, perform training and then save the model as
model.save("model")
. Then, I create a new model based on the shape of new images and only load weights and it works! For instance, if I save my model after training asmodel.save("model")
. I can use it for different image shapes as below.I ask the same question on tensorflow/java as well. #288