Пример #1
0
#
# Define the model structure
# ----------------------------------------------------------------------------
embedding_layer = Embedding(nb_words,
                            EMBEDDING_DIM,
                            weights=[embedding_matrix],
                            input_length=MAX_SEQUENCE_LENGTH,
                            trainable=True)
lstm_layer = Bidirectional(LSTM(num_lstm, dropout=rate_drop_lstm, recurrent_dropout=rate_drop_lstm))
#lstm_layer = LSTM(num_lstm, dropout=rate_drop_lstm)

sequence_1_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
embedded_sequences_1 = embedding_layer(sequence_1_input)
x1 = lstm_layer(embedded_sequences_1)
x1 =  Dropout(rate_drop_dense)(x1)
x1 =  BatchNormalization()(x1)

x1 = Dense(num_dense, activation=act)(x1)
x1 = Dropout(rate_drop_dense)(x1)
x1 = BatchNormalization()(x1)

preds = Dense(5, activation='softmax')(x1)
#preds = Dense(5, activation=act)(x1)
#
#
#attention
print("build attention")
ques1_enc = Sequential()
ques1_enc.add(Embedding(nb_words,
                            EMBEDDING_DIM,
                            weights=[embedding_matrix],
    def __build_network(self):
        embedding_layer = Embedding(self.corpus_size,
                            EMBEDDING_DIM,
                            weights=[self.embedding_matrix],
                            input_length=MAX_SEQUENCE_LENGTH,
                            trainable=False)
        # train a 1D convnet with global maxpooling
        sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
        embedded_sequences = embedding_layer(sequence_input)
        # sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
        # embedded_sequences = embedding_layer(sequence_input)
        x = Convolution1D(128, 5)(embedded_sequences)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)
        x = MaxPooling1D(5)(x)
        x = Convolution1D(128, 5)(x)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)
        x = MaxPooling1D(5)(x)
        print "before 256", x.get_shape()
        x = Convolution1D(128, 5)(x)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)
        print "before 35 ", x.get_shape()
        x = MaxPooling1D(35)(x)
        x = Flatten()(x)
        # print x.shape()

        x = Dense(128, activation='relu')(x)
        print x.get_shape()
        x = Dropout(0.5)(x)
        print x.get_shape()
        preds = Dense(self.class_num, activation='softmax')(x)
        print preds.get_shape()
        # conv_blocks = []
        # for sz in self.filter_sizes:
        #     conv = Convolution1D(filters=self.num_filters, kernel_size=sz, activation="relu", padding='valid', strides=1)(embedded_sequences)
        #     conv = MaxPooling1D(pool_size=2)(conv)
        #     conv = Flatten()(conv)
        #     conv_blocks.append(conv)
        # z = Concatenate()(conv_blocks) if len(conv_blocks) > 1 else conv_blocks[0]
        # z = Dropout(rate=0.5)(z)
        # z = Dense(units=self.hidden_dims, activation="relu")(z)
        # preds = Dense(self.class_num, activation="softmax")(z)
        rmsprop = RMSprop(lr=0.001)
        self.model = Model(sequence_input, preds)
        self.model.compile(loss='categorical_crossentropy', optimizer=rmsprop, metrics=['acc'])
def MusicTaggerCRNN(weights='msd', input_tensor=None, include_top=True):
    '''Instantiate the MusicTaggerCRNN architecture,
    optionally loading weights pre-trained
    on Million Song Dataset. Note that when using TensorFlow,
    for best performance you should set
    `image_dim_ordering="tf"` in your Keras config
    at ~/.keras/keras.json.

    The model and the weights are compatible with both
    TensorFlow and Theano. The dimension ordering
    convention used by the model is the one
    specified in your Keras config file.

    For preparing mel-spectrogram input, see
    `audio_conv_utils.py` in [applications](https://github.com/fchollet/keras/tree/master/keras/applications).
    You will need to install [Librosa](http://librosa.github.io/librosa/)
    to use it.

    # Arguments
        weights: one of `None` (random initialization)
            or "msd" (pre-training on ImageNet).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        include_top: whether to include the 1 fully-connected
            layer (output layer) at the top of the network.
            If False, the network outputs 32-dim features.


    # Returns
        A Keras model instance.
    '''
    if weights not in {'msd', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `msd` '
                         '(pre-training on Million Song Dataset).')

    K.set_image_dim_ordering('th')

    # Determine proper input shape
    if K.image_dim_ordering() == 'th':
        input_shape = (1, 96, 1366)
        # raise RuntimeError("th")
    else:
        input_shape = (96, 1366, 1)
        # raise RuntimeError("tf")

    if input_tensor is None:
        melgram_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            melgram_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            melgram_input = input_tensor

    # Determine input axis
    if K.image_dim_ordering() == 'th':
        channel_axis = 1
        freq_axis = 2
        time_axis = 3
    else:
        channel_axis = 3
        freq_axis = 1
        time_axis = 2

    # Input block
    x = ZeroPadding2D(padding=(0, 37))(melgram_input)
    x = BatchNormalization(axis=freq_axis, name='bn_0_freq')(x)

    # Conv block 1
    x = Convolution2D(64, 3, 3, border_mode='same', name='conv1')(x)
    x = BatchNormalization(axis=channel_axis, mode=0, name='bn1')(x)
    x = ELU()(x)
    x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool1')(x)
    x = Dropout(0.1, name='dropout1')(x)

    # Conv block 2
    x = Convolution2D(128, 3, 3, border_mode='same', name='conv2')(x)
    x = BatchNormalization(axis=channel_axis, mode=0, name='bn2')(x)
    x = ELU()(x)
    x = MaxPooling2D(pool_size=(3, 3), strides=(3, 3), name='pool2')(x)
    x = Dropout(0.1, name='dropout2')(x)

    # Conv block 3
    x = Convolution2D(128, 3, 3, border_mode='same', name='conv3')(x)
    x = BatchNormalization(axis=channel_axis, mode=0, name='bn3')(x)
    x = ELU()(x)
    x = MaxPooling2D(pool_size=(4, 4), strides=(4, 4), name='pool3')(x)
    x = Dropout(0.1, name='dropout3')(x)

    # Conv block 4
    x = Convolution2D(128, 3, 3, border_mode='same', name='conv4')(x)
    x = BatchNormalization(axis=channel_axis, mode=0, name='bn4')(x)
    x = ELU()(x)
    x = MaxPooling2D(pool_size=(4, 4), strides=(4, 4), name='pool4')(x)
    x = Dropout(0.1, name='dropout4')(x)

    # reshaping
    if K.image_dim_ordering() == 'th':
        x = Permute((3, 1, 2))(x)
    x = Reshape((15, 128))(x)

    # GRU block 1, 2, output
    x = GRU(32, return_sequences=True, name='gru1')(x)
    x = GRU(32, return_sequences=False, name='gru2')(x)
    x = Dropout(0.3)(x)
    if include_top:
        x = Dense(50, activation='sigmoid', name='output')(x)

    # Create model
    model = Model(melgram_input, x)
    if weights is None:
        return model
    else:
        # weights used by MSD
        if K.image_dim_ordering() == 'tf':
            raise RuntimeError("Please set image_dim_ordering == 'th'."
                               "You can set it at ~/.keras/keras.json")

        model.load_weights('data/music_tagger_crnn_weights_%s.h5' % K._BACKEND,
                           by_name=True)
        return model
Пример #4
0
print('x_test',np.array(x_test).shape,'y_test',np.array(y_test).shape)



# hyperparameter
keep_prob = 0.4
# length = 10

#
# Listing 5.1. Instantiating a small convnet AlexNet
# Instantiate an empty model
model = models.Sequential()

# 1st Convolutional Layer et maxpooling
model.add(layers.Conv2D(filters = 96, kernel_size = (6, 6), strides = (1,1), padding = 'valid', input_shape=(60, 60, 3)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(layers.MaxPooling2D(pool_size = (3, 3), strides = (2,2), padding = 'valid'))

# 2nd Convolutional layer et maxpooling
model.add(layers.Conv2D(filters = 256, kernel_size = (5,5), strides =(1,1), padding = 'same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(layers.MaxPooling2D(pool_size = (3, 3), strides = (2,2), padding = 'valid' ))

# 3rd Convolutional layer
model.add(layers.Conv2D(filters = 384, kernel_size = (3, 3), strides = (1,1), padding = 'same'))
model.add(BatchNormalization())
model.add(Activation('relu'))

# 4th Convolutional layer
def train_model(train_file='../data/input.txt',
                job_dir='./tmp/',
                embeds_file='glove.840B.300d-char.txt',
                **args):
    input_data_file = file_io.FileIO(train_file, mode='r')
    text = input_data_file.read()

    print('corpus length:', len(text))

    chars = sorted(list(set(text)))
    print('total chars:', len(chars))
    char_indices = dict((c, i) for i, c in enumerate(chars))
    indices_char = dict((i, c) for i, c in enumerate(chars))

    # cut the text in semi-redundant sequences of maxlen characters

    step = 3
    sentences = []
    next_chars = []
    for i in range(0, len(text) - maxlen, step):
        sentences.append(text[i:i + maxlen])
        next_chars.append(text[i + maxlen])
    print('nb sequences:', len(sentences))

    print('Vectorization...')
    X = np.zeros((len(sentences), maxlen), dtype=np.int)
    y = np.zeros((len(sentences), len(chars)), dtype=np.bool)
    for i, sentence in enumerate(sentences):
        for t, char in enumerate(sentence):
            X[i, t] = char_indices[char]
        y[i, char_indices[next_chars[i]]] = 1

    # https://blog.keras.io/using-pre-trained-word-embeddings-in-a-keras-model.html
    if embeds_file:
        print('Processing pretrained character embeds...')
        embedding_vectors = {}

        with file_io.FileIO(embeds_file, mode='r') as f:
            for line in f:
                line_split = line.strip().split(" ")
                vec = np.array(line_split[1:], dtype=float)
                char = line_split[0]
                embedding_vectors[char] = vec

        embedding_matrix = np.zeros((len(chars), 300))
        #embedding_matrix = np.random.uniform(-1, 1, (len(chars), 300))
        for char, i in char_indices.items():
            #print ("{}, {}".format(char, i))
            embedding_vector = embedding_vectors.get(char)
            if embedding_vector is not None:
                embedding_matrix[i] = embedding_vector

        # Use PCA from sklearn to reduce 300D -> 50D
        if use_pca:
            pca = PCA(n_components=embedding_dim)
            pca.fit(embedding_matrix)
            embedding_matrix_pca = np.array(pca.transform(embedding_matrix))
            print(embedding_matrix_pca)
            print(embedding_matrix_pca.shape)

    print('Build model...')
    main_input = Input(shape=(maxlen, ))

    if embeds_file:
        embedding_layer = Embedding(
            len(chars),
            embedding_dim,
            input_length=maxlen,
            weights=[embedding_matrix_pca] if use_pca else [embedding_matrix])
    else:
        embedding_layer = Embedding(len(chars),
                                    embedding_dim,
                                    input_length=maxlen)
    embedded = embedding_layer(main_input)

    # RNN Layer
    rnn = LSTM(256, implementation=consume_less)(embedded)

    aux_output = Dense(len(chars))(rnn)
    aux_output = Activation('softmax', name='aux_out')(aux_output)

    # Hidden Layers
    hidden_1 = Dense(512, use_bias=False)(rnn)
    hidden_1 = BatchNormalization()(hidden_1)
    hidden_1 = Activation('relu')(hidden_1)

    hidden_2 = Dense(256, use_bias=False)(hidden_1)
    hidden_2 = BatchNormalization()(hidden_2)
    hidden_2 = Activation('relu')(hidden_2)

    main_output = Dense(len(chars))(hidden_2)
    main_output = Activation('softmax', name='main_out')(main_output)

    model = Model(inputs=main_input, outputs=[main_output, aux_output])

    optimizer = Adam(lr=lr, decay=lr_decay)
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  loss_weights=[1., 0.2])
    model.summary()

    # plot_model(model, to_file='model.png', show_shapes=True)
    if not os.path.exists('output'):
        os.makedirs('output')

    f = open('output/log.csv', 'w')
    log_writer = csv.writer(f)

    log_writer.writerow(
        ['iteration', 'batch', 'batch_loss', 'epoch_loss', 'elapsed_time'])

    checkpointer = ModelCheckpoint("output/model.hdf5",
                                   monitor='main_out_loss',
                                   save_best_only=True)

    start_time = time.time()
    for iteration in range(1, 20):
        print()
        print('-' * 50)
        print('Iteration', iteration)

        logger = BatchLossLogger(iteration, start_time, log_writer)
        # X_train, y_train = random_subset(X, y)
        # history = model.fit(X_train, [y_train, y_train], batch_size=batch_size,
        #                     epochs=1, callbacks=[logger, checkpointer])
        history = model.fit(X, [y, y],
                            batch_size=batch_size,
                            epochs=1,
                            callbacks=[logger, checkpointer])

        # Store checkpoint data created by model.fit via checkpointer callback,
        # in case job fails later
        store_in_gcs('output/model.hdf5', 'model.hdf5', job_dir)

        loss = str(history.history['main_out_loss'][-1]).replace(".", "_")

        f2_name = 'iter-{:02}-{:.6}.txt'.format(iteration, loss)
        f2_path = 'output/' + f2_name
        f2 = open(f2_path, 'w')

        start_index = random.randint(0, len(text) - maxlen - 1)

        for diversity in [0.2, 0.5, 1.0, 1.2]:
            print()
            print('----- diversity:', diversity)
            f2.write('----- diversity:' + ' ' + str(diversity) + '\n')

            generated = ''
            sentence = text[start_index:start_index + maxlen]
            generated += sentence
            print('----- Generating with seed: "' + sentence + '"')
            f2.write('----- Generating with seed: "' + sentence + '"' +
                     '\n---\n')
            sys.stdout.write(generated)

            for i in range(1200):
                x = np.zeros((1, maxlen), dtype=np.int)
                for t, char in enumerate(sentence):
                    x[0, t] = char_indices[char]

                preds = model.predict(x, verbose=0)[0][0]
                next_index = sample(preds, diversity)
                next_char = indices_char[next_index]

                generated += next_char
                sentence = sentence[1:] + next_char

                sys.stdout.write(next_char)
                sys.stdout.flush()
            f2.write(generated + '\n')
            print()
        f2.close()
        store_in_gcs(f2_path, f2_name, job_dir)

        # Write embeddings for current characters to file
        # The second layer has the embeddings.

        embedding_weights = model.layers[1].get_weights()[0]
        f3 = open('output/char-embeddings.txt', 'w')
        for char in char_indices:
            if ord(char) < 128:
                embed_vector = embedding_weights[char_indices[char], :]
                f3.write(char + " " + " ".join(str(x)
                                               for x in embed_vector) + "\n")
        f3.close()
        store_in_gcs('output/char-embeddings.txt', 'char-embeddings.txt',
                     job_dir)

    f.close()

    store_in_gcs('output/model.hdf5', 'model.hdf5', job_dir)
    store_in_gcs('output/log.csv', 'log.csv', job_dir)
Пример #6
0
def _bn_relu(input):
    """Helper to build a BN -> relu block (by @raghakot)."""
    norm = BatchNormalization(axis=CHANNEL_AXIS)(input)
    return Activation("relu")(norm)
Пример #7
0
def cnn_rnn_tmp(nb_words, EMBEDDING_DIM, \
               embedding_matrix, MAX_SEQUENCE_LENGTH, \
               num_rnn, num_dense, rate_drop_rnn, \
               rate_drop_dense, act):
    '''
    This is the more complex cnn rnn model 

    model: input layer; embedding layer; more complex cnn based attention layer; rnn layer; dense layer; output layer
    '''
    embedding_layer = Embedding(nb_words,
                                EMBEDDING_DIM,
                                weights=[embedding_matrix],
                                input_length=MAX_SEQUENCE_LENGTH,
                                trainable=True)
    rnn_layer = Bidirectional(
        GRU(num_rnn, dropout=rate_drop_rnn, recurrent_dropout=rate_drop_rnn))
    cnn_layer = Conv1D(activation="relu",
                       padding="valid",
                       strides=1,
                       filters=32,
                       kernel_size=4)
    conv1 = Conv1D(filters=128,
                   kernel_size=1,
                   padding='same',
                   activation='relu')
    conv2 = Conv1D(filters=128,
                   kernel_size=2,
                   padding='same',
                   activation='relu')
    conv3 = Conv1D(filters=128,
                   kernel_size=3,
                   padding='same',
                   activation='relu')
    conv4 = Conv1D(filters=128,
                   kernel_size=4,
                   padding='same',
                   activation='relu')
    conv5 = Conv1D(filters=32,
                   kernel_size=5,
                   padding='same',
                   activation='relu')
    conv6 = Conv1D(filters=32,
                   kernel_size=6,
                   padding='same',
                   activation='relu')
    pooling_layer = GlobalMaxPooling1D()
    cnn_dense = Dense(300)
    cnn_dropout1 = Dropout(0.2)
    cnn_dropout2 = Dropout(0.2)
    cnn_batchnormalization = BatchNormalization()
    cnn_repeatvector = RepeatVector(EMBEDDING_DIM)
    cnn_dense1 = Dense(300)
    cnn_timedistributed = TimeDistributed(Dense(1))

    sequence_1_input = Input(shape=(MAX_SEQUENCE_LENGTH, ), dtype='int32')
    embedded_sequences_1 = embedding_layer(sequence_1_input)

    sequence_2_input = Input(shape=(MAX_SEQUENCE_LENGTH, ), dtype='int32')
    embedded_sequences_2 = embedding_layer(sequence_2_input)

    conv1a = conv1(embedded_sequences_1)
    glob1a = GlobalAveragePooling1D()(conv1a)
    glob1a = Dropout(0.5)(glob1a)
    glob1a = BatchNormalization()(glob1a)
    conv1b = conv1(embedded_sequences_2)
    glob1b = GlobalAveragePooling1D()(conv1b)
    glob1b = Dropout(0.5)(glob1b)
    glob1b = BatchNormalization()(glob1b)

    conv2a = conv2(embedded_sequences_1)
    glob2a = GlobalAveragePooling1D()(conv2a)
    glob2a = Dropout(0.5)(glob2a)
    glob2a = BatchNormalization()(glob2a)
    conv2b = conv2(embedded_sequences_2)
    glob2b = GlobalAveragePooling1D()(conv2b)
    glob2b = Dropout(0.5)(glob2b)
    glob2b = BatchNormalization()(glob2b)

    conv3a = conv3(embedded_sequences_1)
    glob3a = GlobalAveragePooling1D()(conv3a)
    glob3a = Dropout(0.5)(glob3a)
    glob3a = BatchNormalization()(glob3a)
    conv3b = conv3(embedded_sequences_2)
    glob3b = GlobalAveragePooling1D()(conv3b)
    glob3b = Dropout(0.5)(glob3b)
    glob3b = BatchNormalization()(glob3b)

    conv4a = conv4(embedded_sequences_1)
    glob4a = GlobalAveragePooling1D()(conv4a)
    glob4a = Dropout(0.5)(glob4a)
    glob4a = BatchNormalization()(glob4a)
    conv4b = conv4(embedded_sequences_2)
    glob4b = GlobalAveragePooling1D()(conv4b)
    glob4b = Dropout(0.5)(glob4b)
    glob4b = BatchNormalization()(glob4b)

    conv5a = conv5(embedded_sequences_1)
    glob5a = GlobalAveragePooling1D()(conv5a)
    glob5a = Dropout(0.5)(glob5a)
    glob5a = BatchNormalization()(glob5a)
    conv5b = conv5(embedded_sequences_2)
    glob5b = GlobalAveragePooling1D()(conv5b)
    glob5b = Dropout(0.5)(glob5b)
    glob5b = BatchNormalization()(glob5b)

    conv6a = conv6(embedded_sequences_1)
    glob6a = GlobalAveragePooling1D()(conv6a)
    glob6a = Dropout(0.5)(glob6a)
    glob6a = BatchNormalization()(glob6a)
    conv6b = conv6(embedded_sequences_2)
    glob6b = GlobalAveragePooling1D()(conv6b)
    glob6b = Dropout(0.5)(glob6b)
    glob6b = BatchNormalization()(glob6b)

    cnn_1 = concatenate([glob1a, glob2a, glob3a, glob4a, glob5a, glob6a])
    cnn_2 = concatenate([glob1b, glob2b, glob3b, glob4b, glob5b, glob6b])

    cnn_1_t = cnn_dense1(cnn_1)
    cnn_2_t = cnn_dense1(cnn_2)

    a1 = multiply([cnn_1_t, embedded_sequences_1])
    a2 = multiply([cnn_2_t, embedded_sequences_2])

    a1 = Permute([2, 1])(a1)
    a2 = Permute([2, 1])(a2)

    a1 = Lambda(lambda x: K.sum(x, axis=1))(a1)
    a2 = Lambda(lambda x: K.sum(x, axis=1))(a2)

    a1 = Activation('sigmoid')(a1)
    a2 = Activation('sigmoid')(a2)

    embedded_sequences_1 = Permute([2, 1])(embedded_sequences_1)
    embedded_sequences_2 = Permute([2, 1])(embedded_sequences_2)

    x1 = multiply([a1, embedded_sequences_1])
    x2 = multiply([a2, embedded_sequences_2])

    x1 = Permute([2, 1])(x1)
    x2 = Permute([2, 1])(x2)

    x1 = rnn_layer(x1)
    x2 = rnn_layer(x2)

    merged = multiply([x1, x2])
    merged = Dropout(rate_drop_dense)(merged)
    merged = BatchNormalization()(merged)

    merged = Dense(num_dense, activation=act)(merged)
    merged = Dropout(rate_drop_dense)(merged)
    merged = BatchNormalization()(merged)

    preds = Dense(3, activation='softmax')(merged)

    ########################################
    ## train the model
    ########################################
    model = Model(inputs=[sequence_1_input, sequence_2_input], outputs=preds)
    model.compile(loss='categorical_crossentropy',
                  optimizer='nadam',
                  metrics=['acc'])
    model.summary()
    # print(STAMP)
    return model
Пример #8
0
def DarknetConv2D_BN_Leaky(*args, **kwargs):
    """Darknet Convolution2D followed by BatchNormalization and LeakyReLU."""
    no_bias_kwargs = {'use_bias': False}
    no_bias_kwargs.update(kwargs)
    return compose(DarknetConv2D(*args, **no_bias_kwargs),
                   BatchNormalization(), LeakyReLU(alpha=0.1))
Пример #9
0
def L2X(datatype, train=True):
    # the whole thing is equation (5)
    x_train, y_train, x_val, y_val, datatype_val, input_shape = create_data(
        datatype, n=int(1e6))

    st1 = time.time()
    st2 = st1
    print(input_shape)
    activation = 'relu'
    # P(S|X) we train the model on this, for capturing the important features.
    model_input = Input(shape=(input_shape, ), dtype='float32')

    net = Dense(100,
                activation=activation,
                name='s/dense1',
                kernel_regularizer=regularizers.l2(1e-3))(model_input)
    net = Dense(100,
                activation=activation,
                name='s/dense2',
                kernel_regularizer=regularizers.l2(1e-3))(net)

    # A tensor of shape, [batch_size, max_sents, 100]
    logits = Dense(input_shape)(net)
    # [BATCH_SIZE, max_sents, 1]
    k = ks[datatype]
    tau = 0.1
    samples = Sample_Concrete(tau, k, name='sample')(logits)

    # q(X_S) variational family
    print(samples)
    new_model_input = Multiply()([model_input, samples])
    net = Dense(32,
                activation=activation,
                name='dense1',
                kernel_regularizer=regularizers.l2(1e-3))(new_model_input)
    net = BatchNormalization()(net)  # Add batchnorm for stability.
    net = Dense(16,
                activation=activation,
                name='dense2',
                kernel_regularizer=regularizers.l2(1e-3))(net)
    net = BatchNormalization()(net)

    preds = Dense(2,
                  activation='softmax',
                  name='dense4',
                  kernel_regularizer=regularizers.l2(1e-3))(net)
    model = Model(model_input, preds)

    if train:
        adam = optimizers.Adam(lr=1e-3)
        model.compile(loss='categorical_crossentropy',
                      optimizer=adam,
                      metrics=['acc'])
        filepath = "models/{}/L2X.hdf5".format(datatype)
        checkpoint = ModelCheckpoint(filepath,
                                     monitor='val_acc',
                                     verbose=1,
                                     save_best_only=True,
                                     mode='max')
        callbacks_list = [checkpoint]
        model.fit(x_train,
                  y_train,
                  validation_data=(x_val, y_val),
                  callbacks=callbacks_list,
                  epochs=1000,
                  batch_size=BATCH_SIZE)
        st2 = time.time()
    else:
        model.load_weights('models/{}/L2X.hdf5'.format(datatype), by_name=True)

    pred_model = Model(model_input, samples)
    pred_model.compile(loss=None, optimizer='rmsprop', metrics=[None])

    scores = pred_model.predict(x_val, verbose=1, batch_size=BATCH_SIZE)

    median_ranks = compute_median_rank(scores,
                                       k=ks[datatype],
                                       datatype_val=datatype_val)

    return median_ranks, time.time() - st2, st2 - st1, scores, x_val, y_val
Пример #10
0
valid_no_lesion_dir = '/media/chad/delft/crop1/no_lesion/crop1_all_valid_folders/'
train_lesion_dir = '/media/chad/delft/crop1/lesion/crop1_all_train_folders/'
train_no_lesion_dir = '/media/chad/delft/crop1/no_lesion/crop1_all_train_folders/'

train_batch_size = 40000
valid_batch_size = 50000

train_batches = BatchesIterator(train_batch_size,train_no_lesion_dir,train_lesion_dir)
valid_batches = BatchesIterator(valid_batch_size,valid_no_lesion_dir,valid_lesion_dir)
X_valid, Y_valid = valid_batches.next()





model.add(BatchNormalization(mode=0, axis=1, input_shape=(3,224,224)))
model.add(Convolution2D(64,3,3,
			 init='he_normal'))
model.add(BatchNormalization(mode=0, axis=1))
model.add(Activation('relu'))


model.add(Convolution2D(64,3,3,init='he_normal'))
model.add(BatchNormalization(mode=0, axis=1))
model.add(Activation('relu'))


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

model.add(Convolution2D(128,3,3, init='he_normal'))
model.add(BatchNormalization(mode=0, axis=1))
    def __create_model(self):
        self.model = Sequential()
        if self._batch_normal:
            if self._layers > 1:
                self.model.add(
                    Bidirectional(self.rnn(self.n_units,
                                           activation=None,
                                           return_sequences=True),
                                  input_shape=(self.time_steps,
                                               self.n_inputs)))
                self.model.add(BatchNormalization())
                self.model.add(Dropout(self._dropout))
                self.model.add(Activation('tanh'))
            else:
                self.model.add(
                    Bidirectional(self.rnn(self.n_units, activation=None),
                                  input_shape=(self.time_steps,
                                               self.n_inputs)))
                self.model.add(BatchNormalization())
                self.model.add(Dropout(self._dropout))
                self.model.add(Activation('tanh'))

            if self._layers > 2:
                for _ in range(self._layers - 2):
                    self.model.add(
                        Bidirectional(
                            self.rnn(self.n_units,
                                     activation=None,
                                     return_sequences=True)))
                    self.model.add(BatchNormalization())
                    self.model.add(Dropout(self._dropout))
                    self.model.add(Activation('tanh'))

            if self._layers > 1:
                self.model.add(
                    Bidirectional(self.rnn(self.n_units, activation=None)))
                self.model.add(BatchNormalization())
                self.model.add(Dropout(self._dropout))
                self.model.add(Activation('tanh'))
        else:
            if self._layers > 1:
                self.model.add(
                    Bidirectional(self.rnn(self.n_units,
                                           return_sequences=True),
                                  input_shape=(self.time_steps,
                                               self.n_inputs)))
                self.model.add(Dropout(self._dropout))
            else:
                self.model.add(
                    Bidirectional(self.rnn(self.n_units),
                                  input_shape=(self.time_steps,
                                               self.n_inputs)))
                self.model.add(Dropout(self._dropout))

            if self._layers > 2:
                for _ in range(self._layers - 2):
                    self.model.add(
                        Bidirectional(
                            self.rnn(self.n_units, return_sequences=True)))
                    self.model.add(Dropout(self._dropout))
            if self._layers > 1:
                self.model.add(Bidirectional(self.rnn(self.n_units)))
                self.model.add(Dropout(self._dropout))

        self.model.add(Dense(self.n_classes, activation='softmax'))

        self.model.compile(loss='categorical_crossentropy',
                           optimizer=self._optimizer,
                           metrics=['accuracy'])
Пример #12
0
from keras.layers.normalization import BatchNormalization
import numpy as np
import pylab as plt

# We create a layer which take as input movies of shape
# (n_frames, width, height, channels) and returns a movie
# of identical shape.

seq = Sequential()
seq.add(
    ConvLSTM2D(filters=40,
               kernel_size=(3, 3),
               input_shape=(None, 40, 40, 1),
               padding='same',
               return_sequences=True))
seq.add(BatchNormalization())

seq.add(
    ConvLSTM2D(filters=40,
               kernel_size=(3, 3),
               padding='same',
               return_sequences=True))
seq.add(BatchNormalization())

seq.add(
    ConvLSTM2D(filters=40,
               kernel_size=(3, 3),
               padding='same',
               return_sequences=True))
seq.add(BatchNormalization())
Пример #13
0
def DenseNet(nb_classes,
             img_dim,
             depth,
             nb_dense_block,
             growth_rate,
             nb_filter,
             dropout_rate=None,
             weight_decay=1E-4):
    """ Build the DenseNet model

    :param nb_classes: int -- number of classes
    :param img_dim: tuple -- (channels, rows, columns)
    :param depth: int -- how many layers
    :param nb_dense_block: int -- number of dense blocks to add to end
    :param growth_rate: int -- number of filters to add
    :param nb_filter: int -- number of filters
    :param dropout_rate: float -- dropout rate
    :param weight_decay: float -- weight decay

    :returns: keras model with nb_layers of conv_factory appended
    :rtype: keras model

    """

    model_input = Input(shape=img_dim)

    assert (depth - 4) % 3 == 0, "Depth must be 3 N + 4"

    # layers in each dense block
    nb_layers = int((depth - 4) / 3)

    # Initial convolution
    x = Conv2D(nb_filter, (3, 3),
               kernel_initializer="he_uniform",
               padding="same",
               name="initial_conv2D",
               use_bias=False,
               kernel_regularizer=l2(weight_decay))(model_input)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        x, nb_filter = denseblock(x,
                                  nb_layers,
                                  nb_filter,
                                  growth_rate,
                                  dropout_rate=dropout_rate,
                                  weight_decay=weight_decay)
        # add transition
        x = transition(x,
                       nb_filter,
                       dropout_rate=dropout_rate,
                       weight_decay=weight_decay)

    # The last denseblock does not have a transition
    x, nb_filter = denseblock(x,
                              nb_layers,
                              nb_filter,
                              growth_rate,
                              dropout_rate=dropout_rate,
                              weight_decay=weight_decay)

    x = BatchNormalization(axis=1,
                           gamma_regularizer=l2(weight_decay),
                           beta_regularizer=l2(weight_decay))(x)
    x = Activation('relu')(x)
    x = GlobalAveragePooling2D(data_format=K.image_data_format())(x)
    x = Dense(nb_classes,
              activation='softmax',
              kernel_regularizer=l2(weight_decay),
              bias_regularizer=l2(weight_decay))(x)

    densenet = Model(inputs=[model_input], outputs=[x], name="DenseNet")

    return densenet
Пример #14
0
def make_generator(dense=True, labels_size=10):
    """Creates a generator model that takes a 100-dimensional noise vector as a "seed", and outputs images
    of size 28x28x1."""
    model = Sequential()

    # ------------------------------ Layer 1: Dense + LeakyReLu ---------------------------------------
    if dense:
        model.add(Dense(1024, input_dim=100 + labels_size))
        model.add(LeakyReLU())
        model.add(Dense(128 * 7 * 7))
    else:
        model.add(Dense(128 * 7 * 7, input_dim=100 + labels_size))

    # ------------------------------ Layer 2: Dense + LeakyReLu ---------------------------------------

    model.add(BatchNormalization())
    model.add(LeakyReLU())

    # - - - - - - - - - - - - - - - - - - - Reshape  - - - - - - - - - - - - - - - -
    if K.image_data_format() == 'channels_first':
        # size: 128 x 7 x 7
        model.add(Reshape((128, 7, 7), input_shape=(128 * 7 * 7, )))
        bn_axis = 1  # first
    else:
        # size: 7 x 7 x 128
        model.add(Reshape((7, 7, 128), input_shape=(128 * 7 * 7, )))
        bn_axis = -1  # last

    # ------------------------------ Layer 3: DeConv2D + LeakyReLu ---------------------------------------
    model.add(
        Conv2DTranspose(filters=128,
                        kernel_size=(5, 5),
                        strides=2,
                        padding='same'))
    model.add(BatchNormalization(axis=bn_axis))
    model.add(LeakyReLU())

    # ------------------------------ Layer 4: Conv2D + LeakyReLu ---------------------------------------
    model.add(Convolution2D(64, (5, 5), padding='same'))
    model.add(BatchNormalization(axis=bn_axis))
    model.add(LeakyReLU())

    # ------------------------------ Layer 5: DeConv2D + LeakyReLu ---------------------------------------
    model.add(Conv2DTranspose(64, (5, 5), strides=2, padding='same'))
    model.add(BatchNormalization(axis=bn_axis))
    model.add(LeakyReLU())

    # ------------------------------ Layer 6: Conv2D + Tanh ---------------------------------------
    # Because we normalized training inputs to lie in the range [-1, 1],
    # the tanh function should be used for the output of the generator to ensure its output
    # also lies in this range.
    model.add(Convolution2D(1, (5, 5), padding='same', activation='tanh'))

    # our idea:
    # seed 100
    # layer1: dense 1024
    # layer2: dense 7*7*128
    # reshape 7 x 7 x 128
    # layer3: Deconv 14 x 14 x 128
    # layer4: Conv   14 x 14 x 64
    # layer5: Deconv 28 x 28 x 64
    # layer6: Conv   28 x 28 x 1

    return model
Пример #15
0
def cnn_rnn(nb_words, EMBEDDING_DIM, \
            embedding_matrix, MAX_SEQUENCE_LENGTH, \
            num_rnn, num_dense, rate_drop_rnn, \
            rate_drop_dense, act):
    '''
    This is the basic cnn rnn model 

    model: input layer; embedding layer; cnn based attention layer; rnn layer; dense layer; output layer
    '''

    embedding_layer = Embedding(nb_words,
                                EMBEDDING_DIM,
                                weights=[embedding_matrix],
                                input_length=MAX_SEQUENCE_LENGTH,
                                trainable=False)

    rnn_layer = Bidirectional(
        GRU(num_rnn, dropout=rate_drop_rnn, recurrent_dropout=rate_drop_rnn))
    cnn_layer = Conv1D(activation="relu",
                       padding="valid",
                       strides=1,
                       filters=128,
                       kernel_size=2)
    # cnn_layer1 = Conv1D(activation="relu", padding="valid", strides=1, filters=64, kernel_size=4)
    pooling_layer = GlobalMaxPooling1D()
    cnn_dense = Dense(300)
    cnn_dropout1 = Dropout(0.35)
    cnn_dropout2 = Dropout(0.35)
    cnn_batchnormalization = BatchNormalization()
    cnn_repeatvector = RepeatVector(EMBEDDING_DIM)
    cnn_dense1 = Dense(300)
    cnn_timedistributed = TimeDistributed(Dense(1))

    sequence_1_input = Input(shape=(MAX_SEQUENCE_LENGTH, ), dtype='int32')
    embedded_sequences_1 = embedding_layer(sequence_1_input)

    sequence_2_input = Input(shape=(MAX_SEQUENCE_LENGTH, ), dtype='int32')
    embedded_sequences_2 = embedding_layer(sequence_2_input)

    cnn_1 = cnn_layer(embedded_sequences_1)
    # cnn_1 = cnn_layer1(cnn_1)
    cnn_1 = pooling_layer(cnn_1)
    cnn_1 = cnn_dropout1(cnn_1)
    cnn_1 = cnn_dense(cnn_1)
    cnn_1 = cnn_dropout2(cnn_1)
    cnn_1 = cnn_batchnormalization(cnn_1)

    cnn_2 = cnn_layer(embedded_sequences_2)
    # cnn_2 = cnn_layer1(cnn_2)
    cnn_2 = pooling_layer(cnn_2)
    cnn_2 = cnn_dropout1(cnn_2)
    cnn_2 = cnn_dense(cnn_2)
    cnn_2 = cnn_dropout2(cnn_2)
    cnn_2 = cnn_batchnormalization(cnn_2)

    # cnn_1 = cnn_repeatvector(cnn_1)
    # cnn_2 = cnn_repeatvector(cnn_2)

    cnn_1_t = cnn_dense1(cnn_1)
    cnn_2_t = cnn_dense1(cnn_2)

    # cnn_1_t = cnn_timedistributed(cnn_1)
    # cnn_2_t = cnn_timedistributed(cnn_2)

    # cnn_1_t = Permute([2, 1])(cnn_1_t)
    # cnn_2_t = Permute([2, 1])(cnn_2_t)

    a1 = multiply([cnn_1_t, embedded_sequences_1])
    a2 = multiply([cnn_2_t, embedded_sequences_2])

    a1 = Permute([2, 1])(a1)
    a2 = Permute([2, 1])(a2)

    a1 = Lambda(lambda x: K.sum(x, axis=1))(a1)
    a2 = Lambda(lambda x: K.sum(x, axis=1))(a2)

    a1 = Activation('softmax')(a1)
    a2 = Activation('softmax')(a2)

    embedded_sequences_1 = Permute([2, 1])(embedded_sequences_1)
    embedded_sequences_2 = Permute([2, 1])(embedded_sequences_2)

    x1 = multiply([a1, embedded_sequences_1])
    x2 = multiply([a2, embedded_sequences_2])

    x1 = Permute([2, 1])(x1)
    x2 = Permute([2, 1])(x2)

    x1 = rnn_layer(x1)
    x2 = rnn_layer(x2)

    merged = multiply([x1, x2])
    merged = Dropout(rate_drop_dense)(merged)
    merged = BatchNormalization()(merged)

    merged = Dense(num_dense, activation=act)(merged)
    merged = Dropout(rate_drop_dense)(merged)
    merged = BatchNormalization()(merged)

    preds = Dense(3, activation='softmax')(merged)

    # x1 = TimeDistributed(Dense(EMBEDDING_DIM, activation='relu'))(embedded_sequences_1)
    # x1 = Lambda(lambda x: K.max(x, axis=1), output_shape=(EMBEDDING_DIM, ))(x1)

    # y1 = TimeDistributed(Dense(EMBEDDING_DIM, activation='relu'))(embedded_sequences_2)
    # y1 = Lambda(lambda x: K.max(x, axis=1), output_shape=(EMBEDDING_DIM, ))(y1)

    ########################################
    ## train the model
    ########################################
    model = Model(inputs=[sequence_1_input, sequence_2_input], outputs=preds)
    model.compile(loss='categorical_crossentropy',
                  optimizer='nadam',
                  metrics=['acc'])
    model.summary()
    # print(STAMP)
    return model
Пример #16
0
def _main(args):
    config_path = os.path.expanduser(args.config_path)
    weights_path = os.path.expanduser(args.weights_path)
    assert config_path.endswith('.cfg'), '{} is not a .cfg file'.format(
        config_path)
    assert weights_path.endswith(
        '.weights'), '{} is not a .weights file'.format(weights_path)

    output_path = os.path.expanduser(args.output_path)
    assert output_path.endswith(
        '.h5'), 'output path {} is not a .h5 file'.format(output_path)
    output_root = os.path.splitext(output_path)[0]

    # Load weights and config.
    print('Loading weights.')
    weights_file = open(weights_path, 'rb')
    weights_header = np.ndarray(
        shape=(5, ), dtype='int32', buffer=weights_file.read(20))
    print('Weights Header: ', weights_header)
    # TODO: Check transpose flag when implementing fully connected layers.
    # transpose = (weight_header[0] > 1000) or (weight_header[1] > 1000)

    print('Parsing Darknet config.')
    unique_config_file = unique_config_sections(config_path)
    cfg_parser = configparser.ConfigParser()
    cfg_parser.read_file(unique_config_file)

    print('Creating Keras model.')
    if args.fully_convolutional:
        image_height, image_width = None, None
    else:
        image_height = int(cfg_parser['net_0']['height'])
        image_width = int(cfg_parser['net_0']['width'])

    prev_layer = Input(shape=(image_height, image_width, 3))
    all_layers = [prev_layer]
    outputs = []

    weight_decay = float(cfg_parser['net_0']['decay']
                         ) if 'net_0' in cfg_parser.sections() else 5e-4
    count = 0

    for section in cfg_parser.sections():
        print('Parsing section {}'.format(section))
        if section.startswith('convolutional'):
            filters = int(cfg_parser[section]['filters'])
            size = int(cfg_parser[section]['size'])
            stride = int(cfg_parser[section]['stride'])
            pad = int(cfg_parser[section]['pad'])
            activation = cfg_parser[section]['activation']
            batch_normalize = 'batch_normalize' in cfg_parser[section]

            # Setting weights.
            # Darknet serializes convolutional weights as:
            # [bias/beta, [gamma, mean, variance], conv_weights]
            prev_layer_shape = K.int_shape(prev_layer)

            # TODO: This assumes channel last dim_ordering.
            weights_shape = (size, size, prev_layer_shape[-1], filters)
            darknet_w_shape = (filters, weights_shape[2], size, size)
            weights_size = np.product(weights_shape)

            print('conv2d', 'bn'
                  if batch_normalize else '  ', activation, weights_shape)

            conv_bias = np.ndarray(
                shape=(filters, ),
                dtype='float32',
                buffer=weights_file.read(filters * 4))
            count += filters

            if batch_normalize:
                bn_weights = np.ndarray(
                    shape=(3, filters),
                    dtype='float32',
                    buffer=weights_file.read(filters * 12))
                count += 3 * filters

                # TODO: Keras BatchNormalization mistakenly refers to var
                # as std.
                bn_weight_list = [
                    bn_weights[0],  # scale gamma
                    conv_bias,  # shift beta
                    bn_weights[1],  # running mean
                    bn_weights[2]  # running var
                ]

            conv_weights = np.ndarray(
                shape=darknet_w_shape,
                dtype='float32',
                buffer=weights_file.read(weights_size * 4))
            count += weights_size

            # DarkNet conv_weights are serialized Caffe-style:
            # (out_dim, in_dim, height, width)
            # We would like to set these to Tensorflow order:
            # (height, width, in_dim, out_dim)
            # TODO: Add check for Theano dim ordering.
            conv_weights = np.transpose(conv_weights, [2, 3, 1, 0])
            conv_weights = [conv_weights] if batch_normalize else [
                conv_weights, conv_bias
            ]

            # Handle activation.
            act_fn = None
            if activation == 'leaky':
                pass  # Add advanced activation later.
            elif activation != 'linear':
                raise ValueError(
                    'Unknown activation function `{}` in section {}'.format(
                        activation, section))

            padding = 'same' if pad == 1 and stride == 1 else 'valid'
            # Adjust padding model for darknet.
            if stride == 2:
                prev_layer = ZeroPadding2D(((1, 0), (1, 0)))(prev_layer)

            # Create Conv2D layer
            conv_layer = (Conv2D(
                filters, (size, size),
                strides=(stride, stride),
                kernel_regularizer=l2(weight_decay),
                use_bias=not batch_normalize,
                weights=conv_weights,
                activation=act_fn,
                padding=padding))(prev_layer)

            if batch_normalize:
                conv_layer = (BatchNormalization(
                    weights=bn_weight_list))(conv_layer)

            prev_layer = conv_layer

            if activation == 'linear':
                all_layers.append(prev_layer)
            elif activation == 'leaky':
                act_layer = LeakyReLU(alpha=0.1)(prev_layer)
                prev_layer = act_layer
                all_layers.append(act_layer)
        elif section.startswith('maxpool'):
            size = int(cfg_parser[section]['size'])
            stride = int(cfg_parser[section]['stride'])
            all_layers.append(
                MaxPooling2D(
                    padding='same',
                    pool_size=(size, size),
                    strides=(stride, stride))(prev_layer))
            prev_layer = all_layers[-1]
        elif section.startswith('avgpool'):
            if cfg_parser.items(section) != []:
                raise ValueError('{} with params unsupported.'.format(section))
            all_layers.append(GlobalAveragePooling2D()(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('route'):
            ids = [int(i) for i in cfg_parser[section]['layers'].split(',')]
            layers = [all_layers[i] for i in ids]

            if len(layers) > 1:
                print('Concatenating route layers:', layers)
                concatenate_layer = concatenate(layers)
                all_layers.append(concatenate_layer)
                prev_layer = concatenate_layer
            else:
                skip_layer = layers[0]  # only one layer to route
                all_layers.append(skip_layer)
                prev_layer = skip_layer

        elif section.startswith('shortcut'):
            ids = [int(i) for i in cfg_parser[section]['from'].split(',')][0]
            activation = cfg_parser[section]['activation']
            shortcut = add([all_layers[ids], prev_layer])
            if activation == 'linear':
                shortcut = Activation('linear')(shortcut)
            all_layers.append(shortcut)
            prev_layer = all_layers[-1]

        elif section.startswith('upsample'):
            stride = int(cfg_parser[section]['stride'])
            all_layers.append(
                UpSampling2D(
                    size=(stride, stride))(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('yolo'):
            classes = int(cfg_parser[section]['classes'])
            # num = int(cfg_parser[section]['num'])
            # mask = int(cfg_parser[section]['mask'])
            n1, n2 = int(prev_layer.shape[1]), int(prev_layer.shape[2])
            n3 = 3
            n4 = (4 + 1 + classes)
            yolo = Reshape((n1, n2, n3, n4))(prev_layer)
            all_layers.append(yolo)
            prev_layer = all_layers[-1]
            outputs.append(len(all_layers) - 1)

        elif (section.startswith('net')):
            pass  # Configs not currently handled during model definition.
        else:
            raise ValueError(
                'Unsupported section header type: {}'.format(section))

    # Create and save model.
    model = Model(inputs=all_layers[0],
                  outputs=[all_layers[i] for i in outputs])
    print(model.summary())
    model.save('{}'.format(output_path))
    print('Saved Keras model to {}'.format(output_path))
    # Check to see if all weights have been read.
    remaining_weights = len(weights_file.read()) / 4
    weights_file.close()
    print('Read {} of {} from Darknet weights.'.format(count, count +
                                                       remaining_weights))
    if remaining_weights > 0:
        print('Warning: {} unused weights'.format(remaining_weights))

    if args.plot_model:
        plot(model, to_file='{}.png'.format(output_root), show_shapes=True)
        print('Saved model plot to {}.png'.format(output_root))
Пример #17
0
def basic_cnn(nb_words, EMBEDDING_DIM, \
              embedding_matrix, MAX_SEQUENCE_LENGTH, \
              num_rnn, num_dense, rate_drop_rnn, \
              rate_drop_dense, act):
    '''
    This is the basic cnn model 

    model: input layer; embedding layer; several cnn layer; dense layer; output layer
    '''
    embedding_layer = Embedding(nb_words,
                                EMBEDDING_DIM,
                                weights=[embedding_matrix],
                                input_length=MAX_SEQUENCE_LENGTH,
                                trainable=True)

    conv1 = Conv1D(filters=128,
                   kernel_size=1,
                   padding='same',
                   activation='relu')
    conv2 = Conv1D(filters=128,
                   kernel_size=2,
                   padding='same',
                   activation='relu')
    conv3 = Conv1D(filters=128,
                   kernel_size=3,
                   padding='same',
                   activation='relu')
    conv4 = Conv1D(filters=128,
                   kernel_size=4,
                   padding='same',
                   activation='relu')
    conv5 = Conv1D(filters=32,
                   kernel_size=5,
                   padding='same',
                   activation='relu')
    conv6 = Conv1D(filters=32,
                   kernel_size=6,
                   padding='same',
                   activation='relu')

    sequence_1_input = Input(shape=(MAX_SEQUENCE_LENGTH, ), dtype='int32')
    embedded_sequences_1 = embedding_layer(sequence_1_input)

    sequence_2_input = Input(shape=(MAX_SEQUENCE_LENGTH, ), dtype='int32')
    embedded_sequences_2 = embedding_layer(sequence_2_input)

    conv1a = conv1(embedded_sequences_1)
    glob1a = GlobalAveragePooling1D()(conv1a)
    conv1b = conv1(embedded_sequences_2)
    glob1b = GlobalAveragePooling1D()(conv1b)

    conv2a = conv2(embedded_sequences_1)
    glob2a = GlobalAveragePooling1D()(conv2a)
    conv2b = conv2(embedded_sequences_2)
    glob2b = GlobalAveragePooling1D()(conv2b)

    conv3a = conv3(embedded_sequences_1)
    glob3a = GlobalAveragePooling1D()(conv3a)
    conv3b = conv3(embedded_sequences_2)
    glob3b = GlobalAveragePooling1D()(conv3b)

    conv4a = conv4(embedded_sequences_1)
    glob4a = GlobalAveragePooling1D()(conv4a)
    conv4b = conv4(embedded_sequences_2)
    glob4b = GlobalAveragePooling1D()(conv4b)

    conv5a = conv5(embedded_sequences_1)
    glob5a = GlobalAveragePooling1D()(conv5a)
    conv5b = conv5(embedded_sequences_2)
    glob5b = GlobalAveragePooling1D()(conv5b)

    conv6a = conv6(embedded_sequences_1)
    glob6a = GlobalAveragePooling1D()(conv6a)
    conv6b = conv6(embedded_sequences_2)
    glob6b = GlobalAveragePooling1D()(conv6b)

    mergea = concatenate([glob1a, glob2a, glob3a, glob4a, glob5a, glob6a])
    mergeb = concatenate([glob1b, glob2b, glob3b, glob4b, glob5b, glob6b])

    # We take the explicit absolute difference between the two sentences
    # Furthermore we take the multiply different entries to get a different measure of equalness
    diff = Lambda(lambda x: K.abs(x[0] - x[1]),
                  output_shape=(4 * 128 + 2 * 32, ))([mergea, mergeb])
    mul = Lambda(lambda x: x[0] * x[1],
                 output_shape=(4 * 128 + 2 * 32, ))([mergea, mergeb])

    merge = concatenate([diff, mul])

    # The MLP that determines the outcome
    x = Dropout(0.2)(merge)
    x = BatchNormalization()(x)
    x = Dense(300, activation='relu')(x)

    x = Dropout(0.2)(x)
    x = BatchNormalization()(x)
    preds = Dense(3, activation='softmax')(x)

    ########################################
    ## train the model
    ########################################
    model = Model(inputs=[sequence_1_input, sequence_2_input], outputs=preds)
    model.compile(loss='categorical_crossentropy',
                  optimizer='nadam',
                  metrics=['acc'])
    model.summary()
    # print(STAMP)
    return model
Пример #18
0
def fcn_8s(num_classes, init_lr, input_shape, vgg_weight_path=None):
    img_input = Input(input_shape)

    # Block 1
    x = Conv2D(64, (3, 3), padding='same', name='block1_conv1')(img_input)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = Conv2D(64, (3, 3), padding='same', name='block1_conv2')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = MaxPooling2D()(x)

    # Block 2
    x = Conv2D(128, (3, 3), padding='same', name='block2_conv1')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = Conv2D(128, (3, 3), padding='same', name='block2_conv2')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = MaxPooling2D()(x)

    # Block 3
    x = Conv2D(256, (3, 3), padding='same', name='block3_conv1')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = Conv2D(256, (3, 3), padding='same', name='block3_conv2')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = Conv2D(256, (3, 3), padding='same', name='block3_conv3')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    block_3_out = MaxPooling2D()(x)

    # Block 4
    x = Conv2D(512, (3, 3), padding='same', name='block4_conv1')(block_3_out)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = Conv2D(512, (3, 3), padding='same', name='block4_conv2')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = Conv2D(512, (3, 3), padding='same', name='block4_conv3')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    block_4_out = MaxPooling2D()(x)

    # Block 5
    x = Conv2D(512, (3, 3), padding='same', name='block5_conv1')(block_4_out)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = Conv2D(512, (3, 3), padding='same', name='block5_conv2')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = Conv2D(512, (3, 3), padding='same', name='block5_conv3')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = MaxPooling2D()(x)

    # Load pretrained weights.
    if vgg_weight_path is not None:
        vgg16 = Model(img_input, x)
        vgg16.load_weights(vgg_weight_path, by_name=True)

    # Convolutinalized fully connected layer.
    x = Conv2D(4096, (7, 7), activation='relu', padding='same')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(4096, (1, 1), activation='relu', padding='same')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    # Classifying layers.
    x = Conv2D(num_classes, (1, 1), strides=(1, 1), activation='linear')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    block_3_out = Conv2D(num_classes, (1, 1), strides=(1, 1), activation='linear')(block_3_out)
    block_3_out = BatchNormalization()(block_3_out)
    block_3_out = Activation('relu')(block_3_out)

    block_4_out = Conv2D(num_classes, (1, 1), strides=(1, 1), activation='linear')(block_4_out)
    block_4_out = BatchNormalization()(block_4_out)
    block_4_out = Activation('relu')(block_4_out)

    x = Lambda(lambda x: tf.image.resize_images(x, (x.shape[1] * 2, x.shape[2] * 2)))(x)
    x = Add()([x, block_4_out])
    x = Lambda(lambda x: tf.image.resize_images(x, (x.shape[1] * 2, x.shape[2] * 2)))(x)
    x = Add()([x, block_3_out])
    x = Lambda(lambda x: tf.image.resize_images(x, (x.shape[1] * 8, x.shape[2] * 8)))(x)

    x = Activation('softmax')(x)
    model = Model(img_input, x)
    model.compile(optimizer=Adam(lr=init_lr, decay=5e-4),
                  loss='categorical_crossentropy',
                  metrics=[dice_coef])

    return model
Пример #19
0
def basic_attention(nb_words, EMBEDDING_DIM, \
                    embedding_matrix, MAX_SEQUENCE_LENGTH, \
                    num_rnn, num_dense, rate_drop_rnn, \
                    rate_drop_dense, act):
    '''
    This is the basic attention model 

    model: input layer; embedding layer; rnn layer; attention layer; dense layer; output layer
    '''
    embedding_layer = Embedding(nb_words,
                                EMBEDDING_DIM,
                                weights=[embedding_matrix],
                                input_length=MAX_SEQUENCE_LENGTH,
                                trainable=True)
    rnn_layer = Bidirectional(
        GRU(num_rnn,
            dropout=rate_drop_rnn,
            recurrent_dropout=rate_drop_rnn,
            return_sequences=True))
    attention_W = TimeDistributed(Dense(350, activation='tanh'))
    attention_w = TimeDistributed(Dense(1))
    attention_softmax = Activation('softmax')
    attention_sum = Lambda(lambda x: K.sum(x, axis=1))

    sequence_1_input = Input(shape=(MAX_SEQUENCE_LENGTH, ), dtype='int32')
    embedded_sequences_1 = embedding_layer(sequence_1_input)
    x1 = rnn_layer(embedded_sequences_1)

    attention1 = attention_W(x1)
    attention1 = attention_w(attention1)
    attention1 = attention_softmax(attention1)
    attention1 = Permute([2, 1])(attention1)
    x1 = Permute([2, 1])(x1)
    x1 = multiply([attention1, x1])
    x1 = Permute([2, 1])(x1)
    x1 = attention_sum(x1)

    sequence_2_input = Input(shape=(MAX_SEQUENCE_LENGTH, ), dtype='int32')
    embedded_sequences_2 = embedding_layer(sequence_2_input)
    x2 = rnn_layer(embedded_sequences_2)

    attention2 = attention_W(x2)
    attention2 = attention_w(attention2)
    attention2 = attention_softmax(attention2)
    attention2 = Permute([2, 1])(attention2)
    x2 = Permute([2, 1])(x2)
    x2 = multiply([attention2, x2])
    x2 = Permute([2, 1])(x2)
    x2 = attention_sum(x2)

    merged = multiply([x1, x2])
    merged = Dropout(rate_drop_dense)(merged)
    merged = BatchNormalization()(merged)

    merged = Dense(num_dense, activation=act)(merged)
    merged = Dropout(rate_drop_dense)(merged)
    merged = BatchNormalization()(merged)

    preds = Dense(3, activation='softmax')(merged)

    ########################################
    ## train the model
    ########################################
    model = Model(inputs=[sequence_1_input, sequence_2_input], outputs=preds)
    model.compile(loss='categorical_crossentropy',
                  optimizer='nadam',
                  metrics=['acc'])
    model.summary()
    # print(STAMP)
    return model
Пример #20
0
def define_epi(sz_input,sz_input2,filt_num,learning_rate):

    input_x = Input(shape=(sz_input,sz_input2,3), name='input_x')
    input_y = Input(shape=(sz_input,sz_input2,3), name='input_y')
    
    mid_merged_uv1=ORM(input_x,filt_num,9,29,'input_x',True)
    mid_merged_uv2=ORM(input_y,filt_num,9,29,'input_y',True)
    
    mid_input_x=layer2_1(sz_input,sz_input2,32,int(filt_num))(mid_merged_uv1)
    mid_input_y=layer2_2(sz_input,sz_input2,32,int(filt_num))(mid_merged_uv2)    
    
    mid_input_x=layer_mid('input_x',filt_num)(mid_input_x)
    mid_input_y=layer_mid('input_y',filt_num)(mid_input_y)
    
    mid_uv1=ORM(mid_input_x,filt_num,1,15,'input_x_mid',False)
    mid_uv2=ORM(mid_input_y,filt_num,1,15,'input_y_mid',False)
     
    '''
    tmp1 = Conv2D(int(filt_num),(1,2), strides=(1, 1),padding='valid',data_format='channels_last', kernel_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None), bias_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None) ,name='S3_c_1_last'  )(mid_merged_uv1)# pow(25/23,2)*12*(maybe7?) 43 3
    tmp1 = Activation('relu', name='S_3_relu1_last' )(tmp1)
    tmp1 = Conv2D(int(filt_num),(1,2), strides=(1, 1),padding='valid',kernel_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None), bias_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None) , name='S3_c2_last')(tmp1)
    tmp1 = BatchNormalization(axis=-1, name='S_3_BN_last')(tmp1)
    tmp1 = Activation('relu', name='S3_relu2_last')(tmp1)
    
    tmp1=Conv2D(int(filt_num),(1,2), strides=(1, 1),padding='valid', kernel_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None), bias_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None) ,name='S1_last_c1')(tmp1)     
    tmp1=Activation('relu', name='S1_last_relu')(tmp1)
    output1=Conv2D(1,(1,2),strides=(1, 1), padding='valid', kernel_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None), bias_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None) ,name='S1_last_c2') (tmp1)
    '''
    '''
    tmp2 = Conv2D(int(filt_num),(1,2), strides=(1, 1),padding='valid',data_format='channels_last', kernel_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None), bias_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None) ,name='S3_c1_last_new'  )(mid_merged_uv2)# pow(25/23,2)*12*(maybe7?) 43 3
    tmp2 = Activation('relu', name='S3_relu1_last_new' )(tmp2)
    tmp2 = Conv2D(int(filt_num),(1,2), strides=(1, 1),padding='valid',kernel_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None), bias_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None) , name='S3_c2_last_')(tmp2)
    tmp2 = BatchNormalization(axis=-1, name='S_3_BN_last_')(tmp2)
    tmp2 = Activation('relu', name='S3_relu2_last_')(tmp2)
   
    
    tmp2=Conv2D(int(filt_num),(1,2), strides=(1, 1),padding='valid', kernel_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None), bias_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None) ,name='S2_last_c1')(tmp2)     
    tmp2=Activation('relu', name='S2_last_relu')(tmp2)
    output2=Conv2D(1,(1,2),strides=(1, 1), padding='valid', kernel_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None), bias_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=None) ,name='S2_last_c2') (tmp2)
    '''
    
    mid_input_x_orm=res_block_1(mid_uv1,filt_num)
    mid_input_y_orm=res_block_2(mid_uv2,filt_num)
   
    
    mid_merged=concatenate([mid_input_x_orm, mid_input_y_orm],  name='mid_merged')  
    
    
    mid_merged=Conv2D(int(filt_num),(1,2), strides=(1, 1),padding='valid', name='s3_last_c5')(mid_merged)
    mid_merged=Activation('relu', name='S3_relu1_last_5')(mid_merged)
    mid_merged=Conv2D(int(filt_num),(1,2), strides=(1, 1),padding='valid', name='s3_last_c6')(mid_merged)
    mid_merged=BatchNormalization(axis=-1, name='S3_BN_last_3')(mid_merged)
    mid_merged=Activation('relu', name='S3_relu1_last_6')(mid_merged)
    
    mid_merged=Conv2D(2*int(filt_num),(1,1), strides=(1, 1),padding='valid',name='S3_c2%d'%(1) )(mid_merged)
    mid_merged=Activation('relu', name='S3_relu_%d' %(1))(mid_merged)
    output=Conv2D(1,(1,1), strides=(1, 1),  padding='valid' ,  name='loss_output')(mid_merged)
    
    
    model_512 = Model(inputs = [input_x, input_y ], outputs = output)   #output1,output2,
    opt = RMSprop(lr=learning_rate)
    #RMSprop(lr=learning_rate)
    model_512.compile(optimizer=opt, loss= 'mae')  #'mae','mae', loss_weights=[0.5, 0.5, 1.]
    
    #model_512.summary() 
    
    return model_512
Пример #21
0
nb_filters = 32

pool_size = (2, 2)

kernel_size = (3, 3)
input_shape = (28, 28, 1)

learning_rate = 0.02
decay_rate = 5e-5
momentum = 0.9

denoise = Sequential()
denoise.add(
    Convolution2D(20, 3, 3, border_mode='valid', input_shape=input_shape))
denoise.add(BatchNormalization(mode=2))
denoise.add(Activation('relu'))
denoise.add(UpSampling2D(size=(2, 2)))
denoise.add(Convolution2D(20, 3, 3, init='glorot_uniform'))
denoise.add(BatchNormalization(mode=2))
denoise.add(Activation('relu'))
denoise.add(Convolution2D(20, 3, 3, init='glorot_uniform'))
denoise.add(BatchNormalization(mode=2))
denoise.add(Activation('relu'))
denoise.add(MaxPooling2D(pool_size=(3, 3)))
denoise.add(Convolution2D(4, 3, 3, init='glorot_uniform'))
denoise.add(BatchNormalization(mode=2))
denoise.add(Activation('relu'))
denoise.add(Reshape((28, 28, 1)))
sgd = SGD(lr=learning_rate,
          momentum=momentum,
Пример #22
0
def se_create_dense_net(nb_layers,
                        growth_rate=12,
                        nb_filter=64,
                        bottleneck=True,
                        reduction=0.1,
                        dropout_rate=None,
                        subsample_initial_block=True):

    inputs = Input(shape=(280, 280, 16, 1))
    print("0 :inputs shape:", inputs.shape)

    # 设定每个denseblock中convblock的数量:nb_layers = [3,3,3]

    concat_axis = -1  # 设定concat的轴(即叠加的轴)
    bn_axis = -1  # 设定BN的轴(即叠加的轴)
    nb_dense_block = nb_layers.__len__(
    )  # nb_dense_block :denseblock的数量,需要和nb_layers对应
    final_nb_layer = nb_layers[-1]
    compression = 1.0 - reduction  # denseblock的通道衰减率,即实际输出通道数=原输出通道数x通道衰减率

    # Initial convolution =======================================================================================
    if subsample_initial_block:
        initial_kernel = (7, 7, 7)
        initial_strides = (2, 2, 1)
    else:
        initial_kernel = (3, 3, 3)
        initial_strides = (1, 1, 1)

    x = Conv3D(nb_filter,
               initial_kernel,
               kernel_initializer='he_normal',
               padding='same',
               strides=initial_strides,
               use_bias=False)(inputs)

    if subsample_initial_block:
        x = BatchNormalization(axis=bn_axis, epsilon=1.1e-5)(x)
        x = Activation('relu')(x)
        x = MaxPooling3D((3, 3, 3), strides=(2, 2, 1), padding='same')(x)

    print("0 :Initial conv shape:", x.shape)
    # Initial convolution finished ================================================================================

    # Add dense blocks start  ==================================================================================
    for block_idx in range(nb_dense_block - 1):
        x, nb_filter = __dense_block(x,
                                     nb_layers[block_idx],
                                     nb_filter,
                                     growth_rate,
                                     concat_axis=concat_axis,
                                     bn_axis=bn_axis,
                                     bottleneck=bottleneck,
                                     dropout_rate=dropout_rate,
                                     grow_nb_filters=True)
        print(block_idx + 1, ":dense_block shape:", x.shape)

        x = __transition_block(x,
                               nb_filter,
                               compression=compression,
                               concat_axis=concat_axis,
                               bias_allow=False)
        print(block_idx + 1, ":transition_block shape:", x.shape)

        x = squeeze_excite_block3d(x)
        print(block_idx + 1, ":se_block_out shape:", x.shape)

        nb_filter = int(nb_filter * compression)
    # Add dense blocks finish ==================================================================================

    # The last dense_block does not have a transition_block
    x, nb_filter = __dense_block(x,
                                 final_nb_layer,
                                 nb_filter,
                                 growth_rate,
                                 concat_axis=concat_axis,
                                 bn_axis=bn_axis,
                                 bottleneck=bottleneck,
                                 dropout_rate=dropout_rate,
                                 grow_nb_filters=True)
    print(nb_dense_block, ":dense_block shape:", x.shape)

    x = BatchNormalization(axis=bn_axis, epsilon=1.1e-5)(x)
    x = Activation('relu')(x)

    out = GlobalAveragePooling3D(data_format='channels_last')(x)
    print("GApooling shape:", out.shape)
    out_drop = Dropout(rate=0.3)(out)
    out = Dense(1, name='fc1')(out_drop)
    print("out shape:", out.shape)
    output = Activation(activation='sigmoid')(out)

    model = Model(input=inputs, output=output)
    #mean_squared_logarithmic_error or binary_crossentropy
    model.compile(optimizer=SGD(lr=1e-6, momentum=0.9),
                  loss=EuiLoss,
                  metrics=[y_t, y_pre, Acc])

    return model
Пример #23
0
def create_dense_net(nb_classes,
                     img_dim,
                     depth=40,
                     nb_dense_block=3,
                     growth_rate=12,
                     nb_filter=-1,
                     bottleneck=False,
                     reduction=0.0,
                     dropout_rate=None,
                     weight_decay=1E-4,
                     verbose=True):
    ''' Build the create_dense_net model

    Args:
        nb_classes: number of classes
        img_dim: tuple of shape (channels, rows, columns) or (rows, columns, channels)
        depth: number or layers
        nb_dense_block: number of dense blocks to add to end (generally = 3)
        growth_rate: number of filters to add per dense block
        nb_filter: initial number of filters. Default -1 indicates initial number of filters is 2 * growth_rate
        bottleneck: add bottleneck blocks
        reduction: reduction factor of transition blocks. Note : reduction value is inverted to compute compression
        dropout_rate: dropout rate
        weight_decay: weight decay
        verbose: print the model type

    Returns: keras tensor with nb_layers of conv_block appended

    '''

    model_input = Input(shape=img_dim)

    concat_axis = 1 if K.image_dim_ordering() == "th" else -1

    assert (depth - 4) % 3 == 0, "Depth must be 3 N + 4"
    if reduction != 0.0:
        assert reduction <= 1.0 and reduction > 0.0, "reduction value must lie between 0.0 and 1.0"

    # layers in each dense block
    nb_layers = int((depth - 4) / 3)

    if bottleneck:
        nb_layers = int(nb_layers // 2)

    # compute initial nb_filter if -1, else accept users initial nb_filter
    if nb_filter <= 0:
        nb_filter = 2 * growth_rate

    # compute compression factor
    compression = 1.0 - reduction

    # Initial convolution
    x = Convolution2D(nb_filter,
                      3,
                      3,
                      init="he_uniform",
                      border_mode="same",
                      name="initial_conv2D",
                      bias=False,
                      W_regularizer=l2(weight_decay))(model_input)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        x, nb_filter = dense_block(x,
                                   nb_layers,
                                   nb_filter,
                                   growth_rate,
                                   bottleneck=bottleneck,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)
        # add transition_block
        x = transition_block(x,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    # The last dense_block does not have a transition_block
    x, nb_filter = dense_block(x,
                               nb_layers,
                               nb_filter,
                               growth_rate,
                               bottleneck=bottleneck,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNormalization(mode=0,
                           axis=concat_axis,
                           gamma_regularizer=l2(weight_decay),
                           beta_regularizer=l2(weight_decay))(x)
    x = Activation('relu')(x)
    x = GlobalAveragePooling2D()(x)
    x = Dense(nb_classes,
              activation='softmax',
              W_regularizer=l2(weight_decay),
              b_regularizer=l2(weight_decay))(x)

    densenet = Model(inputs=model_input, outputs=x, name="create_dense_net")

    if verbose:
        if bottleneck and not reduction:
            print("Bottleneck DenseNet-B-%d-%d created." %
                  (depth, growth_rate))
        elif not bottleneck and reduction > 0.0:
            print("DenseNet-C-%d-%d with %0.1f compression created." %
                  (depth, growth_rate, compression))
        elif bottleneck and reduction > 0.0:
            print(
                "Bottleneck DenseNet-BC-%d-%d with %0.1f compression created."
                % (depth, growth_rate, compression))
        else:
            print("DenseNet-%d-%d created." % (depth, growth_rate))

    return densenet
Пример #24
0
                        global_emb_dim,
                        weights=[emb_matrix],
                        input_length=global_max_seq,
                        trainable=False)

branch_3 = Sequential()
branch_3.add(input_layer)
branch_3.add(
    Conv1D(filters=32,
           kernel_size=3,
           padding='same',
           kernel_regularizer=l2(.01)))
branch_3.add(Activation('relu'))
branch_3.add(MaxPooling1D(pool_size=2))
branch_3.add(Dropout(0.5))
branch_3.add(BatchNormalization())
branch_3.add(LSTM(100))

branch_4 = Sequential()
branch_4.add(input_layer)
branch_4.add(
    Conv1D(filters=32,
           kernel_size=4,
           padding='same',
           kernel_regularizer=l2(.01)))
branch_4.add(Activation('relu'))
branch_4.add(MaxPooling1D(pool_size=2))
branch_4.add(Dropout(0.5))
branch_4.add(BatchNormalization())
branch_4.add(LSTM(100))
Пример #25
0
def create_model(nb_classes, input_shape):
    """Create a VGG-16 like model."""
    model = Sequential()
    print("input_shape: %s" % str(input_shape))
    # input_shape = (None, None, 3)  # for fcn
    model.add(
        Convolution2D(32, (3, 3),
                      padding='same',
                      input_shape=input_shape,
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001)))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(
        Convolution2D(32, (3, 3),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001)))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(
        Convolution2D(32, (2, 2),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001),
                      strides=2))

    model.add(
        Convolution2D(64, (3, 3),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001)))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(
        Convolution2D(64, (3, 3),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001)))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(
        Convolution2D(64, (2, 2),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001),
                      strides=2))

    model.add(
        Convolution2D(64, (3, 3),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001)))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(
        Convolution2D(64, (2, 2),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001),
                      strides=2))

    model.add(Convolution2D(512, (4, 4), padding='valid'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(
        Convolution2D(512, (1, 1),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001)))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(
        Convolution2D(nb_classes, (1, 1),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001)))
    model.add(BatchNormalization())
    model.add(Activation('softmax'))
    model.add(Flatten())  # Remove for FCN
    return model
def FCN_VGG_16():
    input_shape = (None, None, 3)
    classes = 20
    weights = None
    model = Sequential()
    in_puts = Input(shape=input_shape)
    model.add(ZeroPadding2D((1, 1), input_shape=(None, None, 3)))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(128, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(128, (3, 3), activation='relu'))
    model.add(BatchNormalization())
    #model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(256, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(256, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(256, (3, 3), activation='relu'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    #model.add(Flatten())
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    #model.add(Dense(1000, activation='softmax'))

    #x = Conv2D(classes, (3, 3), activation='relu')
    x = model.add(Conv2D(filters=classes, kernel_size=(1, 1)))
    out_put = Conv2DTranspose(filters=classes,
                              kernel_size=(64, 64),
                              strides=(32, 32),
                              padding='same',
                              activation='sigmoid',
                              kernel_initializer=Constant(weights))(x)
    model = Model(inputs=in_puts, outputs=out_put)

    model.compile(loss='binary_crossentropy',
                  optimizer='sgd',
                  metrics=['accuracy'])
    model.summary()
    return model()
Пример #27
0
 def FCBlock(self):
     model = self.model
     model.add(Dense(4096, activation='relu'))
     model.add(BatchNormalization())
     model.add(Dropout(0.5))
Пример #28
0
def cifar10_func():
    st.title("CIFAR-10")
    st.write(
        "The dataset is a collection of images that are commonly used to train machine learning and computer vision algorithms. This dataset is used for training a multiclass classification model that can classify or recognize images belonging to 10 different classes."
    )

    from keras.datasets import cifar10

    (x_train, y_train), (x_test, y_test) = cifar10.load_data()
    classes = [
        'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog',
        'horse', 'ship', 'truck'
    ]

    st.write(" ")
    st.markdown('**Shape**')
    st.write('\nTraining dataset :', x_train.shape, "\nTesting dataset :",
             x_test.shape)

    st.write("**Data** ")

    rand_14 = np.random.randint(0, x_train.shape[0], 14)
    sample_digits = x_train[rand_14]
    sample_labels = y_train[rand_14]

    num_rows, num_cols = 2, 7
    f, ax = plt.subplots(num_rows,
                         num_cols,
                         figsize=(12, 5),
                         gridspec_kw={
                             'wspace': 0.03,
                             'hspace': 0.01
                         },
                         squeeze=True)

    for r in range(num_rows):
        for c in range(num_cols):
            image_index = r * 7 + c
            ax[r, c].axis("off")
            ax[r, c].imshow(sample_digits[image_index], cmap='gray')
            ax[r, c].set_title('%s' % classes[int(sample_labels[image_index])])
    plt.show()
    st.pyplot(clear_figure=False)

    st.write("**Classes** ")
    s = ""
    for i in range(len(classes)):
        if i is not (len(classes) - 1):
            s += str(classes[i]).title()
            s += ","
            s += " "
        else:
            s += str(classes[i])
    st.write(s)

    image_height = x_train.shape[1]
    image_width = x_train.shape[2]
    num_channels = 3

    x_train_min = x_train.min(axis=(1, 2), keepdims=True)
    x_train_max = x_train.max(axis=(1, 2), keepdims=True)
    x_train = (x_train - x_train_min) / (x_train_max - x_train_min)

    x_test_min = x_test.min(axis=(1, 2), keepdims=True)
    x_test_max = x_test.max(axis=(1, 2), keepdims=True)
    x_test = (x_test - x_test_min) / (x_test_max - x_test_min)

    x_train = np.reshape(
        x_train, (x_train.shape[0], image_height, image_width, num_channels))
    x_test = np.reshape(
        x_test, (x_test.shape[0], image_height, image_width, num_channels))

    y_train, y_test = to_categorical(y_train), to_categorical(y_test)

    st.write("")
    st.write("**Build Model**")

    act = st.selectbox("Choose the type of Activation Function ",
                       ('relu', 'sigmoid', 'tanh'))

    pad = st.selectbox("Choose the Padding ", ('same', 'valid'))

    dropout = st.checkbox("Dropout")

    opt = st.selectbox("Choose the type of Optimizer ",
                       ("adam", "sgd", "rmsprop", "adagrad"))

    val = st.checkbox('Validation Set')
    epoch = st.slider("Epochs", 0, 250, step=1)
    b_s = st.slider("Batch Size", 32, 1024, step=32)

    st.write("")
    st.write("")

    if st.button("Train Model"):
        model = Sequential()

        model.add(
            Conv2D(32, kernel_size=3, activation=act, input_shape=(32, 32, 3)))
        model.add(BatchNormalization())
        if dropout:
            model.add(Dropout(0.2))
        model.add(
            Conv2D(64, kernel_size=3, strides=1, activation=act, padding=pad))
        model.add(BatchNormalization())
        model.add(MaxPooling2D((2, 2)))
        model.add(
            Conv2D(128, kernel_size=3, strides=1, padding=pad, activation=act))
        model.add(BatchNormalization())
        model.add(MaxPooling2D((2, 2)))
        model.add(Conv2D(64, kernel_size=3, activation=act))
        model.add(BatchNormalization())
        model.add(MaxPooling2D((2, 2)))
        if dropout:
            model.add(Dropout(0.2))
        model.add(Flatten())
        model.add(Dense(512, activation="relu"))
        model.add(Dropout(0.2))
        model.add(Dense(len(classes), activation="softmax"))
        model.compile(loss="categorical_crossentropy",
                      optimizer=opt,
                      metrics=["accuracy"])
        with st.spinner(
                'Training may take a while, so grab a cup of coffee, or better, go for a run!'
        ):
            if val:
                result = model.fit(x_train,
                                   y_train,
                                   batch_size=int(b_s),
                                   epochs=int(epoch),
                                   validation_split=0.2)
            else:
                result = model.fit(x_train,
                                   y_train,
                                   batch_size=int(b_s),
                                   epochs=int(epoch))
        st.success("Model Trained.")
        results = model.evaluate(x_test, y_test, batch_size=128)
        st.write("Loss: ", results[0])
        st.write("Accuracy: ", results[1])
        model.save("models/cifar10.h5")
        st.write("**Predictions** (Random Test Samples)")
        Images = []
        pred = ""

        for i in range(5):
            r = np.random.randint(0, len(x_test))
            Images.append(x_test[r].reshape(x_train.shape[1],
                                            x_train.shape[2]))
            pred += str(classes[model.predict(x_test[r].reshape(
                -1, x_train.shape[1], x_train.shape[2], 3)).argmax()])
            pred += " "

        st.image(Images, width=100)
        st.write(pred)
def build_model(input_shape):

    xin = Input(input_shape)

    #shift the below down by one
    x1 = conv_block(xin, 8, activation='crelu')  #outputs 13 ch
    x1_ident = AveragePooling3D()(xin)
    x1_merged = merge([x1, x1_ident], mode='concat', concat_axis=1)

    x2_1 = conv_block(x1_merged,
                      24,
                      activation='crelu',
                      init=looks_linear_init)  #outputs 37 ch
    x2_ident = AveragePooling3D()(x1_ident)
    x2_merged = merge([x2_1, x2_ident], mode='concat', concat_axis=1)

    #by branching we reduce the #params
    x3_ident = AveragePooling3D()(x2_ident)

    x3_diam = conv_block(x2_merged,
                         36,
                         activation='crelu',
                         init=looks_linear_init)  #outputs 25 + 16 ch = 41
    x3_lob = conv_block(x2_merged,
                        36,
                        activation='crelu',
                        init=looks_linear_init)  #outputs 25 + 16 ch = 41
    x3_spic = conv_block(x2_merged,
                         36,
                         activation='crelu',
                         init=looks_linear_init)  #outputs 25 + 16 ch = 41
    x3_malig = conv_block(x2_merged,
                          36,
                          activation='crelu',
                          init=looks_linear_init)  #outputs 25 + 16 ch = 41

    x3_diam_merged = merge([x3_diam, x3_ident], mode='concat', concat_axis=1)
    x3_lob_merged = merge([x3_lob, x3_ident], mode='concat', concat_axis=1)
    x3_spic_merged = merge([x3_spic, x3_ident], mode='concat', concat_axis=1)
    x3_malig_merged = merge([x3_malig, x3_ident], mode='concat', concat_axis=1)

    x4_ident = AveragePooling3D()(x3_ident)
    x4_diam = conv_block(x3_diam_merged,
                         36,
                         activation='crelu',
                         init=looks_linear_init)  #outputs 25 + 16 ch = 41
    x4_lob = conv_block(x3_lob_merged,
                        36,
                        activation='crelu',
                        init=looks_linear_init)  #outputs 25 + 16 ch = 41
    x4_spic = conv_block(x3_spic_merged,
                         36,
                         activation='crelu',
                         init=looks_linear_init)  #outputs 25 + 16 ch = 41
    x4_malig = conv_block(x3_malig_merged,
                          36,
                          activation='crelu',
                          init=looks_linear_init)  #outputs 25 + 16 ch = 41

    x4_diam_merged = merge([x4_diam, x4_ident], mode='concat', concat_axis=1)
    x4_lob_merged = merge([x4_lob, x4_ident], mode='concat', concat_axis=1)
    x4_spic_merged = merge([x4_spic, x4_ident], mode='concat', concat_axis=1)
    x4_malig_merged = merge([x4_malig, x4_ident], mode='concat', concat_axis=1)

    x5_diam = conv_block(x4_diam_merged,
                         64,
                         pool=False,
                         init=looks_linear_init)  #outputs 25 + 16 ch = 41
    x5_lob = conv_block(x4_lob_merged, 64, pool=False,
                        init=looks_linear_init)  #outputs 25 + 16 ch = 41
    x5_spic = conv_block(x4_spic_merged,
                         64,
                         pool=False,
                         init=looks_linear_init)  #outputs 25 + 16 ch = 41
    x5_malig = conv_block(x4_malig_merged,
                          64,
                          pool=False,
                          init=looks_linear_init)  #outputs 25 + 16 ch = 41

    xpool_diam = BatchNormalization()(GlobalMaxPooling3D()(x5_diam))
    xpool_lob = BatchNormalization()(GlobalMaxPooling3D()(x5_lob))
    xpool_spic = BatchNormalization()(GlobalMaxPooling3D()(x5_spic))
    xpool_malig = BatchNormalization()(GlobalMaxPooling3D()(x5_malig))

    #from here let's branch and predict different things
    xout_diam = dense_branch(xpool_diam,
                             name='o_d',
                             outsize=1,
                             activation='relu')
    xout_lob = dense_branch(xpool_lob,
                            name='o_lob',
                            outsize=1,
                            activation='sigmoid')
    xout_spic = dense_branch(xpool_spic,
                             name='o_spic',
                             outsize=1,
                             activation='sigmoid')
    xout_malig = dense_branch(xpool_malig,
                              name='o_mal',
                              outsize=1,
                              activation='sigmoid')

    #sphericity
    # xout_spher= dense_branch(xpool_norm,name='o_spher',outsize=4,activation='softmax')

    # xout_text = dense_branch(xpool_norm,name='o_t',outsize=4,activation='softmax')

    #calcification
    # xout_calc = dense_branch(xpool_norm,name='o_c',outsize=7,activation='softmax')

    model = Model(input=xin,
                  output=[xout_diam, xout_lob, xout_spic, xout_malig])

    if input_shape[1] == 32:
        lr_start = .003
    elif input_shape[1] == 64:
        lr_start = .001
    elif input_shape[1] == 128:
        lr_start = 1e-4
    elif input_shape[1] == 96:
        lr_start = 5e-4

    opt = Nadam(lr_start, clipvalue=1.0)
    print 'compiling model'

    model.compile(optimizer=opt,
                  loss={
                      'o_d': 'mse',
                      'o_lob': 'binary_crossentropy',
                      'o_spic': 'binary_crossentropy',
                      'o_mal': 'binary_crossentropy'
                  },
                  loss_weights={
                      'o_d': 1.0,
                      'o_lob': 5.0,
                      'o_spic': 5.0,
                      'o_mal': 5.0
                  })
    return model
Пример #30
0
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Convolution2D(256, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(256, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Convolution2D(1024, 4, 4, border_mode='valid'))
model.add(Activation('relu'))
model.add(Convolution2D(1024, 1, 1, border_mode='valid'))
model.add(Activation('relu'))

model.add(Convolution2D(nb_classes, 1, 1, border_mode='valid'))
vc = BatchNormalization()
model.add(vc)
model.add(Flatten())
#model.add(Dense(nb_classes))
model.add(Activation('softmax'))


model.load_weights("/data/lisatmp4/sarath/data/output/conv/1/weights.hdf5")#/data/lisatmp4/chinna/data/ift6268/temp/1/weights.hdf5")

# let's train the model using SGD + momentum (how original).
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)



convout = theano.function([model.get_input(train=False)], vc.get_output(train=False))
# Alternate attention before fusion layer
#Encoder output is 32 32 512

# attention_output = Reshape([32 * 32, 256 * 3])(encoder_output)
# att_vector = MultiHeadsAttModel(l=32 * 32, d=256 * 3, dv=16 * 3, dout=64, nv=16)
# attention_output = att_vector([attention_output, attention_output, attention_output])
# print(attention_output.shape)
# attention_output = Reshape([8 * 8, 1024])(attention_output)
# attention_output = BatchNormalization()(attention_output)

# Fusion
fusion_output = RepeatVector(32 * 32)(embed_input)
fusion_output = Reshape(([32, 32, 1000]))(fusion_output)
fusion_output = Concatenate(axis=3)([encoder_output, fusion_output])
encoder_output = BatchNormalization()(fusion_output)
fusion_output = Conv2D(256*3, (1, 1), activation='relu', padding='same')(fusion_output)

# Attention
# print(fusion_output.shape)
# attention_output = Reshape([32 * 32, 256 * 3])(fusion_output)
# att_vector = MultiHeadsAttModel(l=32 * 32, d=256 * 3, dv=16 * 3, dout=64, nv=16)
# attention_output = att_vector([attention_output, attention_output, attention_output])
# print(attention_output.shape)
# attention_output = Reshape([32,32, 64])(attention_output)
# attention_output = BatchNormalization()(attention_output)
# print(attention_output.shape)

# Decoder
decoder_output = Conv2D(128, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_normal')(fusion_output)
decoder_output = UpSampling2D((2, 2))(decoder_output)