Facing issue with Custom Function and Model in Tensor flow

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 :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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

New contributor

Ayesha Irshad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật