Python tensorflow.keras.layers.ReLU() Examples

The following are 30 code examples of tensorflow.keras.layers.ReLU(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow.keras.layers , or try the search function .
Example #1
Source File: model.py    From DexiNed with MIT License 7 votes vote down vote up
def __init__(self, out_features,**kwargs):
        super(_DenseLayer, self).__init__(**kwargs)
        k_reg = None if w_decay is None else l2(w_decay)
        self.layers = []
        self.layers.append(tf.keras.Sequential(
            [
                layers.ReLU(),
                layers.Conv2D(
                    filters=out_features, kernel_size=(3,3), strides=(1,1), padding='same',
                    use_bias=True, kernel_initializer=weight_init,
                kernel_regularizer=k_reg),
                layers.BatchNormalization(),
                layers.ReLU(),
                layers.Conv2D(
                    filters=out_features, kernel_size=(3,3), strides=(1,1), padding='same',
                    use_bias=True, kernel_initializer=weight_init,
                    kernel_regularizer=k_reg),
                layers.BatchNormalization(),
            ])) # first relu can be not needed 
Example #2
Source File: layers.py    From CartoonGan-tensorflow with Apache License 2.0 6 votes vote down vote up
def __init__(self,
                 filters,
                 kernel_size,
                 norm_type="instance",
                 pad_type="constant",
                 **kwargs):
        super(ResBlock, self).__init__(name="ResBlock")
        padding = (kernel_size - 1) // 2
        padding = (padding, padding)
        self.model = tf.keras.models.Sequential()
        self.model.add(get_padding(pad_type, padding))
        self.model.add(Conv2D(filters, kernel_size))
        self.model.add(get_norm(norm_type))
        self.model.add(ReLU())
        self.model.add(get_padding(pad_type, padding))
        self.model.add(Conv2D(filters, kernel_size))
        self.model.add(get_norm(norm_type))
        self.add = Add() 
Example #3
Source File: squeezenet.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 padding,
                 data_format="channels_last",
                 **kwargs):
        super(FireConv, self).__init__(**kwargs)
        self.conv = Conv2d(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=kernel_size,
            padding=padding,
            data_format=data_format,
            name="conv")
        self.activ = nn.ReLU() 
Example #4
Source File: polynet.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 strides,
                 padding,
                 num_blocks,
                 data_format="channels_last",
                 **kwargs):
        super(PolyConv, self).__init__(**kwargs)
        self.conv = Conv2d(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=kernel_size,
            strides=strides,
            padding=padding,
            use_bias=False,
            data_format=data_format,
            name="conv")
        self.bns = []
        for i in range(num_blocks):
            self.bns.append(BatchNorm(
                data_format=data_format,
                name="bn{}".format(i + 1)))
        self.activ = nn.ReLU() 
Example #5
Source File: polynet.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 scale,
                 res_block,
                 num_blocks,
                 pre_block,
                 data_format="channels_last",
                 **kwargs):
        super(PolyResidual, self).__init__(**kwargs)
        assert (num_blocks >= 1)
        self.scale = scale

        self.pre_block = pre_block(
            num_blocks=num_blocks,
            data_format=data_format,
            name="pre_block")
        self.res_blocks = [res_block(
            data_format=data_format,
            name="res_block{}".format(i + 1)) for i in range(num_blocks)]
        self.activ = nn.ReLU() 
Example #6
Source File: model.py    From DexiNed with MIT License 6 votes vote down vote up
def __init__(self, mid_features, out_features=None, stride=(1,1),
                 use_bn=True,use_act=True,**kwargs):
        super(DoubleConvBlock, self).__init__(**kwargs)
        self.use_bn =use_bn
        self.use_act =use_act
        out_features = mid_features if out_features is None else out_features
        k_reg = None if w_decay is None else l2(w_decay)

        self.conv1 = layers.Conv2D(
            filters=mid_features, kernel_size=(3, 3), strides=stride, padding='same',
        use_bias=True, kernel_initializer=weight_init,
        kernel_regularizer=k_reg)
        self.bn1 = layers.BatchNormalization()

        self.conv2 = layers.Conv2D(
            filters=out_features, kernel_size=(3, 3), padding='same',strides=(1,1),
        use_bias=True, kernel_initializer=weight_init,
        kernel_regularizer=k_reg)
        self.bn2 = layers.BatchNormalization()
        self.relu = layers.ReLU() 
Example #7
Source File: sknet.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 data_format="channels_last",
                 **kwargs):
        super(SKNetUnit, self).__init__(**kwargs)
        self.resize_identity = (in_channels != out_channels) or (strides != 1)

        self.body = SKNetBottleneck(
            in_channels=in_channels,
            out_channels=out_channels,
            strides=strides,
            data_format=data_format,
            name="body")
        if self.resize_identity:
            self.identity_conv = conv1x1_block(
                in_channels=in_channels,
                out_channels=out_channels,
                strides=strides,
                activation=None,
                data_format=data_format,
                name="identity_conv")
        self.activ = nn.ReLU() 
Example #8
Source File: dla.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 residual,
                 data_format="channels_last",
                 **kwargs):
        super(DLARoot, self).__init__(**kwargs)
        self.residual = residual
        self.data_format = data_format

        self.conv = conv1x1_block(
            in_channels=in_channels,
            out_channels=out_channels,
            activation=None,
            data_format=data_format,
            name="conv")
        self.activ = nn.ReLU() 
Example #9
Source File: xception.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 data_format="channels_last",
                 **kwargs):
        super(XceptionFinalBlock, self).__init__(**kwargs)
        self.conv1 = dws_conv3x3_block(
            in_channels=1024,
            out_channels=1536,
            activate=False,
            data_format=data_format,
            name="conv1")
        self.conv2 = dws_conv3x3_block(
            in_channels=1536,
            out_channels=2048,
            activate=True,
            data_format=data_format,
            name="conv2")
        self.activ = nn.ReLU()
        self.pool = AvgPool2d(
            pool_size=10,
            strides=1,
            data_format=data_format,
            name="pool") 
Example #10
Source File: layers.py    From CartoonGan-tensorflow with Apache License 2.0 6 votes vote down vote up
def __init__(self,
                 filters,
                 kernel_size,
                 stride=1,
                 norm_type="instance",
                 pad_type="constant",
                 **kwargs):
        super(ConvBlock, self).__init__(name="ConvBlock")
        padding = (kernel_size - 1) // 2
        padding = (padding, padding)

        self.model = tf.keras.models.Sequential()
        self.model.add(get_padding(pad_type, padding))
        self.model.add(Conv2D(filters, kernel_size, stride))
        self.model.add(get_padding(pad_type, padding))
        self.model.add(Conv2D(filters, kernel_size))
        self.model.add(get_norm(norm_type))
        self.model.add(ReLU()) 
Example #11
Source File: layers.py    From CartoonGan-tensorflow with Apache License 2.0 6 votes vote down vote up
def __init__(self,
                 filters,  # NOTE: will be filters // 2
                 norm_type="instance",
                 pad_type="constant",
                 **kwargs):
        super(DownShuffleUnitV2, self).__init__(name="DownShuffleUnitV2")
        filters //= 2
        self.r_model = tf.keras.models.Sequential([
            Conv2D(filters, 1, use_bias=False),
            get_norm(norm_type),
            ReLU(),
            DepthwiseConv2D(3, 2, 'same', use_bias=False),
            get_norm(norm_type),
            Conv2D(filters, 1, use_bias=False),
        ])
        self.l_model = tf.keras.models.Sequential([
            DepthwiseConv2D(3, 2, 'same', use_bias=False),
            get_norm(norm_type),
            Conv2D(filters, 1, use_bias=False),
        ])
        self.bn_act = tf.keras.models.Sequential([
            get_norm(norm_type),
            ReLU(),
        ]) 
Example #12
Source File: inceptionv4.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 strides,
                 padding,
                 data_format="channels_last",
                 **kwargs):
        super(InceptConv, self).__init__(**kwargs)
        self.conv = Conv2d(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=kernel_size,
            strides=strides,
            padding=padding,
            use_bias=False,
            data_format=data_format,
            name="conv")
        self.bn = BatchNorm(
            momentum=0.1,
            epsilon=1e-3,
            data_format=data_format,
            name="bn")
        self.activ = nn.ReLU() 
Example #13
Source File: dpn.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 strides,
                 padding,
                 groups,
                 data_format="channels_last",
                 **kwargs):
        super(DPNConv, self).__init__(**kwargs)
        self.bn = dpn_batch_norm(
            channels=in_channels,
            data_format=data_format,
            name="bn")
        self.activ = nn.ReLU()
        self.conv = Conv2d(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=kernel_size,
            strides=strides,
            padding=padding,
            groups=groups,
            use_bias=False,
            data_format=data_format,
            name="conv") 
Example #14
Source File: wrn.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 strides,
                 padding,
                 activate,
                 data_format="channels_last",
                 **kwargs):
        super(WRNConv, self).__init__(**kwargs)
        self.activate = activate

        self.conv = Conv2d(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=kernel_size,
            strides=strides,
            padding=padding,
            use_bias=True,
            data_format=data_format,
            name="conv")
        if self.activate:
            self.activ = nn.ReLU() 
Example #15
Source File: preresnet.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 data_format="channels_last",
                 **kwargs):
        super(PreResInitBlock, self).__init__(**kwargs)
        self.conv = Conv2d(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=7,
            strides=2,
            padding=3,
            use_bias=False,
            data_format=data_format,
            name="conv")
        self.bn = BatchNorm(
            data_format=data_format,
            name="bn")
        self.activ = nn.ReLU()
        self.pool = MaxPool2d(
            pool_size=3,
            strides=2,
            padding=1,
            name="pool") 
Example #16
Source File: nasnet.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 data_format="channels_last",
                 **kwargs):
        super(NasPathBlock, self).__init__(**kwargs)
        self.data_format = data_format
        mid_channels = out_channels // 2

        self.activ = nn.ReLU()
        self.path1 = NasPathBranch(
            in_channels=in_channels,
            out_channels=mid_channels,
            data_format=data_format,
            name="path1")
        self.path2 = NasPathBranch(
            in_channels=in_channels,
            out_channels=mid_channels,
            extra_padding=True,
            data_format=data_format,
            name="path2")
        self.bn = nasnet_batch_norm(
            channels=out_channels,
            data_format=data_format,
            name="bn") 
Example #17
Source File: layers.py    From CartoonGan-tensorflow with Apache License 2.0 6 votes vote down vote up
def __init__(self,
                 filters,  # NOTE: will be filters // 2
                 norm_type="instance",
                 pad_type="constant",
                 **kwargs):
        super(BasicShuffleUnitV2, self).__init__(name="BasicShuffleUnitV2")
        filters //= 2
        self.model = tf.keras.models.Sequential([
            Conv2D(filters, 1, use_bias=False),
            get_norm(norm_type),
            ReLU(),
            DepthwiseConv2D(3, padding='same', use_bias=False),
            get_norm(norm_type),
            Conv2D(filters, 1, use_bias=False),
            get_norm(norm_type),
            ReLU(),
        ]) 
Example #18
Source File: nasnet.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 strides,
                 padding,
                 groups,
                 data_format="channels_last",
                 **kwargs):
        super(NasConv, self).__init__(**kwargs)
        self.activ = nn.ReLU()
        self.conv = Conv2d(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=kernel_size,
            strides=strides,
            padding=padding,
            groups=groups,
            use_bias=False,
            data_format=data_format,
            name="conv")
        self.bn = nasnet_batch_norm(
            channels=out_channels,
            data_format=data_format,
            name="bn") 
Example #19
Source File: pyramidnet.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 data_format="channels_last",
                 **kwargs):
        super(PyrInitBlock, self).__init__(**kwargs)
        self.conv = Conv2d(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=7,
            strides=2,
            padding=3,
            use_bias=False,
            data_format=data_format,
            name="conv")
        self.bn = BatchNorm(
            data_format=data_format,
            name="bn")
        self.activ = nn.ReLU()
        self.pool = MaxPool2d(
            pool_size=3,
            strides=2,
            padding=1,
            data_format=data_format,
            name="pool") 
Example #20
Source File: diracnetv2.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 strides,
                 padding,
                 data_format="channels_last",
                 **kwargs):
        super(DiracConv, self).__init__(**kwargs)
        self.activ = nn.ReLU()
        self.conv = Conv2d(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=kernel_size,
            strides=strides,
            padding=padding,
            use_bias=True,
            data_format=data_format,
            name="conv") 
Example #21
Source File: shufflenet.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 data_format="channels_last",
                 **kwargs):
        super(ShuffleInitBlock, self).__init__(**kwargs)
        self.conv = conv3x3(
            in_channels=in_channels,
            out_channels=out_channels,
            strides=2,
            data_format=data_format,
            name="conv")
        self.bn = BatchNorm(
            # in_channels=out_channels,
            data_format=data_format,
            name="bn")
        self.activ = nn.ReLU()
        self.pool = MaxPool2d(
            pool_size=3,
            strides=2,
            padding=1,
            data_format=data_format,
            name="pool") 
Example #22
Source File: xception.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 strides,
                 padding,
                 activate,
                 data_format="channels_last",
                 **kwargs):
        super(DwsConvBlock, self).__init__(**kwargs)
        self.activate = activate

        if self.activate:
            self.activ = nn.ReLU()
        self.conv = DwsConv(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=kernel_size,
            strides=strides,
            padding=padding,
            data_format=data_format,
            name="conv")
        self.bn = BatchNorm(
            data_format=data_format,
            name="bn") 
Example #23
Source File: module.py    From Centernet-Tensorflow2.0 with Apache License 2.0 5 votes vote down vote up
def convblock(inputs, outchannels, kernel_size):

    x = layers.Conv2D(outchannels, kernel_size, padding='same', use_bias=False)(inputs)
    x = layers.ReLU(max_value=6)(x)
    out = layers.BatchNormalization()(x)
    return out 
Example #24
Source File: basic.py    From autokeras with MIT License 5 votes vote down vote up
def build(self, hp, inputs=None):
        inputs = nest.flatten(inputs)
        utils.validate_num_inputs(inputs, 1)
        input_node = inputs[0]
        output_node = input_node
        output_node = reduction.Flatten().build(hp, output_node)

        num_layers = self.num_layers or hp.Choice('num_layers', [1, 2, 3], default=2)
        use_batchnorm = self.use_batchnorm
        if use_batchnorm is None:
            use_batchnorm = hp.Boolean('use_batchnorm', default=False)
        if self.dropout_rate is not None:
            dropout_rate = self.dropout_rate
        else:
            dropout_rate = hp.Choice('dropout_rate', [0.0, 0.25, 0.5], default=0)

        for i in range(num_layers):
            units = hp.Choice(
                'units_{i}'.format(i=i),
                [16, 32, 64, 128, 256, 512, 1024],
                default=32)
            output_node = layers.Dense(units)(output_node)
            if use_batchnorm:
                output_node = layers.BatchNormalization()(output_node)
            output_node = layers.ReLU()(output_node)
            if dropout_rate > 0:
                output_node = layers.Dropout(dropout_rate)(output_node)
        return output_node 
Example #25
Source File: ibnbresnet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 strides,
                 padding,
                 dilation=1,
                 groups=1,
                 use_bias=False,
                 activate=True,
                 data_format="channels_last",
                 **kwargs):
        super(IBNbConvBlock, self).__init__(**kwargs)
        self.activate = activate

        self.conv = Conv2d(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=kernel_size,
            strides=strides,
            padding=padding,
            dilation=dilation,
            groups=groups,
            use_bias=use_bias,
            data_format=data_format,
            name="conv")
        self.inst_norm = InstanceNorm(
            scale=True,
            data_format=data_format,
            name="inst_norm")
        if self.activate:
            self.activ = nn.ReLU() 
Example #26
Source File: ibnbresnet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 use_inst_norm,
                 data_format="channels_last",
                 **kwargs):
        super(IBNbResUnit, self).__init__(**kwargs)
        self.use_inst_norm = use_inst_norm
        self.resize_identity = (in_channels != out_channels) or (strides != 1)

        self.body = ResBottleneck(
            in_channels=in_channels,
            out_channels=out_channels,
            strides=strides,
            conv1_stride=False,
            data_format=data_format,
            name="body")
        if self.resize_identity:
            self.identity_conv = conv1x1_block(
                in_channels=in_channels,
                out_channels=out_channels,
                strides=strides,
                activation=None,
                data_format=data_format,
                name="identity_conv")
        if self.use_inst_norm:
            self.inst_norm = InstanceNorm(
                scale=True,
                data_format=data_format,
                name="inst_norm")
        self.activ = nn.ReLU() 
Example #27
Source File: get_activations_test.py    From keract with MIT License 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(NestedLayer, self).__init__(*args, **kwargs)
        self.fc = Dense(10, name='fc1')
        self.relu = ReLU(name='relu') 
Example #28
Source File: get_activations_test.py    From keract with MIT License 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(NestedModel, self).__init__(*args, **kwargs)
        self.fc = Dense(10, name='fc1')
        self.relu = ReLU(name='relu') 
Example #29
Source File: nasnet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 strides,
                 padding,
                 extra_padding=False,
                 data_format="channels_last",
                 **kwargs):
        super(NasDwsConv, self).__init__(**kwargs)
        self.extra_padding = extra_padding
        self.data_format = data_format

        self.activ = nn.ReLU()
        self.conv = DwsConv(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=kernel_size,
            strides=strides,
            padding=padding,
            use_bias=False,
            data_format=data_format,
            name="conv")
        self.bn = nasnet_batch_norm(
            channels=out_channels,
            data_format=data_format,
            name="bn")
        if self.extra_padding:
            self.pad = nn.ZeroPadding2D(
                padding=((1, 0), (1, 0)),
                data_format=data_format) 
Example #30
Source File: model.py    From EfficientDet with Apache License 2.0 5 votes vote down vote up
def ConvBlock(num_channels, kernel_size, strides, name, freeze_bn=False):
    f1 = layers.Conv2D(num_channels, kernel_size=kernel_size, strides=strides, padding='same',
                       use_bias=True, name='{}_conv'.format(name))
    f2 = layers.BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON, name='{}_bn'.format(name))
    # f2 = BatchNormalization(freeze=freeze_bn, name='{}_bn'.format(name))
    f3 = layers.ReLU(name='{}_relu'.format(name))
    return reduce(lambda f, g: lambda *args, **kwargs: g(f(*args, **kwargs)), (f1, f2, f3))