Search code examples
python-3.xtensorflowgoogle-colaboratorytensorflow2.0

Tensorflow 2.8 error save model - UnboundLocalError with "logging_verbosity"


I'm working on building and testing a model found here to update it to Tensorflow 2, but after attempting to save the model with tf.keras.Model.save(teacher,"saved_model/tea_model"); and received the following error on Google Colab Pro

<ipython-input-9-b823fb312e0f> in main()
--> 513     tf.keras.Model.save(teacher, "saved_model/tea_model");

/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

/usr/local/lib/python3.7/dist-packages/tensorflow/python/training/tracking/autotrackable.py in _list_functions_for_serialization(self, unused_serialization_cache)
     99           functions[attribute_name] = attribute_value
    100     finally:
--> 101       logging.set_verbosity(logging_verbosity)
    102 
    103     return functions

UnboundLocalError: local variable 'logging_verbosity' referenced before assignment

I do have tf.compat.v1.disable_eager_execution() as it was initially written in v1.14.0 and need to maintain the compatibility, but after attempting the saving methods for both versions of Tensorflow I get the same error.

I can save the weights on the model, but would prefer the SavedModel format.


Solution

  • My solution was to simply define verbosity in the flags I was using as such:

    from tensorflow.python.platform import flags
    ...
    flags.DEFINE_integer('verbosity', 0, 'Verbosity of the error messages')