sreagm
sreagm

Reputation: 378

Convert models( ?weights ) downloaded using applications module to tflite

I am trying to convert mobilenet model downloaded using applications module in tf.keras to tensorflow lite format. TensorFlow version I am using is 1.31. I don't know whether model is actually stored weights only or weights+architecture+optimizer_state. When I tried the conversion command :

from tensorflow import lite

lite.TFLiteConverter.from_keras_model_file( '/path/to/mobilenet_1_0_224_tf.h5' )

It resulted in this error :

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/lite/python/lite.py", line 370, in from_keras_model_file
    keras_model = _keras.models.load_model(model_file)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/engine/saving.py", line 232, in load_model
    raise ValueError('No model found in config file.')
ValueError: No model found in config file.

From this, I assumed that model will be weights only. So, I tried to load the model as one do, using the applications module and tried to save the model using model.save(). But this resulted in the following error.

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 300, in __init__
    fetch, allow_tensor=True, allow_operation=True))
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3478, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3557, in _as_graph_element_locked
    raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("conv1/kernel/Read/ReadVariableOp:0", shape=(3, 3, 3, 32), dtype=float32) is not an element of this graph.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/engine/network.py", line 1334, in save
    save_model(self, filepath, overwrite, include_optimizer)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/engine/saving.py", line 111, in save_model
    save_weights_to_hdf5_group(model_weights_group, model_layers)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/engine/saving.py", line 742, in save_weights_to_hdf5_group
    weight_values = K.batch_get_value(symbolic_weights)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/backend.py", line 2819, in batch_get_value
    return get_session().run(tensors)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 929, in run
    run_metadata_ptr)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1137, in _run
    self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 471, in __init__
    self._fetch_mapper = _FetchMapper.for_fetch(fetches)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 261, in for_fetch
    return _ListFetchMapper(fetch)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 370, in __init__
    self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 370, in <listcomp>
    self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 271, in for_fetch
    return _ElementFetchMapper(fetches, contraction_fn)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 307, in __init__
    'Tensor. (%s)' % (fetch, str(e)))
ValueError: Fetch argument <tf.Variable 'conv1/kernel:0' shape=(3, 3, 3, 32) dtype=float32> cannot be interpreted as a Tensor. (Tensor Tensor("conv1/kernel/Read/ReadVariableOp:0", shape=(3, 3, 3, 32), dtype=float32) is not an element of this graph.)

Does anyone know what is the real problem here? TIA

Upvotes: 0

Views: 756

Answers (1)

geekzeus
geekzeus

Reputation: 895

How did you saved your model,maybe you have saved only weights not model and you are trying to call load model which is not present.

If this is not the problem try to clear session.

from keras.backend import clear_session
clear_session()

I convert the model in this way

converter = tf.lite.TFLiteConverter.from_keras_model_file('model name')
tflite_model = converter.convert()
open("converted/model.tflite", "wb").write(tflite_model)

Upvotes: 0

Related Questions