Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

0 Training parameters in keras custom layer

Recently I have switched to keras from tensorflow and I need to create a custom layer.

I defined the class as below:

class Apply_conv2d(Layer):
def __init__(self, **kwargs):
    super(Apply_conv2d, self).__init__(**kwargs)

def build(self, input_shape):
    super(Apply_conv2d, self).build(input_shape)  # Be sure to call this somewhere!

def call(self, x):
    res = Conv2D(32, (1, 1), padding='same')(x)
    self.shape = res.shape
    res = k.reshape(res, [-1, self.shape[1] * self.shape[2] * self.shape[3]])
    return res

def compute_output_shape(self, input_shape):
    return (None, input_shape[3])


But when I print model.summary() I get 0 trainable parameters in usage of this layer.

What is wrong with this implementation? Thank you


Edit
I changed the class definition to:
class Apply_conv2d(Layer):
def __init__(self, **kwargs):
    self.trainable = True
    super(Apply_conv2d, self).__init__(**kwargs)

def build(self, input_shape):
    w = self.add_weight(name='kernel', shape=(1, 1, 2048, 32), initializer='uniform', trainable=True)
    b = self.add_weight(name='kernel', shape=(32,), initializer='uniform', trainable=True)
    self.kernel = [w, b]
    super(Apply_conv2d, self).build(input_shape)  # Be sure to call this somewhere!

def call(self, x):
    res = Conv2D(32, (1, 1), padding='same', name='feature_conv', weights=self.kernel)(x)
    self.shape = res.shape
    res = k.reshape(res, [-1, self.shape[1] * self.shape[2] * self.shape[3]])
    return res

def compute_output_shape(self, input_shape):
    return (None, input_shape[3])

but still this doesn't work...
The Error is:

    Traceback (most recent call last):
    File "C:\Program Files\JetBrains\PyCharm Community Edition 
    2017.2.3\helpers\pydev\pydevd.py", line 1668, in <module>
       main()
     File "C:\Program Files\JetBrains\PyCharm Community Edition 
   2017.2.3\helpers\pydev\pydevd.py", line 1662, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.3\helpers\pydev\pydevd.py", line 1072, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Reza/Dropbox/Reza/VOC2012-D/script.py", line 123, in <module>
    model = cl.get_model(inputs)
  File "C:/Users/Reza/Dropbox/Reza/VOC2012-D\custom_layers.py", line 77, in get_model
    x3 = Apply_conv2d()(x)
  File "C:\Program Files\Python35\lib\site-packages\keras\engine\topology.py", line 603, in __call__
    output = self.call(inputs, **kwargs)
  File "C:/Users/Reza/Dropbox/Reza/VOC2012-D\custom_layers.py", line 104, in call
    res = Conv2D(32, (1, 1), padding='same', name='feature_conv', weights=self.kernel)(x)
  File "C:\Program Files\Python35\lib\site-packages\keras\engine\topology.py", line 583, in __call__
    self.set_weights(self._initial_weights)
  File "C:\Program Files\Python35\lib\site-packages\keras\engine\topology.py", line 1203, in set_weights
    K.batch_set_value(weight_value_tuples)
  File "C:\Program Files\Python35\lib\site-packages\keras\backend\tensorflow_backend.py", line 2239, in batch_set_value
    value = np.asarray(value, dtype=dtype(x))
  File "C:\Program Files\Python35\lib\site-packages\numpy\core\numeric.py", line 531, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.


Any suggestion?

like image 827
Adam301 Avatar asked Jan 01 '26 09:01

Adam301


1 Answers

After a lot of research and try various approaches Finally I found the solution.
I should have used the raw conv operation from keras so the implementation should have been like this:

class Apply_conv2d(Layer):
def __init__(self, **kwargs):
    super(Apply_conv2d, self).__init__(**kwargs)
    self.trainable = True

def build(self, input_shape):
    self.kernel = self.add_weight(name='kernel', shape=(1, 1, 2048, 32), initializer='uniform', trainable=True)
    self.bias = self.add_weight(name='bias', shape=(32,), initializer='uniform', trainable=True)

def call(self, inputs, **kwargs):
    outputs = k.conv2d(inputs, self.kernel)
    outputs = k.bias_add(outputs, self.bias)
    self.shape = outputs.shape
    outputs = k.reshape(outputs, [-1, self.shape[1] * self.shape[2] * self.shape[3]])
    return outputs

def compute_output_shape(self, input_shape):
    return (input_shape[0], input_shape[3])
like image 151
Adam301 Avatar answered Jan 02 '26 23:01

Adam301



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!