Exemple #1
0
def get_model_bidirectional_avg(embed_size = 200 , 
			  embedding_matrix = None, 
              num_lstm = 50 , 
              rate_drop_dense = 0.1,
              num_dense = 50):
    
    if embedding_matrix is None: 
	    print(">> get_model_bidirectional_avg [no pre-trained word embeddings]<<")
	    inp = Input(shape=(maxlen, ))
	    x = Embedding(max_features, embed_size)(inp)
    else:
        print(">> get_model_bidirectional_avg [pre-trained word embeddings]<<")
        embedding_layer = Embedding(max_features,embed_size,weights=[embedding_matrix],input_length=maxlen,trainable=True)
        inp = Input(shape=(maxlen, ) , dtype='int32')
        x = embedding_layer(inp)
    x = Bidirectional(GRU(num_lstm, return_sequences=True))(x)
    x = Dropout(rate_drop_dense)(x)

    #add a GlobalAveragePooling1D, which will average the embeddings of all words in the document
    x = GlobalAveragePooling1D()(x)

    x = Dense(num_dense, activation="relu")(x)
    x = Dropout(rate_drop_dense)(x)
    #x = BatchNormalization()(x)
    x = Dense(6, activation="sigmoid")(x)

    model = Model(inputs=inp, outputs=x)
    model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

    return model
Exemple #2
0
    def build_decoder(self, input_shape, relu_target): 
        '''Build the decoder architecture that reconstructs from a given VGG relu layer.

            Args:
                input_shape: Tuple of input tensor shape, needed for channel dimension
                relu_target: Layer of VGG to decode from
        '''
        decoder_num = dict(zip(['relu1_1', 'relu2_1', 'relu3_1', 'relu4_1', 'relu5_1'], range(1,6)))[relu_target]

        # Dict specifying the layers for each decoder level. relu5_1 is the deepest decoder and will contain all layers
        decoder_archs = {
            5: [ #    layer    filts      HxW  / InC->OutC                                     
                (Conv2DReflect, 512),  # 16x16 / 512->512
                (UpSampling2D,),       # 16x16 -> 32x32
                (Conv2DReflect, 512),  # 32x32 / 512->512
                (Conv2DReflect, 512),  # 32x32 / 512->512
                (Conv2DReflect, 512)], # 32x32 / 512->512
            4: [
                (Conv2DReflect, 256),  # 32x32 / 512->256
                (UpSampling2D,),       # 32x32 -> 64x64
                (Conv2DReflect, 256),  # 64x64 / 256->256
                (Conv2DReflect, 256),  # 64x64 / 256->256
                (Conv2DReflect, 256)], # 64x64 / 256->256
            3: [
                (Conv2DReflect, 128),  # 64x64 / 256->128
                (UpSampling2D,),       # 64x64 -> 128x128
                (Conv2DReflect, 128)], # 128x128 / 128->128
            2: [
                (Conv2DReflect, 64),   # 128x128 / 128->64
                (UpSampling2D,)],      # 128x128 -> 256x256
            1: [
                (Conv2DReflect, 64)]   # 256x256 / 64->64
        }

        code = Input(shape=input_shape, name='decoder_input_'+relu_target)
        x = code

        ### Work backwards from deepest decoder # and build layer by layer
        decoders = reversed(range(1, decoder_num+1))
        count = 0        
        for d in decoders:
            for layer_tup in decoder_archs[d]:
                # Unique layer names are needed to ensure var naming consistency with multiple decoders in graph
                layer_name = '{}_{}'.format(relu_target, count)

                if layer_tup[0] == Conv2DReflect:
                    x = Conv2DReflect(layer_name, filters=layer_tup[1], kernel_size=3, padding='valid', activation='relu', name=layer_name)(x)
                elif layer_tup[0] == UpSampling2D:
                    x = UpSampling2D(name=layer_name)(x)
                
                count += 1

        layer_name = '{}_{}'.format(relu_target, count) 
        output = Conv2DReflect(layer_name, filters=3, kernel_size=3, padding='valid', activation=None, name=layer_name)(x)  # 256x256 / 64->3
        
        decoder_model = Model(code, output, name='decoder_model_'+relu_target)
        
        print(decoder_model.summary())
        
        return decoder_model
def load_DAG_model(weight_file):
    model = None
    if weight_file.endswith('.weights'):
        model = init_model(weight_file, 25)
    elif weight_file.endswith('.h5') or weight_file.endswith('.hdf5'):
        darknet = DarkNet()
        model = SimpleNet(darknet)
    print model.layers[18]
    print model.layers[18].input_shape, model.layers[18].output_shape
    print model.layers[75]
    print model.layers[75].input_shape, model.layers[75].output_shape
    map56 = model.layers[18].output
    map56 = Convolution2D(128,3,3,init='he_uniform',border_mode='same')(map56)
    map56 = LeakyReLU(alpha=0.1)(map56)
    map56 = Convolution2D(32,3,3,init='he_uniform',border_mode='same')(map56)
    map56 = LeakyReLU(alpha=0.1)(map56)
    # map56 = Convolution2D(8,3,3,init='he_uniform',border_mode='same')(map56)
    # map56 = LeakyReLU(alpha=0.1)(map56)
    map56 = Convolution2D(4,3,3,init='he_uniform',border_mode='same')(map56)
    map56 = LeakyReLU(alpha=0.1)(map56)
    map56_flat = Flatten()(map56)
    map7_flat = model.layers[75].output
    concat = merge([map56_flat, map7_flat], mode='concat')
    fc = Dense(4096, init='he_uniform')(concat)
    fc = Dropout(0.5)(fc)
    fc = LeakyReLU(alpha=0.1)(fc)
    fc = Dense(1331, init='he_uniform')(fc)
    new_model = Model(input=model.inputs, output=fc)
    print new_model.layers[-1].output_shape
    print len(new_model.layers)
    if weight_file.endswith('.h5') or weight_file.endswith('.hdf5'):
            new_model.load_weights(weight_file)
    return new_model
def define_model(length, vocab_size):
	# channel 1: 三个 channel 只是对应的 kernel_size 不同, 表示使用不同的窗口大小来控制 n-gram; 4-grams, 6-grams, and 8-grams
	inputs1 = Input(shape=(length,))
	embedding1 = Embedding(vocab_size, 100)(inputs1)
	conv1 = Conv1D(filters=32, kernel_size=4, activation='relu')(embedding1)
	drop1 = Dropout(0.5)(conv1)
	pool1 = MaxPooling1D(pool_size=2)(drop1)
	flat1 = Flatten()(pool1)
	# channel 2
	inputs2 = Input(shape=(length,))
	embedding2 = Embedding(vocab_size, 100)(inputs2)
	conv2 = Conv1D(filters=32, kernel_size=6, activation='relu')(embedding2)
	drop2 = Dropout(0.5)(conv2)
	pool2 = MaxPooling1D(pool_size=2)(drop2)
	flat2 = Flatten()(pool2)
	# channel 3
	inputs3 = Input(shape=(length,))
	embedding3 = Embedding(vocab_size, 100)(inputs3)
	conv3 = Conv1D(filters=32, kernel_size=8, activation='relu')(embedding3)
	drop3 = Dropout(0.5)(conv3)
	pool3 = MaxPooling1D(pool_size=2)(drop3)
	flat3 = Flatten()(pool3)
	# merge
	merged = concatenate([flat1, flat2, flat3])
	# interpretation
	dense1 = Dense(10, activation='relu')(merged)
	outputs = Dense(1, activation='sigmoid')(dense1)
	model = Model(inputs=[inputs1, inputs2, inputs3], outputs=outputs)
	# compile
	model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
	print(model.summary())
	plot_model(model, show_shapes=True, to_file='tmp_multichannel.png')
	return model
Exemple #5
0
def test_saving_multiple_metrics_outputs():
    inputs = Input(shape=(5,))
    x = Dense(5)(inputs)
    output1 = Dense(1, name='output1')(x)
    output2 = Dense(1, name='output2')(x)

    model = Model(inputs=inputs, outputs=[output1, output2])

    metrics = {'output1': ['mse', 'binary_accuracy'],
               'output2': ['mse', 'binary_accuracy']
               }
    loss = {'output1': 'mse', 'output2': 'mse'}

    model.compile(loss=loss, optimizer='sgd', metrics=metrics)

    # assure that model is working
    x = np.array([[1, 1, 1, 1, 1]])
    out = model.predict(x)
    _, fname = tempfile.mkstemp('.h5')
    save_model(model, fname)

    model = load_model(fname)
    os.remove(fname)

    out2 = model.predict(x)
    assert_allclose(out, out2, atol=1e-05)
Exemple #6
0
def test_layer_sharing_at_heterogeneous_depth_with_concat():
    input_shape = (16, 9, 3)
    input_layer = Input(shape=input_shape)

    A = Dense(3, name='dense_A')
    B = Dense(3, name='dense_B')
    C = Dense(3, name='dense_C')

    x1 = B(A(input_layer))
    x2 = A(C(input_layer))
    output = layers.concatenate([x1, x2])

    M = Model(inputs=input_layer, outputs=output)

    x_val = np.random.random((10, 16, 9, 3))
    output_val = M.predict(x_val)

    config = M.get_config()
    weights = M.get_weights()

    M2 = Model.from_config(config)
    M2.set_weights(weights)

    output_val_2 = M2.predict(x_val)
    np.testing.assert_allclose(output_val, output_val_2, atol=1e-6)
def originalimages_fine_tune(pic_shape, nb_classes):
    '''
    :param pic_shape:输入向量的shape,由于是微调,所以shape一般是(256, 16, 16) [(filter_num, new_pic_shape, new_pic_shape)]
    :param nb_classes:
    :return:
    '''
    input_layer = Input(shape=pic_shape)
    # 16x16 -> 8x8
    conv1 = conv2D_bn(input_layer, 512, 3, 3, subsample=(1, 1), border_mode='same')
    conv1 = conv2D_bn(conv1, 512, 3, 3, subsample=(1, 1), border_mode='same')
    pool1 = MaxPooling2D((2, 2), strides=(2, 2), dim_ordering=DIM_ORDERING)(conv1)
    # 8x8 -> 8x8
    conv2 = conv2D_bn(pool1, 512, 3, 3, subsample=(1, 1), border_mode='same')
    conv2 = conv2D_bn(conv2, 512, 3, 3, subsample=(1, 1), border_mode='same')

    flatten = merge([pool1, conv2], mode='concat', concat_axis=CONCAT_AXIS)
    flatten = Flatten()(flatten)
    flatten = Dropout(0.5)(flatten)

    fc1 = Dense(2048, activation='relu')(flatten)
    fc1 = Dropout(0.5)(fc1)
    fc2 = Dense(1024, activation='relu')(fc1)
    fc2 = Dropout(0.5)(fc2)

    preds = Dense(nb_classes, activation='softmax')(fc2)

    model = Model(input=input_layer, output=preds)
    print model.summary()
    return model
Exemple #8
0
def full_model(summary = True):
    input_img = Input(shape=(m, n, 1))
    generator = generator_model(input_img)
    feat = feat_model(generator)
    model = Model(input=input_img, output=[generator, feat], name='architect')
    model.summary()
    return model
Exemple #9
0
    def test_multiple_outputs():
        def func(x):
            return [x * 0.2, x * 0.3]

        def output_shape(input_shape):
            return [input_shape, input_shape]

        def mask(inputs, mask=None):
            return [None, None]

        i = layers.Input(shape=(64, 64, 3))
        o = layers.Lambda(function=func,
                          output_shape=output_shape,
                          mask=mask)(i)

        o1, o2 = o
        assert o1._keras_shape == (None, 64, 64, 3)
        assert o2._keras_shape == (None, 64, 64, 3)

        model = Model(i, o)

        x = np.random.random((4, 64, 64, 3))
        out1, out2 = model.predict(x)
        assert out1.shape == (4, 64, 64, 3)
        assert out2.shape == (4, 64, 64, 3)
        assert_allclose(out1, x * 0.2, atol=1e-4)
        assert_allclose(out2, x * 0.3, atol=1e-4)
Exemple #10
0
    def size_inference_model(self, trained_model=None):
        print('Building size inference model')

        state_dims = cfg.STATE_DIMS
        feat_dims  = cfg.STATE_DIMS#cfg.FEAT_DIMS

        ########################################################################
        # Input
        ########################################################################
        input_feat = Input(batch_shape=(1, feat_dims[0], feat_dims[1], feat_dims[2]), \
                        name='input_feat')
        input_roi  = Input(batch_shape=(1, state_dims[0], state_dims[1]), \
                        name='input_roi')

        # size branch
        rois = Reshape((state_dims[0] * state_dims[1], ))(input_roi)
        rois = RepeatVector(state_dims[2])(rois)
        rois = Permute((2, 1))(rois)
        rois = Reshape((state_dims[0], state_dims[1], state_dims[2]))(rois)

        size_hidden = Conv2D(state_dims[2], (3, 3), dilation_rate=2, padding='same', activation='relu')(input_feat)
        size_hidden = keras.layers.multiply([size_hidden, rois])
        size_hidden = GlobalMaxPooling2D()(size_hidden)
        size_hidden = Dense(self.size_dims, activation = 'relu')(size_hidden)
        size_output = Dense(self.size_dims, activation = 'softmax', name = 'output_size')(size_hidden)

        ########################################################################
        # Compile inference model
        ########################################################################
        inference_model = Model(inputs  = [input_feat, input_roi], outputs = size_output)

        # print(inference_model.summary())
        if trained_model is not None:
            inference_model.set_weights(self.get_size_branch_weights(trained_model))
        return inference_model
def get_model(embedding_lookup_table, dropout, spatial_dropout):
    input_layer = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
    embedding_layer = Embedding(
        input_dim=embedding_lookup_table.shape[0],
        output_dim=embedding_lookup_table.shape[1],
        weights=[embedding_lookup_table],
        # trainable=False
    )(input_layer)
    layer = embedding_layer
    maxpool_embed = GlobalMaxPooling1D()(layer)
    # spatial_dropout = spatial_dropout - 0.002 + np.random.rand() * 0.004
    embed_after_sdp = SpatialDropout1D(spatial_dropout)(layer)
    print('spatial dropout : {0}'.format(spatial_dropout))
    gru_one = Bidirectional(CuDNNGRU(units=64, return_sequences=True))(embed_after_sdp)
    # hyper-parameter vibration
    dropout = dropout - 0.002 + np.random.rand() * 0.004
    print('dropout : {0}'.format(dropout))
    gru_one_after_dp = Dropout(dropout)(gru_one)
    gru_two = Bidirectional(CuDNNGRU(units=64, return_sequences=True))(gru_one_after_dp)
    layer = concatenate([gru_two, gru_one, maxpool_embed])
    layer = AttentionWeightedAverage()(layer)
    output_layer = Dense(6, activation='sigmoid')(layer)
    model = Model(inputs=input_layer, outputs=output_layer)
    model.compile(loss='binary_crossentropy', optimizer=Nadam(), metrics=['acc'])
    return model
Exemple #12
0
def autoEncoderGen(path, input_shape=theShape):
    if os.path.exists(path):
        print('loading: ' + str(sorted(os.listdir('models/'))[-1]))
        autoencoder = load_model(os.path.join('models', sorted(os.listdir('models/'))[-1]))
        name = str(sorted(os.listdir('models/'))[-1])
        print('loaded: ' + name)
    else:
        print('No previous model found.')
        print('Building a new model.')
        input_img = Input(shape=input_shape)  # adapt this if using `channels_first` image data format

        x = UpSampling2D((2, 2))(input_img)
        x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
        x = UpSampling2D((2,2))(x)
        x = Conv2D(8, (5, 5), activation='relu', padding='same')(x)
        x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
        encoded = x

        x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
        x = Conv2D(8, (5, 5), activation='relu', padding='same')(x)
        x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
        decoded = Conv2D(3, (3, 3), activation='relu', padding='same')(x)

        autoencoder = Model(input_img, decoded)
        autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
        autoencoder.save('models/autoencoder.h5')
        name = 'autoencoderV.h5'
    eye_temp = name.replace('.h5', '')
    try:
        eye = int(eye_temp.replace('autoencoderV', '')) - 10000000
    except ValueError:
        eye = 0

    return autoencoder, name, eye
def build_model(processor):
    logging.info('Build model...')

    action_input = Input(shape=(INPUT_LENGTH, INPUT_DIM))
    actual_flop_input = Input(shape=(INPUT_LENGTH, FLOP_DIM))
    flop_input = actual_flop_input

    # 2 dense layers to encode flop
    for dim in FLOP_INTER_DIM:
        flop_input = TimeDistributed(Dense(dim))(flop_input)

    seq = merge([action_input, flop_input], mode='concat', concat_axis=2)
    
    for dim in INTER_DIM:
        seq = LSTM(dim, return_sequences=True, dropout_W=0.2, dropout_U=0.2,
                   consume_less=processor)(seq)
    seq = TimeDistributed(Dense(OUTPUT_DIM))(seq)
    probs = Activation('softmax')(seq)

    model = Model(input=[action_input, actual_flop_input], output=probs)

    # try using different optimizers and different optimizer configs
    model.compile(loss='categorical_crossentropy',
                  optimizer='rmsprop',
                  metrics=['accuracy'])

    # plot(model, to_file='model.png', show_shapes=True, show_layer_names=True)    
    return model
class GAN(object):
    def __init__(self, model_inputs=[],model_outputs=[], name='gan'):
        self.OPTIMIZER = SGD(lr=2e-4,nesterov=True)
        self.NAME=name

        self.inputs = model_inputs
        self.outputs = model_outputs
        self.gan_model = Model(inputs = self.inputs, outputs = self.outputs)
        self.OPTIMIZER = SGD(lr=0.001)
        self.gan_model.compile(loss=[self_regularization_loss, self_regularization_loss],
                            optimizer=self.OPTIMIZER)
        self.save_model_graph()
        self.summary()

    def summary(self):
        return self.gan_model.summary()

    def save_model_graph(self):
        plot_model(self.gan_model, to_file='/out/GAN_Model.png')

    def save_model(self,epoch,batch):
        self.gan_model.save('/out/'+self.NAME+'_Epoch_'+epoch+'_Batch_'+batch+'model.h5')

    def self_regularization_loss(self,y_true,y_pred):
        return tf.multiply(0.0002,tf.reduce_sum(tf.abs(y_pred-y_true)))

    def local_adversarial_loss(self,y_true,y_pred):
        truth = tf.reshape(y_true,(-1,2))
        predicted = tf.reshape(y_pred,(-1,2))

        computed_loss = tf.nn.softmax_cross_entropy_with_logits(labels=truth,logits=predicted)
        output = tf.reduce_mean(computed_loss)
        return output
def build_model(num_users, num_items, latent_dim):

    positive_item_input = Input((1, ), name='positive_item_input')
    negative_item_input = Input((1, ), name='negative_item_input')

    # Shared embedding layer for positive and negative items
    item_embedding_layer = Embedding(
        num_items, latent_dim, name='item_embedding', input_length=1)

    user_input = Input((1, ), name='user_input')

    positive_item_embedding = Flatten()(item_embedding_layer(
        positive_item_input))
    negative_item_embedding = Flatten()(item_embedding_layer(
        negative_item_input))
    user_embedding = Flatten()(Embedding(
        num_users, latent_dim, name='user_embedding', input_length=1)(
            user_input))

    loss = merge(
        [positive_item_embedding, negative_item_embedding, user_embedding],
        mode=bpr_triplet_loss,
        name='loss',
        output_shape=(1, ))

    model = Model(
        input=[positive_item_input, negative_item_input, user_input],
        output=loss)
    model.compile(loss=identity_loss, optimizer=Adam())

    return model
    def compile(self, optimizer, metrics=[]):
        metrics += [mean_q]  # register default metrics

        # Create target V model. We don't need targets for mu or L.
        self.target_V_model = clone_model(self.V_model, self.custom_model_objects)
        self.target_V_model.compile(optimizer='sgd', loss='mse')

        # Build combined model.
        observation_shape = self.V_model.input._keras_shape[1:]
        a_in = Input(shape=(self.nb_actions,), name='action_input')
        o_in = Input(shape=observation_shape, name='observation_input')
        L_out = self.L_model([a_in, o_in])
        V_out = self.V_model(o_in)
        mu_out = self.mu_model(o_in)
        A_out = NAFLayer(self.nb_actions)(merge([L_out, mu_out, a_in], mode='concat'))
        combined_out = merge([A_out, V_out], mode='sum')
        combined = Model(input=[a_in, o_in], output=combined_out)

        # Compile combined model.
        if self.target_model_update < 1.:
            # We use the `AdditionalUpdatesOptimizer` to efficiently soft-update the target model.
            updates = get_soft_target_model_updates(self.target_V_model, self.V_model, self.target_model_update)
            optimizer = AdditionalUpdatesOptimizer(optimizer, updates)
        
        def clipped_mse(y_true, y_pred):
            delta = K.clip(y_true - y_pred, self.delta_range[0], self.delta_range[1])
            return K.mean(K.square(delta), axis=-1)
        
        combined.compile(loss=clipped_mse, optimizer=optimizer, metrics=metrics)
        self.combined_model = combined

        self.compiled = True
def test_shared_batchnorm():
    '''Test that a BN layer can be shared
    across different data streams.
    '''
    # Test single layer reuse
    bn = normalization.BatchNormalization(input_shape=(10,), mode=0)
    x1 = Input(shape=(10,))
    bn(x1)

    x2 = Input(shape=(10,))
    y2 = bn(x2)

    x = np.random.normal(loc=5.0, scale=10.0, size=(2, 10))
    model = Model(x2, y2)
    assert len(model.updates) == 2
    model.compile('sgd', 'mse')
    model.train_on_batch(x, x)

    # Test model-level reuse
    x3 = Input(shape=(10,))
    y3 = model(x3)
    new_model = Model(x3, y3)
    assert len(model.updates) == 2
    new_model.compile('sgd', 'mse')
    new_model.train_on_batch(x, x)
def baseline_train(noise_examples, hidden_size, noise_dim, glove, hypo_len, version):
    prem_input = Input(shape=(None,), dtype='int32', name='prem_input')
    hypo_input = Input(shape=(hypo_len + 1,), dtype='int32', name='hypo_input')
    noise_input = Input(shape=(1,), dtype='int32', name='noise_input')
    train_input = Input(shape=(None,), dtype='int32', name='train_input')
    class_input = Input(shape=(3,), name='class_input')
    concat_dim = hidden_size + noise_dim + 3
    prem_embeddings = make_fixed_embeddings(glove, None)(prem_input)
    hypo_embeddings = make_fixed_embeddings(glove, hypo_len + 1)(hypo_input)

    premise_layer = LSTM(output_dim=hidden_size, return_sequences=False,
                            inner_activation='sigmoid', name='premise')(prem_embeddings)
    
    noise_layer = Embedding(noise_examples, noise_dim,
                            input_length = 1, name='noise_embeddings')(noise_input)
    flat_noise = Flatten(name='noise_flatten')(noise_layer)    
    merged = merge([premise_layer, class_input, flat_noise], mode='concat')
    creative = Dense(concat_dim, name = 'cmerge')(merged)
    fake_merge = Lambda(lambda x:x[0], output_shape=lambda x:x[0])([hypo_embeddings, creative])
    hypo_layer = FeedLSTM(output_dim=concat_dim, return_sequences=True,
                         feed_layer = creative, inner_activation='sigmoid', 
                         name='attention')([fake_merge])

    hs = HierarchicalSoftmax(len(glove), trainable = True, name='hs')([hypo_layer, train_input])
    inputs = [prem_input, hypo_input, noise_input, train_input, class_input]


    model_name = 'version' + str(version)
    model = Model(input=inputs, output=hs, name = model_name)
    model.compile(loss=hs_categorical_crossentropy, optimizer='adam')

    return model
def baseline_test(train_model, glove, batch_size):
    version = int(train_model.name[-1])
    hidden_size = train_model.get_layer('attention').output_shape[-1]    
    
    premise_input = Input(batch_shape=(batch_size, None, None))
    hypo_input = Input(batch_shape=(batch_size, 1), dtype='int32')
    creative_input = Input(batch_shape=(batch_size, None))
    train_input = Input(batch_shape=(batch_size, 1), dtype='int32')

    hypo_embeddings = make_fixed_embeddings(glove, 1)(hypo_input)
    hypo_layer = FeedLSTM(output_dim=hidden_size, return_sequences=True, 
                         stateful = True, trainable= False, feed_layer = premise_input,
                         name='attention')([hypo_embeddings])
    hs = HierarchicalSoftmax(len(glove), trainable = False, name ='hs')([hypo_layer, train_input])

    inputs = [hypo_input, creative_input, train_input]
    outputs = [hs]

    model = Model(input=inputs, output=outputs, name=train_model.name)
    model.compile(loss=hs_categorical_crossentropy, optimizer='adam')

    update_gen_weights(model, train_model)
    f_inputs = [train_model.get_layer('noise_embeddings').output,
                    train_model.get_layer('class_input').input,
                    train_model.get_layer('prem_input').input]
    func_noise = theano.function(f_inputs,  train_model.get_layer('cmerge').output,
                                     allow_input_downcast=True)

    return model, None, func_noise
Exemple #20
0
    def build_generator(self):

        noise = Input(shape=(self.latent_dim,))

        x = Dense(32 * self.img_rows * self.img_cols, activation='relu')(noise)

        x = Reshape((self.img_rows, self.img_cols, 32))(x)

        x = Conv2DTranspose(512, kernel_size=3, padding='same')(x)
        x = BatchNormalization(momentum=0.8)(x)
        x = Activation("relu")(x)

        x = Conv2DTranspose(256, kernel_size=3, strides=1, padding="same")(x)
        x = BatchNormalization(momentum=0.8)(x)
        x = Activation("relu")(x)

        x = Conv2DTranspose(128, kernel_size=3, strides=1, padding="same")(x)
        x = BatchNormalization(momentum=0.8)(x)
        x = Activation("relu")(x)

        x = Conv2DTranspose(self.channels, kernel_size=3, padding="same")(x)
        img = Activation("tanh")(x)

        model = Model(noise, img)
        model.summary()
        return model
def autoe_train(hidden_size, noise_dim, glove, hypo_len, version):

    prem_input = Input(shape=(None,), dtype='int32', name='prem_input')
    hypo_input = Input(shape=(hypo_len + 1,), dtype='int32', name='hypo_input')
    train_input = Input(shape=(None,), dtype='int32', name='train_input')
    class_input = Input(shape=(3,), name='class_input')

    prem_embeddings = make_fixed_embeddings(glove, None)(prem_input)
    hypo_embeddings = make_fixed_embeddings(glove, hypo_len + 1)(hypo_input)
    premise_encoder = LSTM(output_dim=hidden_size, return_sequences=True,
                            inner_activation='sigmoid', name='premise_encoder')(prem_embeddings)

    hypo_encoder = LSTM(output_dim=hidden_size, return_sequences=True,
                            inner_activation='sigmoid', name='hypo_encoder')(hypo_embeddings)
    class_encoder = Dense(hidden_size, activation='tanh')(class_input)

    encoder = LstmAttentionLayer(output_dim=hidden_size, return_sequences=False,
                  feed_state = True, name='encoder') ([hypo_encoder, premise_encoder, class_encoder])
    if version == 6:
        reduction = Dense(noise_dim, name='reduction', activation='tanh')(encoder)
    elif version == 7:
        z_mean = Dense(noise_dim, name='z_mean')(encoder)
        z_log_sigma = Dense(noise_dim, name='z_log_sigma')(encoder)
          
        def sampling(args):
            z_mean, z_log_sigma = args
            epsilon = K.random_normal(shape=(64, noise_dim,),
                              mean=0., std=0.01)
            return z_mean + K.exp(z_log_sigma) * epsilon
        reduction = Lambda(sampling, output_shape=lambda sh: (sh[0][0], noise_dim,), name = 'reduction')([z_mean, z_log_sigma])
        def vae_loss(args):
            z_mean, z_log_sigma = args
            return - 0.5 * K.mean(1 + z_log_sigma - K.square(z_mean) - K.exp(z_log_sigma), axis=-1)    
        vae = Lambda(vae_loss, output_shape=lambda sh: (sh[0][0], 1,), name = 'vae_output')([z_mean, z_log_sigma])

    merged = merge([class_input, reduction], mode='concat')
    creative = Dense(hidden_size, name = 'expansion', activation ='tanh')(merged)
    premise_decoder = LSTM(output_dim=hidden_size, return_sequences=True,
                            inner_activation='sigmoid', name='premise')(prem_embeddings)
    
    hypo_decoder = LSTM(output_dim=hidden_size, return_sequences=True,
                            inner_activation='sigmoid', name='hypo')(hypo_embeddings)
    attention = LstmAttentionLayer(output_dim=hidden_size, return_sequences=True,
                     feed_state = True, name='attention') ([hypo_decoder, premise_decoder, creative])

    hs = HierarchicalSoftmax(len(glove), trainable = True, name='hs')([attention, train_input])

    inputs = [prem_input, hypo_input, train_input, class_input]

    model_name = 'version' + str(version)
    model = Model(input=inputs, output=(hs if version == 6 else [hs, vae]), name = model_name)
    if version == 6:
        model.compile(loss=hs_categorical_crossentropy, optimizer='adam')
    elif version == 7:
        def minimize(y_true, y_pred):
            return y_pred
        def metric(y_true, y_pred):
            return K.mean(y_pred)
        model.compile(loss=[hs_categorical_crossentropy, minimize], metrics={'hs':word_loss, 'vae_output': metric}, optimizer='adam')
    return model
Exemple #22
0
    def build_net_CNN(self, load_weights = False):

        img = Input(batch_shape=(10, self.img_channels, self.img_rows, self.img_cols), name='input_img')

        model_in = MaskedConvolution2D(self.h,7,7,mask_type='a', direction='Right', border_mode='same', init='he_uniform')(img)

        for _ in range(15):
            model_in = MaskedConvolution2D(self.h,3,3,mask_type='b', direction='Right', border_mode='same', init='he_uniform')(model_in)

        model_out = MaskedConvolution2D(self.h,1,1,mask_type='b', direction='Right', border_mode='same', activation='relu', init='he_uniform')(model_in)
        model_out = MaskedConvolution2D(256*3,1,1,mask_type='b', direction='Right', border_mode='same', activation='relu', init='he_uniform')(model_out)

        Red = GetColors(0)(model_out)
        Green = GetColors(1)(model_out)
        Blue = GetColors(2)(model_out)

        Red_out = SoftmaxLayer(name='Red_out')(Red)
        Green_out = SoftmaxLayer(name='Green_out')(Green)
        Blue_out = SoftmaxLayer(name='Blue_out')(Blue)

        Col_Model = Model(img, [Red_out, Green_out, Blue_out])

        if load_weights:
            Col_Model.load_weights('Data/comp_model.h5')

        print("Compiling...")
        Col_Model.compile(optimizer=self.optimizer,
                              loss={'Red_out': image_categorical_crossentropy,
                                    'Green_out': image_categorical_crossentropy,
                                    'Blue_out': image_categorical_crossentropy},
                              metrics={'Red_out': 'accuracy',
                                       'Green_out': 'accuracy',
                                       'Blue_out': 'accuracy'})
        self.comp_net = Col_Model
Exemple #23
0
def test_recurrent_wrapper__sequence_inputs():
    """The hidden state is not returned, but mapped by an output model.
    """
    def build_recurrent():
        input_current = Input(shape=(10,))
        input_seq = Input(shape=(None, 10))

        hidden = Input(shape=(128,))

        seq = GlobalAveragePooling1D()(input_seq)
        new_hidden = merge([
            Dense(128, activation='relu')(seq),
            Dense(128, activation='relu')(input_current),
            hidden
        ], mode='sum')
        new_hidden = Activation('sigmoid')(new_hidden)

        return RecurrentWrapper(
            input=[input_current],
            sequence_input=[input_seq],
            output=[new_hidden],
            bind={
                hidden: new_hidden
            },
            return_sequences=True,
        )

    seq = Input((None, 10))
    output = build_recurrent()([seq, seq])

    m = Model(input=[seq], output=[output])
    assert m.predict(np.random.uniform(size=(30, 20, 10))).shape == (30, 20, 128)
Exemple #24
0
    def _residual_block(self, injection_index, new_layers, m, member_number):
        #get output shape of last layer before injection from m
        if injection_index > 0:
            input_shape = m.layers[injection_index - 1].output_shape
        else:
            input_shape = m.input_shape
        #make input
        input_layer = Input(shape = input_shape[1:], name = "Input_BARN_{0}".format(member_number))
        #make real layers
        real_layers = input_layer
        for i,l in enumerate(new_layers):
            l['config']['name'] = "BARN-incremental-{0}-{1}".format(
                member_number, i)
            real_layers = layer_from_config(l)(real_layers)
        #make skip layer
        stride_width = input_shape[2] / real_layers._keras_shape[2]
        stride_height = input_shape[3] / real_layers._keras_shape[3]
        equal_channels = real_layers._keras_shape[1] == input_shape[1]
        shortcut = input_layer
        # 1 X 1 conv if shape is different. Else identity.
        if (stride_width > 1 or stride_height > 1 or not equal_channels) and stride_width > 0 and stride_height > 0:
            shortcut = Convolution2D(nb_filter=real_layers._keras_shape[1], nb_row=1, nb_col=1,
                                     subsample=(stride_width, stride_height),
                                     init="he_normal",
                                     border_mode="same",
                                     name="shortcut_BARN_{0}".format(member_number))(input_layer)

        #make merge
        merge_layer = merge([real_layers,shortcut], mode="sum", name = "merge_BARN_{0}".format(member_number))
        #make model
        model = Model(input=input_layer,output=merge_layer,
                name="Model_BARN_{0}".format(member_number))
        #make config
        return {"class_name": "Model", "config": model.get_config()}
Exemple #25
0
def test_recurrent_wrapper__multiple_inputs():
    def build_recurrent():
        hidden = Input((128,))
        input_x = Input((10,))
        input_y = Input((5,))

        x = Dense(128, activation='relu')(input_x)
        y = Dense(128, activation='relu')(input_y)
        x = merge([hidden, x, y], mode='sum')
        new_hidden = Activation('sigmoid')(x)

        return RecurrentWrapper(
            input=[input_x, input_y],
            output=[new_hidden],
            bind={hidden: new_hidden},
            return_sequences=True,
        )

    x = Input((None, 10))
    y = Input((None, 5))
    u = build_recurrent()([x, y])

    m = Model(input=[x, y], output=[u])
    actual = m.predict([
        np.random.uniform(size=(30, 20, 10)),
        np.random.uniform(size=(30, 20, 5))
    ])

    assert actual.shape == (30, 20, 128)
def cnn_rnn_model(input_dim, filters, kernel_size, conv_stride,
    conv_border_mode, units, output_dim=29):
    """ Build a recurrent + convolutional network for speech 
    """
    # Main acoustic input
    input_data = Input(name='the_input', shape=(None, input_dim))
    # Add convolutional layer
    conv_1d = Conv1D(filters, kernel_size, 
                     strides=conv_stride, 
                     padding=conv_border_mode,
                     activation='relu',
                     name='conv1d')(input_data)
    # Add batch normalization
    bn_cnn = BatchNormalization(name='bn_conv_1d')(conv_1d)
    # Add a recurrent layer
    #simp_rnn = SimpleRNN(units, activation='relu',
    simp_rnn = GRU(units, activation='relu',
        return_sequences=True, implementation=2, name='rnn')(bn_cnn)
    # TODO-DONE: Add batch normalization
    bn_rnn = BatchNormalization(name='bn_rnn')(simp_rnn)
    # TODO-DONE: Add a TimeDistributed(Dense(output_dim)) layer
    time_dense = TimeDistributed(Dense(output_dim))(bn_rnn)
    # Add softmax activation layer
    y_pred = Activation('softmax', name='softmax')(time_dense)
    # Specify the model
    model = Model(inputs=input_data, outputs=y_pred)
    model.output_length = lambda x: cnn_output_length(
        x, kernel_size, conv_border_mode, conv_stride)
    print(model.summary())
    return model
Exemple #27
0
def VGG16_Model(img_rows=224, img_cols=224, train=False):
    if K.image_data_format() == 'channels_first':
        shape_ord = (3, img_rows, img_cols)
    else:  # channel_last
        shape_ord = (img_rows, img_cols, 3)

    vgg16_model = vgg16.VGG16(weights=None, include_top=False, input_tensor=Input(shape_ord))
    # vgg16_model.summary()

    for layer in vgg16_model.layers:
        layer.trainable = train  # freeze layer

    #add last fully-connected layers
    x = Flatten(input_shape=vgg16_model.output.shape)(vgg16_model.output)
    x = Dense(4096, activation='relu', name='ft_fc1')(x)
    x = Dropout(0.5)(x)
    x = BatchNormalization()(x)
    predictions = Dense(43, activation='softmax')(x)

    model = Model(inputs=vgg16_model.input, outputs=predictions)

    #compile the model
    model.compile(optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
                loss='categorical_crossentropy', metrics=['accuracy'])

    for layer in model.layers:
        layer.trainable = train  # freeze layer

    return model
def age_gender_model(input_shape, nb_classes):
    """
    """

    resnet_base = ResNet50(include_top = False, weights = 'imagenet',input_tensor = None, input_shape = input_shape ,pooling = 'max')

    for layer in resnet_base.layers:
    	layer.trainable = False
    
        
    #x = Flatten()(x)
    x = Dense(1024, activation = "relu",name='dense-1')(resnet_base.output)
    x = Dropout(rate = 0.5)(x)
    x = Dense(512, activation = "relu",name='dense-2')(x)
    x = Dropout(rate = 0.5)(x)
    x = Dense(512, activation ="relu",name='dense-3')(x)
    x = Dropout(rate = 0.5)(x)
    x = Dense(256, activation ="relu",name='dense-4')(x)
    x = Dropout(rate = 0.5)(x)    

    predictions = Dense(nb_classes, activation="softmax",name="softmax")(x)
    
    model = Model(inputs = resnet_base.input, outputs = predictions)

    print(model.summary())
    return model
Exemple #29
0
def test_build_feature_space(replay, epoch_models):
    from deepreplay.plot import build_2d_grid
    from keras.models import Model

    contour_points = 30
    _, ax = plt.subplots(1, 1)
    replay.build_feature_space(ax, 'hidden', contour_points=contour_points, epoch_start=2, epoch_end=19)
    _, data = replay.feature_space
    _, bent_line, actual_prediction, _ = data
    _, _, actual_bent_contour = bent_line

    contour_lines = build_2d_grid((-1, 1), (-1, 1), contour_points, contour_points).reshape(-1, 2)

    expected_prediction = []
    expected_bent_contour = []
    for model in epoch_models[1:-1]:
        expected_prediction.append(model.predict_classes(contour_lines))
        activations = Model(inputs=model.input,
                            outputs=model.get_layer('hidden').output)
        expected_bent_contour.append(activations.predict(contour_lines))

    expected_prediction = np.array(expected_prediction).reshape(18, contour_points, contour_points)
    expected_bent_contour = np.array(expected_bent_contour).reshape(18, contour_points, contour_points, 2)

    npt.assert_allclose(actual_prediction.squeeze(), expected_prediction, atol=1e-5)
    npt.assert_allclose(actual_bent_contour.squeeze(), expected_bent_contour, atol=1e-5)
def get_model(embedding_lookup_table, dropout, spatial_dropout):
    input_layer = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
    embedding_layer = Embedding(
        input_dim=embedding_lookup_table.shape[0],
        output_dim=embedding_lookup_table.shape[1],
        weights=[embedding_lookup_table],
        trainable=False
    )(input_layer)
    layer = embedding_layer
    layer = SpatialDropout1D(spatial_dropout)(layer)
    print('spatial dropout : {0}'.format(spatial_dropout))
    # hyper-parameter vibration
    dropout = dropout - 0.002 + np.random.rand() * 0.004
    print('dropout : {0}'.format(dropout))
    layer = Bidirectional(CuDNNGRU(units=64, return_sequences=True))(layer)
    layer = Dropout(dropout)(layer)
    layer = Bidirectional(CuDNNGRU(units=64, return_sequences=False))(layer)
    layer = Dense(400, kernel_initializer='he_normal')(layer)
    layer = PReLU()(layer)
    layer = BatchNormalization()(layer)
    layer = Dropout(dropout)(layer)
    output_layer = Dense(6, activation='sigmoid')(layer)
    model = Model(inputs=input_layer, outputs=output_layer)
    model.compile(loss='binary_crossentropy', optimizer=Nadam(), metrics=['acc'])
    return model
model = load_model("model_9.h5")
model._make_predict_function()

# In[3]:


model_temp = ResNet50(weights="imagenet",input_shape=(224,224,3))

model_temp.summary()


# In[4]:


model_resnet = Model(model_temp.input,model_temp.layers[-2].output)
model_resnet._make_predict_function()

# In[5]:


def preprocess_img(img):
    img = image.load_img(img,target_size=(224,224))
    img = image.img_to_array(img)
    img = np.expand_dims(img,axis=0)
    # Normalisation
    img = preprocess_input(img)
    return img


# In[6]:
#                          validation_steps=5000)
# classifier.save('catdog_cnn_model.h5')

# classifier = load_model('catdog_cnn_model.h5')
# classifier.fit(X_train, y_train)
# y_pred=classifier.predict(X_test)

# k.history.keys()
scores = classifier.evaluate(X_validation, Y_validation, verbose=0)
print("%s: %.2f%%" % (classifier.metrics_names[1], scores[1] * 100))
# print("%s: %.2f%%" % (classifier.metrics_names[0], scores[0]*100))

from keras.models import Model
import matplotlib.pyplot as plt
layer_outputs = [layer.output for layer in classifier.layers]
activation_model = Model(inputs=classifier.input, outputs=layer_outputs)
activations = activation_model.predict(X_train[2].reshape(1, 150, 150, 3))


def display_activation(activations, col_size, row_size, act_index):
    activation = activations[act_index]
    activation_index = 0
    fig, ax = plt.subplots(row_size,
                           col_size,
                           figsize=(row_size * 2.5, col_size * 1.5))
    for row in range(0, row_size):
        for col in range(0, col_size):
            ax[row][col].imshow(activation[0, :, :, activation_index],
                                cmap='viridis')
            activation_index += 1
def train_and_predict(x_train, y_train, x_valid, y_valid, fold):
    # data augmentation
    x_train = np.append(x_train, [np.fliplr(x) for x in x_train], axis=0)
    y_train = np.append(y_train, [np.fliplr(x) for x in y_train], axis=0)
    print("x_train after hflip", x_train.shape)
    print("y_train after hflip", y_valid.shape)

    # model
    input_layer = Input((img_size_target, img_size_target, 3))
    output_layer = build_model(input_layer, 16, 0.5)

    model1 = Model(input_layer, output_layer)

    c = optimizers.adam(lr=0.005)
    model1.compile(loss="binary_crossentropy",
                   optimizer=c,
                   metrics=[my_iou_metric])

    save_model_name = f"{basic_name}_stage1_fold{fold}.hdf5"

    early_stopping = EarlyStopping(monitor='my_iou_metric',
                                   mode='max',
                                   patience=15,
                                   verbose=1)
    model_checkpoint = ModelCheckpoint(save_model_name,
                                       monitor='my_iou_metric',
                                       mode='max',
                                       save_best_only=True,
                                       verbose=1)

    reduce_lr = ReduceLROnPlateau(monitor='my_iou_metric',
                                  mode='max',
                                  factor=0.5,
                                  patience=5,
                                  min_lr=0.0001,
                                  verbose=1)

    epochs = 80
    batch_size = 128

    if not PREDICT_ONLY:
        history = model1.fit(
            x_train,
            y_train,
            validation_data=[x_valid, y_valid],
            epochs=epochs,
            batch_size=batch_size,
            callbacks=[early_stopping, model_checkpoint, reduce_lr],
            verbose=2)

    model1 = load_model(save_model_name,
                        custom_objects={'my_iou_metric': my_iou_metric})
    # remove activation layer and use lovasz loss
    input_x = model1.layers[0].input

    output_layer = model1.layers[-1].input
    model = Model(input_x, output_layer)
    c = optimizers.adam(lr=0.01)

    model.compile(loss=lovasz_loss, optimizer=c, metrics=[my_iou_metric_2])

    save_model_name = f"{basic_name}_stage2_fold{fold}.hdf5"

    early_stopping = EarlyStopping(monitor='val_my_iou_metric_2',
                                   mode='max',
                                   patience=30,
                                   verbose=1)
    model_checkpoint = ModelCheckpoint(save_model_name,
                                       monitor='val_my_iou_metric_2',
                                       mode='max',
                                       save_best_only=True,
                                       verbose=1)
    reduce_lr = ReduceLROnPlateau(monitor='val_my_iou_metric_2',
                                  mode='max',
                                  factor=0.5,
                                  patience=5,
                                  min_lr=0.00005,
                                  verbose=1)
    epochs = 120
    batch_size = 128

    if not PREDICT_ONLY:
        history = model.fit(
            x_train,
            y_train,
            validation_data=[x_valid, y_valid],
            epochs=epochs,
            batch_size=batch_size,
            callbacks=[model_checkpoint, reduce_lr, early_stopping],
            verbose=2)

    model = load_model(save_model_name,
                       custom_objects={
                           'my_iou_metric_2': my_iou_metric_2,
                           'lovasz_loss': lovasz_loss
                       })

    def predict_result(model, x_test,
                       img_size_target):  # predict both orginal and reflect x
        x_test_reflect = np.array([np.fliplr(x) for x in x_test])
        preds_test = model.predict(x_test).reshape(-1, img_size_target,
                                                   img_size_target)
        preds_test2_refect = model.predict(x_test_reflect).reshape(
            -1, img_size_target, img_size_target)
        preds_test += np.array([np.fliplr(x) for x in preds_test2_refect])
        return preds_test / 2

    preds_valid = predict_result(model, x_valid, img_size_target)
    preds_test = predict_result(model, x_test, img_size_target)
    return preds_valid, preds_test
input_answer = Input(shape=(maxlen_input,), dtype='int32', name='the answer text up to the current token')
LSTM_encoder = LSTM(sentence_embedding_size, kernel_initializer= 'lecun_uniform', name='Encode context')
LSTM_decoder = LSTM(sentence_embedding_size, kernel_initializer= 'lecun_uniform', name='Encode answer up to the current token')

Shared_Embedding = Embedding(output_dim=word_embedding_size, input_dim=dictionary_size, input_length=maxlen_input, name='Shared')
word_embedding_context = Shared_Embedding(input_context)
context_embedding = LSTM_encoder(word_embedding_context)

word_embedding_answer = Shared_Embedding(input_answer)
answer_embedding = LSTM_decoder(word_embedding_answer)

merge_layer = concatenate([context_embedding, answer_embedding], axis=1, name='concatenate the embeddings of the context and the answer up to current token')
out = Dense(dictionary_size/2, activation="relu", name='relu activation')(merge_layer)
out = Dense(dictionary_size, activation="softmax", name='likelihood of the current token using softmax activation')(out)

model = Model(inputs=[input_context, input_answer], outputs = [out])

model.compile(loss='categorical_crossentropy', optimizer=ad)

# Loading the data:
vocabulary = cPickle.load(open(vocabulary_file, 'rb'))

print("\n \n \n \n    CHAT:     \n \n")

# Processing the user query:
prob = 0
que = ''
last_query  = ' '
last_last_query = ''
text = ' '
last_text = ''
model=applications.VGG16(weights='imagenet',include_top=False)

# VGG16是函数式模型
# top_model=Sequential()
# top_model.add(Flatten(input_shape=model.output_shape[1:]))
# top_model.add(Dense(256,activation='relu'))
# top_model.add(Dropout(0.5))
# top_model.add(Dense(1,activation='sigmoid'))

x=model.output
x = GlobalAveragePooling2D()(x)
x = Dense(256, activation='relu', name='fc1')(x)
x = Dropout(0.5)(x)
predictions=Dense(1,activation='sigmoid')(x)

vgg_model=Model(input=model.input,output=predictions)

# vgg_model.load_weights('E:/ML/models/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5')

# 两个网络的融合,有错
# model.add(top_model)

# 将最后一个卷积块前的卷积参数冻结

for layer in vgg_model.layers[:25]:
    layer.trainable=False

vgg_model.compile(loss='binary_crossentropy',optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),metrics=['accuracy'])

datagen=ImageDataGenerator(
    rescale=1./255,
Exemple #36
0
	def CreateModel(self):
		'''
		定义CNN/LSTM/CTC模型,使用函数式模型
		输入层:200维的特征值序列,一条语音数据的最大长度设为1600(大约16s)
		隐藏层:3*3卷积层
		隐藏层:池化层,池化窗口大小为2
		隐藏层:Dropout层,需要断开的神经元的比例为0.2,防止过拟合
		隐藏层:全连接层
		目标输出层:全连接层,神经元数量为self.MS_OUTPUT_SIZE,使用softmax作为激活函数
		输出层:自定义层,即CTC层,使用CTC的loss作为损失函数,实现连接性时序多输出
		
		'''
		# 每一帧使用13维mfcc特征及其13维一阶差分和13维二阶差分表示,最大信号序列长度为1500
		input_data = Input(name='the_input', shape=(self.AUDIO_LENGTH, self.AUDIO_FEATURE_LENGTH, 1))
		
		layer_h1 = Conv2D(32, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(input_data) # 卷积层
		layer_h1 = Dropout(0.1)(layer_h1)
		layer_h2 = Conv2D(32, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h1) # 卷积层
		layer_h3 = MaxPooling2D(pool_size=2, strides=None, padding="valid")(layer_h2) # 池化层
		#layer_h3 = Dropout(0.2)(layer_h2) # 随机中断部分神经网络连接,防止过拟合
		layer_h3 = Dropout(0.1)(layer_h3)
		layer_h4 = Conv2D(64, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h3) # 卷积层
		layer_h4 = Dropout(0.2)(layer_h4)
		layer_h5 = Conv2D(64, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h4) # 卷积层
		layer_h6 = MaxPooling2D(pool_size=2, strides=None, padding="valid")(layer_h5) # 池化层
		
		layer_h6 = Dropout(0.2)(layer_h6)
		layer_h7 = Conv2D(128, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h6) # 卷积层
		layer_h7 = Dropout(0.3)(layer_h7)
		layer_h8 = Conv2D(128, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h7) # 卷积层
		layer_h9 = MaxPooling2D(pool_size=2, strides=None, padding="valid")(layer_h8) # 池化层
		
		layer_h9 = Dropout(0.3)(layer_h9)
		layer_h10 = Conv2D(128, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h9) # 卷积层
		layer_h10 = Dropout(0.4)(layer_h10)
		layer_h11 = Conv2D(128, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h10) # 卷积层
		layer_h12 = MaxPooling2D(pool_size=1, strides=None, padding="valid")(layer_h11) # 池化层
		
		#test=Model(inputs = input_data, outputs = layer_h12)
		#test.summary()
		
		layer_h10 = Reshape((200, 3200))(layer_h12) #Reshape层
		#layer_h5 = LSTM(256, activation='relu', use_bias=True, return_sequences=True)(layer_h4) # LSTM层
		#layer_h6 = Dropout(0.2)(layer_h5) # 随机中断部分神经网络连接,防止过拟合
		layer_h10 = Dropout(0.4)(layer_h10)
		layer_h11 = Dense(128, activation="relu", use_bias=True, kernel_initializer='he_normal')(layer_h10) # 全连接层
		layer_h11 = Dropout(0.5)(layer_h11)
		layer_h12 = Dense(self.MS_OUTPUT_SIZE, use_bias=True, kernel_initializer='he_normal')(layer_h11) # 全连接层
		
		y_pred = Activation('softmax', name='Activation0')(layer_h12)
		model_data = Model(inputs = input_data, outputs = y_pred)
		#model_data.summary()
		
		labels = Input(name='the_labels', shape=[self.label_max_string_length], dtype='float32')
		input_length = Input(name='input_length', shape=[1], dtype='int64')
		label_length = Input(name='label_length', shape=[1], dtype='int64')
		# Keras doesn't currently support loss funcs with extra parameters
		# so CTC loss is implemented in a lambda layer
		
		#layer_out = Lambda(ctc_lambda_func,output_shape=(self.MS_OUTPUT_SIZE, ), name='ctc')([y_pred, labels, input_length, label_length])#(layer_h6) # CTC
		loss_out = Lambda(self.ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred, labels, input_length, label_length])
		
		
		
		model = Model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out)
		
		#model.summary()
		
		# clipnorm seems to speeds up convergence
		#sgd = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5)
		ada_d = Adadelta(lr = 0.01, rho = 0.95, epsilon = 1e-06)
		
		#model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd)
		model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer = ada_d)
		
		
		# captures output of softmax so we can decode the output during visualization
		test_func = K.function([input_data], [y_pred])
		
		print('[*提示] 创建模型成功,模型编译成功')
		return model, model_data
    #print(key, value)
    train_x, val_x, train_y, val_y = train_test_split(img_x, y, test_size = .2, shuffle = False)

  inputs = Input(shape=(train_x.shape[1], train_x.shape[2], 1))

# a layer instance is callable on a tensor, and returns a tensor
  a = Conv2D(12, kernel_size=(7,7), activation='relu', padding = "valid", data_format="channels_last")(inputs)
  a = MaxPooling2D(2)(a)
  a = Conv2D(12, kernel_size=(5,5), activation='relu', padding = "valid", data_format="channels_last")(a)
  a = MaxPooling2D(2)(a)
#a = Conv2D(12, kernel_size=(3,3), activation='relu', padding = "valid", data_format="channels_last")(a)
#a = MaxPooling2D(2)(a)
  a = Dense(24, activation='relu')(Flatten()(a))
  predictions = Dense(2, activation='relu')(a)

# This creates a model that includes
# the Input layer and three Dense layers
  model = Model(inputs=inputs, outputs=predictions)
  model.compile(optimizer=SGD(args.learning_rate,args.momentum),
              loss=rmse,
              metrics=['mse'])
  hist = model.fit(x = train_x, y = train_y, validation_data = (val_x,val_y), epochs=100, batch_size=20,  verbose=1)
  preds = model.predict(val_x)
  l2dists_mean, l2dists = l2_dist((preds[:, 0], preds[:, 1]), (val_y["x"], val_y["y"]))
  print('l2_loss={}'.format(l2dists_mean))
  sortedl2_deep = np.sort(l2dists)
  dist_acc = []
  for i in [1, 2, 3, 4, 5]:
    dist_acc = dist_acc + [np.sum(sortedl2_deep <= i)/np.size(sortedl2_deep)]
  print(dist_acc)
def get_model(num_users, num_items, layers=[20, 10], reg_layers=[0, 0]):
    assert len(layers) == len(reg_layers)
    num_layer = len(layers)  # Number of layers in the MLP
    # Input variables
    user_input = Input(shape=(1,), dtype='int32', name='user_input')
    item_input = Input(shape=(1,), dtype='int32', name='item_input')

    # Deprecate: init and W_regularizer
    '''
    MLP_Embedding_User = Embedding(input_dim = num_users, output_dim = layers[0]/2, name = 'user_embedding',
                                  init = init_normal, W_regularizer = l2(reg_layers[0]), input_length=1)
    MLP_Embedding_Item = Embedding(input_dim = num_items, output_dim = layers[0]/2, name = 'item_embedding',
                                  init = init_normal, W_regularizer = l2(reg_layers[0]), input_length=1)
    '''

    DMF1_Embedding_User = Embedding(input_dim=num_users, output_dim=layers[0] // 2, name='dmf1_user_embedding',
                                   embeddings_initializer='random_normal', embeddings_regularizer=l2(reg_layers[0]),
                                   input_length=1)
    DMF1_Embedding_Item = Embedding(input_dim=num_items, output_dim=layers[0] // 2, name='dmf1_item_embedding',
                                   embeddings_initializer='random_normal', embeddings_regularizer=l2(reg_layers[0]),
                                   input_length=1)

    DMF2_Embedding_User = Embedding(input_dim=num_users, output_dim=layers[0] // 2, name='dmf2_user_embedding',
                                   embeddings_initializer='random_normal', embeddings_regularizer=l2(reg_layers[0]),
                                   input_length=1)
    DMF2_Embedding_Item = Embedding(input_dim=num_items, output_dim=layers[0] // 2, name='dmf2_item_embedding',
                                   embeddings_initializer='random_normal', embeddings_regularizer=l2(reg_layers[0]),
                                   input_length=1)

    # Crucial to flatten an embedding vector!
    dmf1_user_latent = Flatten()(DMF1_Embedding_User(user_input))
    dmf1_item_latent = Flatten()(DMF1_Embedding_Item(item_input))

    dmf2_user_latent = Flatten()(DMF2_Embedding_User(user_input))
    dmf2_item_latent = Flatten()(DMF2_Embedding_Item(item_input))

    # The 0-th layer is the concatenation of embedding layers 1#
    # vector = Multiply()([user_latent, item_latent])

    # The 0-th layer is the concatenation of embedding layers 2#
    # vector_r = Multiply()([user_latent, item_latent])
    # vector_l = Concatenate()([user_latent, item_latent])
    # layer = Dense(layers[1], kernel_regularizer=l2(reg_layers[0]), activation='linear', name='pre-layer')
    # vector_l = layer(vector_l)
    # vector = Add()([vector_r, vector_l])
    # vector = Activation(activation='relu')(vector)


    # The 0-th layer is the concatenation of embedding layers 3#
    vector_r = Multiply()([dmf1_user_latent, dmf1_item_latent])
    vector_l = Concatenate()([dmf2_user_latent, dmf2_item_latent])

    # linear learning layer 3.1#
    # for idx in range(0, 2):
    #     layer = Dense(layers[idx], kernel_regularizer=l2(reg_layers[0]), activation='linear', name='pre-layer%d' % idx)
    #     vector_l = layer(vector_l)

    layer = Dense(layers[0] // 2, kernel_regularizer=l2(reg_layers[0]), activation='linear', name='pre-layer')
    vector_l = layer(vector_l)
    vector = Concatenate()([vector_r, vector_l])


    # The 0-th layer is the concatenation of embedding layers 4#
    # vector_r = Multiply()([user_latent, item_latent])
    # vector_l = Concatenate()([user_latent, item_latent])
    # vector = Concatenate()([vector_r, vector_l])


    # MLP layers (Dual-wise)
    for idx in range(1, num_layer-3):
        # Deprecate: W_W_regularizer
        '''layer = Dense(layers[idx], W_regularizer=l2(reg_layers[idx]), activation='relu', name='layer%d' % idx)'''
        layer = Dense(layers[idx], kernel_regularizer=l2(reg_layers[idx]), activation='relu', name='layer%d' % idx)
        vector = layer(vector)

    # Final prediction layer

    # Deprecate: init
    '''prediction = Dense(1, activation='sigmoid', init='lecun_uniform', name='prediction')(vector)'''
    prediction = Dense(1, activation='sigmoid', kernel_initializer='lecun_uniform', name='prediction')(vector)

    model = Model(inputs=[user_input, item_input],
                  outputs=prediction)

    return model
Exemple #39
0
def vgg16_model(trainFilePath,
                testFilePath,
                batch_size,
                epochs,
                name,
                lr,
                key_file,
                which_line,
                which_letter,
                key_length,
                test_size,
                load_weight=False,
                weight_path=None,
                evalONtest=True):
    input_tensor = Input(shape=(35000, 1))
    seq = input_tensor
    seq = SeparableConv1D(64, 3, padding='same', activation=None)(seq)
    seq = BatchNormalization(axis=1)(seq)
    seq = ReLU()(seq)
    seq = SeparableConv1D(64, 3, padding='same', activation=None)(seq)
    seq = BatchNormalization(axis=1)(seq)
    seq = ReLU()(seq)
    seq = MaxPooling1D(pool_size=2, strides=2)(seq)

    seq = SeparableConv1D(128, 3, padding='same', activation=None)(seq)
    seq = BatchNormalization(axis=1)(seq)
    seq = ReLU()(seq)
    seq = SeparableConv1D(128, 3, padding='same', activation=None)(seq)
    seq = BatchNormalization(axis=1)(seq)
    seq = ReLU()(seq)
    seq = MaxPooling1D(pool_size=2, strides=2)(seq)

    seq = SeparableConv1D(256, 3, padding='same', activation=None)(seq)
    seq = BatchNormalization(axis=1)(seq)
    seq = ReLU()(seq)
    seq = SeparableConv1D(256, 3, padding='same', activation=None)(seq)
    seq = BatchNormalization(axis=1)(seq)
    seq = ReLU()(seq)
    # seq = SeparableConv1D(256,3,padding='same',activation=None)(seq)
    seq = MaxPooling1D(pool_size=2, strides=2)(seq)

    seq = SeparableConv1D(256, 3, padding='same', activation=None)(seq)
    seq = BatchNormalization(axis=1)(seq)
    seq = ReLU()(seq)
    seq = SeparableConv1D(256, 3, padding='same', activation=None)(seq)
    seq = BatchNormalization(axis=1)(seq)
    seq = ReLU()(seq)
    # seq = SeparableConv1D(256,3,padding='same',activation=None)(seq)
    seq = MaxPooling1D(pool_size=2, strides=2)(seq)

    seq = SeparableConv1D(512, 3, padding='same', activation=None)(seq)
    seq = BatchNormalization(axis=1)(seq)
    seq = ReLU()(seq)
    seq = SeparableConv1D(512, 3, padding='same', activation=None)(seq)
    seq = BatchNormalization(axis=1)(seq)
    seq = ReLU()(seq)
    # seq = SeparableConv1D(512,3,padding='same',activation=None)(seq)
    seq = MaxPooling1D(pool_size=2, strides=2)(seq)

    seq = SeparableConv1D(512, 3, padding='same', activation=None)(seq)
    seq = BatchNormalization(axis=1)(seq)
    seq = ReLU()(seq)
    seq = SeparableConv1D(512, 3, padding='same', activation=None)(seq)
    seq = BatchNormalization(axis=1)(seq)
    seq = ReLU()(seq)
    # seq = SeparableConv1D(512,3,padding='same',activation='relu')(seq)
    seq = GlobalMaxPooling1D()(seq)

    # seq = Flatten()(seq)
    # seq = Dense(1000,activation='relu')(seq)

    seq = Dense(1000, activation='relu')(seq)

    output_tensor = Dense(16, activation='softmax')(seq)

    # model = Model(inputs=[input_tensor], outputs=[output_tensor])

    model = Model(inputs=[input_tensor], outputs=[output_tensor])
    # model = multi_gpu_model(model,gpus=4)
    model.summary()

    if load_weight == True:
        model.load_weights(weight_path, by_name=True)
    else:
        pass

    from keras.optimizers import Adam
    model.compile(
        loss='categorical_crossentropy',  # 交叉熵作为loss
        optimizer=Adam(lr),
        metrics=['accuracy'])

    if evalONtest == True:
        test_model = Model(inputs=[input_tensor], outputs=[output_tensor])

        test_model.compile(
            loss='categorical_crossentropy',  # 交叉熵作为loss
            optimizer=Adam(lr),
            metrics=['accuracy'])
        CSV_FILE_PATH2 = testFilePath
        data_to_test = load_data(CSV_FILE_PATH2)

        # train_x2, test_x2, train_y2, test_y2, Class_dict2
        # train_x2 = np.expand_dims(train_x2, axis=2)
        # test_x2 = np.expand_dims(test_x2, axis=2)

    else:
        pass

    print('开始加载数据')
    data_to_train = load_data(trainFilePath)
    # data_to_train = data_to_train[:10000]
    label_to_train = cut_letter(key_file, which_line, which_letter, key_length)
    label_to_train_lb = to_categorical(label_to_train, 16)

    train_x, test_x, train_y, test_y = train_test_split(data_to_train,
                                                        label_to_train_lb,
                                                        test_size=test_size,
                                                        shuffle=True)

    train_x = np.expand_dims(train_x, axis=2)
    test_x = np.expand_dims(test_x, axis=2)

    b_size = batch_size
    max_epochs = epochs
    print("Starting training ")

    learnratedecay = ReduceLROnPlateau(monitor='val_loss',
                                       factor=0.1,
                                       patience=8,
                                       verbose=1,
                                       mode='auto',
                                       epsilon=0.0001,
                                       cooldown=0,
                                       min_lr=0)
    os.makedirs('/data/wuchenxi/allmodel/new_simeck_model/' + name + '/model',
                exist_ok=True)
    os.makedirs('/data/wuchenxi/allmodel/new_simeck_model/' + name + '/csvlog',
                exist_ok=True)
    os.makedirs('/data/wuchenxi/allmodel/new_simeck_model/' + name +
                '/tensorboard',
                exist_ok=True)

    checkpointer = ModelCheckpoint(
        monitor='val_loss',
        filepath='/data/wuchenxi/allmodel/new_simeck_model/' + name +
        '/model/' + name + '.hdf5',
        verbose=1,
        save_best_only=True)
    picture_output = TensorBoard(
        log_dir='/data/wuchenxi/allmodel/new_simeck_model/' + name +
        '/tensorboard/' + name + '_log',
        histogram_freq=0,
        write_graph=True,
        write_grads=True,
        write_images=True,
    )
    csvlog = CSVLogger(filename='/data/wuchenxi/allmodel/new_simeck_model/' +
                       name + '/csvlog/' + name + '.csv',
                       separator=',',
                       append=False)

    if evalONtest == True:
        pass
        # callback = [checkpointer, picture_output, csvlog, learnratedecay,
        #             EvaluateInputTensor(test_model, train_x2, train_y2,
        #                                 '/data/wuchenxi/allmodel/simeck_key_model/'+name+'/csvlog/' + name + '_test.csv')]
    else:
        callback = [checkpointer, picture_output, csvlog, learnratedecay]
    h = model.fit(train_x,
                  train_y,
                  batch_size=b_size,
                  epochs=max_epochs,
                  validation_data=(test_x, test_y),
                  shuffle=True,
                  verbose=1,
                  callbacks=callback)
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
print(x_train.shape)
print(x_test.shape)

#     构造单隐藏层的自编码器( 784转32再转784)

encoding_dim = 32  # 32 floats -> compression of factor 24.5, assuming the input is 784 floats
# this is our input placeholder
input_img = Input(shape=(784,))
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim, activation='relu')(input_img)
# "decoded" is the lossy reconstruction of the input
decoded = Dense(784, activation='sigmoid')(encoded)
# this model maps an input to its reconstruction
autoencoder = Model(input=input_img, output=decoded)

# 定义编码器(784转32)
# this model maps an input to its encoded representation
encoder = Model(input=input_img, output=encoded)

# 定义解码器(32转784)
# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = Dense(784, activation='sigmoid')
# create the decoder model
decoder = Model(input=encoded_input, output=decoder_layer(encoded_input))

# 编译和训练自编码器
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
Exemple #41
0
def SqueezeUNet(inputs,
                num_classes=None,
                deconv_ksize=3,
                dropout=0.5,
                activation='sigmoid'):
    """SqueezeUNet is a implementation based in SqueezeNetv1.1 and unet for semantic segmentation
    :param inputs: input layer.
    :param num_classes: number of classes.
    :param deconv_ksize: (width and height) or integer of the 2D deconvolution window.
    :param dropout: dropout rate
    :param activation: type of activation at the top layer.
    :returns: SqueezeUNet model
    """
    channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
    if num_classes is None:
        num_classes = K.int_shape(inputs)[channel_axis]

    x01 = Conv2D(64, (3, 3),
                 strides=(2, 2),
                 padding='same',
                 activation='relu',
                 name='conv1')(inputs)
    x02 = MaxPooling2D(pool_size=(3, 3),
                       strides=(2, 2),
                       name='pool1',
                       padding='same')(x01)

    x03 = fire_module(x02, fire_id=2, squeeze=16, expand_1x1=64, expand_3x3=64)
    x04 = fire_module(x03, fire_id=3, squeeze=16, expand_1x1=64, expand_3x3=64)
    x05 = MaxPooling2D(pool_size=(3, 3),
                       strides=(2, 2),
                       name='pool3',
                       padding="same")(x04)

    x06 = fire_module(x05,
                      fire_id=4,
                      squeeze=32,
                      expand_1x1=128,
                      expand_3x3=128)
    x07 = fire_module(x06,
                      fire_id=5,
                      squeeze=32,
                      expand_1x1=128,
                      expand_3x3=128)
    x08 = MaxPooling2D(pool_size=(3, 3),
                       strides=(2, 2),
                       name='pool5',
                       padding="same")(x07)

    x09 = fire_module(x08,
                      fire_id=6,
                      squeeze=48,
                      expand_1x1=192,
                      expand_3x3=192)
    x10 = fire_module(x09,
                      fire_id=7,
                      squeeze=48,
                      expand_1x1=192,
                      expand_3x3=192)
    x11 = fire_module(x10,
                      fire_id=8,
                      squeeze=64,
                      expand_1x1=256,
                      expand_3x3=256)
    x12 = fire_module(x11,
                      fire_id=9,
                      squeeze=64,
                      expand_1x1=256,
                      expand_3x3=256)

    if dropout != 0.0:
        x12 = Dropout(dropout)(x12)

    up1 = concatenate([
        Conv2DTranspose(192, deconv_ksize, strides=(1, 1),
                        padding='same')(x12),
        x10,
    ],
                      axis=channel_axis)
    up1 = fire_module(up1, fire_id=10, squeeze=48, expand=192)

    up2 = concatenate([
        Conv2DTranspose(128, deconv_ksize, strides=(1, 1),
                        padding='same')(up1),
        x08,
    ],
                      axis=channel_axis)
    up2 = fire_module(up2, fire_id=11, squeeze=32, expand=128)

    up3 = concatenate([
        Conv2DTranspose(64, deconv_ksize, strides=(2, 2), padding='same')(up2),
        x05,
    ],
                      axis=channel_axis)
    up3 = fire_module(up3, fire_id=12, squeeze=16, expand=64)

    up4 = concatenate([
        Conv2DTranspose(32, deconv_ksize, strides=(2, 2), padding='same')(up3),
        x02,
    ],
                      axis=channel_axis)
    up4 = fire_module(up4, fire_id=13, squeeze=16, expand=32)
    up4 = UpSampling2D(size=(2, 2))(up4)

    x = concatenate([up4, x01], axis=channel_axis)
    x = Conv2D(64, (3, 3), strides=(1, 1), padding='same',
               activation='relu')(x)
    x = UpSampling2D(size=(2, 2))(x)
    x = Conv2D(num_classes, (1, 1), activation=activation)(x)

    return Model(inputs=inputs, outputs=x)
class Model():
    def __init__(self, max_len=30, max_words=10000, batch_size=128,
                 loss='binary_crossentropy', optimizer='adam', n_epochs=10):
        self.max_len = max_len
        self.max_words = max_words
        self.batch_size = batch_size
        self.loss = loss
        self.optimizer = optimizer
        self.n_epochs = n_epochs
        self.model = Model()

    def build_lstm(self, word_index, emb_mat, d_rate=0.3):
        """ Builds an LSTM model

        Parameters
        ----------
        word_index : numpy array
            an array of index for all words
        emb_mat : numpy matrix
            a matrix of embedding values
        d_rate : float
            a float number indicating the dropout ratio.
        """
        lstm_input = layers.Input((70, ))
        lstm_layer = layers.Embedding(len(word_index) + 1, 300,
                                      weights=[emb_mat],
                                      trainable=False)(lstm_input)
        lstm_layer = layers.SpatialDropout1D(0.3)(lstm_layer)
        lstm_layer = SpatialDropout1D(d_rate)(lstm_layer)
        lstm_layer = Bidirectional(LSTM(128, return_sequences=True))(lstm_layer)
        lstm_layer = Bidirectional(LSTM(128, return_sequences=True))(lstm_layer)
        hidden = concatenate([
            GlobalMaxPooling1D()(lstm_layer),
            GlobalAveragePooling1D()(lstm_layer),
        ])
        hidden = add([hidden, Dense(512, activation='relu')(hidden)])
        hidden = add([hidden, Dense(512, activation='relu')(hidden)])
        lstm_output = Dense(1, activation='sigmoid')(hidden)

        self.model = Model(lstm_input, lstm_output)

    def build_cnn(self, word_index, emb_mat):
        """ Builds a CNN model

        Parameters
        ----------
        word_index : numpy array
            an array of index for all words
        emb_mat : numpy matrix
            a matrix of embedding values
        """
        cnn_input = layers.Input((70, ))
        emb_layer = layers.Embedding(len(word_index) + 1, 300,
                                     weights=[emb_mat],
                                     trainable=False)(cnn_input)
        emb_layer = layers.SpatialDropout1D(0.3)(emb_layer)
        conv_layer = layers.Convolution1D(100, 3, activation="relu")(emb_layer)
        pooling_layer = layers.GlobalMaxPool1D()(conv_layer)
        output_layer1 = layers.Dense(50, activation="relu")(pooling_layer)
        output_layer1 = layers.Dropout(0.25)(output_layer1)
        cnn_output = layers.Dense(1, activation="sigmoid")(output_layer1)
        self.model = Model(inputs=cnn_input, outputs=cnn_output)

    def train(self, X_train, y_train, class_weight={0: 1.0, 1: 1.0},
                    metrics=[fmeasure]):
        """ trains the model

        Parameters
        ----------
        X_train : numpy matrix
            a matrix of input text in the sequence format
        y_train : numpy array
            an array of input labels
        """
        self.model.compile(loss=self.loss, optimizer=optimizer)
        self.model.fit(X_train, y_train, class_weight=class_weight)

    def predict(self, X_test, test_df, submission_file):
        """ predicts labels for unseen data and stores them in a
        file for submission

        Parameters
        ----------
        X_test : numpy matrix
            a matrix of test text in the sequence format
        test_df : pandas dataframe
            the test test in the format of dataframe
        submission_file : string
            the path to the output dataframe ready to be submitted
        """
        predict = self.model.predict(X_test)
        test_df['prediction'] = predict
        test_df = test_df[['id', 'prediction']]
        test_df.to_csv(submission_file, index=False)

    def plot(self, outfile):
        """ plots the model

        Parameters
        ----------
        outfile : string
            the path to the output png file
        """
        plot_model(self.model, to_file=outfile, show_shapes=True)
def small_POD_body(inputs, num_anchors, num_classes):
    podnet = Model(inputs, small_PODnet_body()(inputs))
    x = DarknetConv2D_BN_Leaky(num_anchors * (num_classes + 5),
                               (1, 1))(podnet.output)
    return Model(inputs, x)
    # Be careful, the returned output should be a batch of sequences.
    X = LSTM(128,return_sequences=True)(embeddings)
    # Add dropout with a probability of 0.5
    X = Dropout(0.5)(X)
    # Propagate X trough another LSTM layer with 128-dimensional hidden state
    # Be careful, the returned output should be a single hidden state, not a batch of sequences.
    X = LSTM(128,return_sequences=False)(X)
    # Add dropout with a probability of 0.5
    X = Dropout(0.5)(X)
    # Propagate X through a Dense layer with softmax activation to get back a batch of 5-dimensional vectors.
    X = Dense(5)(X)
    # Add a softmax activation
    X = Activation('softmax')(X)
    
    # Create Model instance which converts sentence_indices into X.
    model = Model(inputs = sentence_indices,outputs = X)
    
    ### END CODE HERE ###
    
    return model


# Run the following cell to create your model and check its summary. Because all sentences in the dataset are less than 10 words, we chose `max_len = 10`.  You should see your architecture, it uses "20,223,927" parameters, of which 20,000,050 (the word embeddings) are non-trainable, and the remaining 223,877 are. Because our vocabulary size has 400,001 words (with valid indices from 0 to 400,000) there are 400,001\*50 = 20,000,050 non-trainable parameters. 

# In[53]:

model = Emojify_V2((maxLen,), word_to_vec_map, word_to_index)
model.summary()


# As usual, after creating your model in Keras, you need to compile it and define what loss, optimizer and metrics your are want to use. Compile your model using `categorical_crossentropy` loss, `adam` optimizer and `['accuracy']` metrics:
print ('Successfully Added', file=fh)

np_epochs = 5
batch_size = 32
input_shape = (200,200,3) 

inception = InceptionV3(include_top = False, weights='imagenet', input_shape=input_shape, pooling='max')
for layer in inception.layers:
    layer.trainable = False
    
output = inception.output
output = Dense(50, activation = 'relu')(output)
output = Dense(10, activation = 'relu')(output)
output = Dense(1, activation='linear')(output)

model = Model(inputs = inception.input, outputs=output)

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

model_yaml = model.to_yaml()
with open('inception_model.yaml', 'w') as yaml_file:
    yaml_file.write(model_yaml)
print ('model.yaml saved to disk')

filepath="weights_inception.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
train_samples = X_train.shape[0]
val_samples = X_val.shape[0]
test_samples = X_test.shape[0]
def yolo3_body(inputs, num_anchors, num_classes):
    base_yolo3 = Model(inputs, darknet53_body(inputs))
    x = DarknetConv2D_BN_Leaky(num_anchors * (num_classes + 5),
                               (1, 1))(base_yolo3.output)
    yolo3 = Model(inputs, x)
    yolo3.summary()
Exemple #47
0
 def make_nested_func_model(input_shape, layer, level=1):
     input = Input(input_shape)
     model = layer
     for i in range(level):
         model = Model(input, model(input))
     return model
    output23,output24,output25,output26,output27,output28,output29,output30]
concat_output = Concatenate(axis = -1)(my_outputs)



predictions = Flatten()(concat_output)
predictions = Dense(300, activation='relu')(predictions)
predictions = Dense(200, activation='relu')(predictions)
predictions = Dense(100, activation='relu')(predictions)

predictions = Dense(2, activation='softmax')(predictions)

from keras.optimizers import SGD
opt = SGD(lr=0.001)

model = Model(inputs=my_inputs, outputs=predictions)
model.compile(optimizer=opt,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

print(model.summary())
# In[23]:


from glob import glob
from random import shuffle

pat_list0 = glob('C:/Users/korea/Desktop/sungmo_RESNET/data/30image/0/*')
pat_list1 = glob('C:/Users/korea/Desktop/sungmo_RESNET/data/30image/1/*')

from pprint import pprint
 def regression_model(input_shapes):
     inputs, x = TrainData_image.base_model(input_shapes)
     predictions = Dense(2, activation='linear', init='normal')(x)
     return Model(inputs=inputs, outputs=predictions)
Exemple #50
0
class VAE(object):
    def __init__(self, original_img_size, latent_dim, intermediate_dim,
                 epsilon_std, epochs, batch_size):
        self.original_img_size = original_img_size
        self.latent_dim = latent_dim
        self.intermediate_dim = intermediate_dim
        self.epsilon_std = epsilon_std
        self.epochs = epochs
        self.batch_size = batch_size

        x, z_mean, z_log_var = self.build_encoder()

        def sampling(args):
            z_mean, z_log_var = args
            epsilon = K.random_normal(shape=(batch_size, latent_dim),
                                      mean=0.,
                                      stddev=epsilon_std)
            return z_mean + K.exp(z_log_var) * epsilon

        def vae_loss(x, x_decoded_mean_squash):
            x = K.flatten(x)
            x_decoded_mean_squash = K.flatten(x_decoded_mean_squash)
            xent_loss = img_rows * img_cols * metrics.binary_crossentropy(
                x, x_decoded_mean_squash)
            kl_loss = -0.5 * K.mean(
                1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
            return K.mean(xent_loss + kl_loss)

        # note that "output_shape" isn't necessary with the TensorFlow backend
        # so you could write `Lambda(sampling)([z_mean, z_log_var])`
        self.z = Lambda(sampling,
                        output_shape=(self.latent_dim, ))([z_mean, z_log_var])

        self.generator, x_decoded_mean_squash = self.build_decoder()

        # build a model to project inputs on the latent space
        self.encoder = Model(x, z_mean)  # where is z_var here?

        self.vae = Model(x, x_decoded_mean_squash)
        self.vae.compile(optimizer='rmsprop', loss=vae_loss)

    def build_encoder(self):
        x = Input(batch_shape=(batch_size, ) + original_img_size)
        conv_1 = Conv2D(img_chns,
                        kernel_size=(2, 2),
                        padding='same',
                        activation='relu')(x)
        conv_2 = Conv2D(filters,
                        kernel_size=(2, 2),
                        padding='same',
                        activation='relu',
                        strides=(2, 2))(conv_1)
        conv_3 = Conv2D(filters,
                        kernel_size=num_conv,
                        padding='same',
                        activation='relu',
                        strides=1)(conv_2)
        conv_4 = Conv2D(filters,
                        kernel_size=num_conv,
                        padding='same',
                        activation='relu',
                        strides=1)(conv_3)
        flat = Flatten()(conv_4)

        hidden = Dense(self.intermediate_dim, activation='relu')(flat)

        z_mean = Dense(self.latent_dim)(hidden)
        z_log_var = Dense(self.latent_dim)(hidden)

        return x, z_mean, z_log_var

    def build_decoder(self):
        # we instantiate these layers separately so as to reuse them later

        decoder_hid = Dense(intermediate_dim, activation='relu')
        decoder_upsample = Dense(filters * 14 * 14, activation='relu')

        if K.image_data_format() == 'channels_first':
            output_shape = (batch_size, filters, 14, 14)
        else:
            output_shape = (batch_size, 14, 14, filters)

        decoder_reshape = Reshape(output_shape[1:])
        decoder_deconv_1 = Conv2DTranspose(filters,
                                           kernel_size=num_conv,
                                           padding='same',
                                           strides=1,
                                           activation='relu')
        decoder_deconv_2 = Conv2DTranspose(filters,
                                           num_conv,
                                           padding='same',
                                           strides=1,
                                           activation='relu')
        if K.image_data_format() == 'channels_first':
            output_shape = (batch_size, filters, 29, 29)
        else:
            output_shape = (batch_size, 29, 29, filters)
        decoder_deconv_3_upsamp = Conv2DTranspose(filters,
                                                  kernel_size=(3, 3),
                                                  strides=(2, 2),
                                                  padding='valid',
                                                  activation='relu')
        decoder_mean_squash = Conv2D(img_chns,
                                     kernel_size=2,
                                     padding='valid',
                                     activation='sigmoid')

        hid_decoded = decoder_hid(self.z)
        up_decoded = decoder_upsample(hid_decoded)
        reshape_decoded = decoder_reshape(up_decoded)
        deconv_1_decoded = decoder_deconv_1(reshape_decoded)
        deconv_2_decoded = decoder_deconv_2(deconv_1_decoded)
        x_decoded_relu = decoder_deconv_3_upsamp(deconv_2_decoded)
        x_decoded_mean_squash = decoder_mean_squash(x_decoded_relu)

        # build a digit generator that can sample from the learned distribution
        decoder_input = Input(shape=(self.latent_dim, ))
        _hid_decoded = decoder_hid(decoder_input)
        _up_decoded = decoder_upsample(_hid_decoded)
        _reshape_decoded = decoder_reshape(_up_decoded)
        _deconv_1_decoded = decoder_deconv_1(_reshape_decoded)
        _deconv_2_decoded = decoder_deconv_2(_deconv_1_decoded)
        _x_decoded_relu = decoder_deconv_3_upsamp(_deconv_2_decoded)
        _x_decoded_mean_squash = decoder_mean_squash(_x_decoded_relu)
        generator = Model(decoder_input, _x_decoded_mean_squash)

        return generator, x_decoded_mean_squash

    def train(self, x_train, x_test, y_test):
        self.vae.fit(x_train,
                     x_train,
                     shuffle=True,
                     epochs=self.epochs,
                     batch_size=self.batch_size,
                     validation_data=(x_test, x_test))

    def save(self, output_dir):
        self.vae.save_weights(os.path.join(output_dir, 'vae_model.h5'))
        self.encoder.save_weights(os.path.join(output_dir, 'encoder_model.h5'))
        self.generator.save_weights(
            os.path.join(output_dir, 'generator_model.h5'))

    def load(self, output_dir):
        vae_filename = os.path.join(output_dir, 'vae_model.h5')
        encoder_filename = os.path.join(output_dir, 'encoder_model.h5')
        generator_filename = os.path.join(output_dir, 'generator_model.h5')

        self.encoder.load_weights(encoder_filename)
        self.vae.load_weights(vae_filename)
        self.generator.load_weights(generator_filename)
                                            categorical_features,
                                            categorical_names,
                                            class_names,
                                            epochs=1,
                                            batch_size=128,
)

model = nn_with_embedding.model
with open('logs/nn_with_embedding_model_architecture.json', 'r') as f:
    model = model_from_json(f.read())
model.load_weights('logs/nn_with_embedding_model_weights.h5')

layer_name = 'fully-connected'

previous_layer_name = 'embedding'
intermediate_layer_model = KerasModel(inputs=model.input,
                                      outputs=model.get_layer(previous_layer_name).output)
intermediate_output = intermediate_layer_model.predict(preprocessing(X_test))
layer_names = [layer.name for layer in model.layers]
layer_idx = layer_names.index(layer_name)
# get the input shape of the desired layer
input_shape = model.layers[layer_idx].get_input_shape_at(0)
new_input = Input(shape=(input_shape[1],))
# create the new nodes for each layer in the path
x = new_input
for layer in model.layers[layer_idx:]:
    x = layer(x)

new_model = KerasModel(new_input, x)
a = new_model.predict(intermediate_output)
 def classification_model(input_shapes, nclasses):
     inputs, x = TrainData_image.base_model(input_shapes)
     predictions = Dense(nclasses,
                         activation='softmax',
                         kernel_initializer='lecun_uniform')(x)
     return Model(inputs=inputs, outputs=predictions)
Exemple #53
0
class GANUnetNoGapFillingModel():

    def __init__(self):

        K.set_image_data_format('channels_last')  # set format
        K.set_image_dim_ordering('tf')
        self.DEBUG = 1

        # Input shape
        self.img_rows = 192
        self.img_cols = 192
        self.img_vols = 192
        self.channels = 1
        self.batch_sz = 1  # for testing locally to avoid memory allocation

        self.crop_size = (self.img_rows, self.img_cols, self.img_vols)

        self.img_shape = self.crop_size + (self.channels,)

        self.output_size = 192
        self.output_shape_g = self.crop_size + (3,)  # phi has three outputs. one for each X, Y, and Z dimensions
        self.input_shape_d = self.crop_size + (1,)

        # Calculate output shape of D
        patch = int(self.output_size / 2 ** 4)
        self.output_shape_d = (patch, patch, patch,  self.channels)

        self.batch_sz = 1 # for testing locally to avoid memory allocation

        # Number of filters in the first layer of G and D
        self.gf = 32
        self.df = 32

        # Train the discriminator faster than the generator
        #optimizerD = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True) # in the paper the learning rate is 0.001 and weight decay is 0.5
        optimizerD = Adam(0.001, decay=0.05)  # in the paper the decay after 50K iterations by 0.5
        self.decay = 0.5
        self.iterations_decay = 50
        self.learning_rate = 0.001
        #optimizerG = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True) # in the paper the decay after 50K iterations by 0.5
        optimizerG = Adam(0.001, decay=0.05)  # in the paper the decay after 50K iterations by 0.5

        # Build the discriminator
        self.discriminator = self.build_discriminator()
        self.discriminator.summary()
        self.discriminator.compile(loss='binary_crossentropy',
                                    optimizer=optimizerD,
                                    metrics=['accuracy'])

        # Build the generator
        self.generator = self.build_generator()
        self.generator.summary()
        # Build the deformable transformation layer
        self.transformation = self.build_transformation()
        self.transformation.summary()

        # Input images and their conditioning images
        img_S = Input(shape=self.img_shape)
        img_T = Input(shape=self.img_shape)

        # By conditioning on T generate a warped transformation function of S
        phi = self.generator([img_S, img_T])

        # Transform S
        warped_S = self.transformation([img_S, phi])

        # Use Python partial to provide loss function with additional deformable field argument
        partial_gp_loss = partial(self.gradient_penalty_loss, phi=phi)
        partial_gp_loss.__name__ = 'gradient_penalty' # Keras requires function names

        # For the combined model we will only train the generator
        self.discriminator.trainable = False

        # Discriminators determines validity of translated images / condition pairs
        validity = self.discriminator([warped_S, img_T])

        self.combined = Model(inputs=[img_S, img_T], outputs=validity)
        self.combined.summary()
        self.combined.compile(loss = partial_gp_loss, optimizer=optimizerG)

        if self.DEBUG:
            log_path = '/nrs/scicompsoft/elmalakis/GAN_Registration_Data/flydata/forSalma/lo_res/logs_ganunet_nogap/'
            os.makedirs(log_path, exist_ok=True)
            self.callback = TensorBoard(log_path)
            self.callback.set_model(self.combined)

        self.data_loader = DataLoader(batch_sz=self.batch_sz,
                                      crop_size=self.crop_size,
                                      dataset_name='fly',
                                      min_max=False,
                                      restricted_mask=False,
                                      use_hist_equilized_data=False,
                                      use_golden=False)

    """
    Generator Network
    """
    def build_generator(self):
        """U-Net Generator"""
        def conv3d(input_tensor,
                        n_filters,
                        kernel_size=(3, 3, 3),
                        batch_normalization=True,
                        scale=True,
                        padding='valid',
                        use_bias=False,
                        name=''):
            """
            3D convolutional layer (+ batch normalization) followed by ReLu activation
            """
            layer = Conv3D(filters=n_filters,
                           kernel_size=kernel_size,
                           padding=padding,
                           use_bias=use_bias,
                           name=name + '_conv3d')(input_tensor)
            if batch_normalization:
                layer = BatchNormalization(momentum=0.8, name=name+'_bn', scale=scale)(layer)
            #layer = LeakyReLU(alpha=0.2, name=name + '_actleakyrelu')(layer)
            layer = Activation("relu")(layer)
            return layer


        def deconv3d(input_tensor,
                        n_filters,
                        kernel_size=(3, 3, 3),
                        batch_normalization=True,
                        scale=True,
                        padding='valid',
                        use_bias=False,
                        name=''):
            """
            3D deconvolutional layer (+ batch normalization) followed by ReLu activation
            """
            layer = UpSampling3D(size=2)(input_tensor)
            layer = Conv3D(filters=n_filters,
                           kernel_size=kernel_size,
                           padding=padding,
                           use_bias=use_bias,
                           name=name + '_conv3d')(layer)

            if batch_normalization:
                layer = BatchNormalization(momentum=0.8, name=name+'_bn', scale=scale)(layer)
            #layer = LeakyReLU(alpha=0.2, name=name + '_actleakyrelu')(layer)
            layer = Activation("relu")(layer)
            return layer

        img_S = Input(shape=self.img_shape, name='input_img_S')
        img_T = Input(shape=self.img_shape, name='input_img_T')

        combined_imgs = Add(name='combine_imgs_g')([img_S,img_T])

        # downsampling
        down1 = conv3d(input_tensor=combined_imgs, n_filters=self.gf, padding='same', name='down1_1')  # 192
        down1 = conv3d(input_tensor=down1, n_filters=self.gf, padding='same', name='down1_2')          # 192
        pool1 = MaxPooling3D(pool_size=(2, 2, 2), name='pool1')(down1)                                 # 96

        down2 = conv3d(input_tensor=pool1, n_filters=2 * self.gf, padding='same', name='down2_1')      # 96
        down2 = conv3d(input_tensor=down2, n_filters=2 * self.gf, padding='same', name='down2_2')      # 96
        pool2 = MaxPooling3D(pool_size=(2, 2, 2), name='pool2')(down2)                                 # 48

        down3 = conv3d(input_tensor=pool2, n_filters=4 * self.gf, padding='same', name='down3_1')      # 48
        down3 = conv3d(input_tensor=down3, n_filters=4 * self.gf, padding='same', name='down3_2')      # 48
        pool3 = MaxPooling3D(pool_size=(2, 2, 2), name='pool3')(down3)                                 #24

        down4 = conv3d(input_tensor=pool3, n_filters=8 * self.gf, padding='same', name='down4_1')      # 24
        down4 = conv3d(input_tensor=down4, n_filters=8 * self.gf, padding='same', name='down4_2')      # 24
        pool4 = MaxPooling3D(pool_size=(2, 2, 2), name='pool4')(down4)                                 # 12

        down5 = conv3d(input_tensor=pool4, n_filters=8 * self.gf, padding='same', name='down5_1')      # 12
        down5 = conv3d(input_tensor=down5, n_filters=8 * self.gf, padding='same', name='down5_2')      # 12
        pool5 = MaxPooling3D(pool_size=(2, 2, 2), name='pool5')(down5)                                  # 6

        center = conv3d(input_tensor=pool5, n_filters=16 * self.gf, padding='same', name='center1')     # 6
        center = conv3d(input_tensor=center, n_filters=16 * self.gf, padding='same', name='center2')    # 6

        # upsampling
        up5 = deconv3d(input_tensor=center, n_filters = 8*self.gf, padding='same', name='up5')          # 12
        up5 = concatenate([up5,down5])                                                                  # 12
        up5 = conv3d(input_tensor=up5, n_filters=8 * self.gf, padding='same', name='up5_1')             # 12
        up5 = conv3d(input_tensor=up5, n_filters=8 * self.gf, padding='same', name='up5_2')             # 12

        up4 = deconv3d(input_tensor=up5, n_filters=8 * self.gf, padding='same', name='up4')             #24
        up4 = concatenate([up4, down4])                                                                 # 24
        up4 = conv3d(input_tensor=up4, n_filters=8 * self.gf, padding='same', name='up4_1')             # 24
        up4 = conv3d(input_tensor=up4, n_filters=8 * self.gf, padding='same', name='up4_2')             # 24

        up3 = deconv3d(input_tensor=up4, n_filters=4 * self.gf, padding='same', name='up3')             #48
        up3 = concatenate([up3, down3])                                                                 # 48
        up3 = conv3d(input_tensor=up3, n_filters=4 * self.gf, padding='same', name='up3_1')            # 48
        up3 = conv3d(input_tensor=up3, n_filters=4 * self.gf, padding='same', name='up3_2')            # 48

        up2 = deconv3d(input_tensor=up3, n_filters=2 * self.gf, padding='same', name='up2')             # 96
        up2 = concatenate([up2, down2])                                                                # 96
        up2 = conv3d(input_tensor=up2, n_filters=2 * self.gf, padding='same', name='up2_1')            # 96
        up2 = conv3d(input_tensor=up2, n_filters=2 * self.gf, padding='same', name='up2_2')            # 96

        up1 = deconv3d(input_tensor=up2, n_filters=self.gf, padding='same', name='up1')                 # 192
        up1 = concatenate([up1, down1])                                                                 # 192
        up1 = conv3d(input_tensor=up1, n_filters=self.gf, padding='same', name='up1_1')                # 192
        up1 = conv3d(input_tensor=up1, n_filters=self.gf, padding='same', name='up1_2')                # 192

        phi = Conv3D(filters=3, kernel_size=(1, 1, 1), strides=1, use_bias=False, padding='same', name='phi')(up1)                 #192

        model = Model([img_S, img_T], outputs=phi, name='generator_model')

        return model

    """
    Discriminator Network
    """
    def build_discriminator(self):

        def d_layer(layer_input, filters, f_size=4, bn=True):
            """Discriminator layer"""
            d = Conv3D(filters, kernel_size=f_size, strides=2, padding='same')(layer_input)
            if bn:
                d = BatchNormalization(momentum=0.8)(d)
            #d = LeakyReLU(alpha=0.2)(d)
            d = Activation("relu")(d)
            return d

        img_S = Input(shape=self.img_shape) #192 S
        img_T = Input(shape=self.img_shape) #192 T

        combined_imgs = Add()([img_S, img_T])

        d1 = d_layer(combined_imgs, self.df, bn=False)
        d2 = d_layer(d1, self.df*2)
        d3 = d_layer(d2, self.df*4)
        d4 = d_layer(d3, self.df*8)

        validity = Conv3D(1, kernel_size=4, strides=1, padding='same', activation='sigmoid', name='disc_sig')(d4)

        return Model([img_S, img_T], validity, name='discriminator_model')

    """
    Transformation Network
    """
    def build_transformation(self):
        img_S = Input(shape=self.img_shape, name='input_img_S_transform')      # 192
        phi = Input(shape=self.output_shape_g, name='input_phi_transform')     # 192

        warped_S = Lambda(dense_image_warp_3D, output_shape=self.input_shape_d)([img_S, phi])

        return Model([img_S, phi], warped_S,  name='transformation_layer')


    """
    Define losses
    """
    def gradient_penalty_loss(self, y_true, y_pred, phi):
        """
        Computes gradient penalty on phi to ensure smoothness
        """
        lr = K.mean(K.binary_crossentropy(y_true, y_pred), axis=-1)
        # compute the numerical gradient of phi
        gradients = numerical_gradient_3D(phi)
        # #if self.DEBUG: gradients = K.print_tensor(gradients, message='gradients are:')
        #
        # compute the euclidean norm by squaring ...
        gradients_sqr = K.square(gradients)
        #   ... summing over the rows ...
        gradients_sqr_sum = K.sum(gradients_sqr, axis=np.arange(1, len(gradients_sqr.shape)))
        # #   ... and sqrt
        gradient_l2_norm = K.sqrt(gradients_sqr_sum)
        # # compute lambda * (1 - ||grad||)^2 still for each single sample
        # #gradient_penalty = K.square(1 - gradient_l2_norm)
        # # return the mean as loss over all the batch samples
        return K.mean(gradient_l2_norm) + lr
        #return gradients_sqr_sum + lr


    """
    Training
    """
    def train(self, epochs, batch_size=1, sample_interval=50):

        # Adversarial loss ground truths
        # hard labels
        valid = np.ones((self.batch_sz,) + self.output_shape_d)
        fake = np.zeros((self.batch_sz,) + self.output_shape_d)

        start_time = datetime.datetime.now()
        for epoch in range(epochs):
            for batch_i, (batch_img, batch_img_template, batch_img_golden) in enumerate(self.data_loader.load_batch()):
                # ---------------------
                #  Train Discriminator
                # ---------------------
                # Condition on B and generate a translate
                phi = self.generator.predict([batch_img, batch_img_template])
                transform = self.transformation.predict([batch_img, phi])  # 256x256x256
                # Create a ref image by perturbing th subject image with the template image
                perturbation_factor_alpha = 0.1 if epoch > epochs / 2 else 0.2
                batch_ref = perturbation_factor_alpha * batch_img + (1 - perturbation_factor_alpha) * batch_img_template

                d_loss_real = self.discriminator.train_on_batch([batch_ref, batch_img_template], valid)
                d_loss_fake = self.discriminator.train_on_batch([transform, batch_img_template], fake)
                d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

                # ---------------------
                #  Train Generator
                # ---------------------
                g_loss = self.combined.train_on_batch([batch_img, batch_img_template], valid)

                elapsed_time = datetime.datetime.now() - start_time

                print(
                    "[Epoch %d/%d] [Batch %d/%d] [D loss average: %f, acc average: %3d%%, D loss fake:%f, acc: %3d%%, D loss real: %f, acc: %3d%%] [G loss: %f]  time: %s"
                    % (epoch, epochs,
                       batch_i, self.data_loader.n_batches,
                       d_loss[0], 100 * d_loss[1],
                       d_loss_fake[0], 100 * d_loss_fake[1],
                       d_loss_real[0], 100 * d_loss_real[1],
                       g_loss,
                       elapsed_time))

                if self.DEBUG:
                    self.write_log(self.callback, ['g_loss'], [g_loss], batch_i)
                    self.write_log(self.callback, ['d_loss'], [d_loss[0]], batch_i)

                # If at save interval => save generated image samples
                if batch_i % sample_interval == 0 and epoch != 0 and epoch % 5 == 0:
                    self.sample_images(epoch, batch_i)


    def write_log(self, callback, names, logs, batch_no):
        #https://github.com/eriklindernoren/Keras-GAN/issues/52
        for name, value in zip(names, logs):
            summary = tf.Summary()
            summary_value = summary.value.add()
            summary_value.simple_value = value
            summary_value.tag = name
            callback.writer.add_summary(summary, batch_no)
            callback.writer.flush()


    def sample_images(self, epoch, batch_i):
        path = '/nrs/scicompsoft/elmalakis/GAN_Registration_Data/flydata/forSalma/lo_res/'
        os.makedirs(path+'generated_unet_nogap/' , exist_ok=True)

        idx, imgs_S = self.data_loader.load_data(is_validation=True)
        imgs_T = self.data_loader.img_template

        predict_img = np.zeros(imgs_S.shape, dtype=imgs_S.dtype)
        predict_phi = np.zeros(imgs_S.shape + (3,), dtype=imgs_S.dtype)

        input_sz = self.crop_size
        output_sz = (self.output_size, self.output_size, self.output_size)
        step = (64, 64, 64)

        start_time = datetime.datetime.now()

        for row in range(0, imgs_S.shape[0] - input_sz[0], step[0]):
            for col in range(0, imgs_S.shape[1] - input_sz[1], step[1]):
                for vol in range(0, imgs_S.shape[2] - input_sz[2], step[2]):
                    patch_sub_img = np.zeros((1, input_sz[0], input_sz[1], input_sz[2], 1), dtype=imgs_S.dtype)
                    patch_templ_img = np.zeros((1, input_sz[0], input_sz[1], input_sz[2], 1), dtype=imgs_T.dtype)

                    patch_sub_img[0, :, :, :, 0] = imgs_S[row:row + input_sz[0],
                                                   col:col + input_sz[1],
                                                   vol:vol + input_sz[2]]
                    patch_templ_img[0, :, :, :, 0] = imgs_T[row:row + input_sz[0],
                                                     col:col + input_sz[1],
                                                     vol:vol + input_sz[2]]

                    patch_predict_phi = self.generator.predict([patch_sub_img, patch_templ_img])
                    patch_predict_warped = self.transformation.predict([patch_sub_img, patch_predict_phi])

                    predict_img[row:row + output_sz[0],
                                col:col + output_sz[1],
                                vol:vol + output_sz[2]] = patch_predict_warped[0, :, :, :, 0]
                    predict_phi[row :row  + output_sz[0],
                               col :col  + output_sz[1],
                               vol :vol  + output_sz[2],:] = patch_predict_phi[0, :, :, :, :]

        elapsed_time = datetime.datetime.now() - start_time
        print(" --- Prediction time: %s" % (elapsed_time))

        nrrd.write(path+"generated_unet_nogap/%d_%d_%d" % (epoch, batch_i, idx), predict_img)
        self.data_loader._write_nifti(path+"generated_unet_nogap/phi%d_%d_%d" % (epoch, batch_i, idx), predict_phi)

        if epoch%10 == 0:
            file_name = 'gan_network'+ str(epoch)
            # save the whole network
            self.combined.save(path+ 'generated_unet_nogap/'+ file_name + '.whole.h5', overwrite=True)
            print('Save the whole network to disk as a .whole.h5 file')
            model_jason = self.combined.to_json()
            with open(path+ 'generated_unet_nogap/'+file_name + '_arch.json', 'w') as json_file:
                json_file.write(model_jason)
                self.combined.save_weights(path+ 'generated_unet_nogap/'+file_name + '_weights.h5', overwrite=True)
            print('Save the network architecture in .json file and weights in .h5 file')
# binary 'classifier' Fully connected layer 
#model.add(Conv2D(filters=1, kernel_size=(8, 8), name='fcn', activation="sigmoid"))
 
 return model, 'trained'



trainSamples = aux.createSamples(xTrain, yTrain)
validationSamples = aux.createSamples(xVal, yVal)

sourceModel, modelName = train()

x = sourceModel.output
#x = Flatten()(x) #comment if using CNN
model = Model(inputs=sourceModel.input, outputs=x)

print(model.summary())

#loss is 'mse' or 'binary_crossentropy'

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


useFlips= True
batchSize = aux.promptForInt(message='Please specify the batch size (32, 64, etc.): ')
epochCount = aux.promptForInt(message='Please specify the number of epochs: ')

trainGen =aux. generator(samples=trainSamples,batchSize=batchSize) #,useFlips=useFlips)
validGen = aux.generator(samples=validationSamples,batchSize=batchSize)#, useFlips=useFlips)
Exemple #55
0
def get_unet(the_lr=1e-1, num_class=2, num_channel=10, size=2048 * 25):
    inputs = Input((size, num_channel))  # 2^11
    print(inputs.shape)

    num_blocks = 4  # 128*25 -> 200
    initial_filter = num_channel * 2  #15
    scale_filter = 2
    size_kernel = 7
    activation = 'relu'
    padding = 'same'

    layer_down = []
    layer_up = []

    conv0 = BatchNormalization()(Conv1D(initial_filter, size_kernel, \
        activation=activation, padding=padding)(inputs))
    conv0 = BatchNormalization()(Conv1D(initial_filter, size_kernel, \
        activation=activation, padding=padding)(conv0))

    layer_down.append(conv0)
    num = initial_filter
    for i in range(num_blocks):
        num = int(num * scale_filter)
        the_layer = pcc(layer_down[i],
                        num,
                        size_kernel,
                        activation=activation,
                        padding=padding)
        layer_down.append(the_layer)

    layer_up.append(the_layer)
    for i in range(num_blocks):
        num = int(num / scale_filter)
        the_layer = ucc(layer_up[i],
                        layer_down[-(i + 2)],
                        num,
                        size_kernel,
                        activation=activation,
                        padding=padding)
        layer_up.append(the_layer)

    #convn = Conv1D(1, 1, activation='sigmoid')(layer_up[-1])
    #convn = Conv1D(1, 1, activation='relu')(layer_up[-1])

    # output 25bp
    pool1 = MaxPooling1D(pool_size=5)(layer_up[-1])
    convn = BatchNormalization()(Conv1D(int(initial_filter * 5),
                                        size_kernel,
                                        activation=activation,
                                        padding=padding)(pool1))
    convn = BatchNormalization()(Conv1D(int(initial_filter * 5),
                                        size_kernel,
                                        activation=activation,
                                        padding=padding)(convn))

    pool2 = MaxPooling1D(pool_size=5)(convn)
    convn = BatchNormalization()(Conv1D(int(initial_filter * 5),
                                        size_kernel,
                                        activation=activation,
                                        padding=padding)(pool2))
    convn = BatchNormalization()(Conv1D(int(initial_filter * 5),
                                        size_kernel,
                                        activation=activation,
                                        padding=padding)(convn))

    convn = Conv1D(num_class, 1, activation='relu')(convn)

    model = Model(inputs=[inputs], outputs=[convn])

    #    model.compile(optimizer=Adam(lr=the_lr,beta_1=0.9, beta_2=0.999,decay=1e-5), loss=dice_coef_loss, metrics=[dice_coef])
    model.compile(optimizer=Adam(lr=the_lr,
                                 beta_1=0.9,
                                 beta_2=0.999,
                                 decay=1e-5),
                  loss='mean_squared_error',
                  metrics=[abse])
    #    model.compile(optimizer=Adam(lr=the_lr,beta_1=0.9, beta_2=0.999,decay=1e-5), loss=abse, metrics=[mse_loss])
    #    model.compile(optimizer=Adam(lr=the_lr,beta_1=0.9, beta_2=0.999,decay=1e-5), loss='mean_squared_error', metrics=[mse_loss])

    return model
Exemple #56
0
    def __init__(self):

        K.set_image_data_format('channels_last')  # set format
        K.set_image_dim_ordering('tf')
        self.DEBUG = 1

        # Input shape
        self.img_rows = 192
        self.img_cols = 192
        self.img_vols = 192
        self.channels = 1
        self.batch_sz = 1  # for testing locally to avoid memory allocation

        self.crop_size = (self.img_rows, self.img_cols, self.img_vols)

        self.img_shape = self.crop_size + (self.channels,)

        self.output_size = 192
        self.output_shape_g = self.crop_size + (3,)  # phi has three outputs. one for each X, Y, and Z dimensions
        self.input_shape_d = self.crop_size + (1,)

        # Calculate output shape of D
        patch = int(self.output_size / 2 ** 4)
        self.output_shape_d = (patch, patch, patch,  self.channels)

        self.batch_sz = 1 # for testing locally to avoid memory allocation

        # Number of filters in the first layer of G and D
        self.gf = 32
        self.df = 32

        # Train the discriminator faster than the generator
        #optimizerD = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True) # in the paper the learning rate is 0.001 and weight decay is 0.5
        optimizerD = Adam(0.001, decay=0.05)  # in the paper the decay after 50K iterations by 0.5
        self.decay = 0.5
        self.iterations_decay = 50
        self.learning_rate = 0.001
        #optimizerG = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True) # in the paper the decay after 50K iterations by 0.5
        optimizerG = Adam(0.001, decay=0.05)  # in the paper the decay after 50K iterations by 0.5

        # Build the discriminator
        self.discriminator = self.build_discriminator()
        self.discriminator.summary()
        self.discriminator.compile(loss='binary_crossentropy',
                                    optimizer=optimizerD,
                                    metrics=['accuracy'])

        # Build the generator
        self.generator = self.build_generator()
        self.generator.summary()
        # Build the deformable transformation layer
        self.transformation = self.build_transformation()
        self.transformation.summary()

        # Input images and their conditioning images
        img_S = Input(shape=self.img_shape)
        img_T = Input(shape=self.img_shape)

        # By conditioning on T generate a warped transformation function of S
        phi = self.generator([img_S, img_T])

        # Transform S
        warped_S = self.transformation([img_S, phi])

        # Use Python partial to provide loss function with additional deformable field argument
        partial_gp_loss = partial(self.gradient_penalty_loss, phi=phi)
        partial_gp_loss.__name__ = 'gradient_penalty' # Keras requires function names

        # For the combined model we will only train the generator
        self.discriminator.trainable = False

        # Discriminators determines validity of translated images / condition pairs
        validity = self.discriminator([warped_S, img_T])

        self.combined = Model(inputs=[img_S, img_T], outputs=validity)
        self.combined.summary()
        self.combined.compile(loss = partial_gp_loss, optimizer=optimizerG)

        if self.DEBUG:
            log_path = '/nrs/scicompsoft/elmalakis/GAN_Registration_Data/flydata/forSalma/lo_res/logs_ganunet_nogap/'
            os.makedirs(log_path, exist_ok=True)
            self.callback = TensorBoard(log_path)
            self.callback.set_model(self.combined)

        self.data_loader = DataLoader(batch_sz=self.batch_sz,
                                      crop_size=self.crop_size,
                                      dataset_name='fly',
                                      min_max=False,
                                      restricted_mask=False,
                                      use_hist_equilized_data=False,
                                      use_golden=False)
#x = GlobalAveragePooling2D()(x)
x = Flatten()(x)

x = concatenate([x, angle_layer])
# x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x)
#x = Dropout(0.2)(x)
#x = Dropout(0.25)(x)
#x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
#x = Dropout(0.25)(x)
predictions = Dense(1, activation='sigmoid')(x)

#model = Model(inputs=[input_tensor,input_2], outputs=predictions)
model = Model(inputs=[base_model.input, input_2], outputs=predictions)
#for layer in base_model.layers:
#    layer.trainable = False
for layer in model.layers[:15]:
    layer.trainable = False
for layer in model.layers[15:]:
    layer.trainable = True

from keras.optimizers import SGD
from keras.optimizers import Adam
from keras.optimizers import RMSprop
from keras.optimizers import Adagrad
from keras.optimizers import Adadelta
from keras.optimizers import Adamax
from keras.optimizers import Nadam
Exemple #58
0
    def build_generator(self):
        """U-Net Generator"""
        def conv3d(input_tensor,
                        n_filters,
                        kernel_size=(3, 3, 3),
                        batch_normalization=True,
                        scale=True,
                        padding='valid',
                        use_bias=False,
                        name=''):
            """
            3D convolutional layer (+ batch normalization) followed by ReLu activation
            """
            layer = Conv3D(filters=n_filters,
                           kernel_size=kernel_size,
                           padding=padding,
                           use_bias=use_bias,
                           name=name + '_conv3d')(input_tensor)
            if batch_normalization:
                layer = BatchNormalization(momentum=0.8, name=name+'_bn', scale=scale)(layer)
            #layer = LeakyReLU(alpha=0.2, name=name + '_actleakyrelu')(layer)
            layer = Activation("relu")(layer)
            return layer


        def deconv3d(input_tensor,
                        n_filters,
                        kernel_size=(3, 3, 3),
                        batch_normalization=True,
                        scale=True,
                        padding='valid',
                        use_bias=False,
                        name=''):
            """
            3D deconvolutional layer (+ batch normalization) followed by ReLu activation
            """
            layer = UpSampling3D(size=2)(input_tensor)
            layer = Conv3D(filters=n_filters,
                           kernel_size=kernel_size,
                           padding=padding,
                           use_bias=use_bias,
                           name=name + '_conv3d')(layer)

            if batch_normalization:
                layer = BatchNormalization(momentum=0.8, name=name+'_bn', scale=scale)(layer)
            #layer = LeakyReLU(alpha=0.2, name=name + '_actleakyrelu')(layer)
            layer = Activation("relu")(layer)
            return layer

        img_S = Input(shape=self.img_shape, name='input_img_S')
        img_T = Input(shape=self.img_shape, name='input_img_T')

        combined_imgs = Add(name='combine_imgs_g')([img_S,img_T])

        # downsampling
        down1 = conv3d(input_tensor=combined_imgs, n_filters=self.gf, padding='same', name='down1_1')  # 192
        down1 = conv3d(input_tensor=down1, n_filters=self.gf, padding='same', name='down1_2')          # 192
        pool1 = MaxPooling3D(pool_size=(2, 2, 2), name='pool1')(down1)                                 # 96

        down2 = conv3d(input_tensor=pool1, n_filters=2 * self.gf, padding='same', name='down2_1')      # 96
        down2 = conv3d(input_tensor=down2, n_filters=2 * self.gf, padding='same', name='down2_2')      # 96
        pool2 = MaxPooling3D(pool_size=(2, 2, 2), name='pool2')(down2)                                 # 48

        down3 = conv3d(input_tensor=pool2, n_filters=4 * self.gf, padding='same', name='down3_1')      # 48
        down3 = conv3d(input_tensor=down3, n_filters=4 * self.gf, padding='same', name='down3_2')      # 48
        pool3 = MaxPooling3D(pool_size=(2, 2, 2), name='pool3')(down3)                                 #24

        down4 = conv3d(input_tensor=pool3, n_filters=8 * self.gf, padding='same', name='down4_1')      # 24
        down4 = conv3d(input_tensor=down4, n_filters=8 * self.gf, padding='same', name='down4_2')      # 24
        pool4 = MaxPooling3D(pool_size=(2, 2, 2), name='pool4')(down4)                                 # 12

        down5 = conv3d(input_tensor=pool4, n_filters=8 * self.gf, padding='same', name='down5_1')      # 12
        down5 = conv3d(input_tensor=down5, n_filters=8 * self.gf, padding='same', name='down5_2')      # 12
        pool5 = MaxPooling3D(pool_size=(2, 2, 2), name='pool5')(down5)                                  # 6

        center = conv3d(input_tensor=pool5, n_filters=16 * self.gf, padding='same', name='center1')     # 6
        center = conv3d(input_tensor=center, n_filters=16 * self.gf, padding='same', name='center2')    # 6

        # upsampling
        up5 = deconv3d(input_tensor=center, n_filters = 8*self.gf, padding='same', name='up5')          # 12
        up5 = concatenate([up5,down5])                                                                  # 12
        up5 = conv3d(input_tensor=up5, n_filters=8 * self.gf, padding='same', name='up5_1')             # 12
        up5 = conv3d(input_tensor=up5, n_filters=8 * self.gf, padding='same', name='up5_2')             # 12

        up4 = deconv3d(input_tensor=up5, n_filters=8 * self.gf, padding='same', name='up4')             #24
        up4 = concatenate([up4, down4])                                                                 # 24
        up4 = conv3d(input_tensor=up4, n_filters=8 * self.gf, padding='same', name='up4_1')             # 24
        up4 = conv3d(input_tensor=up4, n_filters=8 * self.gf, padding='same', name='up4_2')             # 24

        up3 = deconv3d(input_tensor=up4, n_filters=4 * self.gf, padding='same', name='up3')             #48
        up3 = concatenate([up3, down3])                                                                 # 48
        up3 = conv3d(input_tensor=up3, n_filters=4 * self.gf, padding='same', name='up3_1')            # 48
        up3 = conv3d(input_tensor=up3, n_filters=4 * self.gf, padding='same', name='up3_2')            # 48

        up2 = deconv3d(input_tensor=up3, n_filters=2 * self.gf, padding='same', name='up2')             # 96
        up2 = concatenate([up2, down2])                                                                # 96
        up2 = conv3d(input_tensor=up2, n_filters=2 * self.gf, padding='same', name='up2_1')            # 96
        up2 = conv3d(input_tensor=up2, n_filters=2 * self.gf, padding='same', name='up2_2')            # 96

        up1 = deconv3d(input_tensor=up2, n_filters=self.gf, padding='same', name='up1')                 # 192
        up1 = concatenate([up1, down1])                                                                 # 192
        up1 = conv3d(input_tensor=up1, n_filters=self.gf, padding='same', name='up1_1')                # 192
        up1 = conv3d(input_tensor=up1, n_filters=self.gf, padding='same', name='up1_2')                # 192

        phi = Conv3D(filters=3, kernel_size=(1, 1, 1), strides=1, use_bias=False, padding='same', name='phi')(up1)                 #192

        model = Model([img_S, img_T], outputs=phi, name='generator_model')

        return model
Exemple #59
0
def unet(num_filters=16, factor=2, optimizer="adam", loss="binary_crossentropy", img_size=(None, None),
         num_channels=3, max_val=255., **kwargs):

    settings_str = "\tnum_filters: {num_filters}\n" \
                   "\tfactor: {factor}\n" \
                   "\toptimizer: {optimizer}\n" \
                   "\tloss: {loss}\n" \
                   "\timg_size: {img_size}\n" \
                   "\tnum_channels: {num_channels}\n" \
                   "\tmax_val: {max_val}\n".format(num_filters=num_filters, factor=factor, optimizer=optimizer,
                                                   loss=loss, img_size=img_size, num_channels=num_channels,
                                                   max_val=max_val)

    logging.info("Creating keras unet model with settings:\n" + settings_str)
    inputs = Input(tuple(img_size) + (num_channels,), name="input")

    s = BatchNormalization()(Conv2D(num_filters, (1, 1), padding='same')(Lambda(lambda x: (1./max_val) * x)(inputs)))
    c1 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(s)))
    c1 = Dropout(0.1)(c1)
    c1 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(c1)))
    p1 = MaxPooling2D((2, 2))(c1)
    
    num_filters *= factor   
    c2 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(p1)))
    c2 = Dropout(0.1)(c2)
    c2 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(c2)))
    p2 = MaxPooling2D((2, 2))(c2)

    num_filters *= factor
    c3 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(p2)))
    c3 = Dropout(0.2)(c3)
    c3 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(c3)))
    p3 = MaxPooling2D((2, 2))(c3)

    num_filters *= factor
    c4 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(p3)))
    c4 = Dropout(0.2)(c4)
    c4 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(c4)))
    p4 = MaxPooling2D(pool_size=(2, 2))(c4)

    num_filters *= factor
    c5 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(p4)))
    c5 = Dropout(0.3)(c5)
    c5 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(c5)))

    num_filters //= factor
    u6 = UpSampling2D(size=(2, 2))(c5)
    u6 = concatenate([u6, c4])
    c6 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(u6)))
    c6 = Dropout(0.2)(c6)
    c6 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(c6)))

    num_filters //= factor
    u7 = UpSampling2D(size=(2, 2))(c6)
    u7 = concatenate([u7, c3])
    c7 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(u7)))
    c7 = Dropout(0.2)(c7)
    c7 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(c7)))

    num_filters //= factor
    u8 = UpSampling2D(size=(2, 2))(c7)
    u8 = concatenate([u8, c2])
    c8 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(u8)))
    c8 = Dropout(0.1)(c8)
    c8 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(c8)))

    num_filters //= factor
    u9 = UpSampling2D(size=(2, 2))(c8)
    u9 = concatenate([u9, c1], axis=3)
    c9 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(u9)))
    c9 = Dropout(0.1)(c9)
    c9 = Activation('relu')(BatchNormalization()(Conv2D(num_filters, (3, 3), padding='same')(c9)))

    outputs = Conv2D(1, (1, 1), activation='sigmoid')(c9)

    model = Model(inputs=[inputs], outputs=[outputs])
    model.compile(optimizer=optimizer, loss=get_loss(loss), metrics=[model_utils.mean_iou])
    model.summary()
    return model
Exemple #60
0
    def train_sampled_softmax(self):

        ### Encoder model

        in_encoder = Input(shape=(self.encoder_length, ),
                           dtype='int32',
                           name='encoder_input')
        embed_encoder = Embedding(self.vocab_size,
                                  self.embedding_dim,
                                  input_length=self.encoder_length,
                                  name='embedding_encoder')
        in_enc_embedded = embed_encoder(in_encoder)

        fwd_encoder = GRU(self.birnn_dim,
                          return_state=True,
                          name='fwd_encoder')
        bwd_encoder = GRU(self.birnn_dim,
                          return_state=True,
                          name='bwd_encoder',
                          go_backwards=True)
        out_encoder_1, _eh1 = fwd_encoder(in_enc_embedded)
        out_encoder_2, _eh2 = bwd_encoder(in_enc_embedded)
        out_bidir_encoder = concatenate([out_encoder_1, out_encoder_2],
                                        axis=-1,
                                        name='bidir_encoder')

        encoder_model = Model(inputs=in_encoder, outputs=out_bidir_encoder)
        self.encoder_model = encoder_model

        ### Decoder model

        in_decoder = Input(shape=(None, ), name='decoder_input', dtype='int32')
        embed_decoder = Embedding(self.vocab_size,
                                  self.embedding_dim,
                                  name='embedding_decoder')
        in_dec_embedded = embed_decoder(in_decoder)

        labels = Input((self.decoder_length + 1, 1),
                       dtype='int32',
                       name='labels_')

        fwd_decoder = GRU(self.rnn_dim, return_state=True)

        sampling_softmax = SamplingLayer(self.num_samples,
                                         self.vocab_size,
                                         mode='train')

        s0 = Input(shape=(self.rnn_dim, ), name='s0')
        s = [s0]

        losses = []
        for t in range(self.decoder_length + 1):

            label_t = Lambda(lambda x: labels[:, t, :],
                             name='label-%s' % t)(labels)
            x_dec = Lambda(lambda x: in_dec_embedded[:, t, :],
                           name='dec_embedding-%s' % t)(in_dec_embedded)
            x_dec = Reshape((1, self.embedding_dim))(x_dec)

            if t == 0:
                s = out_bidir_encoder
            s, _ = fwd_decoder(x_dec, initial_state=s)
            loss = sampling_softmax([s, label_t])
            losses.append(loss)
            s = [s]

        model = Model(inputs=[in_encoder, in_decoder, s0, labels],
                      outputs=losses)

        self.train_model = model
        self.in_encoder = in_encoder
        self.out_bidir_encoder = out_bidir_encoder
        self.in_decoder = in_decoder
        self.embed_decoder = embed_decoder
        self.in_dec_embedded = in_dec_embedded
        self.labels = labels
        self.fwd_decoder = fwd_decoder

        return self.train_model