keras-team / keras-cv

Industry-strength Computer Vision workflows with Keras
Other
1k stars 332 forks source link

[SOLVED] YOLOV8Backbone Error : Cannot interpret '<KerasTensor: shape=(None, None, None, 32) dtype=float32 (created by layer 'tf.split')>' as a data type #1926

Closed shreypareek1991 closed 1 year ago

shreypareek1991 commented 1 year ago

I am trying to create a model using the YOLOV8BackBone but getting a strange error. This error was not present in the past and has appeared after updating to keras-cv 0.5.1. I tried downgrading to 0.5.0 but that is breaking other things.

The code works with a ResNet BackBone.

Code

model = keras_cv.models.YOLOV8Backbone.from_preset(
    "yolo_v8_xs_backbone_coco"

got a similar error with

model = keras_cv.models.YOLOV8Backbone(
    stackwise_channels=[128, 256, 512, 1024],
    stackwise_depth=[3, 9, 9, 3],
    include_rescaling=False,
)

Error

TypeError                                 Traceback (most recent call last)
Cell In[12], line 1
----> 1 model = keras_cv.models.YOLOV8Backbone.from_preset(
      2     "yolo_v8_xs_backbone_coco"
      3 )

File ~/mambaforge/envs/cv_pipeline/lib/python3.10/site-packages/keras_cv/models/backbones/backbone.py:143, in Backbone.__init_subclass__.<locals>.from_preset(calling_cls, *args, **kwargs)
    142 def from_preset(calling_cls, *args, **kwargs):
--> 143     return super(cls, calling_cls).from_preset(*args, **kwargs)

File ~/mambaforge/envs/cv_pipeline/lib/python3.10/site-packages/keras_cv/models/backbones/backbone.py:119, in Backbone.from_preset(cls, preset, load_weights, **kwargs)
    117 metadata = cls.presets[preset]
    118 config = metadata["config"]
--> 119 model = cls.from_config({**config, **kwargs})
    121 if preset not in cls.presets_with_weights or load_weights is False:
    122     return model

File ~/mambaforge/envs/cv_pipeline/lib/python3.10/site-packages/keras_cv/models/backbones/backbone.py:47, in Backbone.from_config(cls, config)
     43 @classmethod
     44 def from_config(cls, config):
     45     # The default `from_config()` for functional models will return a
     46     # vanilla `keras.Model`. We override it to get a subclass instance back.
---> 47     return cls(**config)

File ~/mambaforge/envs/cv_pipeline/lib/python3.10/site-packages/keras_cv/models/object_detection/yolo_v8/yolo_v8_backbone.py:168, in YOLOV8Backbone.__init__(self, stackwise_channels, stackwise_depth, include_rescaling, activation, input_shape, input_tensor, **kwargs)
    159 if stack_id >= 1:
    160     x = apply_conv_bn(
    161         x,
    162         channel,
   (...)
    166         name=f"{stack_name}_downsample",
    167     )
--> 168 x = apply_csp_block(
    169     x,
    170     depth=depth,
    171     expansion=0.5,
    172     activation=activation,
    173     name=f"{stack_name}_c2f",
    174 )
    176 if stack_id == len(stackwise_depth) - 1:
    177     x = apply_spatial_pyramid_pooling_fast(
    178         x,
    179         pool_size=5,
    180         activation=activation,
    181         name=f"{stack_name}_spp_fast",
    182     )

File ~/mambaforge/envs/cv_pipeline/lib/python3.10/site-packages/keras_cv/models/object_detection/yolo_v8/yolo_v8_layers.py:93, in apply_csp_block(inputs, channels, depth, shortcut, expansion, activation, name)
     79     deep = apply_conv_bn(
     80         deep,
     81         hidden_channels,
   (...)
     84         name=f"{name}_pre_{id}_1",
     85     )
     86     deep = apply_conv_bn(
     87         deep,
     88         hidden_channels,
   (...)
     91         name=f"{name}_pre_{id}_2",
     92     )
---> 93     deep = (out[-1] + deep) if shortcut else deep
     94     out.append(deep)
     95 out = tf.concat(out, axis=channel_axis)

File ~/mambaforge/envs/cv_pipeline/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    151 except Exception as e:
    152   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153   raise e.with_traceback(filtered_tb) from None
    154 finally:
    155   del filtered_tb

File <__array_function__ internals>:180, in result_type(*args, **kwargs)

TypeError: Cannot interpret '<KerasTensor: shape=(None, None, None, 16) dtype=float32 (created by layer 'tf.split_1')>' as a data type

Versions

M1 Mac

keras                     2.13.1                   pypi_0    pypi
keras-cv                  0.5.1                    pypi_0    pypi
tensorflow-deps           2.9.0                         0    apple
tensorflow-estimator      2.13.0                   pypi_0    pypi
tensorflow-macos          2.13.0                   pypi_0    pypi
tensorflow-metadata       1.13.1                   pypi_0    pypi
tensorflow-metal          1.0.1                    pypi_0    pypi
shreypareek1991 commented 1 year ago

The issue was a tensor flow config enabling dumpy behavior. Removing that from the script seems to have worked.

from tensorflow.python.ops.numpy_ops import np_config
np_config.enable_numpy_behavior()