Example #1
0
def run_dnn(X_train, X_val, y_train, y_val, df_test, n_orgs):

    # For use with organization embeddings later:
    X_train_array = [X_train['orguuid'], (X_train.drop('orguuid', axis=1))]
    X_val_array = [X_val['orguuid'], (X_val.drop('orguuid', axis=1))]

    import tensorflow as tf
    from tensorflow import keras

    # PReLU with constant
    from keras.initializers import Constant
    # embedding with L2 regularization
    from keras.regularizers import l2

    # # Gelu
    # from keras.utils.generic_utils import get_custom_objects
    # def gelu(x):
    #     return 0.5 * x * (1 + tf.tanh(tf.sqrt(2 / np.pi) * (x + 0.044715 * tf.pow(x, 3))))
    # get_custom_objects().update({'gelu': keras.layers.Activation(gelu)})

    n_latent_factors_orgs = 30

    org_input = keras.layers.Input(shape=[1])
    org_embedding = keras.layers.Embedding(
        n_orgs + 1,
        n_latent_factors_orgs,
        embeddings_initializer='he_normal',
        embeddings_regularizer=l2(1e-6))(org_input)
    org_vec = keras.layers.Flatten()(org_embedding)
    org_vec = keras.layers.Dropout(0.2)(org_vec)

    other_input = keras.layers.Input(shape=(3, ))

    concat = keras.layers.Concatenate()([org_vec, other_input])

    dense_1 = keras.layers.Dense(32, name='FullyConnected-1')(concat)
    act_1 = keras.layers.PReLU(alpha_initializer=Constant(value=0.25))(dense_1)
    dense_2 = keras.layers.Dense(16, name='FullyConnected-2')(act_1)
    act_2 = keras.layers.PReLU(alpha_initializer=Constant(value=0.25))(dense_2)
    dense_3 = keras.layers.Dense(16, name='FullyConnected-3')(act_2)
    act_3 = keras.layers.PReLU(alpha_initializer=Constant(value=0.25))(dense_3)
    dense_4 = keras.layers.Dense(16, name='FullyConnected-4')(act_3)
    act_4 = keras.layers.PReLU(alpha_initializer=Constant(value=0.25))(dense_4)
    output = keras.layers.Dense(1, activation=tf.nn.sigmoid,
                                name='Output')(act_4)

    model = keras.Model([org_input, other_input], output)

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

    history = model.fit(X_train_array,
                        y_train,
                        epochs=20,
                        batch_size=1,
                        verbose=1,
                        validation_data=(X_val_array, y_val))

    val_loss, val_acc = model.evaluate(X_val_array, y_val)
    print('Validation accuracy:', val_acc)

    rank_results = test_results(df_test, alg="dnn", model=model)
    return rank_results
Example #2
0
EPOCHS = 100

modelfile = os.path.basename(__file__).split(".")[0]

loss = "squared_hinge"
optimizer = "nadam"

sequence = Input(shape=(totallen, ))
seqsa = Lambda(lambda x: x[:, 0:5])(sequence)
seqsb = Lambda(lambda x: x[:, 5:])(sequence)
seqsc = Lambda(lambda x: x[:, 5:])(sequence)

network_emb = sparse.load_npz("model/weibo_wembedding.npz").todense()
embedded = Embedding(len(words) + 1,
                     word_size,
                     embeddings_initializer=Constant(network_emb),
                     input_length=seqlen,
                     mask_zero=False,
                     trainable=True)(seqsb)

networkcore_emb = sparse.load_npz("model/weibo_coreembedding.npz").todense()
embeddedc = Embedding(len(words) + 1,
                      actors_size,
                      embeddings_initializer=Constant(networkcore_emb),
                      input_length=seqlen,
                      mask_zero=False,
                      trainable=True)(seqsc)
#
# concat = concatenate([embedded, embeddedc])

dropout = Dropout(rate=Dropoutrate)(seqsa)
Example #3
0
    def create_net(self):
        e_input = Input(shape=(self.input_shape, self.input_shape, 1))
        x = Conv2D(self.filters, (3, 3), padding='same')(e_input)
        x = BatchNormalization()(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = MaxPooling2D((2, 2), padding='same')(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = BatchNormalization()(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = MaxPooling2D((2, 2), padding='same')(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = BatchNormalization()(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = MaxPooling2D((2, 2), padding='same')(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = BatchNormalization()(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = MaxPooling2D((2, 2), padding='same')(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = BatchNormalization()(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = MaxPooling2D((2, 2), padding='same')(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = BatchNormalization()(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = MaxPooling2D((2, 2), padding='same')(x)

        shape_before_flatten = K.int_shape(x)[1:]
        x = Flatten()(x)

        self.encoder_mu = Dense(self.latent_dim)(x)
        self.encoder_log_variance = Dense(self.latent_dim)(x)

        e_output = Lambda(self.sampling)([self.encoder_mu, self.encoder_log_variance])

        # Keep the encoder part
        self.e = Model(e_input, e_output)

        # And now the decoder part
        d_input = Input(shape=[self.latent_dim])
        x = Dense(np.prod(shape_before_flatten))(d_input)
        x = Reshape(target_shape=shape_before_flatten)(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = BatchNormalization()(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = UpSampling2D((2, 2))(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = BatchNormalization()(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = UpSampling2D((2, 2))(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = BatchNormalization()(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = UpSampling2D((2, 2))(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = BatchNormalization()(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = UpSampling2D((2, 2))(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = BatchNormalization()(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = UpSampling2D((2, 2))(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = BatchNormalization()(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = UpSampling2D((2, 2))(x)
        d_output = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

        self.d = Model(d_input, d_output)

        # Finalize the VAE
        vae_input = Input(shape=(self.input_shape, self.input_shape, 1))
        vae_enc_out = self.e(vae_input)
        vae_dec_out = self.d(vae_enc_out)

        self.vae = Model(vae_input, vae_dec_out)
        return
Example #4
0
 def build(self, input_shape):
     self.temp = self.add_weight(name='temp', shape=[], initializer=Constant(self.start_temp), trainable=False)
     self.logits = self.add_weight(name='logits', shape=[self.output_dim, input_shape[1]],
                                   initializer=glorot_normal(), trainable=True)
     super(ConcreteSelect, self).build(input_shape)
from keras.models import Sequential
from keras.layers import Dense, Flatten
from keras.callbacks import TensorBoard, EarlyStopping
from keras.optimizers import SGD
from keras.initializers import Constant
import keras.backend as K
import pickle
import time
from copy import deepcopy
from shutil import copy
from random import randint

model = Sequential()
model.add(Flatten(input_shape=(28, 28, 3)))
model.add(Dense(512, kernel_initializer='glorot_normal',
                bias_initializer=Constant(0.1), activation='relu'))
model.add(Dense(512, kernel_initializer='glorot_normal',
                bias_initializer=Constant(0.1), activation='relu'))
model.add(Dense(512, kernel_initializer='glorot_normal',
                bias_initializer=Constant(0.1), activation='relu'))
model.add(Dense(10, kernel_initializer='glorot_normal',
                bias_initializer=Constant(0.1), activation='softmax'))

early_stop = EarlyStopping(monitor='loss', min_delta=0.0001, patience=5)
now = str(time.time())
tb_callback = TensorBoard(log_dir='../Tensorboard/mlp3/' + now)

img = tf.placeholder(tf.float32, [28, 28, 3])
norm_image = tf.image.per_image_standardization(img)

opt = SGD(lr=0.01, momentum=0.9)
Example #6
0
 def build(self, dim_input):
     self.bin_map = self.add_weight(name = 'bin_map', shape = [self.dim_output, dim_input[1]], initializer = glorot_normal(), trainable = 1)
     self.temp = self.add_weight(name = 'temp', shape = [], initializer = Constant(self.temp_initial), trainable = 0)
     super(CoRAESelect, self).build(dim_input)
Example #7
0
 def build(self, input_shape):
     self.p = self.add_weight(shape=(),
                              name='p',
                              initializer=Constant(value=self.init_p),
                              trainable=False)
     super(HB, self).build(input_shape)
                    print('output to file.')
                    sX = pickle.dumps(X)
                    fx.write(sX)
                    sy = pickle.dumps(y)
                    fy.write(sy)

if MODE==2:
    loss = "squared_hinge"
    optimizer = "nadam"
    metric= "accuracy"
    sequence = Input(shape=(maxlen,nFeatures,))
    seqsa, seqsb, seqsc, seqsd, seqse = Lambda(lambda x: [x[:,:,0],x[:,:,1],x[:,:,2],x[:,:,3],x[:,:,4]])(sequence)

    zhwiki_emb = numpy.load("msr_dic/zhwiki_embedding.npy")
    embeddeda = Embedding(len(chars) + 1, word_size,embeddings_initializer=Constant(zhwiki_emb), input_length=maxlen, mask_zero=False)(seqsa)
    embeddedb = Embedding(len(chars) + 1, word_size,embeddings_initializer=Constant(zhwiki_emb), input_length=maxlen, mask_zero=False)(seqsb)
    embeddedc = Embedding(len(chars) + 1, word_size,embeddings_initializer=Constant(zhwiki_emb), input_length=maxlen, mask_zero=False)(seqsc)

    maximuma = Maximum()([embeddeda, embeddedb])
    maximumb = Maximum()([embeddedc, embeddedb])

    # zhwiki_biemb = numpy.load("model/zhwiki_biembedding.npy")
    zhwiki_biemb = sparse.load_npz("model/zhwiki_biembedding.npz").todense()
    embeddedd = Embedding(len(bigrams) + 1, word_size, input_length=maxlen,embeddings_initializer=Constant(zhwiki_biemb),
                          mask_zero=False)(seqsd)
    embeddede = Embedding(len(bigrams) + 1, word_size, input_length=maxlen,embeddings_initializer=Constant(zhwiki_biemb),
                          mask_zero=False)(seqse)

    concat = concatenate([embeddeda, maximuma, maximumb, embeddedd, embeddede])
    char = TimeDistributed(Flatten())(maxpool_out)
    char = Dropout(0.5)(char)
    output = concatenate([words, casing,char])
    output = Bidirectional(LSTM(200, return_sequences=True, dropout=0.50, recurrent_dropout=0.25))(output)
    output = TimeDistributed(Dense(len(label2Idx), activation='softmax'))(output)
    model = Model(inputs=[words_input, casing_input,character_input], outputs=[output])
    model.compile(loss='sparse_categorical_crossentropy', optimizer='nadam')
    model.summary()
    '''

    sents_input = Input(shape=(5, Maxwordsent), name='sents_input')
    #words = TimeDistributed(Embedding(input_dim=wordEmbeddings.shape[0], output_dim=wordEmbeddings.shape[1],  weights=[wordEmbeddings], trainable=False))(sents_input)#shape[0]is the vacoabilry size, shape[1] is output size #embedding_1 (Embedding)         (None, None, 100)  the first None is the batch, the second None is the number of word in a sentence
    words = TimeDistributed(
        Embedding(input_dim=wordEmbeddings.shape[0],
                  output_dim=wordEmbeddings.shape[1],
                  embeddings_initializer=Constant(wordEmbeddings),
                  trainable=False)
    )(
        sents_input
    )  #shape[0]is the vacoabilry size, shape[1] is output size #embedding_1 (Embedding)         (None, None, 100)  the first None is the batch, the second None is the number of word in a sentence

    conv1d_out = TimeDistributed(
        Conv1D(kernel_size=3,
               filters=Maxwordsent,
               padding='same',
               activation='tanh',
               strides=1)
    )(
        words
    )  #(None, None, 52, 30) filters is the kernel number, it decide output how many vecters
    maxpool_out = TimeDistributed(MaxPooling1D(Maxwordsent))(
Example #10
0
def model_fourth(X,
                 drop_ra=0.,
                 l1_reg=0.,
                 bias=0.1,
                 g_noise=0.,
                 nodes=[32, 16, 5, 16, 32]):
    #a list of layers, each comprised of a dictionary of layer elements

    input_dim = X.shape[1]

    layers = []

    layers.append({
        'layer':
        Dense(nodes[0], input_dim=input_dim, kernel_regularizer=l1(0.)),
        'advanced_activation':
        PReLU(),
        'normalisation':
        BatchNormalization()
    })
    layers.append({
        'layer':
        Dense(nodes[1], bias_initializer=Constant(value=bias)),
        'advanced_activation':
        PReLU(),
        'dropout_rate':
        drop_ra,
        'normalisation':
        BatchNormalization()
    })

    layers.append({
        'layer':
        Dense(nodes[2], bias_initializer=Constant(value=bias)),
        'advanced_activation':
        PReLU()
    })

    layers.append({
        'layer':
        Dense(nodes[3], bias_initializer=Constant(value=bias)),
        'advanced_activation':
        PReLU(),
        'dropout_rate':
        drop_ra
    })

    layers.append({
        'layer':
        Dense(nodes[4], bias_initializer=Constant(value=bias)),
        'advanced_activation':
        PReLU(),
        'dropout_rate':
        drop_ra
    })

    layers.append({
        'layer': Dense(input_dim, kernel_regularizer=l1(l1_reg)),
        'activation': 'linear'
    })

    return layers
Example #11
0
def model_tenth(X,
                drop_ra=0.,
                l1_reg=0.,
                bias=0.,
                g_noise=0.,
                ker_init=None,
                nodes=[32, 16, 5, 16, 32]):
    #a list of layers, each comprised of a dictionary of layer elements
    #K.clear_session()
    input_dim = X.shape[1]

    layers = []

    layers.append({
        'layer':
        Dense(nodes[0],
              input_dim=input_dim,
              kernel_initializer=ker_init,
              bias_initializer=Constant(value=bias)),
        'advanced_activation':
        PReLU(),
        'noise':
        GaussianNoise(g_noise)
    })
    layers.append({
        'layer':
        Dense(nodes[1],
              kernel_initializer=ker_init,
              bias_initializer=Constant(value=bias)),
        'advanced_activation':
        PReLU(),
        'noise':
        GaussianNoise(g_noise),
        'dropout_rate':
        drop_ra,
        'normalisation':
        BatchNormalization()
    })

    layers.append({'layer': MaxoutDense(nodes[2], init=ker_init)})

    layers.append({
        'layer':
        Dense(nodes[3],
              bias_initializer=Constant(value=bias),
              kernel_initializer=ker_init),
        'advanced_activation':
        PReLU(),
        'dropout_rate':
        drop_ra
    })

    layers.append({
        'layer':
        Dense(nodes[4],
              bias_initializer=Constant(value=bias),
              kernel_initializer=ker_init),
        'advanced_activation':
        PReLU(),
        'dropout_rate':
        drop_ra
    })

    layers.append({
        'layer':
        Dense(input_dim,
              kernel_regularizer=l1(l1_reg),
              kernel_initializer=ker_init)
    })

    return layers
Example #12
0
def build_model(embeddings):
    # input representation features
    words_input = Input(shape=[SEQUENCE_LEN], dtype='int32')
    pos1_input = Input(shape=[SEQUENCE_LEN], dtype='int32')
    pos2_input = Input(shape=[SEQUENCE_LEN], dtype='int32')
    segs_input = Input(shape=[SEQUENCE_LEN, 3], dtype='float32')

    # lexical features
    e1_input = Input(shape=[ENTITY_LEN], dtype='int32')  # L1
    e2_input = Input(shape=[ENTITY_LEN], dtype='int32')  # L2
    e1context_input = Input(shape=[2], dtype='int32')  # L3
    e2context_input = Input(shape=[2], dtype='int32')  # L4

    # word embedding
    we = embeddings["word_embeddings"]
    words_embed = Embedding(we.shape[0], we.shape[1], weights=[we])
    words = words_embed(words_input)
    e1 = words_embed(e1_input)
    e2 = words_embed(e2_input)
    e1context = words_embed(e1context_input)
    e2context = words_embed(e2context_input)

    # lexical feature
    e1_flat = Flatten()(e1)
    e2_flat = Flatten()(e2)
    e1context_flat = Flatten()(e1context)
    e2context_flat = Flatten()(e2context)

    # position embedding
    pe1 = embeddings["position_embeddings_1"]
    pos1 = Embedding(pe1.shape[0], pe1.shape[1], weights=[pe1])(pos1_input)
    pe2 = embeddings["position_embeddings_2"]
    pos2 = Embedding(pe2.shape[0], pe2.shape[1], weights=[pe2])(pos2_input)

    # input representation
    input_repre = Concatenate()([words, pos1, pos2])
    input_repre = Dropout(DROPOUT)(input_repre)

    # input attention
    e1_repeat = RepeatVector(SEQUENCE_LEN)(e1_flat)
    e2_repeat = RepeatVector(SEQUENCE_LEN)(e2_flat)
    concat = Concatenate()([words, e1_repeat, e2_repeat])
    alpha = Dense(1, activation="softmax")(concat)
    alpha = Reshape([SEQUENCE_LEN])(alpha)
    alpha = RepeatVector(WORD_REPRE_SIZE)(alpha)
    alpha = Permute([2, 1])(alpha)
    input_repre = Multiply()([input_repre, alpha])

    # word-level convolution
    input_conved = Conv1D(filters=NB_FILTERS_WORD,
                          kernel_size=WINDOW_SIZE_WORD,
                          padding="same",
                          activation="relu",
                          kernel_initializer=TruncatedNormal(stddev=0.1),
                          bias_initializer=Constant(0.1))(input_repre)
    input_pooled = PiecewiseMaxPool()([input_conved, segs_input])

    # fully connected
    outputs = [input_pooled, e1_flat, e2_flat, e1context_flat, e2context_flat]
    output = Concatenate()(outputs)
    output = Dropout(DROPOUT)(output)
    output = Dense(
        units=NB_RELATIONS,
        activation="softmax",
        kernel_initializer=TruncatedNormal(stddev=0.1),
        bias_initializer=Constant(0.1),
        kernel_regularizer='l2',
        bias_regularizer='l2',
    )(output)

    model = Model(inputs=[
        words_input, pos1_input, pos2_input, e1_input, e2_input,
        e1context_input, e2context_input, segs_input
    ],
                  outputs=[output])
    model.compile(loss="sparse_categorical_crossentropy",
                  optimizer='sgd',
                  metrics=['accuracy'])
    # model.summary()
    return model
Example #13
0
from keras.layers import Flatten
from keras.layers import LSTM
from keras.initializers import Constant
from keras.layers.wrappers import Bidirectional
from pickle import dump, load
import json
from keras.preprocessing.sequence import pad_sequences

import numpy as np

from underthesea import word_tokenize

embedding_matrix = load(open(r"app\Controllers\embedding_matrix.pkl", "rb"))

model = Sequential()
model.add(Embedding(4616, 300, embeddings_initializer=Constant(embedding_matrix),
    input_length=80))
model.add(Bidirectional(LSTM(64, dropout = 0.2, recurrent_dropout = 0.2)))
model.add(Flatten())
model.add(Dense(5000, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

model.load_weights('app\Controllers\model_linhKien.h5')


file = open(r'app\Controllers\list_CORPUS.txt', encoding="utf8")
list_CORPUS = file.read().split('\n')
file.close()
Example #14
0
def run_lstm(X_train, X_val, y_train, y_val, df_test, org_features,
             proj_features):
    import tensorflow as tf
    from tensorflow import keras

    # PReLU with constant
    from keras.initializers import Constant
    #Embedding with L2 regularization
    from keras.regularizers import l2

    # LSTM_a
    org_input = keras.layers.Input(shape=(None, len(org_features)))
    lstm_org_1 = keras.layers.LSTM(32, name='Left-LSTM')(org_input)
    org_vec = keras.layers.Dropout(0.2, name='Left-Dropout')(lstm_org_1)
    org_dense_1 = keras.layers.Dense(32, name='Left-FullyConnected-1')(org_vec)
    org_act_1 = keras.layers.PReLU(alpha_initializer=Constant(
        value=0.25))(org_dense_1)
    org_dense_2 = keras.layers.Dense(16,
                                     name='Left-FullyConnected-2')(org_act_1)
    org_act_2 = keras.layers.PReLU(alpha_initializer=Constant(
        value=0.25))(org_dense_2)
    org_fin = keras.layers.Flatten()(org_act_2)

    # LSTM_b
    proj_input = keras.layers.Input(shape=(None, len(proj_features)))
    lstm_proj_1 = keras.layers.Bidirectional(keras.layers.LSTM(
        32, name='Right-LSTM'),
                                             merge_mode='sum')(proj_input)
    proj_vec = keras.layers.Dropout(0.2, name='Right-Dropout')(lstm_proj_1)
    proj_dense_1 = keras.layers.Dense(32,
                                      name='Right-FullyConnected-1')(proj_vec)
    proj_act_1 = keras.layers.PReLU(alpha_initializer=Constant(
        value=0.25))(proj_dense_1)
    proj_dense_2 = keras.layers.Dense(
        16, name='Right-FullyConnected-2')(proj_act_1)
    proj_act_2 = keras.layers.PReLU(alpha_initializer=Constant(
        value=0.25))(proj_dense_2)
    proj_fin = keras.layers.Flatten()(proj_act_2)

    output = ManDist(name='Combined-Similarity')([org_vec, proj_vec])

    model = keras.Model([org_input, proj_input], output)

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

    history = model.fit(X_train,
                        y_train,
                        epochs=12,
                        batch_size=1,
                        verbose=1,
                        validation_data=(X_val, y_val))

    val_loss, val_acc = model.evaluate(X_val, y_val)
    print('Validation accuracy:', val_acc)

    rank_results = test_results(df_test,
                                alg="lstm",
                                model=model,
                                org_features=org_features,
                                proj_features=proj_features)
    return rank_results
Example #15
0
 def build(self, input_shape=None):
     self.log_vars = []
     for i in range(2):
         self.log_vars += [
             self.add_weight(name="log_var" + str(i), shape=(1,), initializer=Constant(0.), trainable=False)]
     super(MultiLossLayer, self).build(input_shape)
Example #16
0
    return K.square((K.squeeze(K.batch_dot(y_true, y_pred, axes=1),
           axis=-1) / K.sum(y_true, axis=-1)) - K.max(y_true, axis=-1))

# Neural Network Model
if 'model_pong.h5' in os.listdir('.'):
    model = load_model('model_pong.h5')
    print "\nLoaded model\n"
else:
    model = Sequential()
    model.add(Conv2D(32, input_shape=(4, 84, 84), kernel_size=8, data_format=\
                    'channels_first', activation='relu'))
    model.add(Conv2D(64, kernel_size=4, activation='relu'))
    model.add(Conv2D(64, kernel_size=2, activation='relu'))
    model.add(Flatten())
    model.add(Dense(128, kernel_initializer=glorot_normal(), bias_initializer=\
                    Constant(0.1), activation='relu'))
    model.add(Dense(6, kernel_initializer=glorot_normal(), bias_initializer=\
                    Constant(0.001)))

    opt = RMSprop()
    model.compile(loss='mse', optimizer=opt)
    print "\nCreated model\n"

tb_callback = TensorBoard(log_dir='/home/rharish/Programs/Python/RL'\
                          '/Tensorboard/' + str(time.time()))

pong_experience = deque()
pong_policy_epsilon = 0.6
pong_ep_num = 0

def saver():
Example #17
0
#!/usr/bin/env python
from __future__ import print_function
from builtins import input
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout
from keras.callbacks import EarlyStopping
from keras.initializers import glorot_normal, Constant
from get_data import get_data

model = Sequential()

model.add(Conv2D(32, 5, strides=5, input_shape=(50, 50, 1),
                 activation='relu', padding='same',
                 kernel_initializer=glorot_normal(),
                 bias_initializer=Constant(0.1)))
model.add(MaxPooling2D((2, 2), padding='same'))
model.add(Dropout(0.25))
model.add(Conv2D(64, 3, strides=3, activation='relu', padding='same',
                 kernel_initializer=glorot_normal(),
                 bias_initializer=Constant(0.1)))
model.add(MaxPooling2D((2, 2), padding='same'))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(100, activation='relu', kernel_initializer=glorot_normal(),
                bias_initializer=Constant(0.1)))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid', kernel_initializer=glorot_normal(),
                bias_initializer=Constant(0.1)))

early_stop = EarlyStopping(monitor='loss', min_delta=0.025, patience=5)
model.compile(loss='binary_crossentropy', optimizer='adam',
Example #18
0
def build_model_FCN_model_api(batch_size,
                              optimizer,
                              patch_size=(128, 128),
                              base_weight_decay=0.0005,
                              output_ROI_mask=True):
    print('Using build_model_FCN_model_api')

    # define the shared model:
    net_name = 'Multi-view_FCN'

    scale_number = 3
    ##################### input  ###############################################
    input_shape0 = (batch_size, patch_size[0], patch_size[1], 1)
    input_shape1 = (batch_size, patch_size[0] / 2, patch_size[1] / 2, 1)
    input_shape2 = (batch_size, patch_size[0] / 4, patch_size[1] / 4, 1)

    input_shape3 = (1, patch_size[0] / 4, patch_size[1] / 4, 1)

    input_patches1_s0 = Input(batch_shape=input_shape0, name='patches1_s0')
    input_patches1_s1 = Input(batch_shape=input_shape1, name='patches1_s1')
    input_patches1_s2 = Input(batch_shape=input_shape2, name='patches1_s2')

    input_patches2_s0 = Input(batch_shape=input_shape0, name='patches2_s0')
    input_patches2_s1 = Input(batch_shape=input_shape1, name='patches2_s1')
    input_patches2_s2 = Input(batch_shape=input_shape2, name='patches2_s2')

    input_patches3_s0 = Input(batch_shape=input_shape0, name='patches3_s0')
    input_patches3_s1 = Input(batch_shape=input_shape1, name='patches3_s1')
    input_patches3_s2 = Input(batch_shape=input_shape2, name='patches3_s2')

    input_patches4_s0 = Input(batch_shape=input_shape0, name='patches4_s0')
    input_patches4_s1 = Input(batch_shape=input_shape1, name='patches4_s1')
    input_patches4_s2 = Input(batch_shape=input_shape2, name='patches4_s2')

    input_depth_maps_v1 = Input(batch_shape=input_shape3,
                                name='depth_ratio_v1')
    input_depth_maps_v2 = Input(batch_shape=input_shape3,
                                name='depth_ratio_v2')
    input_depth_maps_v3 = Input(batch_shape=input_shape3,
                                name='depth_ratio_v3')
    input_depth_maps_v4 = Input(batch_shape=input_shape3,
                                name='depth_ratio_v4')

    if output_ROI_mask:
        # the output density patch/map is down-sampled by a factor of 4
        output_masks = Input(batch_shape=(batch_size, patch_size[0],
                                          patch_size[1], 1),
                             name='output_masks')

    train_flag = False
    ####################### view 1 #############################################
    # image pyramids:
    x1_s0_output = feature_extraction_view1(base_weight_decay,
                                            input_patches1_s0, train_flag)
    x1_s1_output = feature_extraction_view1(base_weight_decay,
                                            input_patches1_s1, train_flag)
    x1_s2_output = feature_extraction_view1(base_weight_decay,
                                            input_patches1_s2, train_flag)

    # view 1 decoder
    # x1_7 = view1_decoder(base_weight_decay, x1_s0_output)

    # conv block 5
    x1_5 = Conv2D(data_format='channels_last',
                  trainable=True,
                  filters=64,
                  kernel_size=(5, 5),
                  strides=(1, 1),
                  kernel_initializer='he_normal',
                  padding='same',
                  kernel_regularizer=l2(base_weight_decay),
                  use_bias=True,
                  activation=None,
                  name='conv_block_5')(x1_s0_output)
    x1_5 = Activation('relu', name='conv_block_5_act')(x1_5)

    # conv block 6
    x1_6 = Conv2D(data_format='channels_last',
                  trainable=True,
                  filters=32,
                  kernel_size=(5, 5),
                  strides=(1, 1),
                  kernel_initializer='he_normal',
                  padding='same',
                  kernel_regularizer=l2(base_weight_decay),
                  use_bias=True,
                  activation=None,
                  name='conv_block_6')(x1_5)
    x1_6 = Activation('relu', name='conv_block_6_act')(x1_6)

    # conv block 7
    x1_7 = Conv2D(data_format='channels_last',
                  trainable=True,
                  filters=1,
                  kernel_size=(5, 5),
                  strides=(1, 1),
                  kernel_initializer='he_normal',
                  padding='same',
                  kernel_regularizer=l2(base_weight_decay),
                  use_bias=True,
                  activation=None,
                  name='conv_block_7')(x1_6)
    x1_7 = Activation('relu', name='conv_block_7_act')(x1_7)

    ####################### view 2 #############################################
    # image pyramids:
    x2_s0_output = feature_extraction_view2(base_weight_decay,
                                            input_patches2_s0, train_flag)
    x2_s1_output = feature_extraction_view2(base_weight_decay,
                                            input_patches2_s1, train_flag)
    x2_s2_output = feature_extraction_view2(base_weight_decay,
                                            input_patches2_s2, train_flag)
    # view 2 decoder
    # x2_7 = view2_decoder(base_weight_decay, x2_s0_output)

    # dmap
    # conv block 5
    x2_5 = Conv2D(data_format='channels_last',
                  trainable=True,
                  filters=64,
                  kernel_size=(5, 5),
                  strides=(1, 1),
                  kernel_initializer='he_normal',
                  padding='same',
                  kernel_regularizer=l2(base_weight_decay),
                  use_bias=True,
                  activation=None,
                  name='conv_block_5_2')(x2_s0_output)
    x2_5 = Activation('relu', name='conv_block_5_2_act')(x2_5)

    # conv block 6
    x2_6 = Conv2D(data_format='channels_last',
                  trainable=True,
                  filters=32,
                  kernel_size=(5, 5),
                  strides=(1, 1),
                  kernel_initializer='he_normal',
                  padding='same',
                  kernel_regularizer=l2(base_weight_decay),
                  use_bias=True,
                  activation=None,
                  name='conv_block_6_2')(x2_5)
    x2_6 = Activation('relu', name='conv_block_6_2_act')(x2_6)

    # conv block 7
    x2_7 = Conv2D(data_format='channels_last',
                  trainable=True,
                  filters=1,
                  kernel_size=(5, 5),
                  strides=(1, 1),
                  kernel_initializer='he_normal',
                  padding='same',
                  kernel_regularizer=l2(base_weight_decay),
                  use_bias=True,
                  activation=None,
                  name='conv_block_7_2')(x2_6)
    x2_7 = Activation('relu', name='conv_block_7_2_act')(x2_7)

    ####################### view 3 #############################################
    # image pyramids:
    x3_s0_output = feature_extraction_view3(base_weight_decay,
                                            input_patches3_s0, train_flag)
    x3_s1_output = feature_extraction_view3(base_weight_decay,
                                            input_patches3_s1, train_flag)
    x3_s2_output = feature_extraction_view3(base_weight_decay,
                                            input_patches3_s2, train_flag)

    # view 3 decoder
    # x3_7 = view3_decoder(base_weight_decay, x3_s0_output)

    # conv block 5
    x3_5 = Conv2D(data_format='channels_last',
                  trainable=True,
                  filters=64,
                  kernel_size=(5, 5),
                  strides=(1, 1),
                  kernel_initializer='he_normal',
                  padding='same',
                  kernel_regularizer=l2(base_weight_decay),
                  use_bias=True,
                  activation=None,
                  name='conv_block_5_3')(x3_s0_output)
    x3_5 = Activation('relu', name='conv_block_5_3_act')(x3_5)

    # conv block 6
    x3_6 = Conv2D(data_format='channels_last',
                  trainable=True,
                  filters=32,
                  kernel_size=(5, 5),
                  strides=(1, 1),
                  kernel_initializer='he_normal',
                  padding='same',
                  kernel_regularizer=l2(base_weight_decay),
                  use_bias=True,
                  activation=None,
                  name='conv_block_6_3')(x3_5)
    x3_6 = Activation('relu', name='conv_block_6_3_act')(x3_6)

    # conv block 7
    x3_7 = Conv2D(data_format='channels_last',
                  trainable=True,
                  filters=1,
                  kernel_size=(5, 5),
                  strides=(1, 1),
                  kernel_initializer='he_normal',
                  padding='same',
                  kernel_regularizer=l2(base_weight_decay),
                  use_bias=True,
                  activation=None,
                  name='conv_block_7_3')(x3_6)
    x3_7 = Activation('relu', name='conv_block_7_3_act')(x3_7)

    ####################### view 4 #############################################
    # image pyramids:
    x4_s0_output = feature_extraction_view4(base_weight_decay,
                                            input_patches4_s0, train_flag)
    x4_s1_output = feature_extraction_view4(base_weight_decay,
                                            input_patches4_s1, train_flag)
    x4_s2_output = feature_extraction_view4(base_weight_decay,
                                            input_patches4_s2, train_flag)

    # view 4 decoder
    # x4_7 = view4_decoder(base_weight_decay, x4_s0_output)

    # conv block 5
    x4_5 = Conv2D(data_format='channels_last',
                  trainable=True,
                  filters=64,
                  kernel_size=(5, 5),
                  strides=(1, 1),
                  kernel_initializer='he_normal',
                  padding='same',
                  kernel_regularizer=l2(base_weight_decay),
                  use_bias=True,
                  activation=None,
                  name='conv_block_5_4')(x4_s0_output)
    x4_5 = Activation('relu', name='conv_block_5_4_act')(x4_5)

    # conv block 6
    x4_6 = Conv2D(data_format='channels_last',
                  trainable=True,
                  filters=32,
                  kernel_size=(5, 5),
                  strides=(1, 1),
                  kernel_initializer='he_normal',
                  padding='same',
                  kernel_regularizer=l2(base_weight_decay),
                  use_bias=True,
                  activation=None,
                  name='conv_block_6_4')(x4_5)
    x4_6 = Activation('relu', name='conv_block_6_4_act')(x4_6)

    # conv block 7
    x4_7 = Conv2D(data_format='channels_last',
                  trainable=True,
                  filters=1,
                  kernel_size=(5, 5),
                  strides=(1, 1),
                  kernel_initializer='he_normal',
                  padding='same',
                  kernel_regularizer=l2(base_weight_decay),
                  use_bias=True,
                  activation=None,
                  name='conv_block_7_4')(x4_6)
    x4_7 = Activation('relu', name='conv_block_7_4_act')(x4_7)

    #################################### fusion #############################################
    ################# get the scale-selection mask #####################
    # view depth of image
    batch_size = x1_s0_output.shape[0].value
    height = x1_s0_output.shape[1].value
    width = x1_s0_output.shape[2].value
    num_channels = x1_s0_output.shape[3].value
    output_shape = [1, height, width, 1]

    # view1_depth = feature_scale_fusion_layer_mask(scale_number=scale_number,
    #                                               view = 1, output_shape=output_shape)
    # view2_depth = feature_scale_fusion_layer_mask(scale_number=scale_number,
    #                                               view = 2, output_shape=output_shape)
    # view3_depth = feature_scale_fusion_layer_mask(scale_number=scale_number,
    #                                               view = 3, output_shape=output_shape)

    # view1_scale = scale_selection_mask(base_weight_decay, input_depth_maps_v1)
    # view2_scale = scale_selection_mask(base_weight_decay, input_depth_maps_v2)
    # view3_scale = scale_selection_mask(base_weight_decay, input_depth_maps_v3)
    view1_scale = Conv2D(data_format='channels_last',
                         trainable=True,
                         filters=1,
                         kernel_size=(1, 1),
                         strides=(1, 1),
                         kernel_initializer=Constant(value=-1),
                         padding='same',
                         kernel_regularizer=l2(base_weight_decay),
                         use_bias=True,
                         bias_initializer='ones',
                         activation=None,
                         name='scale_fusion1')(input_depth_maps_v1)

    view2_scale = Conv2D(data_format='channels_last',
                         trainable=True,
                         filters=1,
                         kernel_size=(1, 1),
                         strides=(1, 1),
                         kernel_initializer=Constant(value=-1),
                         padding='same',
                         kernel_regularizer=l2(base_weight_decay),
                         use_bias=True,
                         bias_initializer='ones',
                         activation=None,
                         name='scale_fusion2')(input_depth_maps_v2)

    view3_scale = Conv2D(data_format='channels_last',
                         trainable=True,
                         filters=1,
                         kernel_size=(1, 1),
                         strides=(1, 1),
                         kernel_initializer=Constant(value=-1),
                         padding='same',
                         kernel_regularizer=l2(base_weight_decay),
                         use_bias=True,
                         bias_initializer='ones',
                         activation=None,
                         name='scale_fusion3')(input_depth_maps_v3)

    view4_scale = Conv2D(data_format='channels_last',
                         trainable=True,
                         filters=1,
                         kernel_size=(1, 1),
                         strides=(1, 1),
                         kernel_initializer=Constant(value=-1),
                         padding='same',
                         kernel_regularizer=l2(base_weight_decay),
                         use_bias=True,
                         bias_initializer='ones',
                         activation=None,
                         name='scale_fusion4')(input_depth_maps_v4)

    view1_scale_mask = feature_scale_fusion_layer_rbm(
        scale_number=scale_number)(view1_scale)
    view2_scale_mask = feature_scale_fusion_layer_rbm(
        scale_number=scale_number)(view2_scale)
    view3_scale_mask = feature_scale_fusion_layer_rbm(
        scale_number=scale_number)(view3_scale)
    view4_scale_mask = feature_scale_fusion_layer_rbm(
        scale_number=scale_number)(view4_scale)

    #################### fusion with mask ##################
    # view 1
    ## conv
    x1_s0_output_fusion = fusion_conv_v1(base_weight_decay, x1_s0_output)
    x1_s1_output_fusion = fusion_conv_v1(base_weight_decay, x1_s1_output)
    x1_s2_output_fusion = fusion_conv_v1(base_weight_decay, x1_s2_output)

    ## up sampling
    x1_s1_output_fusion = UpSampling_layer(size=[height, width])(
        [x1_s1_output_fusion])
    x1_s2_output_fusion = UpSampling_layer(size=[height, width])(
        [x1_s2_output_fusion])

    concatenated_map_v1 = Concatenate(name='cat_map_v1')(
        [x1_s0_output_fusion, x1_s1_output_fusion, x1_s2_output_fusion])
    fusion_v1 = feature_scale_fusion_layer(scale_number=scale_number)(
        [concatenated_map_v1, view1_scale_mask])

    ## proj
    fusion_v1_proj = SpatialTransformer(
        1, [int(480 / 4), int(640 / 4)])(fusion_v1)

    # view 2
    ## conv
    x2_s0_output_fusion = fusion_conv_v2(base_weight_decay, x2_s0_output)
    x2_s1_output_fusion = fusion_conv_v2(base_weight_decay, x2_s1_output)
    x2_s2_output_fusion = fusion_conv_v2(base_weight_decay, x2_s2_output)

    ## up sampling
    x2_s1_output_fusion = UpSampling_layer(size=[height, width])(
        [x2_s1_output_fusion])
    x2_s2_output_fusion = UpSampling_layer(size=[height, width])(
        [x2_s2_output_fusion])

    concatenated_map_v2 = Concatenate(name='cat_map_v2')(
        [x2_s0_output_fusion, x2_s1_output_fusion, x2_s2_output_fusion])
    fusion_v2 = feature_scale_fusion_layer(scale_number=scale_number)(
        [concatenated_map_v2, view2_scale_mask])

    ## proj
    fusion_v2_proj = SpatialTransformer(
        2, [int(480 / 4), int(640 / 4)])(fusion_v2)

    # view 3
    ## conv
    x3_s0_output_fusion = fusion_conv_v3(base_weight_decay, x3_s0_output)
    x3_s1_output_fusion = fusion_conv_v3(base_weight_decay, x3_s1_output)
    x3_s2_output_fusion = fusion_conv_v3(base_weight_decay, x3_s2_output)

    ## up sampling
    x3_s1_output_fusion = UpSampling_layer(size=[height, width])(
        [x3_s1_output_fusion])
    x3_s2_output_fusion = UpSampling_layer(size=[height, width])(
        [x3_s2_output_fusion])

    concatenated_map_v3 = Concatenate(name='cat_map_v3')(
        [x3_s0_output_fusion, x3_s1_output_fusion, x3_s2_output_fusion])

    fusion_v3 = feature_scale_fusion_layer(scale_number=scale_number)(
        [concatenated_map_v3, view3_scale_mask])

    ## proj
    fusion_v3_proj = SpatialTransformer(
        3, [int(480 / 4), int(640 / 4)])(fusion_v3)

    # view 4
    ## conv
    x4_s0_output_fusion = fusion_conv_v4(base_weight_decay, x4_s0_output)
    x4_s1_output_fusion = fusion_conv_v4(base_weight_decay, x4_s1_output)
    x4_s2_output_fusion = fusion_conv_v4(base_weight_decay, x4_s2_output)

    ## up sampling
    x4_s1_output_fusion = UpSampling_layer(size=[height, width])(
        [x4_s1_output_fusion])
    x4_s2_output_fusion = UpSampling_layer(size=[height, width])(
        [x4_s2_output_fusion])

    concatenated_map_v4 = Concatenate(name='cat_map_v4')(
        [x4_s0_output_fusion, x4_s1_output_fusion, x4_s2_output_fusion])

    fusion_v4 = feature_scale_fusion_layer(scale_number=scale_number)(
        [concatenated_map_v4, view4_scale_mask])

    ## proj
    fusion_v4_proj = SpatialTransformer(
        4, [int(480 / 4), int(640 / 4)])(fusion_v4)

    ################# concatenate ################
    concatenated_map = Concatenate(name='cat_map_fusion')(
        [fusion_v1_proj, fusion_v2_proj, fusion_v3_proj, fusion_v4_proj])
    fusion_v123 = Conv2D(data_format='channels_last',
                         trainable=True,
                         filters=96,
                         kernel_size=(1, 1),
                         strides=(1, 1),
                         kernel_initializer='he_normal',
                         padding='same',
                         kernel_regularizer=l2(base_weight_decay),
                         use_bias=True,
                         activation=None,
                         name='scale_fusion')(concatenated_map)
    fusion_v123 = Activation('relu', name='scale_fusion_act')(fusion_v123)

    #################### fusion and decode #####################################
    # conv block 9
    x = Conv2D(data_format='channels_last',
               trainable=True,
               filters=64,
               kernel_size=(5, 5),
               strides=(1, 1),
               kernel_initializer='he_normal',
               padding='same',
               kernel_regularizer=l2(base_weight_decay),
               use_bias=True,
               activation=None,
               name='conv_block_fusion1')(fusion_v123)
    x = Activation('relu', name='conv_block_fusion1_act')(x)

    x = Conv2D(data_format='channels_last',
               trainable=True,
               filters=32,
               kernel_size=(5, 5),
               strides=(1, 1),
               kernel_initializer='he_normal',
               padding='same',
               kernel_regularizer=l2(base_weight_decay),
               use_bias=True,
               activation=None,
               name='conv_block_fusion2')(x)
    x = Activation('relu', name='conv_block_fusion2_act')(x)

    x = Conv2D(data_format='channels_last',
               trainable=True,
               filters=1,
               kernel_size=(5, 5),
               strides=(1, 1),
               kernel_initializer='he_normal',
               padding='same',
               kernel_regularizer=l2(base_weight_decay),
               use_bias=True,
               activation=None,
               name='conv_block_fusion3')(x)
    x_output = Activation('relu', name='conv_block_fusion3_act')(x)

    if output_ROI_mask:
        rgr_output = 'den_map_roi'
        output = Multiply(name=rgr_output)([x_output, output_masks])
        print('Layer name of regression output: {}'.format(rgr_output))
        model = Model(inputs=[
            input_patches1_s0, input_patches1_s1, input_patches1_s2,
            input_patches2_s0, input_patches2_s1, input_patches2_s2,
            input_patches3_s0, input_patches3_s1, input_patches3_s2,
            input_patches4_s0, input_patches4_s1, input_patches4_s2,
            input_depth_maps_v1, input_depth_maps_v2, input_depth_maps_v3,
            input_depth_maps_v4, output_masks
        ],
                      outputs=[x_output],
                      name=net_name)
    else:
        model = Model(
            inputs=[
                input_patches1_s0,
                input_patches1_s1,
                input_patches1_s2,
                input_patches2_s0,
                input_patches2_s1,
                input_patches2_s2,
                input_patches3_s0,
                input_patches3_s1,
                input_patches3_s2,
                input_patches4_s0,
                input_patches4_s1,
                input_patches4_s2,
                input_depth_maps_v1,
                input_depth_maps_v2,
                input_depth_maps_v3,
                input_depth_maps_v4,
            ],
            outputs=[x_output],  #x1_7, x2_7, x3_7, x4_7,
            name=net_name + 'overall')

    print('Compiling ...')
    model.compile(optimizer=optimizer, loss='mse')
    return model
    str(dropout_reg)
colorprint(Color.BLUE, tmp_str)

#Build the Multi Layer Perceptron model
model = Sequential()
model.add(
    Reshape((IMG_SIZE * IMG_SIZE * 3, ),
            input_shape=(IMG_SIZE, IMG_SIZE, 3),
            name='reshape'))
#model.add(Dense(units=2048, activation='relu',name='second'))
#model.add(Dropout(0.2))
model.add(
    Dense(units=DIM_HIDDEN[0],
          activation='relu',
          kernel_initializer='glorot_normal',
          bias_initializer=Constant(value=0.01),
          name='first_dense',
          kernel_constraint=maxnorm(5),
          kernel_regularizer=regularizers.l2(0.01)))
#kernel_constraint=maxnorm(3)))
#kernel_constraint=maxnorm(3)))
#model.add(BatchNormalization())
#kernel_regularizer=regularizers.l2(0.001)))
#kernel_constraint=maxnorm(3)))
# kernel_regularizer=regularizers.l2(0.001)))
model.add(Dropout(drop_prob))

model.add(
    Dense(units=DIM_HIDDEN[1],
          activation='relu',
          kernel_initializer='glorot_normal',
Example #20
0
    def create_network(self):
        # first level convolutional group
        conv1a = Conv2D(
            64,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        conv1b = Conv2D(
            64,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        pool1 = MaxPooling2D(pool_size=(2, 2))
        drop1 = Dropout(0.7)
        # second level convolutional group
        conv2a = Conv2D(
            128,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        conv2b = Conv2D(
            128,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        pool2 = MaxPooling2D(pool_size=(2, 2))
        drop2 = Dropout(0.7)
        # third level convolutional group
        conv3a = Conv2D(
            256,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        conv3b = Conv2D(
            256,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        pool3 = MaxPooling2D(pool_size=(2, 2))
        drop3 = Dropout(0.7)
        # fourth level convolutional group
        conv4a = Conv2D(
            512,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        conv4b = Conv2D(
            512,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        pool4 = MaxPooling2D(pool_size=(2, 2))
        drop4 = Dropout(0.7)
        # fifth level convolutional group
        conv5a = Conv2D(
            1024,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        conv5b = Conv2D(
            1024,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        # fourth level upsampling
        up4 = UpSampling2D(size=(2, 2))
        upconv4 = Conv2D(
            512,
            2,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        # fourth level merge
        merge4 = Concatenate(axis=3)
        drop42 = Dropout(0.7)
        # fourth level second convolutional group
        conv42a = Conv2D(
            512,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        conv42b = Conv2D(
            512,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        # third level upsampling
        up3 = UpSampling2D(size=(2, 2))
        upconv3 = Conv2D(
            256,
            2,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        # third level merge
        merge3 = Concatenate(axis=3)
        drop32 = Dropout(0.7)
        # third level second convolutional group
        conv32a = Conv2D(
            256,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        conv32b = Conv2D(
            256,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        # second level upsampling
        up2 = UpSampling2D(size=(2, 2))
        upconv2 = Conv2D(
            128,
            2,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        # second level merge
        merge2 = Concatenate(axis=3)
        drop22 = Dropout(0.7)
        # second level second convolutional group
        conv22a = Conv2D(
            128,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        conv22b = Conv2D(
            128,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        # first level upsampling
        up1 = UpSampling2D(size=(2, 2))
        upconv1 = Conv2D(
            64,
            2,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        # first level merge
        merge1 = Concatenate(axis=3)
        drop12 = Dropout(0.7)
        # first level second convolutional group
        conv12a = Conv2D(
            64,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        conv12b = Conv2D(
            64,
            3,
            activation="relu",
            padding="same",
            kernel_initializer="he_normal",
        )
        # output layer
        convout_mean = Conv2D(
            1,
            1,
            activation=None,
            padding="same",
            kernel_initializer="he_normal",
        )
        convout_log_var = Conv2D(
            1,
            1,
            activation=None,
            padding="same",
            kernel_initializer="he_normal",
            bias_initializer=Constant(-3),
        )
        convout = Concatenate(name="probout")

        inputs = Input(self.input_size)

        # first level convolutional group
        c1 = conv1b(conv1a(inputs))
        p1 = drop1(pool1(c1))
        # second level convolutional group
        c2 = conv2b(conv2a(p1))
        p2 = drop2(pool2(c2))
        # third level convolutional group
        c3 = conv3b(conv3a(p2))
        p3 = drop3(pool3(c3))
        # fourth level convolutional group
        c4 = conv4b(conv4a(p3))
        p4 = drop4(pool4(c4))
        # fifth level convolutional group
        c5 = conv5b(conv5a(p4))
        # fourth level upsampling
        u4 = upconv4(up4(c5))
        m4 = drop42(merge4([c4, u4]))
        # fourth level second convolutional group
        c42 = conv42b(conv42a(m4))
        # third level upsampling
        u3 = upconv3(up3(c42))
        m3 = drop32(merge3([c3, u3]))
        # third level second convolutional group
        c32 = conv32b(conv32a(m3))
        # second level upsampling
        u2 = upconv2(up2(c32))
        m2 = drop22(merge2([c2, u2]))
        # second level second convolutional group
        c22 = conv22b(conv22a(m2))
        # first level upsampling
        u1 = upconv1(up1(c22))
        m1 = drop12(merge1([c1, u1]))
        # first level second convolutional group
        c12 = conv12b(conv12a(m1))
        # output layer
        outputs = convout([convout_mean(c12), convout_log_var(c12)])

        return Model(inputs, outputs)
Example #21
0
 def __init__(self):
     super().__init__(3, self.lppl, Constant(0.5))
Example #22
0
    def create_net(self, input_shape):
        net_input = Input(shape=input_shape)
        x = Conv2D(self.filters, (3, 3), padding='same')(net_input)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = MaxPooling2D((2, 2), padding='same')(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = MaxPooling2D((2, 2), padding='same')(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = MaxPooling2D((2, 2), padding='same')(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = MaxPooling2D((2, 2), padding='same')(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = MaxPooling2D((2, 2), padding='same')(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = MaxPooling2D((2, 2), padding='same')(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        x = MaxPooling2D((2, 2), padding='same')(x)

        x = Conv2D(self.filters, (3, 3), padding='same')(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)
        self.encoded = MaxPooling2D((2, 2), padding='same')(x)

        # Keep the encoder part
        self.encoder = Model(net_input, self.encoded)

        # And now the decoder part
        x = Conv2DTranspose(self.filters, (3,3), strides=(2,2), padding='same')(self.encoded)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)

        x = Conv2DTranspose(self.filters, (3,3), strides=(2,2), padding='same')(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)

        x = Conv2DTranspose(self.filters, (3,3), strides=(2,2), padding='same')(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)

        x = Conv2DTranspose(self.filters, (3,3), strides=(2,2), padding='same')(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)

        x = Conv2DTranspose(self.filters, (3,3), strides=(2,2), padding='same')(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)

        x = Conv2DTranspose(self.filters, (3,3), strides=(2,2), padding='same')(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)

        x = Conv2DTranspose(self.filters, (3,3), strides=(2,2), padding='same')(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)

        x = Conv2DTranspose(self.filters, (3,3), strides=(2,2), padding='same')(x)
        x = PReLU(alpha_initializer=Constant(value=0.25))(x)

        self.decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

        self.model = Model(net_input, self.decoded)
        return
    train = y[~y.index.isin(test_index)]
    train_index = train.index.values

    y_values = y.ix[:, 0].values

    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y_values[train_index], y_values[test_index]
    y_train = np.eye(n_classes)[y_train - 1]
    print X_train.shape  # (2465, 900)

    # input layer
    input_signal = Input(shape=(signal_rows, 1))
    print K.int_shape(input_signal)  # (None, 900, 1)

    # define initial parameters
    b_init = Constant(value=0.0)
    k_init = TruncatedNormal(mean=0.0, stddev=0.01, seed=2018)

    # first feature extractor
    conv11 = Conv1D(16,
                    kernel_size=32,
                    strides=1,
                    padding='valid',
                    bias_initializer=b_init,
                    kernel_initializer=k_init)(input_signal)
    bn11 = BatchNormalization()(conv11)
    actv11 = Activation('relu')(bn11)
    conv12 = Conv1D(32,
                    kernel_size=32,
                    strides=1,
                    padding='valid',
clf = Sequential()

clf.add(InputLayer(input_shape=(1, 28, 28)))
clf.add(BatchNormalization())

clf.add(Conv2D(64, (4, 4), padding='same'))
clf.add(LeakyReLU())
clf.add(MaxPool2D(padding='same'))

clf.add(
    Conv2D(
        128,
        (4, 4),
        padding='same',
        bias_initializer=Constant(0.01),
        kernel_initializer='random_uniform',
    ))

clf.add(
    Conv2D(
        64,
        (4, 4),
        padding='same',
        bias_initializer=Constant(0.01),
        kernel_initializer='random_uniform',
    ))
clf.add(LeakyReLU())

clf.add(MaxPool2D(padding='same'))
    other_mat_train = np.concatenate((other_mat_train, ncode_mat_train),
                                     axis=1)
other_mat_trn = other_mat_train
other_mat_val = other_mat_tst

y_trn = train_df.readm30.astype(int).values
y_val = y_true
Y_trn = to_categorical(y_trn)
Y_val = to_categorical(y_val)

# model building
if model_name == 'setsum_nn':
    input_DX1 = Input(shape=(1, ))
    DX1_embed = Embedding(input_dim=n_DX1_cat + len(dx1_ccs_dict) + 1,
                          output_dim=DX1_dim,
                          embeddings_initializer=Constant(embeds['DX1_embed']),
                          name='DX1_embed')(input_DX1)
    DX1_embed = Reshape((DX1_dim, ))(DX1_embed)
    input_DX = Input(shape=(len(DXs), ))
    DX_embed = Embedding(input_dim=n_DX_cat + len(dx_ccs_dict) + 1,
                         output_dim=DX_dim,
                         mask_zero=True,
                         embeddings_initializer=Constant(embeds['DX_embed']),
                         name='DX_embed')(input_DX)
    DX_embed = MaskedDense(DX_dim, activation='relu')(DX_embed)
    DX_embed = MaskedSum()(DX_embed)
    input_PR = Input(shape=(len(PRs), ))
    PR_embed = Embedding(input_dim=n_PR_cat + len(pr_ccs_dict) + 1,
                         output_dim=PR_dim,
                         mask_zero=True,
                         embeddings_initializer=Constant(embeds['PR_embed']),
Example #26
0
def FCN_8s_pretrained(classes, in_shape, l2_reg, nopad=False, test=False):
    vgg16 = VGG16(include_top=False, weights="imagenet", input_shape=in_shape)
    inputs = Input(shape=in_shape)
    if nopad:
        x = inputs
    else:
        x = ZeroPadding2D(padding=(100, 100))(inputs)
    for layer in vgg16.layers[1:]:
        if "conv" in layer.name:
            W, b = layer.get_weights()
            config = layer.get_config()
            config["kernel_regularizer"] = l2(l2_reg)
            config["kernel_initializer"] = Constant(W)
            config["bias_initializer"] = Constant(b)
            conv = Conv2D.from_config(config)
            x = conv(x)
        elif "pool" in layer.name:
            x = MaxPooling2D()(x)

        if layer.name == "block3_pool":
            feat3 = x
        elif layer.name == "block4_pool":
            feat4 = x
    x = Conv2D(filters=4096,
               kernel_size=(7, 7),
               padding="valid",
               activation="relu",
               kernel_regularizer=l2(l2_reg),
               name="fc1")(x)
    x = Dropout(0.5)(x)
    x = Conv2D(filters=4096,
               kernel_size=(1, 1),
               padding="valid",
               activation="relu",
               kernel_regularizer=l2(l2_reg),
               name="fc2")(x)
    x = Dropout(0.5)(x)

    score5 = Conv2D(filters=classes,
                    kernel_size=(1, 1),
                    kernel_regularizer=l2(l2_reg),
                    activation="relu")(x)

    if nopad:
        score5 = Conv2DTranspose(filters=classes,
                                 kernel_size=(14, 14),
                                 strides=(1, 1),
                                 padding="valid",
                                 activation="linear",
                                 kernel_regularizer=l2(l2_reg),
                                 kernel_initializer=Constant(
                                     bilinear_upsample_weights(
                                         "full", classes)
                                 ))(score5)
    else:
        score5 = Conv2DTranspose(filters=classes,
                                 kernel_size=(4, 4),
                                 strides=(2, 2),
                                 padding="same",
                                 activation="linear",
                                 kernel_regularizer=l2(l2_reg),
                                 kernel_initializer=Constant(
                                     bilinear_upsample_weights(2, classes)
                                 ))(score5)

    # pool3 のfeature mapを次元圧縮
    score3 = Conv2D(filters=classes,
                    kernel_size=(1, 1),
                    kernel_regularizer=l2(l2_reg),
                    activation='relu')(feat3)
    # pool4のfeature mapを次元圧縮
    score4 = Conv2D(filters=classes,
                    kernel_size=(1, 1),
                    kernel_regularizer=l2(l2_reg),
                    activation="relu")(feat4)

    # merge p4 and p5
    score4 = CroppingLike2D(K.int_shape(score5))(score4)
    score45 = Add()([score4, score5])

    # p4+p5 を x2 upsampling
    if not nopad:
        score45 = ZeroPadding2D(padding=(1, 1))(score45)
    score45 = Conv2DTranspose(filters=classes,
                              kernel_size=(4, 4),
                              strides=(2, 2),
                              padding="same",
                              activation="linear",
                              kernel_regularizer=l2(l2_reg),
                              kernel_initializer=Constant(
                                  bilinear_upsample_weights(2, classes)
                              ))(score45)

    # p3とp45をmerge
    score3 = CroppingLike2D(K.int_shape(score45))(score3)
    score345 = Add()([score3, score45])

    # p3+p4+p5を x8 upsampling
    if not nopad:
        score345 = ZeroPadding2D(padding=(1, 1))(score345)
    x = Conv2DTranspose(filters=classes,
                        kernel_size=(16, 16),
                        strides=(8, 8),
                        padding="same",
                        activation="linear",
                        kernel_regularizer=l2(l2_reg),
                        kernel_initializer=Constant(
                            bilinear_upsample_weights(8, classes)
                        ))(score345)

    x = CroppingLike2D(K.int_shape(inputs))(x)
    if test:
        x = Activation("softmax")(x)
    model = Model(inputs=inputs, outputs=x)
    return model
Example #27
0
    if i > num_words:
        continue
    embeddings_vector = embeddings_index.get(w)
    if embeddings_vector is not None:
        embeddings_matrix[i] = embeddings_vector

prob_thresh = (y_train_df.sum() / y_train_df.shape[0]).clip(upper=0.5)
prob_thresh
from keras.initializers import Constant
####################################################################
filter_length = 256
cnn_model = Sequential()
cnn_model.add(
    Embedding(vocab_len + 1,
              300,
              embeddings_initializer=Constant(embeddings_matrix),
              input_length=x_train.shape[1]))
cnn_model.add(Dropout(0.2))
cnn_model.add(
    Conv1D(filter_length, 5, padding='valid', activation='relu', strides=1))
cnn_model.add(GlobalMaxPool1D())
cnn_model.add(Dense(y_train_df.shape[1]))
cnn_model.add(Activation('sigmoid'))

cnn_model.compile(optimizer='adam',
                  loss='binary_crossentropy',
                  metrics=['accuracy'])
cnn_model.summary()

callbacks = [
    ReduceLROnPlateau(),
Example #28
0
def FCN_8s(classes, in_shape, l2_reg, nopad=False, test=False):
    """
    VGG16 based FCN model,
    classes: int, number of classes

    return: keras Model object
    """
    inputs = Input(shape=in_shape)
    if nopad:
        x = inputs
    else:
        x = ZeroPadding2D(padding=(100, 100))(inputs)
    x = Conv2D(filters=64,
               kernel_size=(3, 3),
               padding='same',
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = Conv2D(filters=64,
               kernel_size=(3, 3),
               padding='same',
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = MaxPooling2D()(x)

    x = Conv2D(filters=128,
               kernel_size=(3, 3),
               padding="same",
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = Conv2D(filters=128,
               kernel_size=(3, 3),
               padding="same",
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = MaxPooling2D()(x)

    x = Conv2D(filters=256,
               kernel_size=(3, 3),
               padding="same",
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = Conv2D(filters=256,
               kernel_size=(3, 3),
               padding="same",
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = Conv2D(filters=256,
               kernel_size=(3, 3),
               padding="same",
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = MaxPooling2D()(x)

    # pool3のfeature mapを取得
    p3 = x

    x = Conv2D(filters=512,
               kernel_size=(3, 3),
               padding="same",
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = Conv2D(filters=512,
               kernel_size=(3, 3),
               padding="same",
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = Conv2D(filters=512,
               kernel_size=(3, 3),
               padding="same",
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = MaxPooling2D()(x)

    # pool4のfeature mapを取得
    p4 = x

    x = Conv2D(filters=512,
               kernel_size=(3, 3),
               padding="same",
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = Conv2D(filters=512,
               kernel_size=(3, 3),
               padding="same",
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = Conv2D(filters=512,
               kernel_size=(3, 3),
               padding="same",
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = MaxPooling2D()(x)

    x = Conv2D(filters=4096,
               kernel_size=(7, 7),
               padding="valid",
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = Dropout(0.5)(x)
    x = Conv2D(filters=4096,
               kernel_size=(1, 1),
               padding="valid",
               kernel_regularizer=l2(l2_reg))(x)
    x = Activation("relu")(x)
    x = Dropout(0.5)(x)

    score_p5 = Conv2D(filters=classes,
                      kernel_size=(1, 1),
                      kernel_regularizer=l2(l2_reg),
                      activation="relu")(x)

    if nopad:
        score_p5 = Conv2DTranspose(filters=classes,
                                   kernel_size=(14, 14),
                                   strides=(1, 1),
                                   padding="valid",
                                   activation="linear",
                                   kernel_regularizer=l2(l2_reg),
                                   kernel_initializer=Constant(
                                       bilinear_upsample_weights(
                                           "full", classes)
                                   ))(score_p5)
    else:
        score_p5 = Conv2DTranspose(filters=classes,
                                   kernel_size=(4, 4),
                                   strides=(2, 2),
                                   padding="same",
                                   activation="linear",
                                   kernel_regularizer=l2(l2_reg),
                                   kernel_initializer=Constant(
                                       bilinear_upsample_weights(2, classes)
                                   ))(score_p5)

    # pool3 のfeature mapを次元圧縮
    score_p3 = Conv2D(filters=classes,
                      kernel_size=(1, 1),
                      kernel_regularizer=l2(l2_reg),
                      activation='relu')(p3)
    # pool4のfeature mapを次元圧縮
    score_p4 = Conv2D(filters=classes,
                      kernel_size=(1, 1),
                      kernel_regularizer=l2(l2_reg),
                      activation="relu")(p4)

    # merge p4 and p5
    score_p4 = CroppingLike2D(K.int_shape(score_p5))(score_p4)
    score_p45 = Add()([score_p4, score_p5])

    # p4+p5 を x2 upsampling
    if not nopad:
        score_p45 = ZeroPadding2D(padding=(1, 1))(score_p45)
    score_p45 = Conv2DTranspose(filters=classes,
                                kernel_size=(4, 4),
                                strides=(2, 2),
                                padding="same",
                                activation="linear",
                                kernel_regularizer=l2(l2_reg),
                                kernel_initializer=Constant(
                                    bilinear_upsample_weights(2, classes)
                                ))(score_p45)

    # p3とp45をmerge
    score_p3 = CroppingLike2D(K.int_shape(score_p45))(score_p3)
    score_p345 = Add()([score_p3, score_p45])

    # p3+p4+p5を x8 upsampling
    if not nopad:
        score_p345 = ZeroPadding2D(padding=(1, 1))(score_p345)
    x = Conv2DTranspose(filters=classes,
                        kernel_size=(16, 16),
                        strides=(8, 8),
                        padding="same",
                        activation="linear",
                        kernel_regularizer=l2(l2_reg),
                        kernel_initializer=Constant(
                            bilinear_upsample_weights(8, classes)
                        ))(score_p345)

    x = CroppingLike2D(K.int_shape(inputs))(x)
    if test:
        x = Activation("softmax")(x)
    model = Model(inputs=inputs, outputs=x)
    return model
Example #29
0
    def make_layers(self):
        self.word_embedding_layer = Embedding(
            len(self.dataset.word_embedding_matrix),
            self.word_embedding_dim,
            embeddings_initializer=Constant(
                self.dataset.word_embedding_matrix),
            trainable=self.trainable_word_emb,
            mask_zero=self.flag_embedding_layer_mask_zero,
            name='word_emb',
            embeddings_regularizer=l2(self.embedding_regularizer_coefficient))
        self.word_rnn = self.stack_rnn(self.stack[0])
        self.tanh1 = self.make_dense(DIMENSION_ATTENTION, activation='tanh')
        self.att1 = AttentionLayer(name='att1')

        self.rnn2 = self.stack_rnn(self.stack[1])
        self.tanh2 = self.make_dense(DIMENSION_ATTENTION, activation='tanh')
        self.att2 = AttentionLayer(name='att2')
        self.logit = self.make_dense(self.dataset.n_classes,
                                     activation='softmax',
                                     name='logit')
        if self.optimizer_name == 'rmsprop':
            self.optimizer = optimizers.RMSprop(lr=self.initial_learning_rate,
                                                **self.optimizer_kwargs)
        elif self.optimizer_name == 'sgd':
            self.optimizer = optimizers.SGD(lr=self.initial_learning_rate,
                                            momentum=0.9,
                                            **self.optimizer_kwargs)
        elif self.optimizer_name == 'adam':
            self.optimizer = optimizers.Adam(lr=self.initial_learning_rate,
                                             **self.optimizer_kwargs)
        else:
            self.log.write('unknown optimizer name')
            sys.exit(1)
        if self.CHAR:
            self.dataset.make_character_embedding_index()
            self.character_embedding_layer = Embedding(
                len(self.dataset.char_embedding_index),
                len(self.dataset.char_embedding_index) - 2,
                # weights=[self.dataset.char_embedding_matrix],
                embeddings_initializer=Constant(
                    self.dataset.char_embedding_matrix),
                mask_zero=self.char_rnn_flag,
                trainable=True,
                name='ch_emb',
                embeddings_regularizer=l2(
                    self.embedding_regularizer_coefficient))
            if not self.char_rnn_flag:  # character cnn
                self.conv1 = self.make_conv(self.conv_unit_size, 5)
                self.conv2 = self.make_conv(self.conv_unit_size, 2)
            else:  # character rnn
                self.char_rnn = self.make_rnn(self.CHARACTER_RNN_DIMENSION,
                                              False)
            # temp
            self.word_linear = self.make_dense(self.word_embedding_dim,
                                               activation='linear')
            self.char_linear = self.make_dense(self.word_embedding_dim,
                                               activation='linear')
            self.max_tanh = self.make_dense(self.word_embedding_dim,
                                            activation='tanh')
            # layers for merging words and characters
            self.conv_dense = self.make_dense(
                self.word_embedding_dim, activation=self.conv_dense_activation)
            self.max_relu = self.make_dense(self.word_embedding_dim,
                                            activation='relu')
Example #30
0
def fcn_VGG16_8s(INPUT_SIZE,nb_classes):    # previous name: fcn8s_take2
    """ Returns Keras FCN-8 model definition.

      """
    fcn32_flag = False

    inputs = Input(shape=(INPUT_SIZE, INPUT_SIZE, 3))
    # Start from VGG16 layers
    vgg16 = VGG16(weights='imagenet', include_top=False, input_tensor=inputs)

    # Skip connections from pool3, 256 channels
    vgg16_upto_intermediate_layer_pool3 = Model(inputs=vgg16.input, outputs=vgg16.get_layer('block3_pool').output)
    score_pool3 = vgg16_upto_intermediate_layer_pool3.output
    # 1x1 conv layer to reduce number of channels to nb_classes:
    score_pool3c = Conv2D(filters=nb_classes,kernel_size=(1, 1),name="score_pool3c")(score_pool3)

    # Skip connections from pool4, 512 channels
    vgg16_upto_intermediate_layer_pool4 = Model(inputs=vgg16.input, outputs=vgg16.get_layer('block4_pool').output)
    score_pool4 = vgg16_upto_intermediate_layer_pool4.output
    # 1x1 conv layer to reduce number of channels to nb_classes:
    score_pool4c = Conv2D(filters=nb_classes, kernel_size=(1, 1))(score_pool4)

    # score from the top vgg16 layer:
    score_pool5 = vgg16.output
    #n = 4096
    score6c = Conv2D(filters=4096, kernel_size=(7, 7), padding='same', name="conv6")(score_pool5)
    score7c = Conv2D(filters=4096, kernel_size=(1, 1), padding='same', name="conv7")(score6c)

    #score7c = Conv2D(filters=nb_classes,kernel_size=(1, 1))(score6c)
    score7c_upsample = Conv2DTranspose(filters=nb_classes,
                                       kernel_size=(4, 4),
                                       strides=(2, 2),
                                       padding='same',
                                       activation = None,
                                       kernel_initializer = Constant(bilinear_upsample_weights(2, nb_classes)),
                                       name="score_pool7c_upsample")(score7c)

    # Fuse scores
    score_7_4 = Add()([score7c_upsample, score_pool4c])
    # upsample:
    score_7_4_up = Conv2DTranspose(filters=nb_classes,
                                      kernel_size=(4, 4),
                                      strides=(2, 2),
                                      padding='same',
                                      activation= None,
                                      kernel_initializer=Constant(bilinear_upsample_weights(2, nb_classes)),
                                      name="score_7_4_up")(score_7_4)

    # Fuse scores
    score_7_4_3 = Add()([score_7_4_up, score_pool3c])
    # upsample:
    score_7_4_3_up = Conv2DTranspose(filters=nb_classes,
                                   kernel_size=(16, 16),
                                   strides=(8, 8),
                                   padding='same',
                                   activation=None,
                                   kernel_initializer=Constant(bilinear_upsample_weights(8, nb_classes)),
                                   name="score_7_4_3_up")(score_7_4_3)

    # Batch Normalization: (optional)
    #score_7_4_3_up = BatchNormalization()(score_7_4_3_up)

    output = (Activation('softmax'))(score_7_4_3_up)

    # # -- There's another way to match the tensor sizes from earlier layers, using a Cropping2D layer --
    # # e.g., for fcn-16, we can crop layer 'score_pool4c' to get the same size as layer 'score_7c'
    # score_pool4c_cropped = Cropping2D((5+3, 5+3))(score_pool4c)
    # # fuse layers,
    # score_7_4_cropped = Add()([score7c, score_pool4c_cropped])
    # # then upsample to input size:
    # x = Conv2DTranspose(filters=nb_classes,
    #                     kernel_size=(64, 64),
    #                     strides=(32+2,32+2),
    #                     padding='same',
    #                     activation='sigmoid',
    #                     kernel_initializer=Constant(bilinear_upsample_weights(32, nb_classes)))(score_7_4_cropped)

    # Creating the model:
    model = Model(inputs=inputs, outputs=output, name='fcn_VGG16_8s')

    # Fixing weighs in lower layers
    for layer in model.layers[:15]:  # sometimes I use it, sometimes not.
        layer.trainable = False
    return model