为什么在添加自定义损失函数后会出现此错误:未知损失函数:DSSIMObjective?

我想使用 DSSIM 损失函数,我把这个损失函数的代码放在我的代码中,但它产生了这个错误

回溯(最近一次调用最后一次):

文件“”,第 218 行,在 w_extraction.compile(optimizer=opt, loss={'decoder_output':'DSSIMObjective','wprim':'binary_crossentropy'}, loss_weights={'decoder_output': 1.0, 'wprim': 1.0 },metrics=['mae'])

文件“D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\training.py”,第 129 行,编译 loss_functions.append(losses.get(loss.get(name)))

文件“D:\software\Anaconda3\envs\py36\lib\site-packages\keras\losses.py”,第 133 行,在 get 中返回反序列化(标识符)

文件“D:\software\Anaconda3\envs\py36\lib\site-packages\keras\losses.py”,第114行,反序列化printable_module_name='loss function')

文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\utils\generic_utils.py",第 165 行,在 deserialize_keras_object ':' + function_name)

ValueError:未知损失函数:DSSIMObjective

我不知道我应该把这个损失函数的定义放在哪里?我把这段代码放在我的网络结构之上。

import keras_contrib.backend as KC



class DSSIMObjective:

    """Difference of Structural Similarity (DSSIM loss function).

    Clipped between 0 and 0.5

    Note : You should add a regularization term like a l2 loss in addition to this one.

    Note : In theano, the `kernel_size` must be a factor of the output size. So 3 could

           not be the `kernel_size` for an output of 32.

    # Arguments

        k1: Parameter of the SSIM (default 0.01)

        k2: Parameter of the SSIM (default 0.03)

        kernel_size: Size of the sliding window (default 3)

        max_value: Max value of the output (default 1.0)

    """


    def __init__(self, k1=0.01, k2=0.03, kernel_size=3, max_value=1.0):

        self.__name__ = 'DSSIMObjective'

        self.kernel_size = kernel_size

        self.k1 = k1

        self.k2 = k2

        self.max_value = max_value

        self.c1 = (self.k1 * self.max_value) ** 2

        self.c2 = (self.k2 * self.max_value) ** 2

        self.dim_ordering = K.image_data_format()

        self.backend = K.backend()


    def __int_shape(self, x):

        return K.int_shape(x) if self.backend == 'tensorflow' else K.shape(x)


紫衣仙女
浏览 304回答 1
1回答

呼如林

您应该通过提供对象实例而不是字符串名称来调用此损失:w_extraction.compile(optimizer=opt, loss={'decoder_output': DSSIMObjective(),'wprim':'binary_crossentropy'}, loss_weights={'decoder_output': 1.0, 'wprim': 1.0},metrics=['mae'])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python