import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.layers import LSTM, Dense, GlobalAveragePooling2D, Dropout
from tensorflow.keras.activations import relu
class Model(tf.keras.Model):
def __init__(self, num_classes, latent_dim=2048, lstm_layers=1, hidden_dim=2048, bidirectional=False):
super(Model, self).__init__()
self.base_model = tf.keras.Sequential([
ResNet50(include_top=False, weights=None, input_shape=(None, None, 3)),
], name='resnet50_base')
self.base_model.layers[0].name = 'resnet50_base' # Set a unique name for the ResNet50 model
self.base_model.layers[0].trainable = False # Ensure the ResNet50 model is not trainable
self.lstm = LSTM(hidden_dim, return_sequences=True, return_state=True, name='lstm_layer')
self.relu = relu
self.dp = Dropout(0.4, name='dropout_layer')
self.linear1 = Dense(num_classes, name='output_layer')
self.avgpool = GlobalAveragePooling2D(name='global_avg_pooling')
self.built = True # Mark the model as built
def call(self, inputs):
batch_size, seq_length, h, w, c = inputs.shape.as_list() # TensorFlow shape order is different
inputs = tf.reshape(inputs, (batch_size * seq_length, h, w, c))
fmap = self.base_model(inputs)
x = self.avgpool(fmap)
x = tf.reshape(x, (batch_size, seq_length, -1))
x_lstm, _, _ = self.lstm(x)
x = tf.reduce_mean(x_lstm, axis=1)
x = self.dp(x)
x = self.linear1(x)
return fmap, x
model = Model(num_classes=2) # Binary classification with 2 classes: 'real' and 'fake'`
after training and saving it as model.save(“vedio_model.keras”), when i try to load , the model ,
it gives error loaded_model = tf.keras.models.load_model(“vedio_model.keras”)
TypeError Traceback (most recent call last)
Cell In[34], line 1
—-> 1 loaded_model = tf.keras.models.load_model(“vedio_model.keras”)
File /usr/local/lib/python3.11/dist-packages/keras/src/saving/saving_api.py:176, in load_model(filepath, custom_objects, compile, safe_mode)
173 is_keras_zip = True
175 if is_keras_zip:
–> 176 return saving_lib.load_model(
177 filepath,
178 custom_objects=custom_objects,
179 compile=compile,
180 safe_mode=safe_mode,
181 )
182 if str(filepath).endswith((“.h5”, “.hdf5”)):
183 return legacy_h5_format.load_model_from_hdf5(filepath)
File /usr/local/lib/python3.11/dist-packages/keras/src/saving/saving_lib.py:155, in load_model(filepath, custom_objects, compile, safe_mode)
153 # Construct the model from the configuration file in the archive.
154 with ObjectSharingScope():
–> 155 model = deserialize_keras_object(
156 config_dict, custom_objects, safe_mode=safe_mode
157 )
159 all_filenames = zf.namelist()
160 if _VARS_FNAME + “.h5” in all_filenames:
File /usr/local/lib/python3.11/dist-packages/keras/src/saving/serialization_lib.py:687, in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
684 if obj is not None:
685 return obj
–> 687 cls = _retrieve_class_or_fn(
688 class_name,
689 registered_name,
690 module,
691 obj_type=”class”,
692 full_config=config,
693 custom_objects=custom_objects,
694 )
696 if isinstance(cls, types.FunctionType):
697 return cls
File /usr/local/lib/python3.11/dist-packages/keras/src/saving/serialization_lib.py:805, in _retrieve_class_or_fn(name, registered_name, module, obj_type, full_config, custom_objects)
“ 802 if obj is not None:
803 return obj
–> 805 raise TypeError(
806 f”Could not locate {obj_type} ‘{name}’. ”
807 “Make sure custom classes are decorated with ”
808 “`@keras.saving.r
``
egister_keras_serializable()`. ”
809 f”Full object config: {full_config}”
810 )
TypeError: Could not locate class ‘Model’. Make sure custom classes are decorated with @keras.saving.register_keras_serializable()
. Full object config: {‘module’: None, ‘class_name’: ‘Model’, ‘config’: {‘trainable’: True, ‘dtype’: ‘float32’}, ‘registered_name’: ‘Model’}
There was no error while , training and saving. but it is e=giving error while loading
Shivanand Garg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.