Пример #1
0
    def define_model(self):
        input_img = Input(shape=(self.model_parameters.img_height,
                                 self.model_parameters.img_width,
                                 self.model_parameters.num_channels))

        x = layers.Conv2D(filters=32,
                          kernel_size=(3, 3),
                          strides=(1, 1),
                          padding='same')(input_img)
        x = layers.BatchNormalization()(x)
        x = layers.LeakyReLU()(x)

        x = layers.Conv2D(filters=32,
                          kernel_size=(3, 3),
                          strides=(1, 1),
                          padding='same')(x)
        x = layers.BatchNormalization()(x)
        x = layers.LeakyReLU()(x)

        x = layers.Conv2D(filters=32,
                          kernel_size=(3, 3),
                          strides=(1, 1),
                          padding='same')(x)
        x = layers.BatchNormalization()(x)
        x = layers.LeakyReLU()(x)

        model = Model(name=self.model_name, inputs=input_img, outputs=x)

        return model
    def get_model(self, numpy_matrix_embeddings: numpy.ndarray, **kwargs) -> Model:
        # get default parameters
        dropout = kwargs.get('dropout', 0.9)
        assert isinstance(dropout, float)
        lstm_layer_size = kwargs.get('lstm_layer_size', 64)
        assert isinstance(lstm_layer_size, int)

        # this is the placeholder tensor for the input sequences
        # input sequence is a numpy array of word indices
        input_sequence = Input(shape=(None,), dtype='int32', name='input_sequence')
        # this embedding layer will transform the sequences of integers
        # into vectors of size dimension of embeddings
        embedded = Embedding(numpy_matrix_embeddings.shape[0], numpy_matrix_embeddings.shape[1],
                             weights=[numpy_matrix_embeddings], mask_zero=True)(input_sequence)

        # bidirectional LSTM using the neat Keras wrapper
        lstm_output1 = Bidirectional(LSTM(lstm_layer_size, return_sequences=True))(embedded)

        # stack the next one
        lstm_output2 = Bidirectional(LSTM(lstm_layer_size))(lstm_output1)

        # dropout
        dropout_layer = Dropout(dropout)(lstm_output2)

        # one extra dense layer
        fully_connected = Dense(lstm_layer_size / 2, activation='relu')(dropout_layer)

        # classification
        output = Dense(2, activation='softmax')(fully_connected)
        model = Model(inputs=[input_sequence], outputs=output)

        print("Compiling model")
        model.compile('adam', loss=categorical_crossentropy)

        return model
Пример #3
0
def create_preprocessing_model(
        output_shape=(224, 224, 3), model_variant="senet50"):
    """Preprocessing model. Use this as the first model to preprocess images before using the original models.
    Alternatively, preprocessing can be done using numpy/ PIL and on Android, Android.graphics.bitmap.createBitmap, but
    they're are not consistent.

    The values used as arguments to [DepthwiseNormalization] were taken from [keras_vggface.utils.preprocess_input],
    specifically, the version 2 parameters. This means this function/ preprocessing model only supports
    RESNET50 and SENET50, since version 1 is used for VGG16.

    Args:
        model_variant: either vgg16, resnet50 or senet50.
        output_shape: The output shape for the processing model, which should match the input
        shape of the subsequent model, formatted with channels-last

    Returns:
        model: Keras model containing layers to preprocess images as per the original keras-vggface/utils.preprocess_inputs
    """
    input_shape = (None, None, 3)
    input = Input(shape=input_shape, batch_size=1, name="input_image")

    x = ChannelReversal()(input)
    x = Resizing(output_shape[0],
                 output_shape[1],
                 interpolation='bilinear',
                 name="Resize")(x)
    if model_variant == "senet50" or model_variant == "resnet50":
        output = DepthwiseNormalization([91.4953, 103.8827, 131.0912])(x)
    elif model_variant == "vgg16":
        output = DepthwiseNormalization([93.5940, 104.7624, 129.1863])(x)
    else:
        raise ValueError(f"Unsupported model_variant: {model_variant}")

    model = Model(input, output, name='preprocessing')
    return model
Пример #4
0
def darknet4det4(input_shape, output_channels):
    inputs = Input(shape=(input_shape[0], input_shape[1], 3))
    darknet = Model(inputs, darknet_body(inputs))

    # downsample
    subsample_x = compose(
        DarknetConv2D_BN_Leaky(1024, (3, 3), strides=(1, 1)),
        DarknetConv2D_BN_Leaky(512, (3, 3), strides=(1, 1)),
        DarknetConv2D_BN_Leaky(512, (3, 3), strides=(2, 2), padding='SAME')
    )(darknet.output)
    _, y4 = make_last_layers(subsample_x, 512, output_channels, output_layer_name="output4")

    x, y1 = make_last_layers(darknet.output, 512, output_channels, output_layer_name='output1')
    # print(type(x))

    # upsmaple
    x = compose(
        DarknetConv2D_BN_Leaky(256, (1, 1)),
        UpSampling2D(2))(x)
    x = Concatenate()([x, darknet.layers[152].output])
    x, y2 = make_last_layers(x, 256, output_channels, output_layer_name='output2')

    # upsample
    x = compose(
        DarknetConv2D_BN_Leaky(128, (1, 1)),
        UpSampling2D(2))(x)
    x = Concatenate()([x, darknet.layers[92].output])
    x, y3 = make_last_layers(x, 128, output_channels, output_layer_name='output3')

    # order in descending feature size
    return Model(inputs, [y3, y2, y1, y4])
Пример #5
0
def ResneXt_IN(input_shape=None,
               cardinality=8,
               weight_decay=5e-4,
               classes=None):
    # TODO 主函数
    # model = ResneXt_IN((1, 11, 11, 200), cardinality=8, classes=16)

    # 判断底层,tf,channel在最后
    _handle_dim_ordering()

    if len(input_shape) != 4:
        raise Exception(
            "Input shape should be a tuple (nb_channels, kernel_dim1, kernel_dim2, kernel_dim3)"
        )

    print('original input shape:', input_shape)
    # orignal input shape(1,11,11,200)

    if K.image_data_format() == 'channels_last':
        input_shape = (input_shape[1], input_shape[2], input_shape[3],
                       input_shape[0])
    print('change input shape:', input_shape)

    # TODO 数据输入
    input = Input(shape=input_shape)

    # TODO 网络搭建
    x_dense = __create_res_next(classes, input, cardinality, weight_decay)

    model = Model(input, x_dense, name='resnext_IN')

    return model
Пример #6
0
def make_deep_autoencoder(dims,
                          act='relu',
                          init='glorot_uniform',
                          layer_decorator: typing.Optional[typing.Callable[
                              [Layer, bool], Layer]] = None,
                          compile=True) -> typing.Tuple[Model, Model]:
    r"""Build a fully-connected symmetric autoencoder model.

    :param dims: Per-layer neuron counts for the entire AE.
    :param act: Activation function for all neurons (except input and output layers)
    :param init: Initialization for the weights of every neuron.
    :param layer_decorator: Callable that can modify every layer of the network.
    :param compile: Compile the model now or leave it up to the caller.
    :return: (ae_model, encoder_model), Model of autoencoder and model of encoder
    """
    input_img = Input(shape=(dims[0], ), name='input')
    n_stacks = len(dims) - 1
    assert n_stacks > 0

    x = input_img

    # internal layers in encoder
    for i in range(n_stacks - 1):
        x = Dense(dims[i + 1],
                  activation=act,
                  kernel_initializer=init,
                  name='encoder_%d' % i)(x)
        if layer_decorator:
            x = layer_decorator(x, is_output=False)

    # encoder output layer (the last hidden encoding layer in the autoencoder)
    # features are extracted from here
    x = Dense(dims[-1],
              kernel_initializer=init,
              name='encoder_%d' % (n_stacks - 1))(x)
    if layer_decorator:
        x = layer_decorator(x, is_output=False)
    encoded = x

    # internal layers in decoder
    for i in range(n_stacks - 1, 0, -1):
        x = Dense(dims[i],
                  activation=act,
                  kernel_initializer=init,
                  name='decoder_%d' % i)(x)
        if layer_decorator:
            x = layer_decorator(x, is_output=False)

    # output
    x = Dense(dims[0], kernel_initializer=init, name='decoder_0')(x)
    if layer_decorator:
        x = layer_decorator(x, is_output=True)
    decoded = x

    encoder = Model(inputs=input_img, outputs=encoded, name='encoder')
    autoencoder = Model(inputs=input_img, outputs=decoded, name='AE')
    if compile:
        autoencoder.compile(optimizer='adadelta', loss='mse')
    return autoencoder, encoder
Пример #7
0
    def build_train_model(self):
        shared_model = self.base_network_build_fn()
        # shared_model = build_multi_attention_model(self.input_steps)
        # shared_model = build_stacked_rnn_model(self)
        t_status = shared_model.output
        p = layers.Dense(self.action_num, activation='softmax',
                         name='p')(t_status)

        r = Input(shape=(self.action_num, ), name='r')
        q = Input(shape=(self.action_num, ), name='q')

        loss = PPO_Loss()([r, q, p])
        train_input = shared_model.input + [r, q]
        model = Model(train_input, loss, name=self.model_type)
        # 减小学习率,避免策略改变的太快
        model.compile(optimizer=Adam(lr=self.lr), loss=None)
        return model
Пример #8
0
 def _build_net(self):
     inputs = Input(shape=(self.n_features,))
     x = Dense(32, activation='relu', kernel_regularizer=l2(self.l2))(inputs)
     x = Dense(16, activation='relu', kernel_regularizer=l2(self.l2))(x)
     output = Dense(self.n_actions, activation='softmax', kernel_regularizer=l2(self.l2))(x)
     self.model = Model(inputs=inputs, outputs=output)
     self.model.compile(optimizer=Adam(learning_rate=self.lr), loss=tf.keras.losses.SparseCategoricalCrossentropy(),
                        metrics=['accuracy'])
Пример #9
0
def policy_model(input_dim=(6, 6, 1), depth=5) -> Model:
    inp = Input(input_dim)
    conv = conv_layer(inp)
    res = conv
    for _ in range(depth):
        res = residual_layer(res)
    pol = policy_layer(res)
    return Model(inputs=inp, outputs=pol)
Пример #10
0
 def testMultiHeadAttentionModelOutputShape(self):
     max_sequence_length = 50
     embedding_size = 64
     inputs = Input(shape=[max_sequence_length, embedding_size])
     g = attention.multihead_attention_model(inputs)
     actual_output_shape = g.shape
     expected_output_shape = (1, 28, 28, 1)
     self.assertEqual(actual_output_shape, expected_output_shape)
Пример #11
0
    def get_model(self):
        input = Input(self.maxlen)
        embedding = self.embedding_layer(input)
        x = Bidirectional(self._RNN(128))(embedding)  # LSTM or GRU

        output = Dense(self.num_class, activation=self.last_activation)(x)
        model = Model(inputs=input, outputs=output)
        return model
Пример #12
0
def stage_5(mult=1):
    x = inputs = Input([None, None, 3])
    x = darknet_conv(x, 32 * mult, 3)
    x = blocking_convolution(x, 64 * mult, 1)
    x = blocking_convolution(x, 128 * mult, 2)
    x = x_36 = blocking_convolution(x, 256 * mult, 8)
    x = x_61 = blocking_convolution(x, 512 * mult, 8)
    return tf.keras.Model(inputs, x)
Пример #13
0
def build_model(dim, max_seq, n_input_voca, n_output_voca):
    #W2 = tf.keras.layers.Dense(n_output_voca, use_bias=False)
    #W2 = K.random_normal_variable(shape=(n_output_voca, dim), mean=0, scale=1)
    np_val = np.reshape(np.random.normal(size=n_output_voca * dim),
                        [n_output_voca, dim])
    W2 = K.constant(np_val)

    code_id = Input(shape=[1], name=str_code_id)
    token_ids = Input(shape=(max_seq, ), dtype=tf.int32, name=str_desc_tokens)

    W1 = tf.keras.layers.Embedding(n_input_voca, dim)
    h0 = W1(code_id)

    #logits = W2(h)
    #logits = MyLayer(n_output_voca, dim, max_seq)(h)
    h = tf.reshape(h0, [-1, dim])
    h = tf.nn.l2_normalize(h, -1)
    W2 = tf.nn.l2_normalize(W2, -1)
    logits = tf.matmul(h, W2, transpose_b=True)
    #logits = tf.reshape(logits, [-1, max_seq, n_output_voca])

    log_probs = tf.nn.log_softmax(logits, axis=-1)

    y = tf.one_hot(token_ids,
                   depth=n_output_voca)  #[batch, max_seq, n_output_voca]

    pos_val = logits * y  # [ batch, max_seq, voca]
    neg_val = logits - tf.reduce_sum(pos_val, axis=1)  #[ batch, voca]
    t = tf.reduce_sum(pos_val, axis=2)  # [batch, max_seq]
    correct_map = tf.expand_dims(t, 2)  # [batch, max_seq, 1]
    print(correct_map.shape)
    #logits_correct = tf.expand_dims(tf.reduce_sum(logits * y, axis=-1), -1)
    wrong_map = tf.expand_dims(neg_val, 1)  # [batch, 1, voca]
    print(wrong_map.shape)
    t = wrong_map - correct_map + 1
    print(t.shape)
    loss = tf.reduce_mean(tf.math.maximum(t, 0), axis=-1)
    mask = tf.cast(tf.not_equal(token_ids, 0), tf.float32)  # batch, seq_len
    print(mask.shape)
    #    loss = -tf.reduce_sum(input_tensor=log_probs * y, axis=[-1])
    loss = mask * loss
    loss = tf.reduce_sum(loss, axis=1)  # over the sequence
    loss = tf.reduce_mean(loss)
    print(loss.shape)
    model = Model(inputs=[code_id, token_ids], outputs=h0)
    return loss, model
Пример #14
0
 def generate_categorical_input(self, column, column_config: Dict):
     hash_bucket_size, embedding_dimensions, feature_column = \
         self.create_categorical_column_with_hash_bucket(column, column_config)
     embedding_feature_column = \
         tf.feature_column.embedding_column(
             feature_column, dimension=embedding_dimensions)
     keras_input = Input(name=column.name, shape=[embedding_dimensions])
     return embedding_feature_column, keras_input, keras_input
Пример #15
0
def value_model(input_dim=(6, 6, 1), depth=5) -> Model:
    inp = Input(input_dim)
    conv = conv_layer(inp)
    res = conv
    for _ in range(depth):
        res = residual_layer(res)
    val = value_layer(res)
    return Model(inputs=inp, outputs=val)
Пример #16
0
def runRNN():
    # Assumes you're in the root level of the dataset directory.
    # If you aren't, you'll need to change the relative paths here.
    train_data = prepareData('./train')
    test_data = prepareData('./test')

    for text_batch, label_batch in train_data.take(1):
        print(text_batch.numpy()[0])
        print(label_batch.numpy()[0])  # 0 = negative, 1 = positive

    model = Sequential()

    # ----- 1. INPUT
    # We need this to use the TextVectorization layer next.
    model.add(Input(shape=(1,), dtype="string"))

    # ----- 2. TEXT VECTORIZATION
    # This layer processes the input string and turns it into a sequence of
    # max_len integers, each of which maps to a certain token.
    max_tokens = 1000
    max_len = 100
    vectorize_layer = TextVectorization(
        # Max vocab size. Any words outside of the max_tokens most common ones
        # will be treated the same way: as "out of vocabulary" (OOV) tokens.
        max_tokens=max_tokens,
        # Output integer indices, one per string token
        output_mode="int",
        # Always pad or truncate to exactly this many tokens
        output_sequence_length=max_len,
    )

    # Call adapt(), which fits the TextVectorization layer to our text dataset.
    # This is when the max_tokens most common words (i.e. the vocabulary) are selected.
    train_texts = train_data.map(lambda text, label: text)
    vectorize_layer.adapt(train_texts)

    model.add(vectorize_layer)

    # ----- 3. EMBEDDING
    # This layer turns each integer (representing a token) from the previous layer
    # an embedding. Note that we're using max_tokens + 1 here, since there's an
    # out-of-vocabulary (OOV) token that gets added to the vocab.
    model.add(Embedding(max_tokens + 1, 128))

    # ----- 4. RECURRENT LAYER
    model.add(LSTM(64))

    # ----- 5. DENSE HIDDEN LAYER
    model.add(Dense(64, activation="relu"))

    # ----- 6. OUTPUT
    model.add(Dense(1, activation="sigmoid"))

    # Compile and train the model.
    model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
    model.fit(train_data, epochs=1)

    model.save_weights('rnn')
Пример #17
0
    def define_model(self):
        input_img = Input(shape=[
            self.model_parameters.img_height,
            self.model_parameters.img_width,
            self.model_parameters.num_channels,
        ])
        class_id = Input(shape=[1])

        embedded_id = layers.Embedding(input_dim=10, output_dim=50)(class_id)
        embedded_id = layers.Dense(units=input_img.shape[1] *
                                   input_img.shape[2])(embedded_id)
        embedded_id = layers.Flatten()(embedded_id)

        x = layers.Conv2D(filters=128,
                          kernel_size=(3, 3),
                          strides=(1, 1),
                          padding='same')(input_img)
        x = layers.BatchNormalization(momentum=0.9)(x)
        x = layers.LeakyReLU(alpha=0.1)(x)

        x = layers.Conv2D(filters=128,
                          kernel_size=(4, 4),
                          strides=(2, 2),
                          padding='same')(x)
        x = layers.BatchNormalization(momentum=0.9)(x)
        x = layers.LeakyReLU(alpha=0.1)(x)

        x = layers.Conv2D(filters=128,
                          kernel_size=(4, 4),
                          strides=(2, 2),
                          padding='same')(x)
        x = layers.BatchNormalization(momentum=0.9)(x)
        x = layers.LeakyReLU(alpha=0.1)(x)
        x = layers.Flatten()(x)

        x = layers.Concatenate()([x, embedded_id])
        x = layers.Dense(units=512, activation='relu')(x)

        x = layers.Dense(units=1)(x)

        model = Model(name=self.model_name,
                      inputs=[input_img, class_id],
                      outputs=x)

        return model
 def test_invalid_variable_input(self):
   with context.eager_mode():
     inputs = Input(shape=(1,))
     outputs = testing_utils.Bias()(inputs)
     model = Model(inputs, outputs)
     with self.assertRaisesRegexp(
         ValueError,
         'Expected a symbolic Tensors or a callable for the loss value'):
       model.add_loss(model.weights[0])
Пример #19
0
 def test_invalid_constant_input(self):
     inputs = Input(shape=(1, ))
     outputs = testing_utils.Bias()(inputs)
     model = Model(inputs, outputs)
     with self.assertRaisesRegex(
             ValueError,
             'Expected a symbolic Tensors or a callable for the loss value'
     ):
         model.add_loss(1.)
Пример #20
0
    def yolo_conv(x_in):
        if isinstance(x_in, tuple):
            inputs = Input(x_in[0].shape[1:]), Input(x_in[1].shape[1:])
            x, x_skip = inputs

            # concat with skip connection
            x = DarknetConv(x, filters, 1)
            x = UpSampling2D(2)(x)
            x = Concatenate()([x, x_skip])
        else:
            x = inputs = Input(x_in.shape[1:])

        x = DarknetConv(x, filters, 1)
        x = DarknetConv(x, filters * 2, 3)
        x = DarknetConv(x, filters, 1)
        x = DarknetConv(x, filters * 2, 3)
        x = DarknetConv(x, filters, 1)
        return Model(inputs, x, name=name)(x_in)
Пример #21
0
    def __init__(self, game, encoder):
        """
        NNet model, copied from Othello NNet, with reduced fully connected layers fc1 and fc2 and reduced nnet_args.num_channels
        :param game: game configuration
        :param encoder: Encoder, used to encode game boards
        """
        from rts.src.config_class import CONFIG

        # game params
        self.board_x, self.board_y, num_encoders = game.getBoardSize()
        self.action_size = game.getActionSize()
        """
        num_encoders = CONFIG.nnet_args.encoder.num_encoders
        """
        num_encoders = encoder.num_encoders

        # Neural Net
        self.input_boards = Input(shape=(
            self.board_x, self.board_y,
            num_encoders))  # s: batch_size x board_x x board_y x num_encoders

        x_image = Reshape(
            (self.board_x, self.board_y, num_encoders)
        )(self.input_boards)  # batch_size  x board_x x board_y x num_encoders
        h_conv1 = Activation('relu')(BatchNormalization(axis=3)(Conv2D(
            CONFIG.nnet_args.num_channels, 3, padding='same', use_bias=False)(
                x_image)))  # batch_size  x board_x x board_y x num_channels
        h_conv2 = Activation('relu')(BatchNormalization(axis=3)(Conv2D(
            CONFIG.nnet_args.num_channels, 3, padding='same', use_bias=False)(
                h_conv1)))  # batch_size  x board_x x board_y x num_channels
        h_conv3 = Activation('relu')(
            BatchNormalization(axis=3)(Conv2D(CONFIG.nnet_args.num_channels,
                                              3,
                                              padding='valid',
                                              use_bias=False)(h_conv2))
        )  # batch_size  x (board_x-2) x (board_y-2) x num_channels
        h_conv4 = Activation('relu')(
            BatchNormalization(axis=3)(Conv2D(CONFIG.nnet_args.num_channels,
                                              3,
                                              padding='valid',
                                              use_bias=False)(h_conv3))
        )  # batch_size  x (board_x-4) x (board_y-4) x num_channels
        h_conv4_flat = Flatten()(h_conv4)
        s_fc1 = Dropout(CONFIG.nnet_args.dropout)(Activation('relu')(
            BatchNormalization(axis=1)(Dense(
                256, use_bias=False)(h_conv4_flat))))  # batch_size x 1024
        s_fc2 = Dropout(CONFIG.nnet_args.dropout)(Activation('relu')(
            BatchNormalization(axis=1)(Dense(
                128, use_bias=False)(s_fc1))))  # batch_size x 1024
        self.pi = Dense(self.action_size, activation='softmax',
                        name='pi')(s_fc2)  # batch_size x self.action_size
        self.v = Dense(1, activation='tanh', name='v')(s_fc2)  # batch_size x 1

        self.model = Model(inputs=self.input_boards, outputs=[self.pi, self.v])
        self.model.compile(
            loss=['categorical_crossentropy', 'mean_squared_error'],
            optimizer=Adam(CONFIG.nnet_args.lr))
Пример #22
0
def create_deep_autoencoder():
    input_shape = Input(shape=(INPUT_LENGTH, ))
    model = Model(input_shape, create_deep_decoder(create_deep_encoder(input_shape)))

    model.compile(optimizer=Adam(lr=0.0001), loss='binary_crossentropy')

    print(model.summary())

    return model
Пример #23
0
def Darknet(name=None):
    x = inputs = Input([None, None, 3])
    x = DarknetConv(x, 32, 3)
    x = DarknetBlock(x, 64, 1)
    x = DarknetBlock(x, 128, 2)  # skip connection
    x = x_36 = DarknetBlock(x, 256, 8)  # skip connection
    x = x_61 = DarknetBlock(x, 512, 8)
    x = DarknetBlock(x, 1024, 4)
    return tf.keras.Model(inputs, (x_36, x_61, x), name=name)
Пример #24
0
 def Build(self):
     # input layer is from GlobalAveragePooling:
     inLayer = Input([self.latentSize])
     # reexpand the input from flat:
     net = Dense(self.intermediateSize, activation='relu')(inLayer)
     net = Dense(np.prod(self.inputShape), activation='sigmoid')(net)
     net = Reshape(self.inputShape)(net)
     
     return Model(inLayer, net)
Пример #25
0
    def get_model(self):
        input = Input(self.maxlen)

        embedding = self.embedding_layer(input)
        x = GlobalAveragePooling1D()(embedding)

        output = Dense(self.num_class, activation=self.last_activation)(x)
        model = Model(inputs=input, outputs=output)
        return model
Пример #26
0
def build_model(fragment_length, nb_filters, nb_output_bins, dilation_depth, nb_stacks, use_skip_connections,
                learn_all_outputs, _log, desired_sample_rate, use_bias, res_l2, final_l2):
    def residual_block(x):
        original_x = x
        # TODO: initalization, regularization?
        # Note: The AtrousConvolution1D with the 'causal' flag is implemented in github.com/basveeling/keras#@wavenet.
        tanh_out = CausalAtrousConvolution1D(nb_filters, 2, dilation_rate=2 ** i, padding='valid', causal=True,
                                             use_bias=use_bias,
                                             name='dilated_conv_%d_tanh_s%d' % (2 ** i, s), activation='tanh',
                                             kernel_regularizer=l2(res_l2))(x)
        sigm_out = CausalAtrousConvolution1D(nb_filters, 2, dilation_rate=2 ** i, padding='valid', causal=True,
                                             use_bias=use_bias,
                                             name='dilated_conv_%d_sigm_s%d' % (2 ** i, s), activation='sigmoid',
                                             kernel_regularizer=l2(res_l2))(x)
        x = layers.Multiply(name='gated_activation_%d_s%d' % (i, s))([tanh_out, sigm_out])

        res_x = layers.Convolution1D(nb_filters, 1, padding='same', use_bias=use_bias,
                                     kernel_regularizer=l2(res_l2))(x)
        skip_x = layers.Convolution1D(nb_filters, 1, padding='same', use_bias=use_bias,
                                      kernel_regularizer=l2(res_l2))(x)
        res_x = layers.add([original_x, res_x])
        return res_x, skip_x

    input = Input(shape=(fragment_length, nb_output_bins), name='input_part')
    out = input
    skip_connections = []
    out = CausalAtrousConvolution1D(nb_filters, 2,
                                    dilation_rate=1,
                                    padding='valid',
                                    causal=True,
                                    name='initial_causal_conv'
                                    )(out)
    for s in range(nb_stacks):
        for i in range(0, dilation_depth + 1):
            out, skip_out = residual_block(out)
            skip_connections.append(skip_out)

    if use_skip_connections:
        out = layers.add(skip_connections)
    out = layers.Activation('relu')(out)
    out = layers.Convolution1D(nb_output_bins, 1, padding='same',
                               kernel_regularizer=l2(final_l2))(out)
    out = layers.Activation('relu')(out)
    out = layers.Convolution1D(nb_output_bins, 1, padding='same')(out)

    if not learn_all_outputs:
        raise DeprecationWarning('Learning on just all outputs is wasteful, now learning only inside receptive field.')
        out = layers.Lambda(lambda x: x[:, -1, :], output_shape=(out._keras_shape[-1],))(
            out)  # Based on gif in deepmind blog: take last output?

    out = layers.Activation('softmax', name="output_softmax")(out)
    model = Model(input, out)

    receptive_field, receptive_field_ms = compute_receptive_field()

    _log.info('Receptive Field: %d (%dms)' % (receptive_field, int(receptive_field_ms)))
    return model
Пример #27
0
 def combine_streams(models: List[StreamModel], combine_fn=None):
     outs = []
     ins = []
     for stream in models:
         inp = Input(stream.get_input_shape())
         ins.append(inp)
         outs.append(stream.get_stream_model(inp))
     out = combine_fn(ins)
     return Model(ins, out)
Пример #28
0
def backbone(name=None, size=(None, None)):
    x = inputs = Input([size[0], size[0], 3])
    x = darknet_conv(x, 32, 3)
    x = blocking_convolution(x, 64, 1)
    x = blocking_convolution(x, 128, 2)  # skip connection
    x = x_36 = blocking_convolution(x, 256, 8)  # skip connection
    x = x_61 = blocking_convolution(x, 512, 8)
    x = blocking_convolution(x, 1024, 4)
    return tf.keras.Model(inputs, (x_36, x_61, x), name=name)
Пример #29
0
    def Build(self):
        sequence_input = Input((self.encoder_frames_no, 128, 128, 3), self.batch_size,
                               name="nvae_seq_input")
        mask_input = Input((128, 128, 1), self.batch_size,
                           name="nvae_mask_input")
        encoder_output = self.encoder_model(sequence_input)
        self.mean_1024 = encoder_output[0]
        self.stddev_1024 = encoder_output[1]
        self.skip_4x4x512 = encoder_output[2]
        self.skip_8x8x256 = encoder_output[3]
        self.skip_16x16x128 = encoder_output[4]
        self.skip_32x32x64 = encoder_output[5]
        self.skip_64x64x32 = encoder_output[6]
        self.skip_128x128x16 = encoder_output[7]

        mask_encoder_output = self.mask_encoder_model(mask_input)
        self.mask_1024 = mask_encoder_output[0]
        self.mask4x4x512 = mask_encoder_output[1]
        self.mask8x8x256 = mask_encoder_output[2]
        self.mask16x16x128 = mask_encoder_output[3]
        self.mask32x32x64 = mask_encoder_output[4]
        self.mask64x64x32 = mask_encoder_output[5]
        self.mask128x128x16 = mask_encoder_output[6]

        decoder_output = self.decoder_model(encoder_output + mask_encoder_output)

        net = decoder_output[0]
        self.mean_1024 = decoder_output[1]
        self.stddev_1024 = decoder_output[2]
        self.mean_4x4x512 = decoder_output[3]
        self.stddev_4x4x512 = decoder_output[4]
        self.mean_8x8x256 = decoder_output[5]
        self.stddev_8x8x256 = decoder_output[6]
        self.mean_16x16x128 = decoder_output[7]
        self.stddev_16x16x128 = decoder_output[8]
        self.mean_32x32x64 = decoder_output[9]
        self.stddev_32x32x64 = decoder_output[10]
        self.mean_64x64x32 = decoder_output[11]
        self.stddev_64x64x32 = decoder_output[12]
        self.mean_128x128x16 = decoder_output[13]
        self.stddev_128x128x16 = decoder_output[14]

        net = DummyMaskLayer()(net)
        return Model(inputs=[sequence_input, mask_input], outputs=net)
def build_model(num_lags,
                model="MLP",
                summary=True,
                date_time=False,
                combined_model=False,
                event_info=False):
    assert model in models
    input_lags = Input(shape=(num_lags, ), name="pickup_lags")
    inputs = []
    concat = []
    x = input_lags
    if event_info:
        input_events = Input(shape=(max_count, ), name="events")
        events_feat = Embedding(max_count, 8)(input_events)
        events_feat = LSTM(16)(events_feat)
        inputs.append(input_events)
        concat.append(events_feat)
    if date_time:
        input_date_time = Input(shape=(75, ), name="date_time")
        inputs.append(input_date_time)
        concat.append(input_date_time)
    if combined_model:
        pickup_zone = Input(shape=(4, ), name="pickup_zone")
        inputs.append(pickup_zone)
        concat.append(pickup_zone)
    if model.upper() == "MLP":
        # x = concatenate([x] + concat)
        x = MLP(x)
    elif model.upper() == "LSTM":
        x = Reshape((1, 48))(x)
        x = SLSTM(x)

    if len(inputs) > 0:
        x = concatenate([x] + concat)
        # x = Dense(units=10, activation="relu")(x)
        inputs.append(input_lags)
    else:
        inputs = input_lags
    preds = Dense(units=1, name="predicted")(x)
    model = Model(inputs, preds)
    model.compile(loss="mean_squared_error", optimizer="adam")
    if summary:
        print(model.summary())
    return model