• keras中自定义Layer


    最近在学习SSD的源码,其中有两个自定的层,特此学习一下并记录。

     1 import keras.backend as K
     2 from keras.engine.topology import InputSpec
     3 from keras.engine.topology import Layer
     4 import numpy as np
     5 
     6 class L2Normalization(Layer):
     7     '''
     8     Performs L2 normalization on the input tensor with a learnable scaling parameter
     9     as described in the paper "Parsenet: Looking Wider to See Better" (see references)
    10     and as used in the original SSD model.
    11 
    12     Arguments:
    13         gamma_init (int): The initial scaling parameter. Defaults to 20 following the
    14             SSD paper.
    15 
    16     Input shape:
    17         4D tensor of shape `(batch, channels, height, width)` if `dim_ordering = 'th'`
    18         or `(batch, height, width, channels)` if `dim_ordering = 'tf'`.
    19 
    20     Returns:
    21         The scaled tensor. Same shape as the input tensor.
    22     '''
    23 
    24     def __init__(self, gamma_init=20, **kwargs):
    25         if K.image_dim_ordering() == 'tf':
    26             self.axis = 3
    27         else:
    28             self.axis = 1
    29         self.gamma_init = gamma_init
    30         super(L2Normalization, self).__init__(**kwargs)
    31 
    32     def build(self, input_shape):
    33         self.input_spec = [InputSpec(shape=input_shape)]
    34         gamma = self.gamma_init * np.ones((input_shape[self.axis],))
    35         self.gamma = K.variable(gamma, name='{}_gamma'.format(self.name))
    36         self.trainable_weights = [self.gamma]
    37         super(L2Normalization, self).build(input_shape)
    38 
    39     def call(self, x, mask=None):
    40         output = K.l2_normalize(x, self.axis)
    41         output *= self.gamma
    42         return output

    首先说一下这个层是用来做什么的。就是对于每一个通道进行归一化,不过通道使用的是不同的归一化参数,也就是说这个参数是需要进行学习的,因此需要通过 自定义层来完成。

    在keras中,每个层都是对象,真的,可以通过dir(Layer对象)来查看具有哪些属性。

    具体说来:

    __init__():用来进行初始化的(这不是废话么),gamma就是要学习的参数。

    bulid():是用来创建这层的权重向量的,也就是要学习的参数“壳”。

    33:设置该层的input_spec,这个是通过InputSpec函数来实现。

    34:分配权重“壳”的实际空间大小

    35,:由于底层使用的Tensorflow来进行实现的,因此这里使用Tensorflow中的variable来保存变量。

    36:根据keras官网的要求,可训练的权重是要添加至trainable_weights列表中的

    37:我不想说了,官网给的实例都是这么做的。

    call():用来进行具体实现操作的。

    40:沿着指定的轴对输入数据进行L2正则化

    41:使用学习的gamma来对正则化后的数据进行加权

    42:将最后的数据最为该层的返回值,这里由于是和输入形式相同的,因此就没有了compute_output_shape函数,如果输入和输出的形式不同,就需要进行输入的调整。

    就这样子吧。

  • 相关阅读:
    VMware workstation中安装Ubuntu18.04server
    python一行命令安装chromedriver
    vim配置&相关问题
    博客园美化
    期望DP——HDU4035Maze
    [学习笔记]虚树
    线段树——51nod1593&CF515E 公园晨跑
    [STL] multiset
    [学习笔记] 线性基
    泛化物品优化树型DP——[HAOI2010]软件安装
  • 原文地址:https://www.cnblogs.com/andyniu/p/7466154.html
Copyright © 2020-2023  润新知