apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.46k stars 648 forks source link

AttributeError: 'Sequential' object has no attribute '_get_save_spec'. Did you mean: '_set_save_spec'? #2279

Closed Ostralyan closed 4 months ago

Ostralyan commented 4 months ago

🐞Describing the bug

Unable to convert tensorflow model to coreml model.

Stack Trace

Traceback (most recent call last):
  File "/Users/ostralyan/Code/mlgesture-api/ml/computeModel.py", line 72, in <module>
    mlmodel = coremltools.convert(model, convert_to="mlprogram", source="tensorflow")
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ostralyan/Code/mlgesture-api/ml/venv/lib/python3.12/site-packages/coremltools/converters/_converters_entry.py", line 581, in convert
    mlmodel = mil_convert(
              ^^^^^^^^^^^^
  File "/Users/ostralyan/Code/mlgesture-api/ml/venv/lib/python3.12/site-packages/coremltools/converters/mil/converter.py", line 188, in mil_convert
    return _mil_convert(model, convert_from, convert_to, ConverterRegistry, MLModel, compute_units, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ostralyan/Code/mlgesture-api/ml/venv/lib/python3.12/site-packages/coremltools/converters/mil/converter.py", line 212, in _mil_convert
    proto, mil_program = mil_convert_to_proto(
                         ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ostralyan/Code/mlgesture-api/ml/venv/lib/python3.12/site-packages/coremltools/converters/mil/converter.py", line 288, in mil_convert_to_proto
    prog = frontend_converter(model, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ostralyan/Code/mlgesture-api/ml/venv/lib/python3.12/site-packages/coremltools/converters/mil/converter.py", line 98, in __call__
    return tf2_loader.load()
           ^^^^^^^^^^^^^^^^^
  File "/Users/ostralyan/Code/mlgesture-api/ml/venv/lib/python3.12/site-packages/coremltools/converters/mil/frontend/tensorflow/load.py", line 61, in load
    self._graph_def = self._graph_def_from_model(output_names)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ostralyan/Code/mlgesture-api/ml/venv/lib/python3.12/site-packages/coremltools/converters/mil/frontend/tensorflow2/load.py", line 132, in _graph_def_from_model
    cfs, graph_def = self._get_concrete_functions_and_graph_def()
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ostralyan/Code/mlgesture-api/ml/venv/lib/python3.12/site-packages/coremltools/converters/mil/frontend/tensorflow2/load.py", line 103, in _get_concrete_functions_and_graph_def
    cfs = self._concrete_fn_from_tf_keras(self.model)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ostralyan/Code/mlgesture-api/ml/venv/lib/python3.12/site-packages/coremltools/converters/mil/frontend/tensorflow2/load.py", line 315, in _concrete_fn_from_tf_keras
    input_signature = _saving_utils.model_input_signature(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ostralyan/Code/mlgesture-api/ml/venv/lib/python3.12/site-packages/tensorflow/python/keras/saving/saving_utils.py", line 74, in model_input_signature
    input_specs = model._get_save_spec(dynamic_batch=not keep_original_batch_size)  # pylint: disable=protected-access
                  ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Sequential' object has no attribute '_get_save_spec'. Did you mean: '_set_save_spec'?

To Reproduce

Compile the model

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

Train the model

model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test, y_test))

Evaluate the model

loss, accuracy = model.evaluate(X_test, y_test) print(f'Accuracy on test data: {accuracy}')

model.save("my_model.mlpackage")

mlmodel = coremltools.convert(model, convert_to="mlprogram", source="tensorflow")


- If the model conversion succeeds, but there is a numerical mismatch in predictions, please include the code used for comparisons.

## System environment (please complete the following information):
 - tensorflow==2.17.0
 - keras==3.4.1
 - coremltools==7.2

## Additional context
- I've tried using .h5, .pb, the folder and none of them work.
TobyRoseman commented 4 months ago

The max version of TensorFlow 2 that we support is 2.12.0. You should have received a warning about this when importing coremltools. Please try with that version of TensorFlow.

Ostralyan commented 4 months ago

Ahhh yes I see it now, the stack trace just pushed it back that I missed it.

That solved it. Thank you!

hzy1992 commented 3 weeks ago

Ahhh yes I see it now, the stack trace just pushed it back that I missed it.

That solved it. Thank you!

I cannot install 2.12.0 tensorflow, the minimal version is 2.16.0. How do you install?

hzy1992 commented 3 weeks ago

The max version of TensorFlow 2 that we support is 2.12.0. You should have received a warning about this when importing coremltools. Please try with that version of TensorFlow. I canno install 2.12.0 tensorflow: ERROR: Could not find a version that satisfies the requirement tensorflow==2.12.0 (from versions: 2.16.0rc0, 2.16.1, 2.16.2, 2.17.0rc0, 2.17.0rc1, 2.17.0, 2.17.1, 2.18.0rc0, 2.18.0rc1, 2.18.0rc2, 2.18.0)