Exemplo n.º 1
0
    def _gru_ctc_init(self):
        self.input_data = layers.Input(name='the_input', shape=(self.AUDIO_LENGTH, self.AUDIO_FEATURE_LENGTH, 1))
        layers_h1 = layers.Reshape((-1, 200))(self.input_data)
        layers_h2 = GRUCTCAM._dense(128, layers_h1)
        layers_h3 = GRUCTCAM._bi_gru(64, layers_h2)
        y_pred = GRUCTCAM._dense(self.OUTPUT_SIZE, layers_h3, activation='softmax')

        self.gru_model = models.Model(inputs=self.input_data, outputs=y_pred)

        self.labels = layers.Input(name='the_label', shape=[self.LABEL_SEQUENCE_LENGTH], dtype='float32')
        self.input_length = layers.Input(name='input_length', shape=[1], dtype='int64')
        self.label_length = layers.Input(name='label_length', shape=[1], dtype='int64')
        self.loss = layers.Lambda(function=self._ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred,
                                                                                                  self.labels,
                                                                                                  self.input_length,
                                                                                                  self.label_length])

        self.ctc_model = models.Model(inputs=[self.input_data, self.labels, self.input_length, self.label_length],
                                      outputs=self.loss)
        optimizer = optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, decay=0.0, epsilon=10e-8)

        self.ctc_model.compile(optimizer=optimizer, loss={'ctc': lambda y_true, y_pred: y_pred})
        print('[*Info] Create Model Successful, Compiles Model Successful. ')

        return self.gru_model, self.ctc_model
Exemplo n.º 2
0
    def grouped_convolution(y, nb_channels, _strides):
        # when `cardinality` == 1 this is just a standard convolution
        if cardinality == 1:
            return layers.Conv2D(nb_channels,
                                 kernel_size=(3, 3),
                                 strides=_strides,
                                 padding='same')(y)

        assert not nb_channels % cardinality
        _d = nb_channels // cardinality

        # in a grouped convolution layer, input and output channels are divided into `cardinality` groups,
        # and convolutions are separately performed within each group
        groups = []
        for j in range(cardinality):
            group = layers.Lambda(lambda z: z[:, :, :, j * _d:j * _d + _d])(y)
            groups.append(
                layers.Conv2D(_d,
                              kernel_size=(3, 3),
                              strides=_strides,
                              padding='same')(group))

        # the grouped convolutional layer concatenates them as the outputs of the layer
        y = layers.concatenate(groups)

        return y
Exemplo n.º 3
0
    def build_model(self):
        """Build an actor (policy) network that maps states -> actions."""
        # Define input layer (states)
        states = layers.Input(shape=(self.state_size, ), name='states')
        '''# Add hidden layers
        net = layers.Dense(units=32, activation='relu')(states)
        net = layers.Dense(units=64, activation='relu')(net)
        net = layers.Dense(units=32, activation='relu')(net)
        
        # Try different layer sizes, activations, add batch normalization, regularizers, etc.

        # Add final output layer with sigmoid activation
        raw_actions = layers.Dense(units=self.action_size, activation='sigmoid',
            name='raw_actions')(net)
        '''
        ###################################
        # Add hidden layers
        net = layers.Dense(units=400,
                           kernel_regularizer=regularizers.l2(1e-6))(states)
        net = layers.BatchNormalization()(net)
        net = layers.LeakyReLU(1e-2)(net)
        net = layers.Dense(units=300,
                           kernel_regularizer=regularizers.l2(1e-6))(net)
        net = layers.BatchNormalization()(net)
        net = layers.LeakyReLU(1e-2)(net)

        # Add final output layer with sigmoid activation
        raw_actions = layers.Dense(
            units=self.action_size,
            activation='sigmoid',
            name='raw_actions',
            kernel_initializer=initializers.RandomUniform(minval=-0.003,
                                                          maxval=0.003))(net)
        #######################################

        # Scale [0, 1] output for each action dimension to proper range
        actions = layers.Lambda(lambda x:
                                (x * self.action_range) + self.action_low,
                                name='actions')(raw_actions)

        # Create Keras model
        self.model = models.Model(inputs=states, outputs=actions)

        # Define loss function using action value (Q value) gradients
        action_gradients = layers.Input(shape=(self.action_size, ))
        loss = K.mean(-action_gradients * actions)

        # Incorporate any additional losses here (e.g. from regularizers)

        # Define optimizer and training function
        optimizer = optimizers.Adam(lr=1e-6)
        updates_op = optimizer.get_updates(params=self.model.trainable_weights,
                                           loss=loss)
        self.train_fn = K.function(
            inputs=[self.model.input, action_gradients,
                    K.learning_phase()],
            outputs=[],
            updates=updates_op)
Exemplo n.º 4
0
def keras_efficientnet(blocks_args, global_params, training=False):
    inp = layers.Input((224, 224, 3))
    x = layers.Conv2D(32, 3, padding='same', strides=2, name='stem_conv2d', use_bias=False)(inp)
    x = em.batchnorm(name='stem_tpu_batch_normalization')(x)
    x = layers.Lambda(lambda x: em.relu_fn(x))(x)
    idx = 0
    for block in blocks_args:
        x = el.mbConvBlock(x, block, global_params, idx, training=training)
        # x = MBConvBlock(block, global_params, idx)(x, training=training)
        idx += 1
        if block.num_repeat > 1:
            block = block._replace(
                input_filters=block.output_filters, strides=[1, 1])
        for _ in range(block.num_repeat - 1):
            x = el.mbConvBlock(x, block, global_params, idx, training=training)
            idx += 1
    x = layers.Conv2D(1280, 1, name='head_conv2d', use_bias=False)(x)
    x = em.batchnorm(name='head_tpu_batch_normalization')(x)
    x = layers.Lambda(lambda x: em.relu_fn(x))(x)
    x = layers.GlobalAveragePooling2D()(x)
    x = layers.Dense(1000, activation='softmax', name='head_dense', )(x)
    model = models.Model(inp, x, name='efficientnet-b0')
    return model
Exemplo n.º 5
0
def expend_as(tensor, rep):
    return layers.Lambda(
        lambda x, repnum: K.repeat_elements(x, repnum, axis=3),
        arguments={'repnum': rep})(tensor)
Exemplo n.º 6
0
    def create_model(self, img_shape, num_class):

        concat_axis = 3
        inputs = layers.Input(shape=img_shape)
        scale = layers.Lambda(lambda x: x / 255)(inputs)
        conv1 = layers.Conv2D(32, (3, 3),
                              activation='relu',
                              padding='same',
                              name='conv1_1')(scale)
        conv1 = layers.Dropout(0.1)(conv1)
        conv1 = layers.Conv2D(32, (3, 3), activation='relu',
                              padding='same')(conv1)
        pool1 = layers.MaxPooling2D(pool_size=(2, 2))(conv1)
        conv2 = layers.Conv2D(64, (3, 3), activation='relu',
                              padding='same')(pool1)
        conv2 = layers.Dropout(0.1)(conv2)
        conv2 = layers.Conv2D(64, (3, 3), activation='relu',
                              padding='same')(conv2)
        pool2 = layers.MaxPooling2D(pool_size=(2, 2))(conv2)

        conv3 = layers.Conv2D(128, (3, 3), activation='relu',
                              padding='same')(pool2)
        conv3 = layers.Dropout(0.1)(conv3)
        conv3 = layers.Conv2D(128, (3, 3), activation='relu',
                              padding='same')(conv3)
        pool3 = layers.MaxPooling2D(pool_size=(2, 2))(conv3)

        conv4 = layers.Conv2D(256, (3, 3), activation='relu',
                              padding='same')(pool3)
        conv4 = layers.Dropout(0.1)(conv4)
        conv4 = layers.Conv2D(256, (3, 3), activation='relu',
                              padding='same')(conv4)
        pool4 = layers.MaxPooling2D(pool_size=(2, 2))(conv4)

        conv5 = layers.Conv2D(512, (3, 3), activation='relu',
                              padding='same')(pool4)
        conv5 = layers.Dropout(0.1)(conv5)
        conv5 = layers.Conv2D(512, (3, 3), activation='relu',
                              padding='same')(conv5)

        up_conv5 = layers.UpSampling2D(size=(2, 2))(conv5)
        ch, cw = self.get_crop_shape(conv4, up_conv5)
        crop_conv4 = layers.Cropping2D(cropping=(ch, cw))(conv4)
        up6 = layers.concatenate([up_conv5, crop_conv4], axis=concat_axis)
        conv6 = layers.Conv2D(256, (3, 3), activation='relu',
                              padding='same')(up6)
        conv6 = layers.Dropout(0.1)(conv6)
        conv6 = layers.Conv2D(256, (3, 3), activation='relu',
                              padding='same')(conv6)

        up_conv6 = layers.UpSampling2D(size=(2, 2))(conv6)
        ch, cw = self.get_crop_shape(conv3, up_conv6)
        crop_conv3 = layers.Cropping2D(cropping=(ch, cw))(conv3)
        up7 = layers.concatenate([up_conv6, crop_conv3], axis=concat_axis)
        conv7 = layers.Conv2D(128, (3, 3), activation='relu',
                              padding='same')(up7)
        conv7 = layers.Dropout(0.1)(conv7)
        conv7 = layers.Conv2D(128, (3, 3), activation='relu',
                              padding='same')(conv7)

        up_conv7 = layers.UpSampling2D(size=(2, 2))(conv7)
        ch, cw = self.get_crop_shape(conv2, up_conv7)
        crop_conv2 = layers.Cropping2D(cropping=(ch, cw))(conv2)
        up8 = layers.concatenate([up_conv7, crop_conv2], axis=concat_axis)
        conv8 = layers.Conv2D(64, (3, 3), activation='relu',
                              padding='same')(up8)
        conv8 = layers.Dropout(0.1)(conv8)
        conv8 = layers.Conv2D(64, (3, 3), activation='relu',
                              padding='same')(conv8)

        up_conv8 = layers.UpSampling2D(size=(2, 2))(conv8)
        ch, cw = self.get_crop_shape(conv1, up_conv8)
        crop_conv1 = layers.Cropping2D(cropping=(ch, cw))(conv1)
        up9 = layers.concatenate([up_conv8, crop_conv1], axis=concat_axis)
        conv9 = layers.Conv2D(32, (3, 3), activation='relu',
                              padding='same')(up9)
        conv9 = layers.Dropout(0.1)(conv9)
        conv9 = layers.Conv2D(32, (3, 3), activation='relu',
                              padding='same')(conv9)

        ch, cw = self.get_crop_shape(inputs, conv9)
        conv9 = layers.ZeroPadding2D(padding=((ch[0], ch[1]), (cw[0],
                                                               cw[1])))(conv9)
        conv10 = layers.Conv2D(num_class, (1, 1), activation='sigmoid')(conv9)

        model = models.Model(inputs=inputs, outputs=conv10)

        return model
def mbConvBlock(inputs,
                block_args,
                global_params,
                idx,
                training=True,
                drop_connect_rate=None):
    filters = block_args.input_filters * block_args.expand_ratio
    batch_norm_momentum = global_params.batch_norm_momentum
    batch_norm_epsilon = global_params.batch_norm_epsilon
    has_se = (block_args.se_ratio is not None) and (
        block_args.se_ratio > 0) and (block_args.se_ratio <= 1)
    x = inputs
    # block_name = 'efficientnet-b0_' + 'blocks_' + str(idx) + '_'
    block_name = 'blocks_' + str(idx) + '_'
    project_conv_name = block_name + 'conv2d'
    project_bn_name = block_name + 'tpu_batch_normalization_1'
    ndbn_name = block_name + 'tpu_batch_normalization'
    if block_args.expand_ratio != 1:
        # Expansion phase:
        expand_conv = layers.Conv2D(
            filters,
            kernel_size=[1, 1],
            strides=[1, 1],
            kernel_initializer=em.conv_kernel_initializer,
            padding='same',
            use_bias=False,
            name=project_conv_name)(x)
        bn0 = em.batchnorm(momentum=batch_norm_momentum,
                           epsilon=batch_norm_epsilon,
                           name=ndbn_name)(expand_conv)
        project_conv_name = block_name + 'conv2d_1'
        ndbn_name = block_name + 'tpu_batch_normalization_1'
        project_bn_name = block_name + 'tpu_batch_normalization_2'

        x = layers.Lambda(lambda x: em.relu_fn(x))(bn0)

    kernel_size = block_args.kernel_size
    # Depth-wise convolution phase:

    depthwise_conv = em.utils.DepthwiseConv2D(
        [kernel_size, kernel_size],
        strides=block_args.strides,
        depthwise_initializer=em.conv_kernel_initializer,
        padding='same',
        use_bias=False,
        name=block_name + 'depthwise_conv2d')(x)
    bn1 = em.batchnorm(momentum=batch_norm_momentum,
                       epsilon=batch_norm_epsilon,
                       name=ndbn_name)(depthwise_conv)
    x = layers.Lambda(lambda x: em.relu_fn(x))(bn1)

    if has_se:
        num_reduced_filters = max(
            1, int(block_args.input_filters * block_args.se_ratio))
        # Squeeze and Excitation layer.
        se_tensor = ReduceMean()(x)

        se_reduce = layers.Conv2D(
            num_reduced_filters,
            kernel_size=[1, 1],
            strides=[1, 1],
            kernel_initializer=em.conv_kernel_initializer,
            padding='same',
            name=block_name + 'se_' + 'conv2d',
            use_bias=True)(se_tensor)
        se_reduce = layers.Lambda(lambda x: em.relu_fn(x))(se_reduce)
        se_expand = layers.Conv2D(
            filters,
            kernel_size=[1, 1],
            strides=[1, 1],
            kernel_initializer=em.conv_kernel_initializer,
            padding='same',
            name=block_name + 'se_' + 'conv2d_1',
            use_bias=True)(se_reduce)
        x = SigmoidMul()([x, se_expand])

    # Output phase:
    filters = block_args.output_filters
    project_conv = layers.Conv2D(filters,
                                 kernel_size=[1, 1],
                                 strides=[1, 1],
                                 kernel_initializer=em.conv_kernel_initializer,
                                 padding='same',
                                 name=project_conv_name,
                                 use_bias=False)(x)
    x = em.batchnorm(momentum=batch_norm_momentum,
                     epsilon=batch_norm_epsilon,
                     name=project_bn_name)(project_conv)
    # x = layers.Lambda(lambda x: em.relu_fn(x))(bn2)
    if block_args.id_skip:
        if all(s == 1 for s in block_args.strides
               ) and block_args.input_filters == block_args.output_filters:
            # only apply drop_connect if skip presents.
            if drop_connect_rate:
                x = em.utils.drop_connect(x, training, drop_connect_rate)
            x = layers.add([x, inputs])
    return x
    def _cnn_ctc_init(self):
        self.input_data = layers.Input(name='the_input',
                                       shape=(self.AUDIO_LENGTH,
                                              self.AUDIO_FEATURE_LENGTH, 1))

        layers_h1 = layers.Conv2D(filters=32,
                                  kernel_size=(3, 3),
                                  use_bias=False,
                                  activation='relu',
                                  padding='same',
                                  kernel_initializer='he_normal')(
                                      self.input_data)
        layers_h1 = layers.Dropout(rate=0.05)(layers_h1)

        layers_h2 = layers.Conv2D(filters=32,
                                  kernel_size=(3, 3),
                                  use_bias=True,
                                  activation='relu',
                                  padding='same',
                                  kernel_initializer='he_normal')(layers_h1)

        layers_h3 = layers.MaxPooling2D(pool_size=2,
                                        strides=None,
                                        padding='valid')(layers_h2)
        layers_h3 = layers.Dropout(rate=0.05)(layers_h3)

        layers_h4 = layers.Conv2D(filters=64,
                                  kernel_size=(3, 3),
                                  use_bias=True,
                                  activation='relu',
                                  padding='same',
                                  kernel_initializer='he_normal')(layers_h3)
        layers_h4 = layers.Dropout(rate=0.1)(layers_h4)

        layers_h5 = layers.Conv2D(filters=64,
                                  kernel_size=(3, 3),
                                  use_bias=True,
                                  activation='relu',
                                  padding='same',
                                  kernel_initializer='he_normal')(layers_h4)

        layers_h6 = layers.MaxPooling2D(pool_size=2,
                                        strides=None,
                                        padding='valid')(layers_h5)
        layers_h6 = layers.Dropout(rate=0.1)(layers_h6)

        layers_h7 = layers.Conv2D(filters=128,
                                  kernel_size=(3, 3),
                                  use_bias=True,
                                  activation='relu',
                                  padding='same',
                                  kernel_initializer='he_normal')(layers_h6)
        layers_h7 = layers.Dropout(rate=0.15)(layers_h7)

        layers_h8 = layers.Conv2D(filters=128,
                                  kernel_size=(3, 3),
                                  use_bias=True,
                                  activation='relu',
                                  padding='same',
                                  kernel_initializer='he_normal')(layers_h7)

        layers_h9 = layers.MaxPooling2D(pool_size=2,
                                        strides=None,
                                        padding='valid')(layers_h8)
        layers_h9 = layers.Dropout(rate=0.15)(layers_h9)

        layers_h10 = layers.Conv2D(filters=128,
                                   kernel_size=(3, 3),
                                   use_bias=True,
                                   activation='relu',
                                   padding='same',
                                   kernel_initializer='he_normal')(layers_h9)
        layers_h10 = layers.Dropout(rate=0.2)(layers_h10)

        layers_h11 = layers.Conv2D(filters=128,
                                   kernel_size=(3, 3),
                                   use_bias=True,
                                   activation='relu',
                                   padding='same',
                                   kernel_initializer='he_normal')(layers_h10)

        layers_h12 = layers.MaxPooling2D(pool_size=1,
                                         strides=None,
                                         padding='valid')(layers_h11)

        layers_h12 = layers.Dropout(rate=0.2)(layers_h12)

        layers_h13 = layers.Conv2D(filters=128,
                                   kernel_size=(3, 3),
                                   use_bias=True,
                                   activation='relu',
                                   padding='same',
                                   kernel_initializer='he_normal')(layers_h12)
        layers_h13 = layers.Dropout(rate=0.2)(layers_h13)

        layers_h14 = layers.Conv2D(filters=128,
                                   kernel_size=(3, 3),
                                   use_bias=True,
                                   activation='relu',
                                   padding='same',
                                   kernel_initializer='he_normal')(layers_h13)

        layers_h15 = layers.MaxPooling2D(pool_size=1,
                                         strides=None,
                                         padding='valid')(layers_h14)

        layers_h16 = layers.Reshape(
            (self.AUDIO_FEATURE_LENGTH, self.AUDIO_LENGTH * 2))(layers_h15)
        layers_h16 = layers.Dropout(rate=0.3)(layers_h16)

        layers_h17 = layers.Dense(units=128,
                                  use_bias=True,
                                  activation='relu',
                                  kernel_initializer='he_normal')(layers_h16)
        layers_h17 = layers.Dropout(rate=0.3)(layers_h17)

        layers_h18 = layers.Dense(units=self.OUTPUT_SIZE,
                                  use_bias=True,
                                  kernel_initializer='he_normal')(layers_h17)

        y_pred = layers.Activation('softmax', name='activation_0')(layers_h18)

        self.cnn_model = models.Model(inputs=self.input_data, outputs=y_pred)

        self.labels = layers.Input(name='the_label',
                                   shape=[self.LABEL_SEQUENCE_LENGTH],
                                   dtype='float32')
        self.input_length = layers.Input(name='input_length',
                                         shape=[1],
                                         dtype='int64')
        self.label_length = layers.Input(name='label_length',
                                         shape=[1],
                                         dtype='int64')
        self.loss = layers.Lambda(function=self._ctc_lambda_func,
                                  output_shape=(1, ),
                                  name='ctc')([
                                      y_pred, self.labels, self.input_length,
                                      self.label_length
                                  ])

        self.ctc_model = models.Model(inputs=[
            self.input_data, self.labels, self.input_length, self.label_length
        ],
                                      outputs=self.loss)
        optimizer = optimizers.Adam(lr=0.0001,
                                    beta_1=0.9,
                                    beta_2=0.999,
                                    decay=0.0,
                                    epsilon=10e-8)
        self.ctc_model.compile(optimizer=optimizer,
                               loss={
                                   'ctc': lambda y_true, y_pred: y_pred
                               })
        print('[*Info] Create Model Successful, Compiles Model Successful. ')

        return self.cnn_model, self.ctc_model