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

**URL:** <https://discuss.ai.google.dev/t/i-always-fail-when-trying-to-save-the-model-as-a-tflite-file/30837>\
**Category:** General Discussion\
**Tags:** python, tflite\
**Created:** [April 18, 2024, 3:45am UTC](https://discuss.ai.google.dev/t/i-always-fail-when-trying-to-save-the-model-as-a-tflite-file/30837 "2024-04-18T03:45:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luka\_Luka](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/luka_luka/32/16849_2.png) [@Luka\_Luka](https://discuss.ai.google.dev/u/Luka_Luka)\
**Post date:** [April 18, 2024, 3:45am UTC](https://discuss.ai.google.dev/t/i-always-fail-when-trying-to-save-the-model-as-a-tflite-file/30837/1 "2024-04-18T03:45:46Z")

</div>

* * *

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’

---

<div class="post-metadata">

**Author:** ![Kiran\_Sai\_Ramineni](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/kiran_sai_ramineni/32/8859_2.png) [@Kiran\_Sai\_Ramineni](https://discuss.ai.google.dev/u/Kiran_Sai_Ramineni)\
**Post date:** [April 24, 2024, 8:23am UTC](https://discuss.ai.google.dev/t/i-always-fail-when-trying-to-save-the-model-as-a-tflite-file/30837/3 "2024-04-24T08:23:25Z")

</div>

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](https://gist.github.com/kiransair/ca718d53b64f97f81eb504676568a222) 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.
