Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Could not build a TypeSpec for <KerasTensor: shape=(None, None, 4) dtype=float32 (created by layer 'tf.math.truediv')> with type KerasTensor #5

Open
trueToastedCode opened this issue Sep 5, 2022 · 1 comment

Comments

@trueToastedCode
Copy link

My environment:

Chip: Apple M1
Python Platform: macOS-12.5.1-arm64-arm-64bit
Tensor Flow Version: 2.5.0
Keras Version: 2.5.0

Python 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:14) 
[Clang 12.0.1 ]
Pandas 1.4.4
Scikit-Learn 1.1.2
GPU is available

Error causing code:
model = MaskRCNN(mode='training', model_dir="logs", config=config)
Error:

TypeError                                 Traceback (most recent call last)
Input In [7], in <cell line: 2>()
      1 # define the model
----> 2 model = MaskRCNN(mode='training', model_dir="logs", config=config)

File ~/VSCode/ai1/mrcnn/model.py:1846, in MaskRCNN.__init__(self, mode, config, model_dir)
   1844 self.model_dir = model_dir
   1845 self.set_log_dir()
-> 1846 self.keras_model = self.build(mode=mode, config=config)

File ~/VSCode/ai1/mrcnn/model.py:1884, in MaskRCNN.build(self, mode, config)
   1881 input_gt_boxes = KL.Input(
   1882     shape=[None, 4], name="input_gt_boxes", dtype=tf.float32)
   1883 # Normalize coordinates
-> 1884 gt_boxes = KL.Lambda(lambda x: norm_boxes_graph(
   1885     x, K.shape(input_image)[1:3]))(input_gt_boxes)
   1886 # 3. GT Masks (zero padded)
   1887 # [batch, height, width, MAX_GT_INSTANCES]
   1888 if config.USE_MINI_MASK:

File ~/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/engine/base_layer.py:945, in Layer.__call__(self, *args, **kwargs)
    939 # Functional Model construction mode is invoked when `Layer`s are called on
    940 # symbolic `KerasTensor`s, i.e.:
    941 # >> inputs = tf.keras.Input(10)
    942 # >> outputs = MyLayer()(inputs)  # Functional construction mode.
    943 # >> model = tf.keras.Model(inputs, outputs)
    944 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
--> 945   return self._functional_construction_call(inputs, args, kwargs,
    946                                             input_list)
    948 # Maintains info about the `Layer.call` stack.
    949 call_context = base_layer_utils.call_context()

File ~/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/engine/base_layer.py:1083, in Layer._functional_construction_call(self, inputs, args, kwargs, input_list)
   1078     training_arg_passed_by_framework = True
   1080 with call_context.enter(
   1081     layer=self, inputs=inputs, build_graph=True, training=training_value):
   1082   # Check input assumptions set after layer building, e.g. input shape.
-> 1083   outputs = self._keras_tensor_symbolic_call(
   1084       inputs, input_masks, args, kwargs)
   1086   if outputs is None:
   1087     raise ValueError('A layer\'s `call` method should return a '
   1088                      'Tensor or a list of Tensors, not None '
   1089                      '(layer: ' + self.name + ').')

File ~/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/engine/base_layer.py:816, in Layer._keras_tensor_symbolic_call(self, inputs, input_masks, args, kwargs)
    814   return tf.nest.map_structure(keras_tensor.KerasTensor, output_signature)
    815 else:
--> 816   return self._infer_output_signature(inputs, args, kwargs, input_masks)

File ~/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/engine/base_layer.py:861, in Layer._infer_output_signature(self, inputs, args, kwargs, input_masks)
    858     self._handle_activity_regularization(inputs, outputs)
    859   self._set_mask_metadata(inputs, outputs, input_masks,
    860                           build_graph=False)
--> 861   outputs = tf.nest.map_structure(
    862       keras_tensor.keras_tensor_from_tensor, outputs)
    864 if hasattr(self, '_set_inputs') and not self.inputs:
    865   # TODO(kaftan): figure out if we need to do this at all
    866   # Subclassed network: explicitly set metadata normally set by
    867   # a call to self._set_inputs().
    868   self._set_inputs(inputs, outputs)

File ~/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/util/nest.py:867, in map_structure(func, *structure, **kwargs)
    863 flat_structure = (flatten(s, expand_composites) for s in structure)
    864 entries = zip(*flat_structure)
    866 return pack_sequence_as(
--> 867     structure[0], [func(*x) for x in entries],
    868     expand_composites=expand_composites)

File ~/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/util/nest.py:867, in <listcomp>(.0)
    863 flat_structure = (flatten(s, expand_composites) for s in structure)
    864 entries = zip(*flat_structure)
    866 return pack_sequence_as(
--> 867     structure[0], [func(*x) for x in entries],
    868     expand_composites=expand_composites)

File ~/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/engine/keras_tensor.py:580, in keras_tensor_from_tensor(tensor)
    577     keras_tensor_cls = cls
    578     break
--> 580 out = keras_tensor_cls.from_tensor(tensor)
    582 if hasattr(tensor, '_keras_mask'):
    583   out._keras_mask = keras_tensor_from_tensor(tensor._keras_mask)  # pylint: disable=protected-access

File ~/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/engine/keras_tensor.py:172, in KerasTensor.from_tensor(cls, tensor)
    169 else:
    170   # Fallback to the generic arbitrary-typespec KerasTensor
    171   name = getattr(tensor, 'name', None)
--> 172   type_spec = tf.type_spec_from_value(tensor)
    173   return cls(type_spec, name=name)

File ~/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/framework/type_spec.py:579, in type_spec_from_value(value)
    575 except (ValueError, TypeError) as e:
    576   logging.vlog(
    577       3, "Failed to convert %r to tensor: %s" % (type(value).__name__, e))
--> 579 raise TypeError("Could not build a TypeSpec for %r with type %s" %
    580                 (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
@tibacher
Copy link

tibacher commented Nov 8, 2022

got same error. In Azure Databricks Env:
tensorflow==2.5.0
keras==2.4.3 SAME WITH keras==2.5.0rc0
numpy
scipy
Pillow
cython
matplotlib
scikit-image
opencv-python-headless
h5py
imgaug
pycocotools

I tried some fixes suggested here: matterport/Mask_RCNN#2458
but apperently they did not help or brought up new errors.

TypeError: Could not build a TypeSpec for <KerasTensor: shape=(None, None, 4) dtype=float32 (created by layer 'tf.math.truediv')> with type KerasTensor
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<command-3129758012267412> in <module>
      1 # Create model in training mode
----> 2 model = modellib.MaskRCNN(mode="training", config=config,
      3                           model_dir=MODEL_DIR)

/Workspace/Repos/Mask_RCNN_tf_2.x/mrcnn/model.py in __init__(self, mode, config, model_dir)
   1845         self.model_dir = model_dir
   1846         self.set_log_dir()
-> 1847         self.keras_model = self.build(mode=mode, config=config)
   1848 
   1849     def build(self, mode, config):

/Workspace/Repos/Mask_RCNN_tf_2.x/mrcnn/model.py in build(self, mode, config)
   1883                 shape=[None, 4], name="input_gt_boxes", dtype=tf.float32)
   1884             # Normalize coordinates
-> 1885             gt_boxes = KL.Lambda(lambda x: norm_boxes_graph(
   1886                 x, K.shape(input_image)[1:3]))(input_gt_boxes)
   1887             # 3. GT Masks (zero padded)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants