I was implementing a Dehazenet model by using tensorflow where i had to create a Maxout and Brelu class and function as there is nothing built-in in tensorflow for them to use, later while trying to use my saved model i faced errors and exception
ERROR :
<code>Traceback (most recent call last):
File "model/scripts/evaluate.py", line 69, in <module>
metrics = evaluate_model(model_path, input_dir, target_dir)
File "model/scripts/evaluate.py", line 49, in evaluate_model
model = keras.models.load_model(model_path)
File "D:image-dehazingenvlibsite-packageskerassrcsavingsaving_api.py", line 238, in load_model
return legacy_sm_saving_lib.load_model(
File "D:image-dehazingenvlibsite-packageskerassrcutilstraceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "D:image-dehazingenvlibsite-packageskerassrcenginebase_layer.py", line 870, in from_config
TypeError: Error when deserializing class 'Activation' using config={'name': 'activation', 'trainable': True, 'dtype': 'float32', 'activation': {'module': 'builtins', 'class_name': 'function', 'config': 'brelu', 'registered_name': 'function'}}.
Exception encountered: Unknown activation function: 'function'. Please ensure you are using a `keras.utils.custom_object_scope` and that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
<code>Traceback (most recent call last):
File "model/scripts/evaluate.py", line 69, in <module>
metrics = evaluate_model(model_path, input_dir, target_dir)
File "model/scripts/evaluate.py", line 49, in evaluate_model
model = keras.models.load_model(model_path)
File "D:image-dehazingenvlibsite-packageskerassrcsavingsaving_api.py", line 238, in load_model
return legacy_sm_saving_lib.load_model(
File "D:image-dehazingenvlibsite-packageskerassrcutilstraceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "D:image-dehazingenvlibsite-packageskerassrcenginebase_layer.py", line 870, in from_config
raise TypeError(
TypeError: Error when deserializing class 'Activation' using config={'name': 'activation', 'trainable': True, 'dtype': 'float32', 'activation': {'module': 'builtins', 'class_name': 'function', 'config': 'brelu', 'registered_name': 'function'}}.
Exception encountered: Unknown activation function: 'function'. Please ensure you are using a `keras.utils.custom_object_scope` and that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
</code>
Traceback (most recent call last):
File "model/scripts/evaluate.py", line 69, in <module>
metrics = evaluate_model(model_path, input_dir, target_dir)
File "model/scripts/evaluate.py", line 49, in evaluate_model
model = keras.models.load_model(model_path)
File "D:image-dehazingenvlibsite-packageskerassrcsavingsaving_api.py", line 238, in load_model
return legacy_sm_saving_lib.load_model(
File "D:image-dehazingenvlibsite-packageskerassrcutilstraceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "D:image-dehazingenvlibsite-packageskerassrcenginebase_layer.py", line 870, in from_config
raise TypeError(
TypeError: Error when deserializing class 'Activation' using config={'name': 'activation', 'trainable': True, 'dtype': 'float32', 'activation': {'module': 'builtins', 'class_name': 'function', 'config': 'brelu', 'registered_name': 'function'}}.
Exception encountered: Unknown activation function: 'function'. Please ensure you are using a `keras.utils.custom_object_scope` and that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
code:
<code>import tensorflow as tf
from skimage.metrics import mean_squared_error, peak_signal_noise_ratio, structural_similarity
from preprocessing import load_dataset
from tensorflow import keras
# Clear all previously registered custom objects
keras.saving.get_custom_objects().clear()
# Define and register brelu as an activation function
@keras.saving.register_keras_serializable(package="MyActivations", name="brelu")
return tf.maximum(0.0, tf.minimum(1.0, x))
# Define and register Maxout custom layer
@keras.saving.register_keras_serializable(package="MyLayers")
class Maxout(tf.keras.layers.Layer):
def __init__(self, num_units, **kwargs):
super(Maxout, self).__init__(**kwargs)
self.num_units = num_units
tf.debugging.assert_equal(num_channels % self.num_units, 0, message="Number of channels must be divisible by num_units")
new_shape = tf.concat([shape[:-1], [self.num_units, num_channels // self.num_units]], axis=-1)
output = tf.reduce_max(tf.reshape(inputs, new_shape), axis=-1)
config = super(Maxout, self).get_config()
config.update({"num_units": self.num_units})
def evaluate_model(model_path, input_dir, target_dir, size=(256, 256)):
Evaluate the trained model on the test dataset.
- model_path (str): Path to the trained model file.
- input_dir (str): Directory with input (hazed) images.
- target_dir (str): Directory with target (dehazed) images.
- size (tuple): Desired size for resizing the images.
- dict: Evaluation metrics (MSE, PSNR, SSIM).
# Load the trained model with custom objects
with keras.utils.custom_object_scope({'brelu': brelu, 'Maxout': Maxout}):
model = keras.models.load_model(model_path)
# Load and preprocess the dataset
X, y_true = load_dataset(input_dir, target_dir, size)
y_pred = model.predict(X)
# Compute evaluation metrics
mse = mean_squared_error(y_true.flatten(), y_pred.flatten())
psnr = peak_signal_noise_ratio(y_true, y_pred)
ssim = structural_similarity(y_true, y_pred, multichannel=True)
return {'MSE': mse, 'PSNR': psnr, 'SSIM': ssim}
<code>import tensorflow as tf
from skimage.metrics import mean_squared_error, peak_signal_noise_ratio, structural_similarity
from preprocessing import load_dataset
from tensorflow import keras
# Clear all previously registered custom objects
keras.saving.get_custom_objects().clear()
# Define and register brelu as an activation function
@keras.saving.register_keras_serializable(package="MyActivations", name="brelu")
def brelu(x):
return tf.maximum(0.0, tf.minimum(1.0, x))
# Define and register Maxout custom layer
@keras.saving.register_keras_serializable(package="MyLayers")
class Maxout(tf.keras.layers.Layer):
def __init__(self, num_units, **kwargs):
super(Maxout, self).__init__(**kwargs)
self.num_units = num_units
def call(self, inputs):
shape = tf.shape(inputs)
num_channels = shape[-1]
tf.debugging.assert_equal(num_channels % self.num_units, 0, message="Number of channels must be divisible by num_units")
new_shape = tf.concat([shape[:-1], [self.num_units, num_channels // self.num_units]], axis=-1)
output = tf.reduce_max(tf.reshape(inputs, new_shape), axis=-1)
return output
def get_config(self):
config = super(Maxout, self).get_config()
config.update({"num_units": self.num_units})
return config
def evaluate_model(model_path, input_dir, target_dir, size=(256, 256)):
"""
Evaluate the trained model on the test dataset.
Args:
- model_path (str): Path to the trained model file.
- input_dir (str): Directory with input (hazed) images.
- target_dir (str): Directory with target (dehazed) images.
- size (tuple): Desired size for resizing the images.
Returns:
- dict: Evaluation metrics (MSE, PSNR, SSIM).
"""
# Load the trained model with custom objects
with keras.utils.custom_object_scope({'brelu': brelu, 'Maxout': Maxout}):
model = keras.models.load_model(model_path)
# Load and preprocess the dataset
X, y_true = load_dataset(input_dir, target_dir, size)
# Make predictions
y_pred = model.predict(X)
# Compute evaluation metrics
mse = mean_squared_error(y_true.flatten(), y_pred.flatten())
psnr = peak_signal_noise_ratio(y_true, y_pred)
ssim = structural_similarity(y_true, y_pred, multichannel=True)
return {'MSE': mse, 'PSNR': psnr, 'SSIM': ssim}
</code>
import tensorflow as tf
from skimage.metrics import mean_squared_error, peak_signal_noise_ratio, structural_similarity
from preprocessing import load_dataset
from tensorflow import keras
# Clear all previously registered custom objects
keras.saving.get_custom_objects().clear()
# Define and register brelu as an activation function
@keras.saving.register_keras_serializable(package="MyActivations", name="brelu")
def brelu(x):
return tf.maximum(0.0, tf.minimum(1.0, x))
# Define and register Maxout custom layer
@keras.saving.register_keras_serializable(package="MyLayers")
class Maxout(tf.keras.layers.Layer):
def __init__(self, num_units, **kwargs):
super(Maxout, self).__init__(**kwargs)
self.num_units = num_units
def call(self, inputs):
shape = tf.shape(inputs)
num_channels = shape[-1]
tf.debugging.assert_equal(num_channels % self.num_units, 0, message="Number of channels must be divisible by num_units")
new_shape = tf.concat([shape[:-1], [self.num_units, num_channels // self.num_units]], axis=-1)
output = tf.reduce_max(tf.reshape(inputs, new_shape), axis=-1)
return output
def get_config(self):
config = super(Maxout, self).get_config()
config.update({"num_units": self.num_units})
return config
def evaluate_model(model_path, input_dir, target_dir, size=(256, 256)):
"""
Evaluate the trained model on the test dataset.
Args:
- model_path (str): Path to the trained model file.
- input_dir (str): Directory with input (hazed) images.
- target_dir (str): Directory with target (dehazed) images.
- size (tuple): Desired size for resizing the images.
Returns:
- dict: Evaluation metrics (MSE, PSNR, SSIM).
"""
# Load the trained model with custom objects
with keras.utils.custom_object_scope({'brelu': brelu, 'Maxout': Maxout}):
model = keras.models.load_model(model_path)
# Load and preprocess the dataset
X, y_true = load_dataset(input_dir, target_dir, size)
# Make predictions
y_pred = model.predict(X)
# Compute evaluation metrics
mse = mean_squared_error(y_true.flatten(), y_pred.flatten())
psnr = peak_signal_noise_ratio(y_true, y_pred)
ssim = structural_similarity(y_true, y_pred, multichannel=True)
return {'MSE': mse, 'PSNR': psnr, 'SSIM': ssim}
I tried using the documentation , tried the serialization function and gpt also but no solution worked. I only want that my loaded model work prefectly so i can integrate it with my frontend code