高级 API 中的 Tesorflow 自定义层:抛出对象没有属性 '_expects_mask_arg' 错误

Tri*_*dhi 2 layer tensorflow

我正在尝试根据前一层 normal (None,128,128,3)、albedo(None,128,128,3) 和照明 (27) 的三个输入重建图像。但是这里的代码仍然说 object has no attribute '_expects_mask_arg' error 。我在这里展示了我的代码,其中我使用高级 API 使用 Tensorflow v2 beta 实现了一个自定义层。

导入数学类 Reconstruction_Layer(tf.keras.layers.Layer):

def __init__(self,input_shape ):
    super(Reconstruction_Layer, self).__init__()
    #self.num_outputs = num_outputs
    #self.pixel=np.zeros((9),dtype=int)
    self.sphar=np.zeros((9),dtype=float)
    self.y=np.zeros((9),dtype=float)
    self.reconstructed_img=np.zeros((128,128,3),dtype=float)
    #self.y=tf.zeros([128,128,9])
    self.normal_light=np.zeros((128,128,9),dtype=float)
    self.y_temp=np.zeros((9),dtype=float)
    w_init = tf.random_normal_initializer()
    self.r_img = tf.Variable(initial_value=w_init(shape=input_shape),dtype='float32',trainable=True)

def build(self,input_shape):
    super(MyLayer, self).build(input_shape)

def call(self,input_layer):
    self.normal,self.albedo,self.light = input_layer

    for i in range(128):
        for j in range(128):
            #self.y=spherical_harmonic_calc(self.normal(i,j))

            self.pixel=self.normal[i,j,:]


            #self.normal_light(i,j)= self.y
            self.sphar[0]=(1/((4*math.pi)**0.5))
            self.sphar[1]=((3/(4*math.pi))**0.5)*self.pixel[2]
            self.sphar[3]=(((3/(4*math.pi))**0.5)*self.pixel[1])
            self.sphar[4]=((1/2)*((5/(4*math.pi))**0.5)*(3*(self.pixel[2]**2) - 1))
            self.sphar[5]=(3*((5/(12*math.pi))**0.5)*self.pixel[2]*self.pixel[0])
            self.sphar[6]=(3*((5/(12*math.pi))**0.5)*self.pixel[2]*self.pixel[1])
            self.sphar[7]=((3/2)*((5/(12*math.pi))**0.5)*((self.pixel[0]**2)-(self.pixel[1]**2)))
            self.sphar[8]=(3*((5/(12*math.pi))**0.5)*self.pixel[0]*self.pixel[1])

            self.normal_light[i,j,:]=self.sphar

    for j in range(128):
        for k in range(128):
            for i in range(3):

                self.reconstructed_img[j,k,i]=self.albedo[j,k,i]* tf.tensordot(self.normal_light[j,k],self.light[i*9:(i+1)*9 ],axes=1)

    self.reconstructed_img=tf.convert_to_tensor(self.reconstructed_img)
    self.r_img=self.reconstructed_img


    return self.r_img


"""
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-74-06759ef5b0b5> in <module>
      1 import numpy as np
----> 2 x=Reconstruction_Layer((128,128,3))(d)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    580     # explicitly take priority.
    581     input_masks = self._collect_input_masks(inputs, args, kwargs)
--> 582     if (self._expects_mask_arg and input_masks is not None and
    583         not self._call_arg_was_passed('mask', args, kwargs)):
    584       kwargs['mask'] = input_masks

AttributeError: 'Reconstruction_Layer' object has no attribute '_expects_mask_arg'
"""
Run Code Online (Sandbox Code Playgroud)

Edo*_*doG 5

我只是遇到了同样的错误,这是因为我忘记.__init__()在super(). 你做到了,但这让我认为这个错误是由于你派生的基础层的错误初始化造成的。我注意到在 doc 示例中没有必要调用build()基础层,如果您删除该函数,它对我有用(因为它与您的层无关)。