Exemplo n.º 1
0
def build_representation_network(input_shape, filter_size1=3, filter_size2=6, conv_strides=1, avg_pool_strides=2):

    input = Input(input_shape)
    c1 = Conv2D(filters=filter_size1, kernel_size=3, strides=conv_strides, padding='same', activation='relu',
                input_shape=input_shape)(input)

    r1 = residual(filter_size1, filter_size1, c1)
    r2 = residual(filter_size1, filter_size1, r1)

    c2 = Conv2D(filters=filter_size2, kernel_size=3, strides=conv_strides, padding='same', activation='relu',
                input_shape=(input_shape[0]/conv_strides, input_shape[0]/conv_strides, 3))(r2)

    r3 = residual(filter_size2, filter_size2, c2)
    r4 = residual(filter_size2, filter_size2, r3)
    r5 = residual(filter_size2, filter_size2, r4)

    a1 = AveragePooling2D(strides=avg_pool_strides)(r5)

    r6 = residual(filter_size2, filter_size2, a1)
    r7 = residual(filter_size2, filter_size2, r6)
    r8 = residual(filter_size2, filter_size2, r7)

    a2 = AveragePooling2D(strides=avg_pool_strides)(r8)

    model = Model(inputs=input, outputs=a2)
    return model
Exemplo n.º 2
0
    def __init__(self, latent_dim=49):
        config = ConfigProto()
        config.gpu_options.allow_growth = True
        session = InteractiveSession(config=config)

        # ENCODER
        inp = Input((896, 896, 1))
        e = Conv2D(32, (10, 10), activation='relu')(inp)
        e = MaxPooling2D((10, 10))(e)
        e = Conv2D(64, (6, 6), activation='relu')(e)
        e = MaxPooling2D((10, 10))(e)
        e = Conv2D(64, (3, 3), activation='relu')(e)
        l = Flatten()(e)
        l = Dense(49, activation='softmax')(l)
        # DECODER
        d = Reshape((7, 7, 1))(l)
        d = Conv2DTranspose(64, (3, 3),
                            strides=8,
                            activation='relu',
                            padding='same')(d)
        d = BatchNormalization()(d)
        d = Conv2DTranspose(64, (3, 3),
                            strides=8,
                            activation='relu',
                            padding='same')(d)
        d = BatchNormalization()(d)
        d = Conv2DTranspose(64, (3, 3),
                            strides=2,
                            activation='relu',
                            padding='same')(d)
        d = BatchNormalization()(d)
        d = Conv2DTranspose(32, (3, 3), activation='relu', padding='same')(d)
        decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(d)

        self.CAD = tf.keras.Model(inp, decoded)
        opt = tf.keras.optimizers.RMSprop(lr=0.0001, decay=1e-6)

        self.CAD.compile(loss="binary_crossentropy",
                         optimizer=opt,
                         metrics=["accuracy"])

        self.Flow = tf.keras.Sequential([
            tf.keras.layers.LSTM(32, input_shape=(3, 2),
                                 return_sequences=True),
            tf.keras.layers.Dropout(0.4),
            tf.keras.layers.Bidirectional(
                tf.keras.layers.LSTM(32, return_sequences=True)),
            tf.keras.layers.Dropout(0.4),
            tf.keras.layers.TimeDistributed(
                tf.keras.layers.Dense(10, activation='relu')),
            tf.keras.layers.Flatten(),
            tf.keras.layers.Dense(2, activation='relu')
        ])
        opt = tf.keras.optimizers.RMSprop(lr=0.0001, decay=1e-6)
        self.Flow.compile(loss="binary_crossentropy",
                          optimizer="adam",
                          metrics=["accuracy"])

        print(self.Flow.summary())
        print(self.CAD.summary())
Exemplo n.º 3
0
def conv_block(feat_maps_out, prev):
    prev = BatchNormalization(axis=-1)(prev)  # Specifying the axis and mode allows for later merging
    prev = Activation('relu')(prev)
    prev = Conv2D(filters=feat_maps_out, kernel_size=3, padding='same')(prev)
    prev = BatchNormalization(axis=-1)(prev)  # Specifying the axis and mode allows for later merging
    prev = Activation('relu')(prev)
    prev = Conv2D(filters=feat_maps_out, kernel_size=3, padding='same')(prev)
    return prev
Exemplo n.º 4
0
def modelDemoStandardConvLSTMInception(input_shape, parameter=None):
    # define LSTM
    input = Input(shape=input_shape, name='main_input')

    I_1 = TimeDistributed(Conv2D(16, (1, 1),
                                 activation='relu',
                                 padding='same',
                                 name='C_1'),
                          name='I_11')(input)
    I_1 = TimeDistributed(Conv2D(16, (5, 5),
                                 activation='relu',
                                 padding='same',
                                 name='C_2'),
                          name='I_12')(I_1)

    I_2 = TimeDistributed(MaxPooling2D((3, 3),
                                       strides=(1, 1),
                                       padding='same',
                                       name='C_3'),
                          name='I_21')(input)
    I_2 = TimeDistributed(Conv2D(16, (1, 1),
                                 activation='relu',
                                 padding='same',
                                 name='C_4'),
                          name='I_22')(I_2)

    concatenate_output = concatenate([I_1, I_2], axis=-1)

    # x = TimeDistributed(Flatten())(x)
    x = ConvLSTM2D(filters=32,
                   kernel_size=(3, 3),
                   padding='same',
                   return_sequences=False)(concatenate_output)
    #x = MaxPooling2D((3, 3), strides=(1, 1), padding='same', name='M_1')(x)

    x = (Flatten())(x)

    x = RepeatVector(8)(x)
    x = LSTM(50, return_sequences=True)(x)

    output = TimeDistributed(Dense(8, activation='softmax'),
                             name='main_output')(x)
    #with tensorflow.device('/cpu'):
    model = Model(inputs=[input], outputs=[output])
    # compile the model with gpu

    #parallel_model = multi_gpu_model(model, gpus=2)
    #parallel_model.compile(loss={'main_output': 'categorical_crossentropy'},
    #              loss_weights={'main_output': 1.}, optimizer='adam', metrics=['accuracy'])
    #model = multi_gpu(model, gpus=[1, 2])
    model.compile(loss={'main_output': 'categorical_crossentropy'},
                  loss_weights={'main_output': 1.},
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Exemplo n.º 5
0
def build_model():
    model = keras.Sequential()
    model.add(
        Conv2D(64, kernel_size=3, activation='relu', input_shape=(28, 28, 1)))
    model.add(Conv2D(32, kernel_size=3, activation='relu'))
    model.add(Flatten())
    model.add(Dense(10, activation='softmax'))
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Exemplo n.º 6
0
def modelB(row, col, parameter=None):
    # define LSTM
    input = Input(shape=(None, row, col, 1), name='main_input')
    '''    x = TimeDistributed(Conv2D(16, (2, 2)))(input)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Dropout(0.25)(x)
    '''
    # tower_1 = TimeDistributed(Conv2D(16, (1, 1), padding='same', activation='relu'))(input)
    # tower_1 = TimeDistributed(Conv2D(16, (3, 3), padding='same', activation='relu'))(tower_1)

    tower_2 = TimeDistributed(Conv2D(16, (1, 1), padding='same'))(input)
    x = BatchNormalization()(tower_2)
    x = Activation('relu')(x)
    x = Dropout(0.25)(x)
    tower_2 = TimeDistributed(Conv2D(16, (5, 5), padding='same'))(x)
    x = BatchNormalization()(tower_2)
    x = Activation('relu')(x)
    tower_2 = Dropout(0.25)(x)

    tower_3 = TimeDistributed(
        MaxPooling2D((3, 3), strides=(1, 1), padding='same'))(input)
    tower_3 = TimeDistributed(Conv2D(16, (1, 1), padding='same'))(tower_3)
    x = BatchNormalization()(tower_3)
    x = Activation('relu')(x)
    tower_3 = Dropout(0.25)(x)
    concatenate_output = concatenate([tower_2, tower_3], axis=-1)

    x = TimeDistributed(MaxPooling2D(pool_size=(2, 2),
                                     strides=2))(concatenate_output)
    x = Dropout(0.25)(x)
    x = TimeDistributed(Flatten())(x)
    # convLstm = ConvLSTM2D(filters=40, kernel_size=(3, 3),padding='same', return_sequences=False)(x)
    lstm_output = LSTM(75)(x)
    lstm_output = BatchNormalization()(lstm_output)
    # lstm_output = BatchNormalization()(convLstm)
    # auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_output)
    # auxiliary_input = Input(shape=(4,), name='aux_input')
    # x = concatenate([lstm_output, auxiliary_input])

    x = RepeatVector(4)(lstm_output)
    x = LSTM(50, return_sequences=True)(x)
    # model.add(Dropout(0.25))
    x = BatchNormalization()(x)
    output = TimeDistributed(Dense(4, activation='softmax'),
                             name='main_output')(x)

    model = Model(inputs=[input], outputs=[output])
    model.compile(loss={'main_output': 'categorical_crossentropy'},
                  loss_weights={'main_output': 1.},
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Exemplo n.º 7
0
def build_reward_network(shape, filter_size1=3, filter_size2=1):
    input = Input(shape)
    c1 = Conv2D(filters=filter_size1, kernel_size=(3, 3), strides=2, padding='same', activation='relu',
                input_shape=shape)(input)
    a1 = AveragePooling2D(strides=2)(c1)
    c2 = Conv2D(filters=filter_size2, kernel_size=(3, 3), strides=1, padding='same', activation='relu',
                input_shape=shape)(a1)
    a2 = AveragePooling2D(strides=2)(c2)
    f1 = Flatten()(a2)

    model = Model(inputs=input, outputs=f1)
    return model
Exemplo n.º 8
0
def modelStandard(input_shape, parameter=None):
    # define LSTM
    model = Sequential()
    model.add(
        TimeDistributed(Conv2D(16, (2, 2), activation='relu'),
                        input_shape=input_shape))
    model.add(Dropout(parameter['dropout']))
    model.add(BatchNormalization())
    model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2), strides=2)))
    model.add(Dropout(parameter['dropout']))
    model.add(TimeDistributed(Flatten()))
    model.add(LSTM(parameter['cell1']))
    # model.add(Dropout(0.25))
    model.add(BatchNormalization())

    model.add(RepeatVector(8))
    model.add(LSTM(parameter['cell2'], return_sequences=True))
    # model.add(Dropout(0.25))
    model.add(BatchNormalization())
    model.add(TimeDistributed(Dense(5, activation='softmax')))

    # Replicates `model` on 8 GPUs.
    # This assumes that your machine has 8 available GPUs.
    #parallel_model = multi_gpu_model(model, gpus=2)
    #parallel_model.compile(loss='categorical_crossentropy',
    #                       optimizer='adam', metrics=['accuracy'])

    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Exemplo n.º 9
0
def modelC(row, col):
    # define LSTM
    model = Sequential()
    model.add(
        TimeDistributed(Conv2D(16, (2, 2), activation='relu'),
                        input_shape=(None, row, col, 1)))
    model.add(Dropout(0.25))
    model.add(BatchNormalization())
    model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
    model.add(Dropout(0.25))
    model.add(TimeDistributed(Flatten()))
    model.add(LSTM(75))
    # model.add(Dropout(0.25))
    model.add(BatchNormalization())

    model.add(RepeatVector(4))
    model.add(LSTM(50, return_sequences=True))
    # model.add(Dropout(0.25))
    model.add(BatchNormalization())
    model.add(TimeDistributed(Dense(4, activation='softmax')))

    # Replicates `model` on 8 GPUs.
    # This assumes that your machine has 8 available GPUs.
    # parallel_model = multi_gpu_model(model, gpus=[2])
    # parallel_model.compile(loss='categorical_crossentropy',
    #                       optimizer='adam', metrics=['accuracy'])

    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Exemplo n.º 10
0
def build_dynamic_network(shape, filter_size=6, conv_strides=1):
    input = Input(shape)
    c1 = Conv2D(filters=filter_size, kernel_size=(3, 3), strides=conv_strides, padding='same', activation='relu',
                input_shape=shape)(input)
    r1 = residual(filter_size, filter_size, c1)
    model = Model(inputs=input, outputs=r1)
    return model
Exemplo n.º 11
0
    def __init__(self):
        super(Z2Model, self).__init__()
        # self.gcnn1 = tf.keras.layers.Conv2D(filters=20, kernel_size=(3, 3), activation='relu')
        # self.gcnn2 = tf.keras.layers.Conv2D(filters=20, kernel_size=(3, 3), activation='relu')
        # self.gcnn3 = tf.keras.layers.Conv2D(filters=20, kernel_size=(3, 3), activation='relu')
        # self.gcnn4 = tf.keras.layers.Conv2D(filters=20, kernel_size=(3, 3), activation='relu')
        # self.gcnn5 = tf.keras.layers.Conv2D(filters=20, kernel_size=(3, 3), activation='relu')
        # self.gcnn6 = tf.keras.layers.Conv2D(filters=20, kernel_size=(3, 3), activation='relu')
        # self.gcnn7 = tf.keras.layers.Conv2D(filters=20, kernel_size=(4, 4), activation='relu')

        self.gcnn1 = ConvBatchLayer(
            conv=Conv2D(filters=20, kernel_size=(3, 3), activation='relu'))
        self.gcnn2 = ConvBatchLayer(
            conv=Conv2D(filters=20, kernel_size=(3, 3), activation='relu'))
        self.gcnn3 = ConvBatchLayer(
            conv=Conv2D(filters=20, kernel_size=(3, 3), activation='relu'))
        self.gcnn4 = ConvBatchLayer(
            conv=Conv2D(filters=20, kernel_size=(3, 3), activation='relu'))
        self.gcnn5 = ConvBatchLayer(
            conv=Conv2D(filters=20, kernel_size=(3, 3), activation='relu'))
        self.gcnn6 = ConvBatchLayer(
            conv=Conv2D(filters=20, kernel_size=(3, 3), activation='relu'))
        self.gcnn7 = ConvBatchLayer(conv=Conv2D(filters=9, kernel_size=(3, 3)))
        self.flatten = Flatten()
        self.dense = Dense(9)
Exemplo n.º 12
0
def build_policy_network(shape, action_size, regularizer):
    policy_input = Input(shape)
    c1 = Conv2D(filters=1, kernel_size=1, padding='same', activation='linear',
                kernel_regularizer=regularizer)(policy_input)
    b1 = BatchNormalization(axis=-1)(c1)
    l1 = LeakyReLU()(b1)
    f1 = Flatten()(l1)
    d1 = Dense(action_size, use_bias=False, activation='sigmoid', kernel_regularizer=regularizer)(f1)
    policy_model = Model(inputs=policy_input, outputs=d1)
    return policy_model
Exemplo n.º 13
0
def build_value_network(shape, value_support_size):
    value_input = Input(shape)
    c1 = Conv2D(filters=1, kernel_size=1, padding='same', activation='linear')(value_input)
    b1 = BatchNormalization(axis=-1)(c1)
    l1 = LeakyReLU()(b1)
    f1 = Flatten()(l1)
    d2 = Dense(20, use_bias=False, activation='linear')(f1)
    l2 = LeakyReLU()(d2)
    d2 = Dense(value_support_size, use_bias=False, activation='tanh')(l2)
    value_model = Model(inputs=value_input, outputs=d2)
    return value_model
Exemplo n.º 14
0
def ModelInception():
    input_img = Input(shape=(256, 256, 3))

    tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
    tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1)

    tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
    tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2)

    tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)
    tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)

    output = concatenate([tower_1, tower_2, tower_3], axis=1)

    model = Model(inputs=[input_img], outputs=output)

    model.compile(optimizer='rmsprop',
                  loss='binary_crossentropy',
                  metrics=['accuracy'])
    return model
def SingleOutputCNN(
    input_shape,
    output_shape,
    cnns_per_maxpool=1,
    maxpool_layers=1,
    dense_layers=1,
    dense_units=64,
    dropout=0.25,
    regularization=False,
    global_maxpool=False,
    name='',
) -> Model:
    function_name = cast(types.FrameType,
                         inspect.currentframe()).f_code.co_name
    model_name = f"{function_name}-{name}" if name else function_name
    # model_name  = seq([ function_name, name ]).filter(lambda x: x).make_string("-")  # remove dependency on pyfunctional - not in Kaggle repo without internet

    inputs = Input(shape=input_shape)
    x = inputs

    for cnn1 in range(0, maxpool_layers):
        for cnn2 in range(1, cnns_per_maxpool + 1):
            x = Conv2D(32 * cnn2,
                       kernel_size=(3, 3),
                       padding='same',
                       activation='relu')(x)
        x = MaxPooling2D(pool_size=(2, 2))(x)
        x = BatchNormalization()(x)
        x = Dropout(dropout)(x)

    if global_maxpool:
        x = GlobalMaxPooling2D()(x)

    x = Flatten()(x)

    for nn1 in range(0, dense_layers):
        if regularization:
            x = Dense(dense_units,
                      activation='relu',
                      kernel_regularizer=regularizers.l2(0.01),
                      activity_regularizer=regularizers.l1(0.01))(x)
        else:
            x = Dense(dense_units, activation='relu')(x)

        x = BatchNormalization()(x)
        x = Dropout(dropout)(x)

    x = Dense(output_shape, activation='softmax')(x)

    model = Model(inputs, x, name=model_name)
    # plot_model(model, to_file=os.path.join(os.path.dirname(__file__), f"{name}.png"))
    return model
def build_model():
    model = Sequential()
    model.add(Conv2D(filters=16, kernel_size=2, input_shape=(64, 64, 1), activation='relu'))
    model.add(MaxPooling2D(pool_size=2))
    model.add(Dropout(0.2))

    model.add(Conv2D(filters=32, kernel_size=2, activation='relu'))
    model.add(MaxPooling2D(pool_size=2))
    model.add(Dropout(0.2))

    model.add(Conv2D(filters=64, kernel_size=2, activation='relu'))
    model.add(MaxPooling2D(pool_size=2))
    model.add(Dropout(0.2))

    model.add(Conv2D(filters=128, kernel_size=2, activation='relu'))
    model.add(MaxPooling2D(pool_size=2))
    model.add(Dropout(0.2))
    model.add(GlobalAveragePooling2D())

    model.add(Dense(9, activation='softmax'))

    return model
Exemplo n.º 17
0
def ModelSharedVision():
    # First, define the vision modules
    digit_input = Input(shape=(27, 27, 1))
    x = Conv2D(64, (3, 3))(digit_input)
    x = Conv2D(64, (3, 3))(x)
    x = MaxPooling2D((2, 2))(x)
    out = Flatten()(x)

    vision_model = Model(digit_input, out)

    # Then define the tell-digits-apart model
    digit_a = Input(shape=(27, 27, 1))
    digit_b = Input(shape=(27, 27, 1))

    # The vision model will be shared, weights and all
    out_a = vision_model(digit_a)
    out_b = vision_model(digit_b)

    concatenated = concatenate([out_a, out_b])
    out = Dense(1, activation='sigmoid')(concatenated)

    classification_model = Model([digit_a, digit_b], out)
    return classification_model
Exemplo n.º 18
0
def ModelResidual():
    # input tensor for a 3-channel 256x256 image
    x = Input(shape=(256, 256, 3))
    # 3x3 conv with 3 output channels (same as input channels)
    y = Conv2D(3, (3, 3), padding='same')(x)
    # this returns x + y.
    z = add([x, y])

    model = Model(inputs=[x], outputs=z)

    model.compile(optimizer='rmsprop',
                  loss='binary_crossentropy',
                  metrics=['accuracy'])
    return model
Exemplo n.º 19
0
def modelDemoStandard(row, col):
    # define LSTM
    input = Input(shape=(None, row, col, 1), name='main_input')
    x = TimeDistributed(Conv2D(16, (2, 2), activation='relu'))(input)

    x = TimeDistributed(Flatten())(x)
    lstm_output = LSTM(75)(x)

    x = RepeatVector(4)(lstm_output)
    x = LSTM(50, return_sequences=True)(x)

    output = TimeDistributed(Dense(4, activation='softmax'),
                             name='main_output')(x)

    model = Model(inputs=[input], outputs=[output])
    model.compile(loss={'main_output': 'categorical_crossentropy'},
                  loss_weights={'main_output': 1.},
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Exemplo n.º 20
0
def ModelVisualQuestionAnswering():
    # First, let's define a vision model using a Sequential model.
    # This model will encode an image into a vector.
    vision_model = Sequential()
    vision_model.add(
        Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               input_shape=(224, 224, 3)))
    vision_model.add(Conv2D(64, (3, 3), activation='relu'))
    vision_model.add(MaxPooling2D((2, 2)))
    vision_model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
    vision_model.add(Conv2D(128, (3, 3), activation='relu'))
    vision_model.add(MaxPooling2D((2, 2)))
    vision_model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
    vision_model.add(Conv2D(256, (3, 3), activation='relu'))
    vision_model.add(Conv2D(256, (3, 3), activation='relu'))
    vision_model.add(MaxPooling2D((2, 2)))
    vision_model.add(Flatten())

    # Now let's get a tensor with the output of our vision model:
    image_input = Input(shape=(224, 224, 3))
    encoded_image = vision_model(image_input)

    # Next, let's define a language model to encode the question into a vector.
    # Each question will be at most 100 words long,
    # and we will index words as integers from 1 to 9999.
    question_input = Input(shape=(100, ), dtype='int32')
    embedded_question = Embedding(input_dim=10000,
                                  output_dim=256,
                                  input_length=100)(question_input)
    encoded_question = LSTM(256)(embedded_question)

    # Let's concatenate the question vector and the image vector:
    merged = concatenate([encoded_question, encoded_image])

    # And let's train a logistic regression over 1000 words on top:
    output = Dense(1000, activation='softmax')(merged)

    # This is our final model:
    vqa_model = Model(inputs=[image_input, question_input], outputs=output)
    return vqa_model
Exemplo n.º 21
0
def modelA(row, col):
    # define LSTM
    input = Input(shape=(None, row, col, 1), name='main_input')
    x = TimeDistributed(Conv2D(16, (2, 2), activation='relu'))(input)
    x = Dropout(0.25)(x)
    x = BatchNormalization()(x)
    x = TimeDistributed(MaxPooling2D(pool_size=(2, 2), strides=2))(x)
    x = Dropout(0.25)(x)
    x = TimeDistributed(Flatten())(x)
    lstm_output = LSTM(75)(x)
    lstm_output = BatchNormalization()(lstm_output)

    auxiliary_output = Dense(1, activation='sigmoid',
                             name='aux_output')(lstm_output)
    auxiliary_input = Input(shape=(4, ), name='aux_input')
    x = concatenate([lstm_output, auxiliary_input])

    x = RepeatVector(8)(x)
    x = LSTM(50, return_sequences=True)(x)
    # model.add(Dropout(0.25))
    x = BatchNormalization()(x)
    output = TimeDistributed(Dense(5, activation='softmax'),
                             name='main_output')(x)

    model = Model(inputs=[input, auxiliary_input],
                  outputs=[output, auxiliary_output])
    model.compile(loss={
        'main_output': 'categorical_crossentropy',
        'aux_output': 'binary_crossentropy'
    },
                  loss_weights={
                      'main_output': 1.,
                      'aux_output': 0.2
                  },
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Exemplo n.º 22
0
def modelStandardB(row, col):
    # define LSTM
    input_img = Input(shape=(None, row, col, 1), name='input')
    x = TimeDistributed(Conv2D(16, (2, 2), activation='relu'))(input_img)
    x = Dropout(0.25)(x)
    x = BatchNormalization()(x)
    x = TimeDistributed(MaxPooling2D(pool_size=(2, 2), strides=2))(x)
    x = Dropout(0.25)(x)
    x = TimeDistributed(Flatten())(x)
    x = LSTM(75)(x)
    # model.add(Dropout(0.25))
    x = BatchNormalization()(x)

    x = RepeatVector(4)(x)
    x = LSTM(50, return_sequences=True)(x)
    # model.add(Dropout(0.25))
    x = BatchNormalization()(x)
    output = TimeDistributed(Dense(4, activation='softmax'))(x)

    model = Model(input_img, output)
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Exemplo n.º 23
0
    def build(self):
        input_img = Input(shape=(28, 28, 1))

        cnn = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
        cnn = MaxPooling2D((2, 2), padding='same')(cnn)
        cnn = Conv2D(32, (3, 3), activation='relu', padding='same')(cnn)
        cnn = MaxPooling2D((2, 2), padding='same')(cnn)
        cnn = Conv2D(32, (3, 3), activation='relu', padding='same')(cnn)
        encoded = MaxPooling2D((2, 2), padding='same')(cnn)

        cnn = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
        cnn = UpSampling2D((2, 2))(cnn)
        cnn = Conv2D(32, (3, 3), activation='relu', padding='same')(cnn)
        cnn = UpSampling2D((2, 2))(cnn)
        cnn = Conv2D(32, (3, 3), activation='relu')(cnn)
        cnn = UpSampling2D((2, 2))(cnn)
        decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(cnn)

        cnn_autoencoder = Model(input_img, decoded)
        cnn_autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

        x_train = self.x_train.reshape(-1, 28, 28, 1)

        x_train_split, x_valid_split = train_test_split(x_train, test_size=self.train_test_split,
                                                        random_state=self.seed)

        cnn_autoencoder.fit(x_train_split, x_train_split,
                            epochs=self.epochs,
                            batch_size=self.batch_size,
                            validation_data=(x_valid_split, x_valid_split),
                            verbose=self.verbosity)

        x_train_pred = cnn_autoencoder.predict(x_train)
        mse = np.mean(np.power(x_train - x_train_pred, 2), axis=1)

        # Semi-supervised due to given threshold
        self.threshold = np.quantile(mse, 0.9)
        self.cnn_autoencoder = cnn_autoencoder
Exemplo n.º 24
0
    def get_model(self):
        model = Sequential()
        model.add(Conv2D(32, kernel_size=(2, 2), activation='relu',
                         input_shape=(self.feature_dim_1, self.feature_dim_2, self.channel)))
        model.add(Conv2D(64, kernel_size=(2, 2), activation='relu'))
        model.add(Conv2D(128, kernel_size=(2, 2), activation='relu'))
        model.add(MaxPool2D(pool_size=(1, 1)))
        model.add(Dropout(0.5))
        model.add(Conv2D(128, kernel_size=(2, 2), activation='relu'))
        model.add(Conv2D(256, kernel_size=(2, 2), activation='relu'))
        model.add(MaxPool2D(pool_size=(1, 1)))
        model.add(Dropout(0.5))
        model.add(Conv2D(128, kernel_size=(2, 2), activation='relu'))
        model.add(Conv2D(256, kernel_size=(4, 4), activation='relu'))
        model.add(MaxPool2D(pool_size=(2, 2)))
        model.add(Flatten())
        model.add(Dropout(0.5))
        model.add(Dense(256, kernel_regularizer=regularizers.l2(0.2), activation='relu'))
        model.add(Dense(32, kernel_regularizer=regularizers.l2(0.2), activation='relu'))
        model.add(Dense(self.num_classes, activation='softmax'))

        model.compile(loss='categorical_crossentropy', optimizer='RMSProp', metrics=['accuracy'])
        return model
Exemplo n.º 25
0
def ModelVideoQuestionAnswering():
    # First, let's define a vision model using a Sequential model.
    # This model will encode an image into a vector.
    vision_model = Sequential()
    vision_model.add(
        Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               input_shape=(224, 224, 3)))
    vision_model.add(Conv2D(64, (3, 3), activation='relu'))
    vision_model.add(MaxPooling2D((2, 2)))
    vision_model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
    vision_model.add(Conv2D(128, (3, 3), activation='relu'))
    vision_model.add(MaxPooling2D((2, 2)))
    vision_model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
    vision_model.add(Conv2D(256, (3, 3), activation='relu'))
    vision_model.add(Conv2D(256, (3, 3), activation='relu'))
    vision_model.add(MaxPooling2D((2, 2)))
    vision_model.add(Flatten())

    # Now let's get a tensor with the output of our vision model:
    image_input = Input(shape=(224, 224, 3))
    encoded_image = vision_model(image_input)

    # Next, let's define a language model to encode the question into a vector.
    # Each question will be at most 100 words long,
    # and we will index words as integers from 1 to 9999.
    question_input = Input(shape=(100, ), dtype='int32')
    embedded_question = Embedding(input_dim=10000,
                                  output_dim=256,
                                  input_length=100)(question_input)
    encoded_question = LSTM(256)(embedded_question)

    # Let's concatenate the question vector and the image vector:
    merged = concatenate([encoded_question, encoded_image])

    # And let's train a logistic regression over 1000 words on top:
    output = Dense(1000, activation='softmax')(merged)

    # This is our final model:
    # vqa_model = Model(inputs=[image_input, question_input], outputs=output)

    video_input = Input(shape=(100, 224, 224, 3))
    # This is our video encoded via the previously trained vision_model (weights are reused)
    encoded_frame_sequence = TimeDistributed(vision_model)(
        video_input)  # the output will be a sequence of vectors
    encoded_video = LSTM(256)(
        encoded_frame_sequence)  # the output will be a vector

    # This is a model-level representation of the question encoder, reusing the same weights as before:
    question_encoder = Model(inputs=question_input, outputs=encoded_question)

    # Let's use it to encode the question:
    video_question_input = Input(shape=(100, ), dtype='int32')
    encoded_video_question = question_encoder(video_question_input)

    # And this is our video question answering model:
    merged = concatenate([encoded_video, encoded_video_question])
    output = Dense(1000, activation='softmax')(merged)
    video_qa_model = Model(inputs=[video_input, video_question_input],
                           outputs=output)

    return video_qa_model
Exemplo n.º 26
0
    plot_images(augmented_images)

val_image_gen = ImageDataGenerator(rescale=1. / 255)
val_data_gen = val_image_gen.flow_from_directory(batch_size=batch_size,
                                                 directory=val_dir,
                                                 target_size=(IMG_SHAPE,
                                                              IMG_SHAPE),
                                                 class_mode='sparse')

print("Configuring model...")
model = Sequential()

model.add(
    Conv2D(16,
           3,
           padding='same',
           activation='relu',
           input_shape=(IMG_SHAPE, IMG_SHAPE, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, 3, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, 3, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))

model.add(Dropout(0.2))
Exemplo n.º 27
0
def skip_block(feat_maps_in, feat_maps_out, prev):
    if feat_maps_in != feat_maps_out:
        # This adds in a 1x1 convolution on shortcuts that map between an uneven amount of channels
        prev = Conv2D(filters=feat_maps_out, kernel_size=1, padding='same')(prev)
    return prev
Exemplo n.º 28
0
print(f'X_train: {X_train.shape}, Y_train: {Y_train.shape}')
print(f'X_test: {X_test.shape}, Y_test: {Y_test.shape}')

X_train = X_train.reshape(*X_train.shape, 1).astype('float16') / 255
X_test = X_test.reshape(*X_test.shape, 1).astype('float16') / 255
Y_train = to_categorical(Y_train, 10, dtype='float16')
Y_test = to_categorical(Y_test, 10, dtype='float16')

print(f'X_train: {X_train.shape}, Y_train: {Y_train.shape}')
print(f'X_test: {X_test.shape}, Y_test: {Y_test.shape}')

model = Sequential()

model.add(
    Conv2D(filters=32,
           kernel_size=(3, 3),
           activation='relu',
           input_shape=(28, 28, 1)))
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPool2D(pool_size=2))
model.add(Dropout(rate=0.25))
model.add(Flatten())
model.add(Dense(units=128, activation='relu'))
model.add(Dropout(rate=0.5))
model.add(Dense(units=10, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

early_stop = EarlyStopping(monitor='val_loss', patience=10, verbose=0)
Exemplo n.º 29
0
from tensorflow import keras
from tensorflow_core.python.keras import Sequential
from tensorflow_core.python.keras.layers.core import Dense, Dropout, Flatten
from tensorflow_core.python.keras.layers import Conv2D, MaxPooling2D

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)

model = tf.keras.models.Sequential()

#-------------MOD------------------------
model.add(
    Conv2D(16, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))

model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())

model.add(Dense(10, activation='softmax'))
#-------------------------------------
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)

model.evaluate(x_test, y_test, verbose=2)
Exemplo n.º 30
0
class_names = [
    'T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt',
    'Sneaker', 'Bag', 'Ankle boot'
]

# Create CNN Model

NUM_EPOCHS = 25
LR = 0.001
BATCH_SIZE = 32

model = tf.keras.Sequential()
model.add(
    Conv2D(28, (3, 3),
           strides=2,
           padding='same',
           activation='relu',
           input_shape=(28, 28, 1)))
model.add(BatchNormalization())
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(64, activation='relu'))