C1W2_Ungraded_Lab Can not load the savedModel with new TF version applying Keras3

Is there a way to load the pre_trained model in savedModel format with latest TF installed on Colab applying Keras3? For now the load operation would hit a error like this

Keras 3 only supports V3 .keras files and legacy H5 format files (.h5 extension). Note that the legacy SavedModel format is not supported by load_model() in Keras 3.

I have tried to convert it to .keras and h5 with my script but neither work

#!/bin/bash

python -m venv tf2_env
source tf2_env/bin/activate

pip install tensorflow==2.10

python <<EOF
import tensorflow as tf

OLD_MODEL_PATHs = [ā€˜/content/model-balanced-old/ā€™,
ā€˜/content/model-imbalanced-old/ā€™,
ā€˜/content/model-augmented-old/ā€™
]

NEW_MODEL_PATHs = [ā€˜/content/model-balanced.kerasā€™,
ā€˜/content/model-imbalanced.kerasā€™,
ā€˜/content/model-augmented.kerasā€™
]

for old_path, new_path in zip(OLD_MODEL_PATHs, NEW_MODEL_PATHs):
model = tf.keras.models.load_model(old_path)
model.save(new_path)
EOF

deactivate

rm -rf tf2_env
echo ā€œModel has been converted and savedā€

Try using along with tf_keras

#!/bin/bash

python -m venv tf2_env
source tf2_env/bin/activate

pip install tensorflow tf_keras

python <<EOF
import os
os.environ['TF_USE_LEGACY_KERAS']='1'
import tensorflow as tf

OLD_MODEL_PATHs = ['models/imbalanced_model', 'models/balanced_model', 'models/augmented_model']

NEW_MODEL_PATHs = ['models/imbalanced_model.keras', 'models/balanced_model.keras', 'models/augmented_model.keras']

for old_path, new_path in zip(OLD_MODEL_PATHs, NEW_MODEL_PATHs):
  model = tf.keras.models.load_model(old_path)
  model.save(new_path)
EOF

deactivate

rm -rf tf2_env
echo "Model has been converted and saved"

The generated file still cannot be recognized even have been seen in the folder

ValueError: File not found: filepath=/content/model-imbalanced.keras. Please ensure the file is an accessible .keras zip file.

In which environment are you doing things? Here are my steps done locally:

Packages:

$ pip list | grep -E "tensorflow|tf"
tensorflow                   2.18.0
tensorflow-estimator         2.9.0
tensorflow-io-gcs-filesystem 0.37.1
tf_keras                     2.18.0

Download files:

wget -q -P models_downloaded/model-balanced/ https://storage.googleapis.com/mlep-public/course_1/week2/model-balanced/saved_model.pb
wget -q -P models_downloaded/model-balanced/variables/ https://storage.googleapis.com/mlep-public/course_1/week2/model-balanced/variables/variables.data-00000-of-00001
wget -q -P models_downloaded/model-balanced/variables/ https://storage.googleapis.com/mlep-public/course_1/week2/model-balanced/variables/variables.index
wget -q -P models_downloaded/history-balanced/ https://storage.googleapis.com/mlep-public/course_1/week2/history-balanced/history-balanced.csv

wget -q -P models_downloaded/model-imbalanced/ https://storage.googleapis.com/mlep-public/course_1/week2/model-imbalanced/saved_model.pb
wget -q -P models_downloaded/model-imbalanced/variables/ https://storage.googleapis.com/mlep-public/course_1/week2/model-imbalanced/variables/variables.data-00000-of-00001
wget -q -P models_downloaded/model-imbalanced/variables/ https://storage.googleapis.com/mlep-public/course_1/week2/model-imbalanced/variables/variables.index
wget -q -P models_downloaded/history-imbalanced/ https://storage.googleapis.com/mlep-public/course_1/week2/history-imbalanced/history-imbalanced.csv

wget -q -P models_downloaded/model-augmented/ https://storage.googleapis.com/mlep-public/course_1/week2/model-augmented/saved_model.pb
wget -q -P models_downloaded/model-augmented/variables/ https://storage.googleapis.com/mlep-public/course_1/week2/model-augmented/variables/variables.data-00000-of-00001
wget -q -P models_downloaded/model-augmented/variables/ https://storage.googleapis.com/mlep-public/course_1/week2/model-augmented/variables/variables.index
wget -q -P models_downloaded/history-augmented/ https://storage.googleapis.com/mlep-public/course_1/week2/history-augmented/history-augmented.csv

Original files were downloaded into this folder:

$ ls -1 models_downloaded/
history-augmented
history-balanced
history-imbalanced
model-augmented
model-balanced
model-imbalanced

Running model conversion:

>>> import os
>>> os.environ['TF_USE_LEGACY_KERAS']='1'
>>> import tensorflow as tf
>>> OLD_MODEL_PATHs = ['models_downloaded/model-imbalanced', 'models_downloaded/model-balanced', 'models_downloaded/model-augmented']
>>> 
>>> NEW_MODEL_PATHs = ['models_downloaded/model-imbalanced.keras', 'models_downloaded/model-balanced.keras', 'models_downloaded/model-augmented.keras']
>>> 
>>> for old_path, new_path in zip(OLD_MODEL_PATHs, NEW_MODEL_PATHs):
...   model = tf.keras.models.load_model(old_path)
...   model.save(new_path)
... 
WARNING:tensorflow:SavedModel saved prior to TF 2.5 detected when loading Keras model. Please ensure that you are saving the model with model.save() or tf.keras.models.save_model(), *NOT* tf.saved_model.save(). To confirm, there should be a file named "keras_metadata.pb" in the SavedModel directory.
W0000 00:00:1729934635.417644   70265 gpu_device.cc:2344] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
WARNING:tensorflow:SavedModel saved prior to TF 2.5 detected when loading Keras model. Please ensure that you are saving the model with model.save() or tf.keras.models.save_model(), *NOT* tf.saved_model.save(). To confirm, there should be a file named "keras_metadata.pb" in the SavedModel directory.
WARNING:tensorflow:SavedModel saved prior to TF 2.5 detected when loading Keras model. Please ensure that you are saving the model with model.save() or tf.keras.models.save_model(), *NOT* tf.saved_model.save(). To confirm, there should be a file named "keras_metadata.pb" in the SavedModel directory.
WARNING:tensorflow:SavedModel saved prior to TF 2.5 detected when loading Keras model. Please ensure that you are saving the model with model.save() or tf.keras.models.save_model(), *NOT* tf.saved_model.save(). To confirm, there should be a file named "keras_metadata.pb" in the SavedModel directory.
WARNING:tensorflow:SavedModel saved prior to TF 2.5 detected when loading Keras model. Please ensure that you are saving the model with model.save() or tf.keras.models.save_model(), *NOT* tf.saved_model.save(). To confirm, there should be a file named "keras_metadata.pb" in the SavedModel directory.
>>> 

Check new files:

$ ls -1  models_downloaded/*.keras
models_downloaded/model-augmented.keras
models_downloaded/model-balanced.keras
models_downloaded/model-imbalanced.keras

I ran my script on Google Colab, I think itā€™s cannot be recognized since I changed the 2.10 tf version to generate the .keras files.

For now I just changed your solution and deleted the converting logic and just keep this parameter shift the model can be loaded.

Please explain what this means:

I comment this block and execute, so only

os.environ[ā€˜TF_USE_LEGACY_KERASā€™]=ā€˜1ā€™

ran and the old model can be loaded.

I just tried your method and found the generated .keras file cannot be loaded properly

ValueError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py in from_config(cls, config)
233 try:
ā†’ 234 return cls(**config)
235 except Exception as e:

13 frames
/usr/local/lib/python3.10/dist-packages/keras/src/layers/convolutional/conv2d.py in init(self, filters, kernel_size, strides, padding, data_format, dilation_rate, groups, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs)
108 ):
ā†’ 109 super().init(
110 rank=2,

/usr/local/lib/python3.10/dist-packages/keras/src/layers/convolutional/base_conv.py in init(self, rank, filters, kernel_size, strides, padding, data_format, dilation_rate, groups, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, lora_rank, **kwargs)
106 ):
ā†’ 107 super().init(activity_regularizer=activity_regularizer, **kwargs)
108 self.rank = rank

/usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py in init(self, activity_regularizer, trainable, dtype, autocast, name, **kwargs)
265 if kwargs:
ā†’ 266 raise ValueError(
267 "Unrecognized keyword arguments "

ValueError: Unrecognized keyword arguments passed to Conv2D: {ā€˜batch_input_shapeā€™: [None, 150, 150, 3]}

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/keras/src/saving/serialization_lib.py in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
717 try:
ā†’ 718 instance = cls.from_config(inner_config)
719 except TypeError as e:

/usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py in from_config(cls, config)
235 except Exception as e:
ā†’ 236 raise TypeError(
237 f"Error when deserializing class ā€˜{cls.name}ā€™ using "

TypeError: Error when deserializing class ā€˜Conv2Dā€™ using config={ā€˜nameā€™: ā€˜conv2d_20ā€™, ā€˜trainableā€™: True, ā€˜dtypeā€™: ā€˜float32ā€™, ā€˜batch_input_shapeā€™: [None, 150, 150, 3], ā€˜filtersā€™: 32, ā€˜kernel_sizeā€™: [3, 3], ā€˜stridesā€™: [1, 1], ā€˜paddingā€™: ā€˜validā€™, ā€˜data_formatā€™: ā€˜channels_lastā€™, ā€˜dilation_rateā€™: [1, 1], ā€˜groupsā€™: 1, ā€˜activationā€™: ā€˜reluā€™, ā€˜use_biasā€™: True, ā€˜kernel_initializerā€™: {ā€˜moduleā€™: ā€˜keras.initializersā€™, ā€˜class_nameā€™: ā€˜GlorotUniformā€™, ā€˜configā€™: {ā€˜seedā€™: None}, ā€˜registered_nameā€™: None}, ā€˜bias_initializerā€™: {ā€˜moduleā€™: ā€˜keras.initializersā€™, ā€˜class_nameā€™: ā€˜Zerosā€™, ā€˜configā€™: {}, ā€˜registered_nameā€™: None}, ā€˜kernel_regularizerā€™: None, ā€˜bias_regularizerā€™: None, ā€˜activity_regularizerā€™: None, ā€˜kernel_constraintā€™: None, ā€˜bias_constraintā€™: None}.

Exception encountered: Unrecognized keyword arguments passed to Conv2D: {ā€˜batch_input_shapeā€™: [None, 150, 150, 3]}

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/keras/src/saving/serialization_lib.py in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
717 try:
ā†’ 718 instance = cls.from_config(inner_config)
719 except TypeError as e:

/usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py in from_config(cls, config, custom_objects)
346 else:
ā†’ 347 layer = serialization_lib.deserialize_keras_object(
348 layer_config,

/usr/local/lib/python3.10/dist-packages/keras/src/saving/serialization_lib.py in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
719 except TypeError as e:
ā†’ 720 raise TypeError(
721 f"{cls} could not be deserialized properly. Please"

TypeError: <class ā€˜keras.src.layers.convolutional.conv2d.Conv2Dā€™> could not be deserialized properly. Please ensure that components that are Python object instances (layers, models, etc.) returned by get_config() are explicitly deserialized in the modelā€™s from_config() method.

config={ā€˜moduleā€™: ā€˜keras.layersā€™, ā€˜class_nameā€™: ā€˜Conv2Dā€™, ā€˜configā€™: {ā€˜nameā€™: ā€˜conv2d_20ā€™, ā€˜trainableā€™: True, ā€˜dtypeā€™: ā€˜float32ā€™, ā€˜batch_input_shapeā€™: [None, 150, 150, 3], ā€˜filtersā€™: 32, ā€˜kernel_sizeā€™: [3, 3], ā€˜stridesā€™: [1, 1], ā€˜paddingā€™: ā€˜validā€™, ā€˜data_formatā€™: ā€˜channels_lastā€™, ā€˜dilation_rateā€™: [1, 1], ā€˜groupsā€™: 1, ā€˜activationā€™: ā€˜reluā€™, ā€˜use_biasā€™: True, ā€˜kernel_initializerā€™: {ā€˜moduleā€™: ā€˜keras.initializersā€™, ā€˜class_nameā€™: ā€˜GlorotUniformā€™, ā€˜configā€™: {ā€˜seedā€™: None}, ā€˜registered_nameā€™: None}, ā€˜bias_initializerā€™: {ā€˜moduleā€™: ā€˜keras.initializersā€™, ā€˜class_nameā€™: ā€˜Zerosā€™, ā€˜configā€™: {}, ā€˜registered_nameā€™: None}, ā€˜kernel_regularizerā€™: None, ā€˜bias_regularizerā€™: None, ā€˜activity_regularizerā€™: None, ā€˜kernel_constraintā€™: None, ā€˜bias_constraintā€™: None}, ā€˜registered_nameā€™: None, ā€˜build_configā€™: {ā€˜input_shapeā€™: [None, 150, 150, 3]}}.

Exception encountered: Error when deserializing class ā€˜Conv2Dā€™ using config={ā€˜nameā€™: ā€˜conv2d_20ā€™, ā€˜trainableā€™: True, ā€˜dtypeā€™: ā€˜float32ā€™, ā€˜batch_input_shapeā€™: [None, 150, 150, 3], ā€˜filtersā€™: 32, ā€˜kernel_sizeā€™: [3, 3], ā€˜stridesā€™: [1, 1], ā€˜paddingā€™: ā€˜validā€™, ā€˜data_formatā€™: ā€˜channels_lastā€™, ā€˜dilation_rateā€™: [1, 1], ā€˜groupsā€™: 1, ā€˜activationā€™: ā€˜reluā€™, ā€˜use_biasā€™: True, ā€˜kernel_initializerā€™: {ā€˜moduleā€™: ā€˜keras.initializersā€™, ā€˜class_nameā€™: ā€˜GlorotUniformā€™, ā€˜configā€™: {ā€˜seedā€™: None}, ā€˜registered_nameā€™: None}, ā€˜bias_initializerā€™: {ā€˜moduleā€™: ā€˜keras.initializersā€™, ā€˜class_nameā€™: ā€˜Zerosā€™, ā€˜configā€™: {}, ā€˜registered_nameā€™: None}, ā€˜kernel_regularizerā€™: None, ā€˜bias_regularizerā€™: None, ā€˜activity_regularizerā€™: None, ā€˜kernel_constraintā€™: None, ā€˜bias_constraintā€™: None}.

Exception encountered: Unrecognized keyword arguments passed to Conv2D: {ā€˜batch_input_shapeā€™: [None, 150, 150, 3]}

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
in <cell line: 4>()
2
3 imbalanced_history = pd.read_csv(ā€˜history-imbalanced/history-imbalanced.csvā€™)
----> 4 imbalanced_model = tf.keras.models.load_model(ā€˜model-imbalanced.kerasā€™)

/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_api.py in load_model(filepath, custom_objects, compile, safe_mode)
180
181 if is_keras_zip or is_keras_dir:
ā†’ 182 return saving_lib.load_model(
183 filepath,
184 custom_objects=custom_objects,

/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_lib.py in load_model(filepath, custom_objects, compile, safe_mode)
235 )
236 with open(filepath, ā€œrbā€) as f:
ā†’ 237 return _load_model_from_fileobj(
238 f, custom_objects, compile, safe_mode
239 )

/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_lib.py in _load_model_from_fileobj(fileobj, custom_objects, compile, safe_mode)
312 config_json = f.read()
313
ā†’ 314 model = _model_from_config(
315 config_json, custom_objects, compile, safe_mode
316 )

/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_lib.py in _model_from_config(config_json, custom_objects, compile, safe_mode)
301 # Construct the model from the configuration file in the archive.
302 with ObjectSharingScope():
ā†’ 303 model = deserialize_keras_object(
304 config_dict, custom_objects, safe_mode=safe_mode
305 )

/usr/local/lib/python3.10/dist-packages/keras/src/saving/serialization_lib.py in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
718 instance = cls.from_config(inner_config)
719 except TypeError as e:
ā†’ 720 raise TypeError(
721 f"{cls} could not be deserialized properly. Please"
722 " ensure that components that are Python object"

TypeError: <class ā€˜keras.src.models.sequential.Sequentialā€™> could not be deserialized properly. Please ensure that components that are Python object instances (layers, models, etc.) returned by get_config() are explicitly deserialized in the modelā€™s from_config() method.

config={ā€˜moduleā€™: ā€˜kerasā€™, ā€˜class_nameā€™: ā€˜Sequentialā€™, ā€˜configā€™: {ā€˜nameā€™: ā€˜sequential_5ā€™, ā€˜layersā€™: [{ā€˜moduleā€™: ā€˜keras.layersā€™, ā€˜class_nameā€™: ā€˜InputLayerā€™, ā€˜configā€™: {ā€˜batch_input_shapeā€™: [None, 150, 150, 3], ā€˜dtypeā€™: ā€˜float32ā€™, ā€˜sparseā€™: False, ā€˜raggedā€™: False, ā€˜nameā€™: ā€˜conv2d_20_inputā€™}, ā€˜registered_nameā€™: None}, {ā€˜moduleā€™: ā€˜keras.layersā€™, ā€˜class_nameā€™: ā€˜Conv2Dā€™, ā€˜configā€™: {ā€˜nameā€™: ā€˜conv2d_20ā€™, ā€˜trainableā€™: True, ā€˜dtypeā€™: ā€˜float32ā€™, ā€˜batch_input_shapeā€™: [None, 150, 150, 3], ā€˜filtersā€™: 32, ā€˜kernel_sizeā€™: [3, 3], ā€˜stridesā€™: [1, 1], ā€˜paddingā€™: ā€˜validā€™, ā€˜data_formatā€™: ā€˜channels_lastā€™, ā€˜dilation_rateā€™: [1, 1], ā€˜groupsā€™: 1, ā€˜activationā€™: ā€˜reluā€™, ā€˜use_biasā€™: True, ā€˜kernel_initializerā€™: {ā€˜moduleā€™: ā€˜keras.initializersā€™, ā€˜class_nameā€™: ā€˜GlorotUniformā€™, ā€˜configā€™: {ā€˜seedā€™: None}, ā€˜registered_nameā€™: None}, ā€˜bias_initializerā€™: {ā€˜moduleā€™: ā€˜keras.initializersā€™, ā€˜class_nameā€™: ā€˜Zerosā€™, ā€˜configā€™: {}, ā€˜registered_nameā€™: None}, ā€˜kernel_regularizerā€™: None, ā€˜bias_regularizerā€™: None, ā€˜activity_regularizerā€™: None, ā€˜kernel_constraintā€™: None, ā€˜bias_constraintā€™: None}, ā€˜registered_nameā€™: None, ā€˜build_configā€™: {ā€˜input_shapeā€™: [None, 150, 150, 3]}}, {ā€˜moduleā€™: ā€˜keras.layersā€™, ā€˜class_nameā€™: ā€˜MaxPooling2Dā€™, ā€˜configā€™: {ā€˜nameā€™: ā€˜max_pooling2d_20ā€™, ā€˜trainableā€™: True, ā€˜dtypeā€™: ā€˜float32ā€™, ā€˜pool_sizeā€™: [2, 2], ā€˜paddingā€™: ā€˜validā€™, ā€˜stridesā€™: [2, 2], ā€˜data_formatā€™: ā€˜channels_lastā€™}, ā€˜registered_nameā€™: None}, {ā€˜moduleā€™: ā€˜keras.layersā€™, ā€˜class_nameā€™: ā€˜Conv2Dā€™, ā€˜configā€™: {ā€˜nameā€™: ā€˜conv2d_21ā€™, ā€˜trainableā€™: True, 'dā€¦

Exception encountered: <class ā€˜keras.src.layers.convolutional.conv2d.Conv2Dā€™> could not be deserialized properly. Please ensure that components that are Python object instances (layers, models, etc.) returned by get_config() are explicitly deserialized in the modelā€™s from_config() method.

config={ā€˜moduleā€™: ā€˜keras.layersā€™, ā€˜class_nameā€™: ā€˜Conv2Dā€™, ā€˜configā€™: {ā€˜nameā€™: ā€˜conv2d_20ā€™, ā€˜trainableā€™: True, ā€˜dtypeā€™: ā€˜float32ā€™, ā€˜batch_input_shapeā€™: [None, 150, 150, 3], ā€˜filtersā€™: 32, ā€˜kernel_sizeā€™: [3, 3], ā€˜stridesā€™: [1, 1], ā€˜paddingā€™: ā€˜validā€™, ā€˜data_formatā€™: ā€˜channels_lastā€™, ā€˜dilation_rateā€™: [1, 1], ā€˜groupsā€™: 1, ā€˜activationā€™: ā€˜reluā€™, ā€˜use_biasā€™: True, ā€˜kernel_initializerā€™: {ā€˜moduleā€™: ā€˜keras.initializersā€™, ā€˜class_nameā€™: ā€˜GlorotUniformā€™, ā€˜configā€™: {ā€˜seedā€™: None}, ā€˜registered_nameā€™: None}, ā€˜bias_initializerā€™: {ā€˜moduleā€™: ā€˜keras.initializersā€™, ā€˜class_nameā€™: ā€˜Zerosā€™, ā€˜configā€™: {}, ā€˜registered_nameā€™: None}, ā€˜kernel_regularizerā€™: None, ā€˜bias_regularizerā€™: None, ā€˜activity_regularizerā€™: None, ā€˜kernel_constraintā€™: None, ā€˜bias_constraintā€™: None}, ā€˜registered_nameā€™: None, ā€˜build_configā€™: {ā€˜input_shapeā€™: [None, 150, 150, 3]}}.

Exception encountered: Error when deserializing class ā€˜Conv2Dā€™ using config={ā€˜nameā€™: ā€˜conv2d_20ā€™, ā€˜trainableā€™: True, ā€˜dtypeā€™: ā€˜float32ā€™, ā€˜batch_input_shapeā€™: [None, 150, 150, 3], ā€˜filtersā€™: 32, ā€˜kernel_sizeā€™: [3, 3], ā€˜stridesā€™: [1, 1], ā€˜paddingā€™: ā€˜validā€™, ā€˜data_formatā€™: ā€˜channels_lastā€™, ā€˜dilation_rateā€™: [1, 1], ā€˜groupsā€™: 1, ā€˜activationā€™: ā€˜reluā€™, ā€˜use_biasā€™: True, ā€˜kernel_initializerā€™: {ā€˜moduleā€™: ā€˜keras.initializersā€™, ā€˜class_nameā€™: ā€˜GlorotUniformā€™, ā€˜configā€™: {ā€˜seedā€™: None}, ā€˜registered_nameā€™: None}, ā€˜bias_initializerā€™: {ā€˜moduleā€™: ā€˜keras.initializersā€™, ā€˜class_nameā€™: ā€˜Zerosā€™, ā€˜configā€™: {}, ā€˜registered_nameā€™: None}, ā€˜kernel_regularizerā€™: None, ā€˜bias_regularizerā€™: None, ā€˜activity_regularizerā€™: None, ā€˜kernel_constraintā€™: None, ā€˜bias_constraintā€™: None}.

Exception encountered: Unrecognized keyword arguments passed to Conv2D: {ā€˜batch_input_shapeā€™: [None, 150, 150, 3]}

Here is my env:

!pip list | grep -E ā€œtensorflow|tfā€
google-cloud-aiplatform 1.70.0
lazr.restfulclient 0.14.4
platformdirs 4.3.6
pyarrow-hotfix 0.6
tensorflow 2.17.0
tensorflow-datasets 4.9.6
tensorflow-hub 0.16.1
tensorflow-io-gcs-filesystem 0.37.1
tensorflow-metadata 1.16.1
tensorflow-probability 0.24.0
tf_keras 2.17.0
tf-slim 1.1.0

Please set the envionment variable before importing tensorflow.

>>> import os
>>> os.environ['TF_USE_LEGACY_KERAS']='1'
>>> import tensorflow as tf
>>> tf.keras.models.load_model("models/imbalanced_model.keras").summary()
WARNING:absl:Skipping variable loading for optimizer 'Adam', because it has 25 variables whereas the saved optimizer has 1 variables. 
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d (Conv2D)             (None, 148, 148, 32)      896       
                                                                 
 max_pooling2d (MaxPooling2  (None, 74, 74, 32)        0         
 D)                                                              
                                                                 
 conv2d_1 (Conv2D)           (None, 72, 72, 64)        18496     
                                                                 
 max_pooling2d_1 (MaxPoolin  (None, 36, 36, 64)        0         
 g2D)                                                            
                                                                 
 conv2d_2 (Conv2D)           (None, 34, 34, 64)        36928     
                                                                 
 max_pooling2d_2 (MaxPoolin  (None, 17, 17, 64)        0         
 g2D)                                                            
                                                                 
 conv2d_3 (Conv2D)           (None, 15, 15, 128)       73856     
                                                                 
 max_pooling2d_3 (MaxPoolin  (None, 7, 7, 128)         0         
 g2D)                                                            
                                                                 
 flatten (Flatten)           (None, 6272)              0         
                                                                 
 dense (Dense)               (None, 512)               3211776   
                                                                 
 dense_1 (Dense)             (None, 3)                 1539      
                                                                 
=================================================================
Total params: 3343491 (12.75 MB)
Trainable params: 3343491 (12.75 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
>>> 

I found it can be loaded after the convert immediately, but the error raised when it reaches the load step if I run the lab following the experiment step by step. I think there is some unknown issue hiding inside somewhereā€¦

If the environment variable is set before importing tensorflow, all should be good. If thatā€™s not the case, please share the steps so that the staff can look at it.

I have done several experiments and found that if the following steps involve these
from tensorflow.keras import ā€¦ like

from tensorflow.keras import layers, models, optimizers

or

from tensorflow.keras.preprocessing.image import ImageDataGenerator

the load operation would raise error

Yes the env is set properly.

Thanks for that information. The staff are looking into it. Iā€™ve left a note requesting the staff to get in touch with you.
At this point, please make changes to the notebook to get things to work by removing other imports and fixing code accordingly till the staff fix this issue.

1 Like

Thanks for this notice, and another weird issue I found is that I cannot guarantee to convert the model successfully even the OS env set properly.

Some times I can convert the model per the script, and failed after I restart the Colab env and trigger the download and convert; however, if I restart the Colab env again, the error might be disappeared even I didnā€™t change any download and convert logic

Hi Feihong. The lab was developed using a previous TF version (2.15). Please use this Colab instead. The link is also updated in the classroom for new learners. We will update this for Keras 3, but we donā€™t have a timeline yet. Nonetheless, the methods there might be a little different, but the concepts taught will still apply (e.g. using data augmentation). The results will also be the same. Hope this helps.

Hey @chris.favila,are you referring to the version 3 branch? I noticed there is a latest PR merged to the main branch moment ago, but this is pointing to other than the master branch : ]

Yes please use the version_3 branch. The master branch is mainly for the previous version of the course. Thanks.

1 Like