0

is there a solution to sharing the weights of convolution layers with different paramters in dilation? like here

I want to train the weights of the layer with different dilations meaning that every dilation should have an impact to the weights

I've tried something like this:

class customModel(tf.keras.Model):

    def __init__(self,dmin,dmax):
        super().__init()
        self.conv_list=[]
        
        for i in range(dmin,dmax+1):
            conv=                   tf.keras.layers.Conv2D(filters=self.num_filters,
                                                           kernel_size=(1,2),
                                                           dilation_rate=(1,i), 
                                                           kernel_initializer='custom_initializer',
                                                           trainable=True, 
                                                           padding='same')
            self.conv_list.append(conv)
            
    def call(self,inputs):
        
        x=self.conv_list[0](inputs)
        weights=self.conv_list[0].get_weights()
        for i in range(1,len(self.conv_list)):
            conv=self.conv_list[i]
            conv.set_weights(weights=weights)
            after_conv=conv(inputs)
            x=tf.keras.layers.concatenate([x,after_conv], axis=3,trainable=self.trainable)
        return x

I get the error message:

RuntimeError: Cannot get value inside Tensorflow graph function.

I guess I don't fully understand the graph execution. Executing in eager mode by tf.config.run_functions_eagerly(True) Doesnt fix the problem and also blows up my GPU memory as i train with quite large Images.

Also i think im overwriting the weights and only the weights of the first convolution layer with minimum dilation rate are trained.

I'd be glad if someone could help!

1 Answers1

0

I searched for a way to manipulate the dilation rate on a convolutional kernel that is already initialized. So i wrote a "set_dilation_rate" function in the tensorflow Conv class similar to how the original dilation rate is initialized and called it in my model. No Problems with that so far

Anaconda3/envs/MY_ENVIRONMENT/Lib/site-packages/keras/layers/convolutional/base_conv

>  class Conv(Layer):
>         def set_dilation(self,dilation_set):
>             self.dilation_rate=conv_utils.normalize_tuple(
>             dilation_set, self.rank, "dilation_rate"
>             )

in my model:

>  class customModel(tf.keras.Model):
> 
>     def __init__(self,dmin,dmax):
>         super().__init()
>         self.dmin=dmin
>         self.dmax=dmax
>       
>         conv=tf.keras.layers.Conv2D(filters=self.num_filters,
>                                     kernel_size=(1,2),
>                                     dilation_rate=(1,dmin),                                             
>                                     kernel_initializer='custom_initializer',
>                                     trainable=True, 
>                                     padding='same')
>             
>             
>     def call(self,inputs):
>         
>         x=self.conv(inputs)
>         for i in range(dmin+1,dmax+1):
>             self.conv.set_dilation((1,i))
>             after_conv=conv(inputs)
>             x=tf.keras.layers.concatenate([x,after_conv], 
>                                            axis=3,
>                                            trainable=False)
>         return x