Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tesorflow Custom Layer in High level API: throws object has no attribute '_expects_mask_arg' error

I am trying to reconsturct an image based on three inputs of previous layers normal (None,128,128,3),albedo(None,128,128,3) and lighting(27) . But here the code still says object has no attribute '_expects_mask_arg' error .I have presented my code here in which I have implemented a custom layer using Tensorflow v2 beta using the high level API.

import math class 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'
"""
like image 606
Trilok Padhi Avatar asked Jan 25 '26 15:01

Trilok Padhi


1 Answers

I just had the same error and it was due to me forgetting to call .__init__() after super(). You did it, but this make me think that this error is due to wrong initialization of the base layer you are deriving from. I notice that in the doc example it's not necessary to call build() on the base layer, and it works for me if you remove that function (as it does nothing related to your layer).

like image 58
EdoardoG Avatar answered Jan 28 '26 19:01

EdoardoG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!