Mask_RCNN icon indicating copy to clipboard operation
Mask_RCNN copied to clipboard

TypeError: Could not build a TypeSpec for <KerasTensor: shape=(None, None, 4)

Open hisham-zadfresh opened this issue 4 years ago • 39 comments

I got this error when I try to run this command:

python3 balloon.py train --dataset=/home/ubuntu/examples/research/mask-rcnn/samples/balloon/data  --weights=coco
Traceback (most recent call last):
  File "balloon.py", line 330, in <module>
    model = modellib.MaskRCNN(mode="training", config=config, model_dir=args.logs)
  File "/home/ubuntu/examples/research/mask-rcnn/mrcnn/model.py", line 1837, in __init__
    self.keras_model = self.build(mode=mode, config=config)
  File "/home/ubuntu/examples/research/mask-rcnn/mrcnn/model.py", line 1876, in build
    x, K.shape(input_image)[1:3]))(input_gt_boxes)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 952, in __call__
    input_list)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1091, in _functional_construction_call
    inputs, input_masks, args, kwargs)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 822, in _keras_tensor_symbolic_call
    return self._infer_output_signature(inputs, args, kwargs, input_masks)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 869, in _infer_output_signature
    keras_tensor.keras_tensor_from_tensor, outputs)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/nest.py", line 659, in map_structure
    structure[0], [func(*x) for x in entries],
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/nest.py", line 659, in <listcomp>
    structure[0], [func(*x) for x in entries],
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/keras_tensor.py", line 606, in keras_tensor_from_tensor
    out = keras_tensor_cls.from_tensor(tensor)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/keras_tensor.py", line 205, in from_tensor
    type_spec = type_spec_module.type_spec_from_value(tensor)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/type_spec.py", line 554, in type_spec_from_value
    (value, type(value).__name__))
TypeError: Could not build a TypeSpec for <KerasTensor: shape=(None, None, 4) dtype=float32 (created by layer 'tf.math.truediv')> with type KerasTensor

This happens in mrcnn/model.py:1875

hisham-zadfresh avatar Jan 05 '21 15:01 hisham-zadfresh

Same here. A pip freeze > out.txt from someone who has the examples working would be nice. The provided requirements.txt is installing some jank that doesn't work together apparently.

TeamDman avatar Jan 06 '21 03:01 TeamDman

I got the same error when trained with tensorflow 2.4

panovr avatar Jan 10 '21 04:01 panovr

Have a look at #2427. Sounds like this will help

RisingPhoelix avatar Jan 25 '21 12:01 RisingPhoelix

@RisingPhoelix I tried this solution, and it seems worked at first. And I will do more test with this repo and tensorflow 2.4.1. Thanks!

panovr avatar Jan 28 '21 07:01 panovr

@panovr

Hi there,

I am suffering the same problem with tensorflow 2.4.1, could you give me some idea how to fix it?

many thanks

enoceanwei avatar Mar 05 '21 08:03 enoceanwei

Anyone that fixed this problem? Facing the same issue

devbas avatar Mar 10 '21 17:03 devbas

For me, it got fixed by selection tensorflow version %tensorflow_version 1.x

nehakumar31 avatar Apr 01 '21 10:04 nehakumar31

has anyone find any solution??

dcbalderas avatar Apr 16 '21 17:04 dcbalderas

For me, it got fixed by selection tensorflow version %tensorflow_version 1.x

i tried the same but keras version is giving me problems. what is ur keras version? @nehakumar31

GK4444 avatar Apr 17 '21 08:04 GK4444

Have a look at #2427. Sounds like this will help

be sure to checkout the tesorflow2.0 branch

raditha avatar Apr 24 '21 12:04 raditha

I got this error when I try to run this command:

python3 balloon.py train --dataset=/home/ubuntu/examples/research/mask-rcnn/samples/balloon/data  --weights=coco
Traceback (most recent call last):
  File "balloon.py", line 330, in <module>
    model = modellib.MaskRCNN(mode="training", config=config, model_dir=args.logs)
  File "/home/ubuntu/examples/research/mask-rcnn/mrcnn/model.py", line 1837, in __init__
    self.keras_model = self.build(mode=mode, config=config)
  File "/home/ubuntu/examples/research/mask-rcnn/mrcnn/model.py", line 1876, in build
    x, K.shape(input_image)[1:3]))(input_gt_boxes)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 952, in __call__
    input_list)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1091, in _functional_construction_call
    inputs, input_masks, args, kwargs)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 822, in _keras_tensor_symbolic_call
    return self._infer_output_signature(inputs, args, kwargs, input_masks)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 869, in _infer_output_signature
    keras_tensor.keras_tensor_from_tensor, outputs)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/nest.py", line 659, in map_structure
    structure[0], [func(*x) for x in entries],
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/nest.py", line 659, in <listcomp>
    structure[0], [func(*x) for x in entries],
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/keras_tensor.py", line 606, in keras_tensor_from_tensor
    out = keras_tensor_cls.from_tensor(tensor)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/keras_tensor.py", line 205, in from_tensor
    type_spec = type_spec_module.type_spec_from_value(tensor)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/type_spec.py", line 554, in type_spec_from_value
    (value, type(value).__name__))
TypeError: Could not build a TypeSpec for <KerasTensor: shape=(None, None, 4) dtype=float32 (created by layer 'tf.math.truediv')> with type KerasTensor

This happens in mrcnn/model.py:1875

found out solution?

rakesh-kumar-k avatar Apr 30 '21 12:04 rakesh-kumar-k

I resolve the problem and can work with below environment: python : 3.7.10 GPU : RTX3090ti pip install tensorflow==2.3.0 pip install tensorflow-gpu==2.3.0 pip install keras==2.4.3 cudatoolkit==11.0.221

Can solve above problem. But another problem produces:

ValueError:
The following Variables were created within a Lambda layer (anchors)but are not tracked by said layer:<tf.Variable 'anchors/Variable:0' shape=(2, 261888, 4) dtype=float32>The layer cannot safely ensure proper Variable reuse across multiple
calls, and consquently this behavior is disallowed for safety. Lambda layers are not well suited to stateful computation; instead, writing a subclassed Layer is the recommend way to define layers withVariables.

The Solutions : There are 2 choices -

  1. Change to use Tensorflow 1.X.. This error will not be raised.
  2. Replace the Lambda layer with subclass of Keras Layer() (mrcnn/model.py:1946):

anchors = KL.Lambda(lambda x: tf.Variable(anchors), name="anchors")(input_image)

to

class AnchorsLayer(KL.Layer):
    def __init__(self, anchors, name="anchors", **kwargs):
        super(AnchorsLayer, self).__init__(name=name, **kwargs)
        self.anchors = tf.Variable(anchors)

    def call(self, dummy):
        return self.anchors

    def get_config(self):
        config = super(AnchorsLayer, self).get_config()
        return config
   
anchors = AnchorsLayer(anchors, name="anchors")(input_image)

Hope to help you!

youreternity1997 avatar May 03 '21 18:05 youreternity1997

I have the same problem! My TensorFlow is also 2.4.1.

silencessss avatar May 28 '21 16:05 silencessss

I had the same problem, when I replaced all the tf.compat.v1.where by tf.where along with that removed the tf.compat.v1.disable_eager_execution()

Once I put tf.compat.v1.disable_eager_execution() back in the error disappears

linaouro avatar May 31 '21 07:05 linaouro

Hi everyone, I am trying to concatenate basic MLP and EfficientNet to use both image features and meta data feature. But i got same error : Could not build a TypeSpec for <KerasTensor: shape=(None, 6) dtype=float32 (created by layer 'tf.concat_1')> with type KerasTensor

Can anyone help me please?

def build_model(): inputs = layers.Input(shape=(380, 380, 3)) x = img_augmentation(inputs) model = EfficientNetB4(include_top=False, input_tensor=x, weights="imagenet")

# Freeze the pretrained weights
model.trainable = False

# Rebuild top
x = layers.GlobalAveragePooling2D(name="avg_pool")(model.output)
top_dropout_rate = 0.2
x = layers.Dropout(top_dropout_rate, name="top_dropout2")(x)
outputs = layers.Dense(3, activation="softmax", name="pred3")(x)

model = tf.keras.Model(inputs, outputs, name="Transfer_Model")
for layer in model.layers[-20:]:
    if not isinstance(layer, layers.BatchNormalization):
        layer.trainable = True

   
return model

def create_mlp(dim,regularizer=None): """Creates a simple two-layer MLP with inputs of the given dimension""" model = Sequential() model.add(Dense(9, input_dim=dim, activation="relu",kernel_regularizer=regularizer )) model.add(Dense(3, activation="relu",name='second' )) return model mlp1 = create_mlp(3,regularizer=regularizers.l2(0.001)) eff = build_model()

Create the input to the final set of layers as the output of both the MLP and EficentNet combinedInput = concatenate([mlp1.output, eff.output])

Error is giving concatenate part. Thank you in advance !

yemre-data avatar May 31 '21 08:05 yemre-data

I got this error when I try to run this command: Traceback (most recent call last): File "E:/PycharmProjects/Mask_RCNN-2.1/samples/balloon/balloon.py", line 330, in model_dir=args.logs) File "E:\PycharmProjects\Mask_RCNN-2.1\model.py", line 1768, in init self.keras_model = self.build(mode=mode, config=config) File "E:\PycharmProjects\Mask_RCNN-2.1\model.py", line 1807, in build gt_boxes = KL.Lambda(lambda x: x / image_scale)(input_gt_boxes) File "E:\PycharmProjects\Mask_RCNN-2.1\venv\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 970, in call input_list) File "E:\PycharmProjects\Mask_RCNN-2.1\venv\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1108, in _functional_construction_call inputs, input_masks, args, kwargs) File "E:\PycharmProjects\Mask_RCNN-2.1\venv\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 840, in _keras_tensor_symbolic_call return self._infer_output_signature(inputs, args, kwargs, input_masks) File "E:\PycharmProjects\Mask_RCNN-2.1\venv\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 886, in _infer_output_signature keras_tensor.keras_tensor_from_tensor, outputs) File "E:\PycharmProjects\Mask_RCNN-2.1\venv\lib\site-packages\tensorflow\python\util\nest.py", line 867, in map_structure structure[0], [func(*x) for x in entries], File "E:\PycharmProjects\Mask_RCNN-2.1\venv\lib\site-packages\tensorflow\python\util\nest.py", line 867, in structure[0], [func(*x) for x in entries], File "E:\PycharmProjects\Mask_RCNN-2.1\venv\lib\site-packages\tensorflow\python\keras\engine\keras_tensor.py", line 590, in keras_tensor_from_tensor out = keras_tensor_cls.from_tensor(tensor) File "E:\PycharmProjects\Mask_RCNN-2.1\venv\lib\site-packages\tensorflow\python\keras\engine\keras_tensor.py", line 182, in from_tensor type_spec = type_spec_module.type_spec_from_value(tensor) File "E:\PycharmProjects\Mask_RCNN-2.1\venv\lib\site-packages\tensorflow\python\framework\type_spec.py", line 580, in type_spec_from_value (value, type(value).name)) TypeError: Could not build a TypeSpec for <KerasTensor: shape=(None, None, 4) dtype=float32 (created by layer 'tf.math.truediv')> with type KerasTensor

python3.6 Keras2.4.3 TensorFlow2.5.0

xiaobeibi avatar May 31 '21 08:05 xiaobeibi

Same problem here and I tried the solution provided by @linaouro, but it still doesn't work. Does anyone have an idea? Thank you in advance!

Google colab python 3.7 Tensorflow 2.4 Keras 2.4.3 cudatoolkit==11.0.2210

Traceback (most recent call last): File "train.py", line 335, in model_dir=args.logs) File "/content/drive/My Drive/mask_rcnn/Mask_RCNN-TF2/mrcnn/model.py", line 1837, in init self.keras_model = self.build(mode=mode, config=config) File "/content/drive/My Drive/mask_rcnn/Mask_RCNN-TF2/mrcnn/model.py", line 1876, in build x, K.shape(input_image)[1:3]))(input_gt_boxes) File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 952, in call input_list) File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 1091, in _functional_construction_call inputs, input_masks, args, kwargs) File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 822, in _keras_tensor_symbolic_call return self._infer_output_signature(inputs, args, kwargs, input_masks) File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 869, in _infer_output_signature keras_tensor.keras_tensor_from_tensor, outputs) File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/nest.py", line 659, in map_structure structure[0], [func(*x) for x in entries], File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/nest.py", line 659, in structure[0], [func(*x) for x in entries], File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/keras_tensor.py", line 606, in keras_tensor_from_tensor out = keras_tensor_cls.from_tensor(tensor) File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/keras_tensor.py", line 205, in from_tensor type_spec = type_spec_module.type_spec_from_value(tensor) File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/type_spec.py", line 554, in type_spec_from_value (value, type(value).name)) TypeError: Could not build a TypeSpec for <KerasTensor: shape=(None, None, 4) dtype=float32 (created by layer 'tf.math.truediv')> with type KerasTensor

Tzu-Jan avatar Jun 01 '21 00:06 Tzu-Jan

@enoceanwei Hi, I think we should use tensorflow v1, not v2.

panovr avatar Jun 01 '21 01:06 panovr

@panovr But it will give another error because rest of part it made with v2.5

yemre-data avatar Jun 02 '21 11:06 yemre-data

@Tzu-Jan I think you should upgrade your python to 3.8 My solution worked when I used tensorflow 2.4.0 with python3.8 (using the tf2 version of the net by https://github.com/leekunhee/Mask_RCNN/tree/tensorflow2.0)

linaouro avatar Jun 02 '21 12:06 linaouro

@linaouro Hello Linaouro. Thanks for the leekunhee's version. But I have a problem. When I was running the following cell in train_shapes.ipynb.

model.train(dataset_train, dataset_val, 
            learning_rate=config.LEARNING_RATE, 
            epochs=1, 
            layers='heads')

The running button had stopped but the status bar was still executing. Should I wait it or there are some problems I have to solve. Screen Shot 2021-06-10 at 10 35 13 PM

bfhaha avatar Jun 10 '21 14:06 bfhaha

@linaouro I figured out the problem. It had been executing 6 hours then interrupted because of running out of the GPU (Google Colab has a limit). I tried to run it again without GPU and it trained successfully. Thanks.

bfhaha avatar Jun 12 '21 16:06 bfhaha

Hi everyone solved this problem on colab. python 3.7.10 !pip install 'h5py==2.10.0' --force-reinstall !pip uninstall keras-nightly !pip uninstall -y tensorflow !pip install tensorflow_gpu==2.4.0 !pip install keras==2.4.0 !pip install keras --user !pip uninstall keras-preprocessing !pip install git+https://github.com/keras-team/keras-preprocessing.git import tensorflow.compat.v1 as tf tf.disable_v2_behavior() tf.compat.v1.get_default_graph()

you should with this order on colab it worked for me !

yemre-data avatar Jun 16 '21 07:06 yemre-data

Hi, I had the same problem on Colab Notebook. Python 3.7.10 Keras: 2.5.0 Tensorflow: 2.5.0 My solution: In file model.py:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
Replace the Lambda layer with subclass of Keras Layer() (mrcnn/model.py:1946), see 

TypeError: Could not build a TypeSpec for <KerasTensor: shape=(None, None, 4) #2458 (comment) For the AttributeError about tf.log see AttributeError: module 'tensorflow' has no attribute 'log' #1797 (comment) and AttributeError: module 'tensorflow' has no attribute 'log' #1797 (comment)

ag-din avatar Jul 05 '21 15:07 ag-din

Hi, I had the same problem on Colab Notebook. Python 3.7.10 Keras: 2.5.0 Tensorflow: 2.5.0 My solution: In file model.py:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
Replace the Lambda layer with subclass of Keras Layer() (mrcnn/model.py:1946), see 

TypeError: Could not build a TypeSpec for <KerasTensor: shape=(None, None, 4) #2458 (comment) For the AttributeError about tf.log see AttributeError: module 'tensorflow' has no attribute 'log' #1797 (comment) and AttributeError: module 'tensorflow' has no attribute 'log' #1797 (comment)

Hi @agustinadinamarca, I did this and I am not getting the TypeError anymore but as mentioned in your comment, I am getting this error: AttributeError: module 'tensorflow' has no attribute 'log' Which comment in the linked issue helped you? I tried this but I am still getting the log error.

mansi-aggarwal-2504 avatar Jul 19 '21 09:07 mansi-aggarwal-2504

Hi, I had the same problem on Colab Notebook. Python 3.7.10 Keras: 2.5.0 Tensorflow: 2.5.0 My solution: In file model.py:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
Replace the Lambda layer with subclass of Keras Layer() (mrcnn/model.py:1946), see 

TypeError: Could not build a TypeSpec for <KerasTensor: shape=(None, None, 4) #2458 (comment) For the AttributeError about tf.log see AttributeError: module 'tensorflow' has no attribute 'log' #1797 (comment) and AttributeError: module 'tensorflow' has no attribute 'log' #1797 (comment)

Hi @agustinadinamarca, I did this and I am not getting the TypeError anymore but as mentioned in your comment, I am getting this error: AttributeError: module 'tensorflow' has no attribute 'log' Which comment in the linked issue helped you? I tried this but I am still getting the log error.

@mansi-aggarwal-2504 I have also the same error. Which version of the Mask RCNN source code do you use?

getch-geohum avatar Jul 23 '21 09:07 getch-geohum

Hi @getgeosoft. I actually got caught up in errors due to version incompatibility issues. But I started afresh and created a new environment. Refer to this issue I raised. I hope this helps. P.S. I use Google Colab.

mansi-aggarwal-2504 avatar Jul 23 '21 10:07 mansi-aggarwal-2504

In addition to the two options which youreternity1997 provided, here is a third option. we can change : gt_boxes = KL.Lambda(lambda x: norm_boxes_graph(x, K.shape(input_image)[1:3]))(input_gt_boxes) #this for tf1 gt_boxes = KL.Lambda(lambda x: norm_boxes_graph2(x))([input_gt_boxes,input_image]) #this for tf2 where, def norm_boxes_graph2(x): boxes,tensor_for_shape = x shape = tf.shape(tensor_for_shape)[1:3] return norm_boxes_graph(boxes,shape)

rcx986635 avatar Aug 01 '21 06:08 rcx986635

@Tzu-Jan I think you should upgrade your python to 3.8 My solution worked when I used tensorflow 2.4.0 with python3.8 (using the tf2 version of the net by https://github.com/leekunhee/Mask_RCNN/tree/tensorflow2.0)

Is it possible that these changes are merged by the moderators of this repository? @waleedka

Also, why is this repository not actively maintained? despite people finding the use for it? I would be glad if I could help. Please guide me.

adsk2050 avatar Oct 20 '21 12:10 adsk2050

TypeError: Could not build a TypeSpec for KerasTensor(type_spec=TensorSpec(shape=(None, None, 4), dtype=tf.float32, name=None), name='tf.math.truediv/truediv:0', description="created by layer 'tf.math.truediv'") of unsupported type <class 'tensorflow.python.keras.engine.keras_tensor.KerasTensor'>.

i got this error when i try to run maskrcnn code

any one fix this error

BangarrajuMuppidi avatar Nov 20 '21 04:11 BangarrajuMuppidi