Python tensorflow.keras.layers.MaxPool2D() Examples

The following are 19 code examples of tensorflow.keras.layers.MaxPool2D(). 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: palm_detector.py    From blazepalm with Apache License 2.0 6 votes vote down vote up
def residual_block_id(self,tensor, feature_n,name=None):
        if name != None:
            depconv_1  = DepthwiseConv2D(3,2,padding='same',name=name+"/dconv")(tensor)
            conv_2     = Conv2D(feature_n,1,name=name+"/conv")(depconv_1)
        else:
            depconv_1  = DepthwiseConv2D(3,2,padding='same')(tensor)
            conv_2     = Conv2D(feature_n,1)(depconv_1)


        maxpool_1  = MaxPool2D(pool_size=(2,2),strides=(2,2),padding='same')(tensor)
        conv_zeros = Conv2D(feature_n/2,2,strides=2,use_bias=False,kernel_initializer=tf.zeros_initializer())(tensor)

        padding_1  = Concatenate(axis=-1)([maxpool_1,conv_zeros])#self.feature_padding(maxpool_1)

        add = Add()([padding_1,conv_2])
        relu = ReLU()(add)

        return relu
    
    #def feature_padding(self,tensor,channels_n=0):
    #    #pad = tf.keras.layers.ZeroPadding2D(((0,0),(0,0),(0,tensor.shape[3])))(tensor)
    #    return Concatenate(axis=3)([tensor,pad]) 
Example #2
Source File: CNN.py    From nn_builder with MIT License 6 votes vote down vote up
def create_and_append_layer(self, layer, list_to_append_layer_to, activation=None, output_layer=False):
        """Creates and appends a layer to the list provided"""
        layer_name = layer[0].lower()
        assert layer_name in self.valid_cnn_hidden_layer_types, "Layer name {} not valid, use one of {}".format(
            layer_name, self.valid_cnn_hidden_layer_types)
        if layer_name == "conv":
            list_to_append_layer_to.extend([Conv2D(filters=layer[1], kernel_size=layer[2],
                                                strides=layer[3], padding=layer[4], activation=activation,
                                                   kernel_initializer=self.initialiser_function)])
        elif layer_name == "maxpool":
            list_to_append_layer_to.extend([MaxPool2D(pool_size=(layer[1], layer[1]),
                                                   strides=(layer[2], layer[2]), padding=layer[3])])
        elif layer_name == "avgpool":
            list_to_append_layer_to.extend([AveragePooling2D(pool_size=(layer[1], layer[1]),
                                                   strides=(layer[2], layer[2]), padding=layer[3])])
        elif layer_name == "linear":
            list_to_append_layer_to.extend([Dense(layer[1], activation=activation, kernel_initializer=self.initialiser_function)])
        else:
            raise ValueError("Wrong layer name") 
Example #3
Source File: layers.py    From thundernet-tensorflow2.0 with MIT License 5 votes vote down vote up
def __init__(self, num_classes, first_channel=24, channels_per_stage=(132, 264, 528)):
        super(ShuffleNetv2, self).__init__(name="ShuffleNetv2")

        self.num_classes = num_classes

        self.conv1_bn_relu = Conv2D_BN_ReLU(first_channel, 3, 2)
        self.pool1 = MaxPool2D(3, strides=2, padding="SAME")
        self.stage2 = ShufflenetStage(first_channel, channels_per_stage[0], 4)
        self.stage3 = ShufflenetStage(channels_per_stage[0], channels_per_stage[1], 8)
        self.stage4 = ShufflenetStage(channels_per_stage[1], channels_per_stage[2], 4)
        #self.conv5_bn_relu = Conv2D_BN_ReLU(1024, 1, 1)
        self.gap = GlobalAveragePooling2D()
        self.linear = Dense(num_classes) 
Example #4
Source File: dla.py    From imgclsmob with MIT License 5 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 body_class=ResBlock,
                 return_down=False,
                 data_format="channels_last",
                 **kwargs):
        super(DLAResBlock, self).__init__(**kwargs)
        self.return_down = return_down
        self.downsample = (strides > 1)
        self.project = (in_channels != out_channels)

        self.body = body_class(
            in_channels=in_channels,
            out_channels=out_channels,
            strides=strides,
            data_format=data_format,
            name="body")
        self.activ = nn.ReLU()
        if self.downsample:
            self.downsample_pool = nn.MaxPool2D(
                pool_size=strides,
                strides=strides,
                data_format=data_format,
                name="downsample_pool")
        if self.project:
            self.project_conv = conv1x1_block(
                in_channels=in_channels,
                out_channels=out_channels,
                activation=None,
                data_format=data_format,
                name="project_conv") 
Example #5
Source File: CNN.py    From nn_builder with MIT License 5 votes vote down vote up
def __init__(self, layers_info, output_activation=None, hidden_activations="relu", dropout= 0.0, initialiser="default",
                 batch_norm=False, y_range=(), random_seed=0, input_dim=None):
        Model.__init__(self)
        self.valid_cnn_hidden_layer_types = {'conv', 'maxpool', 'avgpool', 'linear'}
        self.valid_layer_types_with_no_parameters = (MaxPool2D, AveragePooling2D)
        Base_Network.__init__(self, layers_info, output_activation, hidden_activations, dropout, initialiser,
                              batch_norm, y_range, random_seed, input_dim) 
Example #6
Source File: custom_unet.py    From stacks-usecase with Apache License 2.0 5 votes vote down vote up
def encode(filters, pool=False, norm=True):
    """downsample sequential model."""
    net = Seq()
    net.add(
        layers.Conv2D(
            filters, 3, strides=2, padding="same", kernel_initializer="he_normal"
        )
    )
    if pool:
        net.add(layers.MaxPool2D(pool_size=(2, 2)))
    if norm:
        net.add(layers.BatchNormalization())
    net.add(layers.ReLU())
    return net 
Example #7
Source File: mv2_hourglass.py    From tf2-mobile-pose-estimation with Apache License 2.0 5 votes vote down vote up
def _hourglass_module(input, stage_index, number_of_keypoints):
    if stage_index == 0:
        return _inverted_bottleneck(input, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3), []
    else:
        # down sample
        x = layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='SAME')(input)

        # block front
        x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
        x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
        x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
        x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
        x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)

        stage_index -= 1

        # block middle
        x, middle_layers = _hourglass_module(x, stage_index=stage_index, number_of_keypoints=number_of_keypoints)

        # block back
        x = _inverted_bottleneck(x, up_channel_rate=6, channels=number_of_keypoints, is_subsample=False, kernel_size=3)

        # up sample
        upsampling_size = (2, 2)  # (x.shape[1] * 2, x.shape[2] * 2)
        x = layers.UpSampling2D(size=upsampling_size, interpolation='bilinear')(x)
        upsampling_layer = x

        # jump layer
        x = _inverted_bottleneck(input, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
        x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
        x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
        x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
        x = _inverted_bottleneck(x, up_channel_rate=6, channels=number_of_keypoints, is_subsample=False, kernel_size=3)
        jump_branch_layer = x

        # add
        x = upsampling_layer + jump_branch_layer

        middle_layers.append(x)

        return x, middle_layers 
Example #8
Source File: module.py    From Centernet-Tensorflow2.0 with Apache License 2.0 5 votes vote down vote up
def base_module(inputs, outchannels):
    x = layers.Conv2D(128, 7, strides=2, padding="same")(inputs)
    x = layers.BatchNormalization()(x)
    x = layers.MaxPool2D(2, strides=2)(x)
    out = res_layer1(x, outchannels)
    return out 
Example #9
Source File: pipeline_train.py    From models with Apache License 2.0 5 votes vote down vote up
def keras_model():
    from tensorflow.keras.layers import Conv2D, MaxPool2D, Flatten, Dense, Dropout, Input

    inputs = Input(shape=(28, 28, 1))
    x = Conv2D(32, (3, 3),activation='relu', padding='valid')(inputs)
    x = MaxPool2D(pool_size=(2, 2))(x)
    x = Conv2D(64, (3, 3), activation='relu')(x)
    x = MaxPool2D(pool_size=(2, 2))(x)
    x = Flatten()(x)
    x = Dense(512, activation='relu')(x)
    x = Dropout(0.5)(x)
    outputs = Dense(_NUM_CLASSES, activation='softmax')(x)

    return tf.keras.Model(inputs, outputs) 
Example #10
Source File: densenet.py    From DeepPoseKit with Apache License 2.0 5 votes vote down vote up
def __init__(self, compression_factor=0.5, pool_size=2, **kwargs):
        # super(TransitionDown, self).__init__(self, **kwargs)
        self.concat = Concatenate()
        self.compression_factor = compression_factor
        self.pool = layers.MaxPool2D(pool_size) 
Example #11
Source File: hourglass.py    From DeepPoseKit with Apache License 2.0 5 votes vote down vote up
def __init__(self, filters, n_downsample, bottleneck_factor=2):
        self.filters = filters
        self.bottleneck_factor = bottleneck_factor
        n_downsample = n_downsample - 1
        self.n_downsample = int(np.maximum(0, n_downsample))

        self.conv_7x7 = Conv2D(
            filters,
            (7, 7),
            strides=(2, 2),
            padding="same",
            activation="relu",
            use_bias=False,
        )

        self.res_blocks = []
        self.pool_layers = []
        for idx in range(n_downsample):
            res_block = ResidualBlock(filters, bottleneck_factor)
            max_pool = MaxPool2D(pool_size=(2, 2), strides=(2, 2))
            self.res_blocks.append(res_block)
            self.pool_layers.append(max_pool)

        self.res_output = [
            ResidualBlock(filters, bottleneck_factor),
            ResidualBlock(filters, bottleneck_factor),
        ] 
Example #12
Source File: module.py    From Centernet-Tensorflow2.0 with Apache License 2.0 5 votes vote down vote up
def down_module(inputs, out1, out2):
    x = layers.MaxPool2D(2, 2)(inputs)
    x = res_layer0(x, out1)
    out = res_layer1(x, out2)
    return out 
Example #13
Source File: mv2_cpm.py    From tf2-mobile-pose-estimation with Apache License 2.0 4 votes vote down vote up
def _mobilenetV2(input):
    x = _inverted_bottleneck(input, up_channel_rate=1, channels=12, is_subsample=False, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=1, channels=12, is_subsample=False, kernel_size=3)
    mv2_branch_0 = x

    x = _inverted_bottleneck(x, up_channel_rate=6, channels=18, is_subsample=True, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=18, is_subsample=False, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=18, is_subsample=False, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=18, is_subsample=False, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=18, is_subsample=False, kernel_size=3)
    mv2_branch_1 = x

    x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=True, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
    mv2_branch_2 = x

    x = _inverted_bottleneck(x, up_channel_rate=6, channels=48, is_subsample=True, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=48, is_subsample=False, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=48, is_subsample=False, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=48, is_subsample=False, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=48, is_subsample=False, kernel_size=3)
    mv2_branch_3 = x

    x = _inverted_bottleneck(x, up_channel_rate=6, channels=72, is_subsample=True, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=72, is_subsample=False, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=72, is_subsample=False, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=72, is_subsample=False, kernel_size=3)
    x = _inverted_bottleneck(x, up_channel_rate=6, channels=72, is_subsample=False, kernel_size=3)
    mv2_branch_4 = x

    x = layers.Concatenate(axis=3)([
        layers.MaxPool2D(pool_size=(4, 4), strides=(4, 4), padding='SAME')(mv2_branch_0),
        layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='SAME')(mv2_branch_1),
        mv2_branch_2,
        layers.UpSampling2D(size=(2, 2), interpolation='bilinear')(mv2_branch_3),
        layers.UpSampling2D(size=(4, 4), interpolation='bilinear')(mv2_branch_4),
    ])

    return x 
Example #14
Source File: layers.py    From ssd-tf2 with MIT License 4 votes vote down vote up
def create_vgg16_layers():
    vgg16_conv4 = [
        layers.Conv2D(64, 3, padding='same', activation='relu'),
        layers.Conv2D(64, 3, padding='same', activation='relu'),
        layers.MaxPool2D(2, 2, padding='same'),

        layers.Conv2D(128, 3, padding='same', activation='relu'),
        layers.Conv2D(128, 3, padding='same', activation='relu'),
        layers.MaxPool2D(2, 2, padding='same'),

        layers.Conv2D(256, 3, padding='same', activation='relu'),
        layers.Conv2D(256, 3, padding='same', activation='relu'),
        layers.Conv2D(256, 3, padding='same', activation='relu'),
        layers.MaxPool2D(2, 2, padding='same'),

        layers.Conv2D(512, 3, padding='same', activation='relu'),
        layers.Conv2D(512, 3, padding='same', activation='relu'),
        layers.Conv2D(512, 3, padding='same', activation='relu'),
        layers.MaxPool2D(2, 2, padding='same'),

        layers.Conv2D(512, 3, padding='same', activation='relu'),
        layers.Conv2D(512, 3, padding='same', activation='relu'),
        layers.Conv2D(512, 3, padding='same', activation='relu'),
    ]

    x = layers.Input(shape=[None, None, 3])
    out = x
    for layer in vgg16_conv4:
        out = layer(out)

    vgg16_conv4 = tf.keras.Model(x, out)

    vgg16_conv7 = [
        # Difference from original VGG16:
        # 5th maxpool layer has kernel size = 3 and stride = 1
        layers.MaxPool2D(3, 1, padding='same'),
        # atrous conv2d for 6th block
        layers.Conv2D(1024, 3, padding='same',
                      dilation_rate=6, activation='relu'),
        layers.Conv2D(1024, 1, padding='same', activation='relu'),
    ]

    x = layers.Input(shape=[None, None, 512])
    out = x
    for layer in vgg16_conv7:
        out = layer(out)

    vgg16_conv7 = tf.keras.Model(x, out)

    return vgg16_conv4, vgg16_conv7 
Example #15
Source File: hourglass.py    From DeepPoseKit with Apache License 2.0 4 votes vote down vote up
def __init__(self, filters, bottleneck_factor, n_downsample, n_upsample=None):
        self.filters = filters
        self.bottleneck_factor = bottleneck_factor
        self.n_downsample = n_downsample
        if n_upsample:
            self.n_upsample = n_upsample
        else:
            self.n_upsample = n_downsample

        self.down_res_blocks = []
        self.pool_layers = []
        for idx in range(self.n_downsample):
            res_block = ResidualBlock(filters, bottleneck_factor)
            max_pool = MaxPool2D(pool_size=(2, 2), strides=(2, 2))
            self.down_res_blocks.append(res_block)
            self.pool_layers.append(max_pool)

        self.skip_bottleneck = ResidualBlock(filters, bottleneck_factor)
        self.bottleneck_layers = [
            ResidualBlock(filters, bottleneck_factor),
            ResidualBlock(filters, bottleneck_factor),
            ResidualBlock(filters, bottleneck_factor),
        ]
        self.bottleneck_identity_bn = BatchNormalization()
        self.bottleneck_skip_bn = BatchNormalization()
        self.bottleneck_add = Add()

        self.up_res_blocks = []
        self.skip_res_blocks = []
        self.upsample_layers = []
        self.skip_bn_layers = []
        self.add_bn_layers = []
        self.add_layers = []
        for idx in range(self.n_upsample):
            res_block = ResidualBlock(filters, bottleneck_factor)
            self.up_res_blocks.append(res_block)

            res_block = ResidualBlock(filters, bottleneck_factor)
            self.skip_res_blocks.append(res_block)

            upsample = UpSampling2D(size=(2, 2))
            self.upsample_layers.append(upsample)

            add_layer = Add()
            self.add_layers.append(add_layer)

            bn_layer = BatchNormalization()
            self.skip_bn_layers.append(bn_layer)

            bn_layer = BatchNormalization()
            self.add_bn_layers.append(bn_layer) 
Example #16
Source File: model.py    From DexiNed with MIT License 4 votes vote down vote up
def __init__(self,rgb_mean=None,
                 **kwargs):
        super(DexiNed, self).__init__(**kwargs)
        self.rgbn_mean = rgb_mean
        self.block_1 = DoubleConvBlock(32, 64, stride=(2,2),use_act=False)
        self.block_2 = DoubleConvBlock(128,use_act=False)
        self.dblock_3 = _DenseBlock(2, 256)
        self.dblock_4 = _DenseBlock(3, 512)
        self.dblock_5 = _DenseBlock(3, 512)
        self.dblock_6 = _DenseBlock(3, 256)
        self.maxpool = layers.MaxPool2D(pool_size=(3, 3), strides=2, padding='same')

        # first skip connection
        self.side_1 = SingleConvBlock(128,k_size=(1,1),stride=(2,2),use_bs=True,
                                      w_init=weight_init)
        self.side_2 = SingleConvBlock(256,k_size=(1,1),stride=(2,2),use_bs=True,
                                      w_init=weight_init)
        self.side_3 = SingleConvBlock(512,k_size=(1,1),stride=(2,2),use_bs=True,
                                      w_init=weight_init)
        self.side_4 = SingleConvBlock(512,k_size=(1,1),stride=(1,1),use_bs=True,
                                      w_init=weight_init)
        # self.side_5 = SingleConvBlock(256,k_size=(1,1),stride=(1,1),use_bs=True,
        #                               w_init=weight_init)


        self.pre_dense_2 = SingleConvBlock(256,k_size=(1,1),stride=(2,2),
                                      w_init=weight_init) # use_bn=True
        self.pre_dense_3 = SingleConvBlock(256,k_size=(1,1),stride=(1,1),use_bs=True,
                                      w_init=weight_init)
        self.pre_dense_4 = SingleConvBlock(512,k_size=(1,1),stride=(1,1),use_bs=True,
                                      w_init=weight_init)
        self.pre_dense_5_0 = SingleConvBlock(512, k_size=(1,1),stride=(2,2),
                                      w_init=weight_init) # use_bn=True
        self.pre_dense_5 = SingleConvBlock(512,k_size=(1,1),stride=(1,1),use_bs=True,
                                      w_init=weight_init)
        self.pre_dense_6 = SingleConvBlock(256,k_size=(1,1),stride=(1,1),use_bs=True,
                                      w_init=weight_init)

        self.up_block_1 = UpConvBlock(1)
        self.up_block_2 = UpConvBlock(1)
        self.up_block_3 = UpConvBlock(2)
        self.up_block_4 = UpConvBlock(3)
        self.up_block_5 = UpConvBlock(4)
        self.up_block_6 = UpConvBlock(4)

        self.block_cat = SingleConvBlock(
            1,k_size=(1,1),stride=(1,1),
            w_init=tf.constant_initializer(1/5)) 
Example #17
Source File: core.py    From crepe with MIT License 4 votes vote down vote up
def build_and_load_model(model_capacity):
    """
    Build the CNN model and load the weights

    Parameters
    ----------
    model_capacity : 'tiny', 'small', 'medium', 'large', or 'full'
        String specifying the model capacity, which determines the model's
        capacity multiplier to 4 (tiny), 8 (small), 16 (medium), 24 (large),
        or 32 (full). 'full' uses the model size specified in the paper,
        and the others use a reduced number of filters in each convolutional
        layer, resulting in a smaller model that is faster to evaluate at the
        cost of slightly reduced pitch estimation accuracy.

    Returns
    -------
    model : tensorflow.keras.models.Model
        The pre-trained keras model loaded in memory
    """
    from tensorflow.keras.layers import Input, Reshape, Conv2D, BatchNormalization
    from tensorflow.keras.layers import MaxPool2D, Dropout, Permute, Flatten, Dense
    from tensorflow.keras.models import Model

    if models[model_capacity] is None:
        capacity_multiplier = {
            'tiny': 4, 'small': 8, 'medium': 16, 'large': 24, 'full': 32
        }[model_capacity]

        layers = [1, 2, 3, 4, 5, 6]
        filters = [n * capacity_multiplier for n in [32, 4, 4, 4, 8, 16]]
        widths = [512, 64, 64, 64, 64, 64]
        strides = [(4, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1)]

        x = Input(shape=(1024,), name='input', dtype='float32')
        y = Reshape(target_shape=(1024, 1, 1), name='input-reshape')(x)

        for l, f, w, s in zip(layers, filters, widths, strides):
            y = Conv2D(f, (w, 1), strides=s, padding='same',
                       activation='relu', name="conv%d" % l)(y)
            y = BatchNormalization(name="conv%d-BN" % l)(y)
            y = MaxPool2D(pool_size=(2, 1), strides=None, padding='valid',
                          name="conv%d-maxpool" % l)(y)
            y = Dropout(0.25, name="conv%d-dropout" % l)(y)

        y = Permute((2, 1, 3), name="transpose")(y)
        y = Flatten(name="flatten")(y)
        y = Dense(360, activation='sigmoid', name="classifier")(y)

        model = Model(inputs=x, outputs=y)

        package_dir = os.path.dirname(os.path.realpath(__file__))
        filename = "model-{}.h5".format(model_capacity)
        model.load_weights(os.path.join(package_dir, filename))
        model.compile('adam', 'binary_crossentropy')

        models[model_capacity] = model

    return models[model_capacity] 
Example #18
Source File: model.py    From CRNN.tf2 with MIT License 4 votes vote down vote up
def build_model(num_classes, image_width=None, channels=1):
    """
    build CNN-RNN model
    """
    def vgg_style(input_tensor):
        """
        The original feature extraction structure from CRNN paper.
        Related paper: https://ieeexplore.ieee.org/abstract/document/7801919
        """
        x = layers.Conv2D(
            filters=64, 
            kernel_size=3, 
            padding='same',
            activation='relu')(input_tensor)
        x = layers.MaxPool2D(pool_size=2, padding='same')(x)

        x = layers.Conv2D(
            filters=128, 
            kernel_size=3, 
            padding='same',
            activation='relu')(x)
        x = layers.MaxPool2D(pool_size=2, padding='same')(x)

        x = layers.Conv2D(filters=256, kernel_size=3, padding='same')(x)
        x = layers.BatchNormalization()(x)
        x = layers.Activation('relu')(x)
        x = layers.Conv2D(filters=256, kernel_size=3, padding='same',
                          activation='relu')(x)
        x = layers.MaxPool2D(pool_size=(2, 2), strides=(2, 1), 
                             padding='same')(x)

        x = layers.Conv2D(filters=512, kernel_size=3, padding='same')(x)
        x = layers.BatchNormalization()(x)
        x = layers.Activation('relu')(x)
        x = layers.Conv2D(filters=512, kernel_size=3, padding='same',
                          activation='relu')(x)
        x = layers.MaxPool2D(pool_size=(2, 2), strides=(2, 1), 
                             padding='same')(x)

        x = layers.Conv2D(filters=512, kernel_size=2)(x)
        x = layers.BatchNormalization()(x)
        x = layers.Activation('relu')(x)
        return x

    img_input = keras.Input(shape=(32, image_width, channels))
    x = vgg_style(img_input)
    x = layers.Reshape((-1, 512))(x)

    x = layers.Bidirectional(layers.LSTM(units=256, return_sequences=True))(x)
    x = layers.Bidirectional(layers.LSTM(units=256, return_sequences=True))(x)
    x = layers.Dense(units=num_classes)(x)
    return keras.Model(inputs=img_input, outputs=x, name='CRNN') 
Example #19
Source File: chemnet_layers.py    From deepchem with MIT License 4 votes vote down vote up
def _build_layer_components(self):
    """Builds the layers components and set _layers attribute."""
    self.max_pool1 = MaxPool2D(pool_size=(3, 3), strides=2, padding="valid")

    self.conv_block1 = [
        Conv2D(
            int(self.num_filters * 1.5),
            kernel_size=(3, 3),
            strides=2,
            padding="valid",
            activation=tf.nn.relu)
    ]

    self.conv_block2 = [
        Conv2D(
            filters=self.num_filters,
            kernel_size=1,
            strides=1,
            activation=tf.nn.relu,
            padding="same")
    ]
    self.conv_block2.append(
        Conv2D(
            filters=self.num_filters,
            kernel_size=3,
            strides=1,
            activation=tf.nn.relu,
            padding="same"))
    self.conv_block2.append(
        Conv2D(
            filters=int(self.num_filters * 1.5),
            kernel_size=3,
            strides=2,
            activation=tf.nn.relu,
            padding="valid"))

    self.concat_layer = Concatenate()
    self.activation_layer = ReLU()

    self._layers = self.conv_block1 + self.conv_block2
    self._layers.extend(
        [self.max_pool1, self.concat_layer, self.activation_layer])