I always fail when trying to save the model as a tflite file


AttributeError Traceback (most recent call last)
Cell In[20], line 3
1 # Convert the model to TensorFlow Lite format
2 converter = tf.lite.TFLiteConverter.from_keras_model(model)
----> 3 tflite_model = converter.convert()
5 # Save the TensorFlow Lite model to a file
6 with open(‘model.tflite’, ‘wb’) as f:

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\lite\python\lite.py:1175, in _export_metrics..wrapper(self, *args, **kwargs)
1172 @functools.wraps(convert_func)
1173 def wrapper(self, *args, **kwargs):
1174 # pylint: disable=protected-access
→ 1175 return self._convert_and_export_metrics(convert_func, *args, **kwargs)

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\lite\python\lite.py:1129, in TFLiteConverterBase._convert_and_export_metrics(self, convert_func, *args, **kwargs)
1127 self._save_conversion_params_metric()
1128 start_time = time.process_time()
→ 1129 result = convert_func(self, *args, **kwargs)
1130 elapsed_time_ms = (time.process_time() - start_time) * 1000
1131 if result:

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\lite\python\lite.py:1641, in TFLiteKerasModelConverterV2.convert(self)
1637 if saved_model_convert_result:
1638 return saved_model_convert_result
1640 graph_def, input_tensors, output_tensors, frozen_func = (
→ 1641 self._freeze_keras_model()
1642 )
1644 graph_def = self._optimize_tf_model(
1645 graph_def, input_tensors, output_tensors, frozen_func
1646 )
1648 return super(TFLiteKerasModelConverterV2, self).convert(
1649 graph_def, input_tensors, output_tensors
1650 )

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\lite\python\convert_phase.py:215, in convert_phase..actual_decorator..wrapper(*args, **kwargs)
213 except Exception as error:
214 report_error_message(str(error))
→ 215 raise error from None

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\lite\python\convert_phase.py:205, in convert_phase..actual_decorator..wrapper(*args, **kwargs)
202 @functools.wraps(func)
203 def wrapper(*args, **kwargs):
204 try:
→ 205 return func(*args, **kwargs)
206 except ConverterError as converter_error:
207 if converter_error.errors:

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\lite\python\lite.py:1582, in TFLiteKerasModelConverterV2._freeze_keras_model(self)
1573 # If the model’s call is not a tf.function, then we need to first get its
1574 # input signature from model_input_signature method. We can’t directly
1575 # call trace_model_call because otherwise the batch dimension is set
1576 # to None.
1577 # Once we have better support for dynamic shapes, we can remove this.
1578 if not isinstance(self._keras_model.call, _def_function.Function):
1579 # Pass keep_original_batch_size=True will ensure that we get an input
1580 # signature including the batch dimension specified by the user.
1581 # TODO(b/169898786): Use the Keras public API when TFLite moves out of TF
→ 1582 input_signature = _model_input_signature(
1583 self._keras_model, keep_original_batch_size=True
1584 )
1586 # TODO(b/169898786): Use the Keras public API when TFLite moves out of TF
1587 func = _trace_model_call(self._keras_model, input_signature)

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\lite\python\tflite_keras_util.py:84, in model_input_signature(model, keep_original_batch_size)
82 input_specs = input_specs[0][0]
83 else:
—> 84 input_specs = model._get_save_spec( # pylint: disable=protected-access
85 dynamic_batch=not keep_original_batch_size)
86 if input_specs is None:
87 return None

AttributeError: ‘Functional’ object has no attribute ‘_get_save_spec’

Hi @Luka_Luka, I have tried to convert both the sequential and functional models to lite using tf.lite.TFLiteConverter.from_keras_model with Tensorflow 2.15 and did not face any error. please refer to gist this for a working code example. If possible could you please share the model you are trying to convert to debug the cause of the error. Thank You.