コード例 #1
0
    def __init__(self,
                 name,
                 in_channels,
                 groups=2,
                 se_type="cSE",
                 use_residual=True,
                 act_type="relu",
                 **kwargs):

        super(_ShuffleBlock, self).__init__(prefix=name + "_")

        self.in_channels = in_channels
        self.nb_right_channels = in_channels // 2

        self.groups = groups

        self.body = HybridSequential(prefix="")
        self.use_residual = use_residual

        with self.name_scope():
            self.body.add(
                Conv2D(channels=self.nb_right_channels,
                       kernel_size=3,
                       strides=1,
                       padding=1,
                       groups=1,
                       use_bias=False))
            self.body.add(BatchNorm())
            self.body.add(get_act(act_type))
            self.body.add(
                Conv2D(channels=self.nb_right_channels,
                       kernel_size=3,
                       strides=1,
                       padding=1,
                       groups=1,
                       use_bias=False))
            self.body.add(BatchNorm())
            if se_type:
                if se_type == "cSE":
                    # apply squeeze excitation
                    self.body.add(
                        _ChannelSqueezeExcitation("se0",
                                                  self.nb_right_channels, 16,
                                                  act_type))
                elif se_type == "sSE":
                    self.body.add(_SpatialSqueezeExcitation("se0"))
                elif se_type == "scSE":
                    self.body.add(
                        _SpatialChannelSqueezeExcitation(
                            "se0", self.nb_right_channels, 2, act_type))
                else:
                    raise Exception(
                        'Unsupported Squeeze Excitation Module: Choose either [None, "cSE", "sSE", "scSE"'
                    )

        if self.use_residual:
            self.act = get_act(act_type)
        self.shufller = _ShuffleChannelsBlock(groups)
コード例 #2
0
    def __init__(self,
                 name,
                 channels,
                 bn_mom=0.9,
                 act_type="relu",
                 se_type=None):
        """
        Definition of the stem proposed by the alpha zero authors

        :param name: name prefix for all blocks
        :param channels: Number of channels for 1st conv operation
        :param bn_mom: Batch normalization momentum parameter
        :param act_type: Activation type to use
        """

        super(_StemRise, self).__init__(prefix=name + "_")

        self.body = HybridSequential(prefix="")

        with self.name_scope():
            # add all layers to the stem
            self.body.add(
                Conv2D(channels=channels // 2,
                       kernel_size=(3, 3),
                       padding=(1, 1),
                       use_bias=False))
            self.body.add(BatchNorm(momentum=bn_mom))
            self.body.add(get_act(act_type))
            if se_type:
                if se_type == "cSE":
                    # apply squeeze excitation
                    # self.se = _ChannelSqueezeExcitation("se0", channels, 2, act_type)
                    self.body.add(
                        _ChannelSqueezeExcitation("se0", channels // 2, 16,
                                                  act_type))
                elif se_type == "sSE":
                    self.body.add(_SpatialSqueezeExcitation("se0"))
                elif se_type == "scSE":
                    self.body.add(
                        _SpatialChannelSqueezeExcitation(
                            "se0", channels // 2, 2, act_type))
                else:
                    raise Exception(
                        'Unsupported Squeeze Excitation Module: Choose either [None, "cSE", "sSE", "scSE"'
                    )
            self.body.add(
                Conv2D(channels=channels,
                       kernel_size=(3, 3),
                       padding=(1, 1),
                       use_bias=False))
            self.body.add(BatchNorm(momentum=bn_mom))
            self.body.add(get_act(act_type))
コード例 #3
0
ファイル: a0_resnet.py プロジェクト: lazercorn/CrazyAra
    def __init__(self,
                 name,
                 channels=1,
                 fc0=256,
                 bn_mom=0.9,
                 act_type="relu",
                 se_type=None):
        """
        Definition of the value head proposed by the alpha zero authors

        :param name: name prefix for all blocks
        :param channels: Number of channels for 1st conv operation in branch 0
        :param fc0: Number of units in Dense/Fully-Connected layer
        :param bn_mom: Batch normalization momentum parameter
        :param act_type: Activation type to use
        :param se_type: SqueezeExcitation type choose either [None, "cSE", "sSE", csSE"] for no squeeze excitation,
        channelwise squeeze excitation, channel-spatial-squeeze-excitation, respectively
        """

        super(_ValueHeadAlphaZero, self).__init__(prefix=name + "_")

        self.body = HybridSequential(prefix="")

        with self.name_scope():
            self.body.add(
                Conv2D(channels=channels, kernel_size=(1, 1), use_bias=False))
            self.body.add(BatchNorm(momentum=bn_mom))

            if se_type:
                if se_type == "cSE":
                    # apply squeeze excitation
                    self.body.add(
                        _ChannelSqueezeExcitation("se0", channels, 16,
                                                  act_type))
                elif se_type == "sSE":
                    self.body.add(_SpatialSqueezeExcitation("se0"))
                elif se_type == "csSE":
                    self.body.add(
                        _SpatialChannelSqueezeExcitation(
                            "se0", channels, 1, act_type))
                else:
                    raise Exception(
                        'Unsupported Squeeze Excitation Module: Choose either [None, "cSE", "sSE", "csSE"'
                    )

            self.body.add(get_act(act_type))
            self.body.add(Flatten())
            self.body.add(Dense(units=fc0))
            self.body.add(get_act(act_type))
            self.body.add(Dense(units=1))
            self.body.add(get_act("tanh"))
コード例 #4
0
    def __init__(self, unit_name, channels, bn_mom, act_type, se_type="scSE"):
        """

        :param channels: Number of channels used in the conv-operations
        :param bn_mom: Batch normalization momentum
        :param act_type: Activation function to use
        :param unit_name: Unit name of the residual block (only used for description (string))
        """
        super(ResidualBlockX, self).__init__(unit_name + "_")
        self.act_type = act_type
        self.unit_name = unit_name
        self.body = HybridSequential(prefix="")
        self.channels = channels

        with self.name_scope():
            if se_type:
                if se_type == "cSE":
                    # apply squeeze excitation
                    self.body.add(
                        _ChannelSqueezeExcitation("se0", channels, 2,
                                                  act_type))
                elif se_type == "sSE":
                    self.body.add(_SpatialSqueezeExcitation("se0"))
                elif se_type == "scSE":
                    self.body.add(
                        _SpatialChannelSqueezeExcitation(
                            "se0", channels, 2, act_type))
                elif se_type == "GE+":
                    self.body.add(
                        _GatherExcitePlus("ge0", channels, 2, act_type))
                else:
                    raise Exception(
                        'Unsupported Squeeze Excitation Module: Choose either [None, "cSE", "sSE", "scSE",'
                        '"GE+')
            self.body.add(BatchNorm(momentum=bn_mom))
            self.body.add(get_act(self.act_type))
            self.body.add(
                Conv2D(channels=channels,
                       kernel_size=3,
                       padding=1,
                       groups=1,
                       use_bias=False))
            self.body.add(BatchNorm(momentum=bn_mom))
            self.body.add(get_act(self.act_type))
            self.body.add(
                Conv2D(channels=channels,
                       kernel_size=3,
                       padding=1,
                       groups=1,
                       use_bias=False))
コード例 #5
0
    def __init__(self,
                 unit_name,
                 channels,
                 bn_mom=0.9,
                 act_type="relu",
                 se_type="scSE",
                 dim_match=True):
        """

        :param channels: Number of channels used in the conv-operations
        :param bn_mom: Batch normalization momentum
        :param act_type: Activation function to use
        :param unit_name: Unit name of the residual block (only used for description (string))
        """
        super(_ResidualBlockXBottleneck, self).__init__(prefix=unit_name + "_")
        self.unit_name = unit_name
        self.use_se = se_type
        self.dim_match = dim_match
        self.body = HybridSequential(prefix="")

        with self.name_scope():

            if se_type:
                if se_type == "cSE":
                    # apply squeeze excitation
                    self.body.add(
                        _ChannelSqueezeExcitation("se0", channels, 2,
                                                  act_type))
                elif se_type == "sSE":
                    self.body.add(_SpatialSqueezeExcitation("se0"))
                elif se_type == "scSE":
                    self.body.add(
                        _SpatialChannelSqueezeExcitation(
                            "se0", channels, 2, act_type))
                elif se_type == "GE+":
                    self.body.add(
                        _GatherExcitePlus("ge0", channels, 2, act_type))
                else:
                    raise Exception(
                        'Unsupported Squeeze Excitation Module: Choose either [None, "cSE", "sSE", "scSE"'
                    )

            self.body.add(BatchNorm(momentum=bn_mom))
            self.body.add(get_act(act_type))
            self.body.add(
                Conv2D(channels, kernel_size=1, padding=0, use_bias=False))
            self.body.add(BatchNorm(momentum=bn_mom))
            self.body.add(get_act(act_type))
            self.body.add(
                Conv2D(channels,
                       kernel_size=3,
                       padding=1,
                       groups=2,
                       use_bias=False))
            self.body.add(BatchNorm(momentum=bn_mom))
            self.body.add(get_act(act_type))
            #
            # # add a 1x1 branch after concatenation
            self.body.add(
                Conv2D(channels=channels,
                       kernel_size=1,
                       padding=0,
                       use_bias=False))

            if not self.dim_match:
                self.expander = HybridSequential(prefix="")
                self.expander.add(
                    Conv2D(channels=channels,
                           kernel_size=1,
                           use_bias=False,
                           prefix="expander_conv"))
                self.expander.add(BatchNorm())
コード例 #6
0
    def __init__(self,
                 name,
                 nb_in_channels,
                 groups=2,
                 se_type="cSE",
                 use_residual=True,
                 act_type="relu",
                 **kwargs):
        """
        Constructor
        :param name: Layer name 
        :param nb_in_channels: Number of input channels 
        :param groups: Number of groups to use for shuffling
        :param se_type: Squeez excitation type
        :param use_residual: True, if a residual connection shall be used
        :param act_type: Type for the activation function
        :param kwargs: 
        """

        super(_ShuffleBlockNeck, self).__init__(prefix=name + "_")

        self.in_channels = nb_in_channels
        self.nb_right_channels = nb_in_channels // 2

        self.groups = groups

        self.body = HybridSequential(prefix="")
        self.use_residual = use_residual
        with self.name_scope():
            self.body.add(
                Conv2D(channels=self.nb_right_channels,
                       kernel_size=1,
                       strides=1,
                       padding=0,
                       use_bias=False))
            self.body.add(BatchNorm())
            self.body.add(get_act(act_type))
            self.body.add(
                Conv2D(channels=self.nb_right_channels,
                       kernel_size=1,
                       strides=1,
                       padding=0,
                       groups=3,
                       use_bias=False))
            self.body.add(BatchNorm())
            self.body.add(get_act(act_type))
            if se_type:
                if se_type == "cSE":
                    self.body.add(
                        _ChannelSqueezeExcitation("se0",
                                                  self.nb_right_channels, 2,
                                                  act_type))
                elif se_type == "sSE":
                    self.body.add(_SpatialSqueezeExcitation("se0"))
                elif se_type == "scSE":
                    self.body.add(
                        _SpatialChannelSqueezeExcitation(
                            "se0", self.nb_right_channels, 2, act_type))
                else:
                    raise Exception(
                        'Unsupported Squeeze Excitation Module: Choose either [None, "cSE", "sSE", "scSE"'
                    )

        if self.use_residual:
            self.act = get_act(act_type)
        self.shufller = _ShuffleChannelsBlock(groups)