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

Error when running a Sample notebook #2

Open
satishlokkoju opened this issue Jun 27, 2024 · 0 comments
Open

Error when running a Sample notebook #2

satishlokkoju opened this issue Jun 27, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@satishlokkoju
Copy link

satishlokkoju commented Jun 27, 2024

Bug Report

When I run the sample notebook in the conda environment, I get this error

AttributeError: 'KerasTensor' object has no attribute 'type_spec'

I think the error is due to use of type_spec here

One sentence summary of PR:
Unable to run sample notebook

Checklist

( Replace '[ ]' with '[x]' if completed. )

- [x ] I've checked [the issues list](https://github.com/apple/dnikit/issues) to make sure a bug for this is not already filed.
- [x] I've updated to the latest version of DNIKit.
- [x ] I've searched the [documentation](https://apple.github.io/dnikit/index.html), if appropriate, and found no answer.

Description

Expected Behavior

What were you trying to do and what did you expect to happen?
The notebook should run as expected.

Current Behavior

Please describe what happened and include any failure information you think would be useful (stacktrace, logs, etc.).


WARNING:absl:You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`. 
INFO:dnikit_tensorflow.TF2:Instantiating TF2 Model
INFO:dnikit_tensorflow.TF2:GPUs Available: 1
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[18], line 2
      1 from dnikit_tensorflow import TFModelWrapper
----> 2 dni_model =TFModelWrapper.from_keras(mobilenet,tf.keras.applications.mobilenet.preprocess_input)

File ~/anaconda3/envs/python3/lib/python3.10/site-packages/dnikit_tensorflow/_sample_models.py:80, in TFModelWrapper.from_keras(cls, model, preprocessing)
     62 @classmethod
     63 def from_keras(cls,
     64                model: tf.keras.Model,
     65                preprocessing: t.Callable[[np.ndarray], np.ndarray]) -> 'TFModelWrapper':
     66     """
     67     Convenience method for loading as :class:`TFModelWrapper`
     68     from Keras models and preprocessors.
   (...)
     77         preprocessing: keras preprocessing function to transform data
     78     """
     79     return TFModelWrapper(
---> 80         model=cls.load_keras_model(model),
     81         preprocessing=Processor(preprocessing)
     82     )

File ~/anaconda3/envs/python3/lib/python3.10/site-packages/dnikit_tensorflow/_sample_models.py:95, in TFModelWrapper.load_keras_model(model)
     93     model_path = os.path.join(temp_dir, 'model.h5')
     94     model.save(model_path)
---> 95     dni_model = load_tf_model_from_path(model_path)
     96 return dni_model

File ~/anaconda3/envs/python3/lib/python3.10/site-packages/dnikit_tensorflow/_tensorflow/_tensorflow_loading.py:104, in load_tf_model_from_path(path)
     74 def load_tf_model_from_path(path: dt.PathOrStr) -> Model:
     75     """
     76     Initialize a TensorFlow :class:`Model <dnikit.base.Model>` from a model serialized in ``path``
     77 
   (...)
    102         A DNIKit TensorFlow :class:`Model <dnikit.base.Model>`.
    103     """
--> 104     return tf_path_load(path)

File ~/anaconda3/envs/python3/lib/python3.10/site-packages/dnikit_tensorflow/_tensorflow/_tf2_loading.py:74, in load_tf_2_model_from_path(path)
     71 tf.keras.backend.clear_session()
     73 loader = TF2LoadingChain.get_loader(path)
---> 74 return Model(loader.load(pathname=path))

File <string>:4, in __init__(self, _details)

File ~/anaconda3/envs/python3/lib/python3.10/site-packages/dnikit/base/_model.py:199, in Model.__post_init__(self)
    195 """
    196 Initialize common elements for all dnikit ``Model``.
    197 """
    198 super().__init__()
--> 199 response_infos = {r.name: r for r in self._details.get_response_infos()}
    200 input_layers = {r.name: r for r in self._details.get_input_layer_responses()}
    201 object.__setattr__(self, "_response_infos", response_infos)

File ~/anaconda3/envs/python3/lib/python3.10/site-packages/dnikit/base/_model.py:199, in <dictcomp>(.0)
    195 """
    196 Initialize common elements for all dnikit ``Model``.
    197 """
    198 super().__init__()
--> 199 response_infos = {r.name: r for r in self._details.get_response_infos()}
    200 input_layers = {r.name: r for r in self._details.get_input_layer_responses()}
    201 object.__setattr__(self, "_response_infos", response_infos)

File ~/anaconda3/envs/python3/lib/python3.10/site-packages/dnikit_tensorflow/_tensorflow/_tf2_model.py:103, in _Tensorflow2ModelDetails.get_response_infos(self)
     98 def get_response_infos(self) -> t.Iterable[ResponseInfo]:
     99     # now go through all layers (including input) and return output
    100     for layer in self.model.layers:
    101         yield ResponseInfo(
    102             name=layer.name,
--> 103             dtype=_convert_tf_dtype(layer.output.type_spec.dtype),
    104             shape=_convert_tf_shape(layer.output.type_spec.shape),
    105             layer=ResponseInfo.Layer(
    106                 name=_remove_op_number(layer.output.name),
    107                 kind=_convert_tf_operation(layer.output.name),
    108                 typename=_extract_kind(layer.output.name)
    109             )
    110         )

AttributeError: 'KerasTensor' object has no attribute 'type_spec'

Possible Solution, if applicable

This would be the place for related issues, suggestions, links for context, etc.

Environment Setup

  • Python Version:
  • My env
Python implementation: CPython
Python version       : 3.10.14
IPython version      : 8.22.2

numpy     : 1.26.4
pandas    : 2.2.1
tensorflow: 2.16.1
keras     : 3.4.1
  • MacOS or Linux: Linux
  • For general users - Version of DNIKit: 2.0.0
  • For developers - Branch of DNIKit / commit ID (if not latest commit): Latest
  • DNIKit Virtualenv? (Yes/No): conda env
  • Additional dependencies (attach your pip freeze):
@satishlokkoju satishlokkoju added the bug Something isn't working label Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant