Ejemplo n.º 1
0
print("Found %s word vectors." % len(embeddings_index))

embedding_dim = 200

# 200 dimensional vectors for 10000 words in vocab
embedding_matrix = np.zeros((vocab_size, embedding_dim))

for word, i in wordtoix.items():
    # if i < max_words:
    embedding_vector = embeddings_index.get(word)
    if embedding_vector is not None:
        # Words not found in the embedding index will be all zeros
        embedding_matrix[i] = embedding_vector

# Model Building
inputs1 = Input(shape=(2048, ))
fe1 = Dropout(0.5)(inputs1)
fe2 = Dense(256, activation="relu")(fe1)
inputs2 = Input(shape=(max_length, ))
se1 = Embedding(vocab_size, embedding_dim, mask_zero=True)(inputs2)
se2 = Dropout(0.5)(se1)
se3 = LSTM(256)(se2)
decoder1 = add([fe2, se3])
decoder2 = Dense(256, activation="relu")(decoder1)
outputs = Dense(vocab_size, activation="softmax")(decoder2)
model = Model(inputs=[inputs1, inputs2], outputs=outputs)

# Model Summary
print(model.summary())

model.layers[2].set_weights([embedding_matrix])
Ejemplo n.º 2
0
    def __init__(self,
                 X_train,
                 y_train,
                 n_hidden,
                 n_epochs=40,
                 normalize=False,
                 tau=1.0,
                 dropout=0.05):
        """
            Constructor for the class implementing a Bayesian neural network
            trained with the probabilistic back propagation method.

            @param X_train      Matrix with the features for the training data.
            @param y_train      Vector with the target variables for the
                                training data.
            @param n_hidden     Vector with the number of neurons for each
                                hidden layer.
            @param n_epochs     Numer of epochs for which to train the
                                network. The recommended value 40 should be
                                enough.
            @param normalize    Whether to normalize the input features. This
                                is recommended unles the input vector is for
                                example formed by binary features (a
                                fingerprint). In that case we do not recommend
                                to normalize the features.
            @param tau          Tau value used for regularization
            @param dropout      Dropout rate for all the dropout layers in the
                                network.
        """

        # We normalize the training data to have zero mean and unit standard
        # deviation in the training set if necessary

        if normalize:
            self.std_X_train = np.std(X_train, 0)
            self.std_X_train[self.std_X_train == 0] = 1
            self.mean_X_train = np.mean(X_train, 0)
        else:
            self.std_X_train = np.ones(X_train.shape[1])
            self.mean_X_train = np.zeros(X_train.shape[1])

        X_train = (X_train - np.full(X_train.shape, self.mean_X_train)) / \
            np.full(X_train.shape, self.std_X_train)

        self.mean_y_train = np.mean(y_train)
        self.std_y_train = np.std(y_train)

        y_train_normalized = (y_train - self.mean_y_train) / self.std_y_train
        y_train_normalized = np.array(y_train_normalized, ndmin=2).T

        # We construct the network
        N = X_train.shape[0]
        batch_size = 128
        lengthscale = 1e-2
        reg = lengthscale**2 * (1 - dropout) / (2. * N * tau)

        inputs = Input(shape=(X_train.shape[1], ))
        inter = Dropout(dropout)(inputs, training=True)
        inter = Dense(n_hidden[0], activation='relu',
                      W_regularizer=l2(reg))(inter)
        for i in range(len(n_hidden) - 1):
            inter = Dropout(dropout)(inter, training=True)
            inter = Dense(n_hidden[i + 1],
                          activation='relu',
                          W_regularizer=l2(reg))(inter)
        inter = Dropout(dropout)(inter, training=True)
        outputs = Dense(y_train_normalized.shape[1],
                        W_regularizer=l2(reg))(inter)
        model = Model(inputs, outputs)

        model.compile(loss='mean_squared_error', optimizer='adam')

        # We iterate the learning process
        start_time = time.time()
        model.fit(X_train,
                  y_train_normalized,
                  batch_size=batch_size,
                  nb_epoch=n_epochs,
                  verbose=0)
        self.model = model
        self.tau = tau
        self.running_time = time.time() - start_time
Ejemplo n.º 3
0
    def sequence_padding(inputs, padding=0):
        pad_width = (window_size, window_size)
        outputs = []
        for x in inputs:
            x = np.pad(x, pad_width, 'constant', constant_values=padding)
            outputs.append(x)

        return outputs

    x = [sent2id(sent) for sent in sentences]
    x = sequence_padding(x)

    dataset = Dataset(x)
    gen = DataGenerator(dataset, batch_size=batch_size, shuffle=True)

    x_in = Input(shape=(1, ), dtype='int32')
    y_true = Input(shape=(window_size * 2, ), dtype='int32')
    y_out = skip_gram(x_in, vocub_size, word_dim, window_size)
    y_out = Skip_Gram_Loss(output_axis=[i + 1 for i in range(window_size * 2)])([y_true, *y_out])
    model = Model([y_true, x_in], y_out)
    optimizer = Adam(learning_rate=init_lr)
    model.compile(optimizer)

    class Evaluator(Callback):
        def __init__(self):
            super(Evaluator, self).__init__()
            self.best_loss = math.inf

        def on_epoch_end(self, epoch, logs=None):
            if logs['loss'] < self.best_loss:
                model.save_weights('skip_gram_best_wts.weights')
def default_classification_model(input_shape=None,
                                 input_tensor=None,
                                 num_classes=7,
                                 num_dense_layers=2,
                                 num_dense_units=256,
                                 dropout_rate=0.,
                                 pooling=None,
                                 use_output_activation=True,
                                 kernel_regularizer=None):
    """
    :param kernel_regularizer: l1 or l2 or none regularization
    :param num_classes:             # of classes to predict a score for each feature level.
    :param input_shape:             Input shape
    :param input_tensor:            Input tensor
    :param num_dense_layers:         Number of dense layers before the output layer
    :param num_dense_units:              The number of filters to use in the layers in the classification submodel.
    :param dropout_rate:            Dropout Rate
    :param pooling:                 which pooling to use at conv output
    :param use_output_activation:   whether to use output activation
    :return: A keras.model.Model that predicts class
    """

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

    assert kernel_regularizer in [None, 'L1', 'L2', 'L1-L2'], \
        'Unknown regularizer %s' % kernel_regularizer

    if kernel_regularizer == 'L1':
        kernel_regularizer = regularizers.l1(1e-4)
    elif kernel_regularizer == 'L2':
        kernel_regularizer = regularizers.l2(1e-3)
    elif kernel_regularizer == 'L1-L2':
        kernel_regularizer = regularizers.l1_l2(l1=1e-4, l2=1e-3)

    assert pooling in {None, 'avg', 'max',
                       'flatten'}, 'Unknown pooling option %s' % pooling

    if pooling == 'avg':
        outputs = keras.layers.GlobalAveragePooling2D(
            name='avg_pool_our')(img_input)
    elif pooling == 'max':
        outputs = keras.layers.GlobalMaxPooling2D(
            name='max_pool_our')(img_input)
    else:
        outputs = keras.layers.Flatten(name='flatten_our')(img_input)

    if dropout_rate > 0.:
        outputs = keras.layers.Dropout(rate=dropout_rate,
                                       name='drop_5')(outputs, training=True)

    for i in range(num_dense_layers):
        outputs = keras.layers.Dense(
            num_dense_units,
            activation='relu',
            name='fc%d' % (i + 1),
            kernel_regularizer=kernel_regularizer)(outputs)
        if dropout_rate > 0.:
            outputs = keras.layers.Dropout(rate=dropout_rate,
                                           name='drop_6')(outputs,
                                                          training=True)

    outputs = keras.layers.Dense(
        num_classes, name='predictions',
        kernel_regularizer=kernel_regularizer)(outputs)

    if use_output_activation:
        activation = 'sigmoid' if num_classes == 1 else 'softmax'
        outputs = keras.layers.Activation(activation, name='outputs')(outputs)

    return outputs
Ejemplo n.º 5
0

    train = train.sort_index()
    label = label.sort_index()

    X_train, X_test, y_train, y_test = train_test_split(train, label.iloc[:,0])

    #drop_list = list(np.arange(0.65, 0.7, 0.03))
   # drop_list.reverse()
    for dense in [128]:
      for drop_out in [0.63]:
        drop_out = round(drop_out, 2)
        patience=50
        lr = 0.0005
        #搭建融合后的模型
        inputs = Input((X_train.shape[1:]))

        x = Dropout(drop_out)(inputs)

        x = Dense(dense, activation='relu')(x)

        x = Dropout(drop_out)(x)

        x = Dense(22, activation='softmax')(x)
        model = Model(inputs, x)


        ########################################

        # np.random.seed(1337)
        #
Ejemplo n.º 6
0
np.set_printoptions(precision=3,
                    suppress=True,
                    threshold=np.inf,
                    linewidth=200)
# to make this notebook's output stable across runs
np.random.seed(42)
# Python ≥3.5 is required
assert sys.version_info >= (3, 5)
# numpy 1.16.4 is required
assert np.__version__ in ["1.16.5", "1.16.4"]
# ----------------------------------------------------------------------
print("Listing 8.29 GAN 生成器网络")
latent_dim = 32
height, width, channels = 32, 32, 3

generator_input = Input(shape=(latent_dim, ))
# 将输入转换为大小为(16,16)的128个通道的特征图
x = Dense(128 * 16 * 16)(generator_input)
x = LeakyReLU()(x)
x = Reshape((16, 16, 128))(x)

x = Conv2D(256, 5, padding='same')(x)
x = LeakyReLU()(x)

# 上采样为(32,32)
x = Conv2DTranspose(256, 4, strides=2, padding='same')(x)
x = LeakyReLU()(x)

x = Conv2D(256, 5, padding='same')(x)
x = LeakyReLU()(x)
x = Conv2D(256, 5, padding='same')(x)
Ejemplo n.º 7
0
def resnet50_unet_sigmoid(
        input_shape=(IMG_H, IMG_W, IMG_C), weights='imagenet'):
    inp = Input(input_shape)

    x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(inp)
    x = layers.Conv2D(64, (7, 7),
                      strides=(2, 2),
                      padding='valid',
                      name='conv1')(x)
    x = layers.BatchNormalization(axis=BN_AXIS, name='bn_conv1')(x)
    x = layers.Activation('relu')(x)
    c1 = x
    # print("c1")
    # print(c1.shape)
    x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
    c2 = x
    # print("c2")
    # print(c2.shape)

    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')
    c3 = x
    # print("c3")
    # print(c3.shape)

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')
    c4 = x
    # print("c4")
    # print(c4.shape)

    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')
    c5 = x
    # print("c5")
    # print(c5.shape)

    u6 = conv_block_custom(UpSampling2D()(c5), 1024)
    # print("u6")
    # print(u6.shape)
    u6 = concatenate([u6, c4], axis=-1)
    u6 = conv_block_custom(u6, 1024)

    u7 = conv_block_custom(UpSampling2D()(u6), 512)
    # print("u7")
    # print(u7.shape)
    u7 = concatenate([u7, c3], axis=-1)
    u7 = conv_block_custom(u7, 512)

    u8 = conv_block_custom(UpSampling2D()(u7), 256)
    # print("u8")
    # print(u8.shape)
    u8 = concatenate([u8, c2], axis=-1)
    u8 = conv_block_custom(u8, 256)

    u9 = conv_block_custom(UpSampling2D()(u8), 64)
    # print("u9")
    # print(u9.shape)
    u9 = concatenate([u9, c1], axis=-1)
    u9 = conv_block_custom(u9, 64)

    u10 = conv_block_custom(UpSampling2D()(u9), 32)
    u10 = conv_block_custom(u10, 32)

    res = Conv2D(2, (1, 1), activation='sigmoid')(u10)

    model = Model(inp, res)

    if weights == "imagenet":
        resnet50 = ResNet50(weights=weights,
                            include_top=False,
                            input_shape=(input_shape[0], input_shape[1], 3))
        # resnet50.summary()
        print("Loading imagenet weitghts ...")
        for i in tqdm(range(3, len(resnet50.layers) - 2)):
            try:
                model.layers[i].set_weights(resnet50.layers[i].get_weights())
                model.layers[i].trainable = False
            except:
                print(resnet50.layers[i].name)
                exit()
        print("imagenet weights have been loaded.")
        del resnet50

    return model
    def __init__(self, n_cols, number_layers=6, node_size=100,
                 prob_dropout=0.1, sparsity_const=10e-5, activation='relu', different_size=None,
                 beta=1, nodes_range='auto'):
        """
        :param n_cols: Number of columns of the dataset
        :param number_layers: Number of total layers in the network (without considering the output node)
        :param node_size: Number of nodes per layer
        :param prob_dropout: proportion to dropout
        :param sparsity_const: Restrict some nodes and not all (as PCA), using regularization strategy
        :param activation: Activation function
        :param different_size: Different sizes in the nodes between root and auxiliars
        """
        self.n_cols = n_cols
        self.activation = activation
        self.prob_dropout = prob_dropout
        self.number_layers = number_layers
        self.node_size = node_size
        self.sparsity_const = sparsity_const
        self.beta = beta
        self.nodes_range = nodes_range

        input_layer = Input(shape=(n_cols,))
        if nodes_range == 'auto':
            nodes_range = range(n_cols - node_size * 2, node_size - 1, -node_size)
        else:
            nodes_range = self.nodes_range
        print(nodes_range)
        # RESIDUAL LAYER
        if sparsity_const is not None:
            residual = layers.Dense(node_size, activation=self.activation, name='residual_layer_' + str(node_size),
                                    activity_regularizer=
                                    regularizers.l1_l2(sparsity_const, sparsity_const))(input_layer)
        else:
            residual = layers.Dense(node_size, activation=self.activation, name='root_layer_' + str(node_size))(
                input_layer)

        y = residual
        print('residual', y)

        # ROOT LAYERS
        if different_size is None:
            for nodes in nodes_range:
                print(nodes)
                if sparsity_const is not None:
                    y = layers.Dense(node_size, activation=self.activation, activity_regularizer=
                    regularizers.l1_l2(sparsity_const, sparsity_const))(y)
                else:
                    y = layers.Dense(node_size, activation=self.activation)(y)
                if self.prob_dropout is not None:
                    y = layers.Dropout(self.prob_dropout)(y)
                print(y)
        else:
            for nodes in nodes_range:
                if sparsity_const is not None:
                    y = residual
                    y = layers.Dense(nodes, activation=self.activation, name='root_layer_' + str(nodes),
                                     activity_regularizer=
                                     regularizers.l1_l2(sparsity_const, sparsity_const))(y)
                else:
                    y = layers.Dense(nodes + different_size, activation=self.activation,
                                     name='root_layer_' + str(nodes))(y)
                if self.prob_dropout is not None:
                    y = layers.Dropout(self.prob_dropout)(y)

            residual = layers.Dense(node_size + different_size)(residual)

        y = layers.add([y, residual])
        output_tensor = layers.Dense(2, activation='softmax')(y)

        self.model = Model(input_layer, output_tensor)
        plot_model(self.model, to_file=STRING.img_path + 'rc_architecture.png', show_shapes=True)
        print(self.model.summary())
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Problems with tensorflow2
# from keras import Input, Model
# from keras.layers import Dense

from keras import Input, Model
from keras.layers import Dense

# Create the input vector (13 elements).
inputs = Input((13, ))
# Create the first (input) layer (10 nodes) and connect it to the input vector.
input = Dense(10)(inputs)
# Create the next (hidden) layer (10 nodes) and connect it to the input layer.
hidden = Dense(10)(input)
# Create the output layer (1 node) and connect it to the previous (hidden) layer.
output = Dense(1)(hidden)
# Now let's create the neural network, specifying the input layer and output layer.
model = Model(inputs, output)
Ejemplo n.º 10
0
#数据预处理,归一化
x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255

#数据准备,将28*28矩阵转换成1*784,方便BP神经网络输入层784个神经元读取
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:]))

#------------------------------------------构建多层自编码器模型----------------------
input_size = 784
hidden_size = 128
code_size = 64

#定义神经网络层数
x = Input(shape=(input_size,))
hidden_1 = Dense(hidden_size, activation='relu')(x)
h = Dense(code_size, activation='relu')(hidden_1)
hidden_2 = Dense(hidden_size, activation='relu')(h)
r = Dense(input_size, activation='sigmoid')(hidden_2)
autoencoder = Model(inputs=x, outputs=r)
autoencoder.compile(optimizer='adam', loss='mse')

#---------------------------------------------模型训练与可视化------------------------------
epochs = 5
batch_size = 128
history = autoencoder.fit(x_train, x_train,
                          batch_size=batch_size,
                          epochs=epochs,
                          verbose=1,
                          validation_data=(x_test, x_test))
Ejemplo n.º 11
0
    def define_model(self, config, dset):
        supported_models = self.get_supported_models()
        SM = "EfficientNet supported models: " + ",".join(
            [str(x) for x in supported_models.keys()])
        num_layers = config["train"]["model"]["params"]["conv_blocks"]
        error_msg = str(num_layers) + " conv_blocks not in " + SM
        assert num_layers in supported_models.keys(), error_msg
        # Set session

        optimizer = keras.optimizers.SGD(
            lr=config["train"]["model"]["params"]["learning_rate"],
            momentum=0.9,
            nesterov=True)
        loss_func = "categorical_crossentropy"
        if self.is_training is False and "use_pretrained_input_size" in config[
                "profile"].keys():
            input_tensor = Input(
                (config["profile"]["use_pretrained_input_size"],
                 config["profile"]["use_pretrained_input_size"], 3),
                name="input")
            model = self.get_model(config,
                                   input_image=input_tensor,
                                   weights='imagenet',
                                   include_top=True)
        elif self.is_training is True or "use_pretrained_input_size" not in config[
                "profile"].keys():
            input_shape = (
                config["dataset"]["locations"]["box_size"],  # height
                config["dataset"]["locations"]["box_size"],  # width
                len(config["dataset"]["images"]["channels"])  # channels
            )
            input_image = keras.layers.Input(input_shape)
            model = self.get_model(config, input_image=input_image)
            features = keras.layers.GlobalAveragePooling2D(name="pool5")(
                model.layers[-1].output)
            # 2. Create an output embedding for each target
            class_outputs = []

            i = 0
            for t in dset.targets:
                y = keras.layers.Dense(t.shape[1],
                                       activation="softmax",
                                       name=t.field_name)(features)
                class_outputs.append(y)
                i += 1

            # 4. Create and compile model
            model = keras.models.Model(inputs=input_image,
                                       outputs=class_outputs)

            ## Added weight decay following tricks reported in:
            ## https://github.com/keras-team/keras/issues/2717
            regularizer = keras.regularizers.l2(0.00001)
            for layer in model.layers:
                if hasattr(layer, "kernel_regularizer"):
                    setattr(layer, "kernel_regularizer", regularizer)

            model = keras.models.model_from_json(
                model.to_json(), {'AugmentationLayer': AugmentationLayer})

        return model, optimizer, loss_func
Ejemplo n.º 12
0
    #building and compiling discriminator model
    discriminator = build_discriminator()
    dis_optimizer = Adam(lr=0.0002, beta_1=0.5, beta_2=0.999, epsilon=10e-8)
    discriminator.compile(loss=['binary_crossentropy'],
                          optimizer=dis_optimizer)

    #building and compiling generator model
    generator = build_generator()
    gen_optimizer = Adam(lr=0.0002, beta_1=0.5, beta_2=0.999, epsilon=10e-8)
    generator.compile(loss=['binary_crossentropy'], optimizer=gen_optimizer)

    ##building and compiling adversarial model(generator+discriminator)
    discriminator.trainable = False
    #building fake input latent vector and fake input label to make adversarial model
    fake_input_latent = Input(shape=(100, ))
    fake_input_label = Input(shape=(12, ))

    # taking fake output from generator
    fake_gen_output = generator([fake_input_latent, fake_input_label])

    #taking discriminator output for these fake_gen_output
    fake_dis_output = discriminator([fake_gen_output, fake_input_label])

    #making adversarial model
    adversarial_model = Model(inputs=[fake_input_latent, fake_input_label],
                              outputs=[fake_dis_output])
    adversarial_optimizer = Adam(lr=0.0002,
                                 beta_1=0.5,
                                 beta_2=0.999,
                                 epsilon=10e-8)
#Creating one_hot array
X = np.array(x_heme + x_nucle + x_steroid + x_steroid + x_steroid + x_control)
Y = np.array(y_heme + y_nucle + y_steroid + y_steroid + y_steroid + y_control)

X_Y = list(zip(X,Y))
rd.shuffle(X_Y)
X, Y = zip(*X_Y)

oH_x_train = np.array(X)
oH_y_train = np.array(Y)

#Change channel from last to first
set_image_data_format('channels_first')

### Model
inputs = Input(shape=(14, 32, 32, 32))

conv1 = Conv3D(4, (2,2,2), padding="valid", activation="relu")(inputs)
conv2 = Conv3D(16, (2,2,2), padding="valid", activation="relu")(conv1)
drop1 = Dropout(0.2)(conv2)
pool1 = MaxPooling3D(pool_size=(2,2,2))(drop1)
drop2 = Dropout(0.2)(pool1)

flat1 = Flatten()(drop2)
outputs = Dense(4, activation = "softmax")(flat1)
model = Model(inputs=inputs, outputs=outputs)

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

earlyStop = callbacks.EarlyStopping(monitor="val_loss", patience=10, mode="min")
Ejemplo n.º 14
0
    ## set up model
    #############
    if activationFunc=='selu':
        myInitializer="lecun_normal"
    elif activationFunc=='tanh':
        myInitializer="glorot_uniform" 

    tensorBoardDir="../deepNN/%s-pid%d/%s/fold%d"%(dateTime, sysuffix, namePrefix, fold)   #/tensorBoardLog
    if not os.path.exists(tensorBoardDir):
        os.makedirs(tensorBoardDir)
    checkPtFile='../deepNN/%s-pid%d/p2v-fold%d.hdf5'%(dateTime, sysuffix, fold)
    scriptInputArgs='../deepNN/%s-pid%d/scriptInputArgs.txt'%(dateTime, sysuffix)
    with open(scriptInputArgs,'w') as textFile:
        print(syspec, file=textFile)

    protTensor=Input(shape=(protTrainIN.shape[1],), name='FastProt')
    if activationFunc=='selu':
        x1=layers.AlphaDropout(dropoutRate)(protTensor)
    else:
        x1=layers.Dropout(dropoutRate)(protTensor)

    x1=layers.BatchNormalization()(x1)
    x1=layers.Dense(units=32, activation=activationFunc, kernel_initializer=myInitializer, kernel_regularizer=regularizers.l1_l2(l1=0, l2=0.01))(x1)
    x1=layers.BatchNormalization()(x1)
    
    rnaTensor=Input(shape=(rnaTrainIN.shape[1],), name='FastRNA')
    if activationFunc=='selu':
        x2=layers.AlphaDropout(dropoutRate)(rnaTensor)
    else:
        x2=layers.Dropout(dropoutRate)(rnaTensor)
   
SCRIPT_VERSION = "0.0.2"
SCRIPT_NAME = os.path.basename(__file__)
TIMESTAMP = str(datetime.now()).replace(':', '.')
EPOCHS = 60
BATCH_SIZE = 100
TRAINING_SIZE = 100000
TRAIN_VALIDATE_SPLIT = 0.2

# Archive the configuration
copyfile(__file__, 'configs/' + TIMESTAMP + ' ' + SCRIPT_NAME)

univariate = np.repeat([[[52]]], TRAINING_SIZE, axis=0)
univariate = np.append(univariate, np.zeros(shape=(TRAINING_SIZE, 1, 1)), axis=2)
(_, max_points, GEO_VECTOR_LEN) = univariate.shape

inputs = Input(name='Input', shape=(max_points, GEO_VECTOR_LEN))
model = LSTM(GEO_VECTOR_LEN, return_sequences=True)(inputs)
model = Dense(GEO_VECTOR_LEN)(model)
model = Model(inputs, model)
model.compile(
    loss=univariate_gaussian_loss,
    optimizer=Adam(lr=0.001))
model.summary()

callbacks = [
    TensorBoard(log_dir='./tensorboard_log/' + TIMESTAMP + ' ' + SCRIPT_NAME, write_graph=False),
    DecypherAll(lambda x: str(x)),
    EarlyStopping(patience=20, min_delta=0.001)
]

history = model.fit(x=univariate,
Ejemplo n.º 16
0
data = data.astype("float") / 255.0

trainX, testX, trainY, testY = train_test_split(data,
                                                labels,
                                                test_size=0.25,
                                                random_state=42)

lb = LabelBinarizer()

trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)

baseModel = VGG16(weights='imagenet',
                  include_top=False,
                  input_tensor=Input(shape=(224, 224, 3)))

headModel = FCHeadNet.build(baseModel, len(classNames), 256)

model = Model(inputs=baseModel.input, outputs=headModel)

print("layers.len", len(baseModel.layers))
for layer in baseModel.layers:
    layer.trainable = False

opt = RMSprop(lr=0.001)

model.compile(optimizer=opt,
              loss="categorical_crossentropy",
              metrics=["accuracy"])
Ejemplo n.º 17
0
                                           maxlen=max_word_length)
q1_test_word_index = sequence.pad_sequences(q1_test_word_index,
                                            maxlen=max_word_length)
q2_train_word_index = sequence.pad_sequences(q2_train_word_index,
                                             maxlen=max_word_length)
q2_dev_word_index = sequence.pad_sequences(q2_dev_word_index,
                                           maxlen=max_word_length)
q2_test_word_index = sequence.pad_sequences(q2_test_word_index,
                                            maxlen=max_word_length)

voc_size = len(tokenizer.word_index)
embedding_dim = 300
drop_out = 0.5
dense_dim = 100

q1_input = Input(name='q1', shape=(max_word_length, ))
q2_input = Input(name='q2', shape=(max_word_length, ))

embedding = Embedding(voc_size + 1, embedding_dim)
spatialdropout = SpatialDropout1D(drop_out)
dense = Dense(dense_dim, activation='tanh')

# sum or rnn or lstm
# encode = Lambda(lambda x: K.sum(x, axis=1, keepdims=False))
# encode = SimpleRNN(dense_dim)
encode = LSTM(dense_dim)

q1_embed = dense(spatialdropout(embedding(q1_input)))
q2_embed = dense(spatialdropout(embedding(q2_input)))
q1_embed = encode(q1_embed)
q2_embed = encode(q2_embed)
Ejemplo n.º 18
0
from keras import Input, layers, Model

input_tensor = Input(shape=(64,))
x = layers.Dense(32, activation='relu')(input_tensor)
x = layers.Dense(32, activation='relu')(x)
output_tensor = layers.Dense(10,activation='softmax')(x)
model = Model(input_tensor,output_tensor)
Ejemplo n.º 19
0
    discriminator = Model(disc_input, x)

    optimizer = RMSprop(lr=.0001, clipvalue=1.0, decay=1e-8)

    discriminator.compile(optimizer=optimizer, loss='binary_crossentropy')

    return discriminator


# Create both networks to use in GAN
generator = create_generator()
discriminator = create_discriminator()
discriminator.trainable = False

# Input is the latent space of the dataset (images)
gan_input = Input(shape=(LATENT_DIM, ))
# Output is the result of Discriminator => Generator
# (created images from Generator obtained from modifying weights based on the Discriminator output)
gan_output = discriminator(generator(gan_input))
# Model GAN
gan = Model(gan_input, gan_output)
# Optimizar for adapting learning
optimizer = RMSprop(lr=.0001, clipvalue=1.0, decay=1e-8)
gan.compile(optimizer=optimizer, loss='binary_crossentropy')

if not os.path.isdir(RES_DIR):
    os.mkdir(RES_DIR)

control_vectors = np.random.normal(size=(CONTROL_SIZE_SQRT**2, LATENT_DIM)) / 2

# ----------------------- Training process -----------------------
Ejemplo n.º 20
0
import os
from keras.preprocessing.image import ImageDataGenerator
group_folder_path = '2019_endoscopy_image/'
categories = ['0', '1']
img_size = (28, 28)
train_datagen = ImageDataGenerator(rescale=1. / 255)
train_data = train_datagen.flow_from_directory(group_folder_path + 'train',
                                               target_size=img_size,
                                               batch_size=3,
                                               class_mode='categorical')
test_datagen = ImageDataGenerator(rescale=1. / 255)
test_data = test_datagen.flow_from_directory(group_folder_path + 'test',
                                             target_size=img_size,
                                             batch_size=3,
                                             class_mode='categorical')
input_tensor = Input(shape=(220, 220, 3), dtype='float32', name='input')

x = layers.Conv2D(64, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal',
                  kernel_regularizer=regularizers.l2(0.01))(input_tensor)
x = layers.Conv2D(64, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal',
                  kernel_regularizer=regularizers.l2(0.01))(x)
x = layers.MaxPooling2D((2, 2))(x)

x = layers.Conv2D(128, (3, 3),
                  activation='relu',
Ejemplo n.º 21
0
    def GhostNet(self):
        inputdata = Input(shape=(self.size, self.size, self.channel))

        x = Conv2D(16, (3, 3),
                   strides=(2, 2),
                   padding='same',
                   data_format='channels_last',
                   activation=None,
                   use_bias=False)(inputdata)
        x = BatchNormalization(axis=-1)(x)
        x = Activation('relu')(x)

        x = GhostBottleneck(x, 3, 1, 16, 16, 2, False)
        x = GhostBottleneck(x, 3, 2, 48, 24, 2, False)
        x = GhostBottleneck(x, 3, 1, 72, 24, 2, False)
        x = GhostBottleneck(x, 5, 2, 72, 40, 2, True)
        x = GhostBottleneck(x, 5, 1, 120, 40, 2, True)
        x = GhostBottleneck(x, 3, 2, 240, 80, 2, False)
        x = GhostBottleneck(x, 3, 1, 200, 80, 2, False)
        x = GhostBottleneck(x, 3, 1, 184, 80, 2, False)
        x = GhostBottleneck(x, 3, 1, 184, 80, 2, False)
        x = GhostBottleneck(x, 3, 1, 480, 112, 2, True)
        x = GhostBottleneck(x, 3, 1, 672, 112, 2, True)
        x = GhostBottleneck(x, 5, 2, 672, 160, 2, True)
        x = GhostBottleneck(x, 5, 1, 960, 160, 2, False)
        x = GhostBottleneck(x, 5, 1, 960, 160, 2, True)
        x = GhostBottleneck(x, 5, 1, 960, 160, 2, False)
        x = GhostBottleneck(x, 5, 1, 960, 160, 2, True)

        x = Conv2D(960, (1, 1),
                   strides=(1, 1),
                   padding='same',
                   data_format='channels_last',
                   activation=None,
                   use_bias=False)(x)
        x = BatchNormalization(axis=-1)(x)
        x = Activation('relu')(x)

        x = GlobalAveragePooling2D(data_format='channels_last')(x)
        #x = Reshape((1,1,int(x.shape[1])))(x)
        x = Lambda(reshapes)(x)
        x = Conv2D(1280, (1, 1),
                   strides=(1, 1),
                   padding='same',
                   data_format='channels_last',
                   activation=None,
                   use_bias=False)(x)
        x = BatchNormalization(axis=-1)(x)
        x = Activation('relu')(x)

        x = Dropout(0.05)(x)
        x = Conv2D(self.numclass, (1, 1),
                   strides=(1, 1),
                   padding='same',
                   data_format='channels_last',
                   activation=None,
                   use_bias=False)(x)
        #x = K.squeeze(x,1)
        #x = K.squeeze(x,1)
        #out = softmax(x)
        x = Lambda(squeezes)(x)
        x = Lambda(squeezes)(x)
        out = Lambda(softmaxs)(x)

        model = Model(inputdata, out)
        plot_model(model,
                   to_file=os.path.join('weight', "GhostNet_model.png"),
                   show_shapes=True)
        model.compile(loss='categorical_crossentropy',
                      optimizer=Adam(),
                      metrics=['accuracy'])
        return model
Ejemplo n.º 22
0
    img_array = np.array(img)
    img_array = np.expand_dims(img_array, axis=0)
    # img_array = np.expand_dims(img_array, axis=3)

    i1 = vea_model.predict(img_array)
    i1 = i1[0]
    i1 = np.array(i1).reshape(img_size, img_size, 3)

    img1 = image.array_to_img(i1)
    img1.show()


if __name__ == '__main__':
    #--------------------- 创建VEA 开始 ----------------------#
    input_img = Input(shape=img_shape)
    # 编码层
    z_mean, z_log_var, shape_before_flattening = enCoder(input_img)
    z = layers.Lambda(sampling)(
        [z_mean,
         z_log_var])  # 相当于直接调用z = sampling(z_mean, z_log_var), z就是潜在空间中的一个点

    # 解码层
    m_deCoder = getDeCoder(z, shape_before_flattening)
    # m_deCoder.summary()
    z_decoded = m_deCoder(z)  # 得到解码后的z

    # 损失组合层
    input_img1 = MyLayer()([input_img, z_decoded])

    # 构建模型
Ejemplo n.º 23
0
def fit_cnn():
    X_train = train_embed
    y_train = np.array(train_labels)
    X_test = test_embed
    y_test = test_labels
    X_train = X_train.reshape(
        (X_train.shape[0], X_train.shape[1], X_train.shape[2], 1))
    X_test = X_test.reshape(
        (X_test.shape[0], X_test.shape[1], X_test.shape[2], 1))
    y_train, y_test = convert_one_hot(y_train, y_test)
    sequence_length = train_embed.shape[1]  # 60
    embedding_dim = train_embed.shape[2]
    filter_sizes = [2, 3, 4, 5]
    num_filters = 64
    drop = 0.6
    input_shape = train_embed[0].shape
    epochs = 1000
    batch_size = 64
    inputs = Input(shape=(sequence_length, embedding_dim, 1), dtype='float32')
    batch_norm = BatchNormalization(input_shape=input_shape)(inputs)
    conv_0 = Conv2D(num_filters,
                    kernel_size=(filter_sizes[0], embedding_dim),
                    padding='valid',
                    kernel_initializer='normal',
                    activation='relu',
                    input_shape=input_shape,
                    kernel_regularizer=regularizers.l2(0.01))(batch_norm)
    conv_1 = Conv2D(num_filters,
                    kernel_size=(filter_sizes[1], embedding_dim),
                    padding='valid',
                    kernel_initializer='normal',
                    activation='relu',
                    input_shape=input_shape,
                    kernel_regularizer=regularizers.l2(0.01))(batch_norm)
    conv_2 = Conv2D(num_filters,
                    kernel_size=(filter_sizes[2], embedding_dim),
                    padding='valid',
                    kernel_initializer='normal',
                    activation='relu',
                    input_shape=input_shape,
                    kernel_regularizer=regularizers.l2(0.01))(batch_norm)
    conv_3 = Conv2D(num_filters,
                    kernel_size=(filter_sizes[3], embedding_dim),
                    padding='valid',
                    kernel_initializer='normal',
                    activation='relu',
                    input_shape=input_shape,
                    kernel_regularizer=regularizers.l2(0.01))(batch_norm)

    maxpool_0 = MaxPool2D(pool_size=(sequence_length - filter_sizes[0] + 1, 1),
                          strides=(1, 1),
                          padding='valid')(conv_0)
    maxpool_1 = MaxPool2D(pool_size=(sequence_length - filter_sizes[1] + 1, 1),
                          strides=(1, 1),
                          padding='valid')(conv_1)
    maxpool_2 = MaxPool2D(pool_size=(sequence_length - filter_sizes[2] + 1, 1),
                          strides=(1, 1),
                          padding='valid')(conv_2)
    maxpool_3 = MaxPool2D(pool_size=(sequence_length - filter_sizes[3] + 1, 1),
                          strides=(1, 1),
                          padding='valid')(conv_3)

    concatenated_tensor = Concatenate(axis=1)(
        [maxpool_0, maxpool_1, maxpool_2, maxpool_3])
    flatten = Flatten()(concatenated_tensor)
    dropout = Dropout(drop)(flatten)
    output = Dense(units=3,
                   activation='softmax',
                   kernel_regularizer=regularizers.l2(0.1))(dropout)

    # this creates a model that includes
    model = Model(inputs=inputs, outputs=output)
    adam = Adam(lr=1e-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
    model.compile(optimizer=adam,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    print("Traning Model...")
    model.fit(X_train,
              y_train,
              batch_size=batch_size,
              epochs=epochs,
              verbose=1,
              validation_data=(X_test, y_test))
    model.save("model/cnn.h5")
    return model
Ejemplo n.º 24
0
# Read training labels
training_labels = LabelDataset(training_labels_file)

# Read testset
testset = ImageDataset(testset_file)

# Read test labels
test_labels = LabelDataset(test_labels_file)

experiments = list()
repeat = True

# Input type construction
x_dimension, y_dimension = training_set.getImageDimensions()
inChannel = 1
input_img = Input(shape=(x_dimension, y_dimension, inChannel))

# Split dataset into train and validation datasets
X_train, X_validation, y_train, y_validation = train_test_split(
    training_set.getImagesNormalized(),
    to_categorical(training_labels.get_labels(),
                   num_classes=training_labels.num_classes()),
    test_size=0.2,
    random_state=13)

while repeat:
    # User Arguments
    convolutional_layers = int(input("Number of convolutional layers: "))
    convolutional_filter_size = int(input("Convolutional filter size: "))

    convolutional_filters_per_layer = []
Ejemplo n.º 25
0
    def build_model(self):
        self.layer['input'] = Input(shape=(self.input_shape, ))

        self.layer['pre_scaling'] = Scaling(scale_min1=self.scale_min1,
                                            scale_max1=self.scale_max1,
                                            scale_min2=self.scale_min2,
                                            scale_max2=self.scale_max2,
                                            name='pre_scaling')(
                                                self.layer['input'])

        self.layer['pre_reshaping'] = Reshape(
            target_shape=[1, 1, self.input_shape],
            name='pre_reshaping')(self.layer['pre_scaling'])

        self.layer['pre_tiling'] = Tiling(units=self.input_shape,
                                          name='pre_tiling')(
                                              self.layer['pre_reshaping'])

        self.layer['cnn_conv_1'] = Conv2D(
            filters=[AvgFilter(), MaxFilter(),
                     StdDevFilter()],
            window=self.conv_1_window,
            padding='same',
            use_bias=False,
            kernel_size=(self.input_shape, self.input_shape),
            data_format='channels_first',
            name='cnn_conv_1')(self.layer['pre_tiling'])

        self.layer['cnn_activation_1'] = Activation(
            activation=sigmoid,
            name='cnn_activation_1')(self.layer['cnn_conv_1'])

        self.layer['cnn_conv_2'] = Conv2D(
            filters=[AvgFilter(), MaxFilter(),
                     StdDevFilter()],
            window=self.conv_2_window,
            padding='same',
            use_bias=False,
            kernel_size=(self.input_shape, self.input_shape),
            data_format='channels_first',
            name='cnn_conv_2')(self.layer['cnn_activation_1'])

        self.layer['cnn_activation_2'] = Activation(
            activation=sigmoid,
            name='cnn_activation_2')(self.layer['cnn_conv_2'])

        self.layer['cnn_pooling_1'] = MaxPooling2D(
            pool_size=(self.pool_1_size, self.pool_1_size),
            padding='same',
            data_format='channels_first',
            name='cnn_pooling_1')(self.layer['cnn_activation_2'])

        self.layer['cnn_conv_3'] = Conv2D(
            filters=[AvgFilter(), MaxFilter(),
                     StdDevFilter()],
            window=self.conv_3_window,
            padding='same',
            use_bias=False,
            kernel_size=(self.input_shape, self.input_shape),
            data_format='channels_first',
            name='cnn_conv_3')(self.layer['cnn_pooling_1'])

        self.layer['cnn_activation_3'] = Activation(
            activation=sigmoid,
            name='cnn_activation_3')(self.layer['cnn_conv_3'])

        self.layer['cnn_pooling_2'] = MaxPooling2D(
            pool_size=(self.pool_2_size, self.pool_2_size),
            padding='same',
            data_format='channels_first',
            name='cnn_pooling_2')(self.layer['cnn_activation_3'])

        self.layer['bridge_flatten'] = Flatten(name='bridge_flatten')(
            self.layer['cnn_pooling_2'])

        self.layer['elm_1_dense_1'] = Dense(
            units=self.elm_1_dense_1_units,
            activation=None,
            use_bias=True,
            kernel_initializer=Unifinv(minval=self.elm_1_dense_1_kernel_min,
                                       maxval=self.elm_1_dense_1_kernel_max),
            bias_initializer=RandomUniform(minval=self.elm_1_dense_1_bias_min,
                                           maxval=self.elm_1_dense_1_bias_max),
            trainable=False,
            name='elm_1_dense_1')(self.layer['bridge_flatten'])

        self.layer['elm_1_activation_1'] = Activation(
            activation=sigmoid,
            name='elm_1_activation_1')(self.layer['elm_1_dense_1'])

        self.layer['elm_1_dense_2'] = Dense(
            units=self.category_num,
            activation=None,
            use_bias=False,
            kernel_initializer=Zeros(),
            trainable=False,
            name='elm_1_dense_2')(self.layer['elm_1_activation_1'])

        self.layer['elm_2_dense_1'] = Dense(
            units=self.elm_2_dense_1_units,
            activation=None,
            use_bias=True,
            kernel_initializer=Unifinv(minval=self.elm_2_dense_1_kernel_min,
                                       maxval=self.elm_2_dense_1_kernel_max),
            bias_initializer=RandomUniform(minval=self.elm_2_dense_1_bias_min,
                                           maxval=self.elm_2_dense_1_bias_max),
            trainable=False,
            name='elm_2_dense_1')(self.layer['bridge_flatten'])

        self.layer['elm_2_activation_1'] = Activation(
            activation=sigmoid,
            name='elm_2_activation_1')(self.layer['elm_2_dense_1'])

        self.layer['elm_2_dense_2'] = Dense(
            units=self.category_num,
            activation=None,
            use_bias=False,
            kernel_initializer=Zeros(),
            trainable=False,
            name='elm_2_dense_2')(self.layer['elm_2_activation_1'])

        self.layer['elm_3_dense_1'] = Dense(
            units=self.elm_3_dense_1_units,
            activation=None,
            use_bias=True,
            kernel_initializer=Unifinv(minval=self.elm_3_dense_1_kernel_min,
                                       maxval=self.elm_3_dense_1_kernel_max),
            bias_initializer=RandomUniform(minval=self.elm_3_dense_1_bias_min,
                                           maxval=self.elm_3_dense_1_bias_max),
            trainable=False,
            name='elm_3_dense_1')(self.layer['bridge_flatten'])

        self.layer['elm_3_activation_1'] = Activation(
            activation=sigmoid,
            name='elm_3_activation_1')(self.layer['elm_3_dense_1'])

        self.layer['elm_3_dense_2'] = Dense(
            units=self.category_num,
            activation=None,
            use_bias=False,
            kernel_initializer=Zeros(),
            trainable=False,
            name='elm_3_dense_2')(self.layer['elm_3_activation_1'])

        self.layer['fully_connected_concat'] = Concatenate(
            name='fully_connected_concat')([
                self.layer['elm_1_dense_2'], self.layer['elm_2_dense_2'],
                self.layer['elm_3_dense_2']
            ])

        self.layer['fully_connected_reshape'] = Reshape(
            target_shape=(self.fully_connected_num, self.category_num),
            name='fully_connected_reshape')(
                self.layer['fully_connected_concat'])

        self.layer['fully_connected_merge'] = MergeCategorical(
            categorical_length=self.category_num,
            name='fully_connected_merge')(
                self.layer['fully_connected_reshape'])
Ejemplo n.º 26
0
def get_model(n_classes, input_height=224, input_width=224, weights=None):
    n_filters = 16
    kernel_size = 3
    dropout = 0.25

    img_input = Input(shape=(input_height, input_width, 3))  # Assume 224,224,3

    # Start with 224x224x3. Apply 3x3x16 Convolution, padding same and 2x2 Pooling. New dimensions:
    # 112x112x16
    c1 = conv2d_block(n_filters=n_filters * 1,
                      kernel_size=kernel_size,
                      input_tensor=img_input)
    p1 = MaxPooling2D((2, 2))(c1)
    d1 = Dropout(dropout)(p1)

    # 112x112x16. Apply 3x3x32 Convolution, padding same and 2x2 Pooling. New dimensions:
    # 56x56x32
    c2 = conv2d_block(n_filters=n_filters * 2,
                      kernel_size=kernel_size,
                      input_tensor=d1)
    p2 = MaxPooling2D((2, 2))(c2)
    d2 = Dropout(dropout)(p2)

    # 56x56x32. Apply 3x3x64 Convolution, padding same and 2x2 Pooling. New dimensions:
    # 28x28x64
    c3 = conv2d_block(n_filters=n_filters * 4,
                      kernel_size=kernel_size,
                      input_tensor=d2)
    p3 = MaxPooling2D((2, 2))(c3)
    d3 = Dropout(dropout)(p3)

    # 28x28x64. Apply 3x3x128 Convolution, padding same and 2x2 Pooling. New dimensions:
    # 14x14x128
    c4 = conv2d_block(n_filters=n_filters * 8,
                      kernel_size=kernel_size,
                      input_tensor=d3)
    p4 = MaxPooling2D((2, 2))(c4)
    d4 = Dropout(dropout)(p4)

    # 14x14x128. Apply 3x3x256 Convolution, padding same. New dimensions: 14x14x256
    c5 = conv2d_block(n_filters=n_filters * 16,
                      kernel_size=kernel_size,
                      input_tensor=d4)

    # Upsampling part starts here
    # Start with dimensions 14x14x256
    u6 = Conv2DTranspose(n_filters * 8,
                         kernel_size=(kernel_size, kernel_size),
                         strides=(2, 2),
                         padding='same')(c5)
    u6 = Concatenate()([u6, c4])
    d6 = Dropout(dropout)(u6)
    c6 = conv2d_block(n_filters * 8, kernel_size=3, input_tensor=d6)

    u7 = Conv2DTranspose(n_filters * 4,
                         kernel_size=(kernel_size, kernel_size),
                         strides=(2, 2),
                         padding='same')(c6)
    u7 = Concatenate()([u7, c3])
    d7 = Dropout(dropout)(u7)
    c7 = conv2d_block(n_filters * 4, kernel_size=3, input_tensor=d7)

    u8 = Conv2DTranspose(n_filters * 2,
                         kernel_size=(kernel_size, kernel_size),
                         strides=(2, 2),
                         padding='same')(c7)
    u8 = Concatenate()([u8, c2])
    d8 = Dropout(dropout)(u8)
    c8 = conv2d_block(n_filters * 2, kernel_size=3, input_tensor=d8)

    u9 = Conv2DTranspose(n_filters * 1,
                         kernel_size=(kernel_size, kernel_size),
                         strides=(2, 2),
                         padding='same')(c8)
    u9 = Concatenate()([u9, c1])
    d9 = Dropout(dropout)(u9)
    c9 = conv2d_block(n_filters * 1, kernel_size=3, input_tensor=d9)

    # Apply 1x1 convolution
    outputs = Conv2DTranspose(n_classes, (1, 1), activation='softmax')(c9)
    model = Model(inputs=[img_input], outputs=[outputs])

    model.summary()

    if weights is not None:
        model.load_weights(weights)

    opt = Adam(lr=1E-5, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
    model.compile(loss='categorical_crossentropy',
                  optimizer=opt,
                  metrics=['accuracy'])

    return model
print(labels.shape)

#################### train and test split
x_train_lstm_daily = samples_lstm_daily[:4000]
x_test_lstm_daily = samples_lstm_daily[4000:]
y_train = labels[:4000]
y_test = labels[4000:]

print(x_train_lstm_daily.shape,y_train.shape)
print(x_test_lstm_daily.shape,y_test.shape)

plt.figure(figsize=(20,10))
plt.plot(y_test,'r')

################################################ Model: daily Block - Multi-STGCnet
input130 = Input(shape=(pre,1), dtype='float')
input131 = layers.LSTM(35,return_sequences=True)(input130)
input132 = layers.LSTM(12)(input131)
output13 = layers.Dense(1,activation='relu')(input132)
model = models.Model(inputs=[input130],outputs=[output13])
model.summary()
model.compile(optimizer='rmsprop',loss='mae',metrics=['mae','mse','mape'])
callbacks_list = [
    keras.callbacks.ModelCheckpoint(filepath='lstm_daily.h5',
                                    monitor='val_loss',
                                    save_best_only=True,)

################################################ Model training
epochs = 1000
H = model.fit([x_train_lstm_daily], y_train,callbacks=callbacks_list,batch_size=interval_batch,epochs=epochs,validation_data=([x_test_lstm_daily],y_test))
Ejemplo n.º 28
0
    x = up_conv(x, [512, 256])

    source = vgg16.get_layer('block4_conv3').output
    x = Interpolate(target_layer=source, name='resize_2')(x)
    x = concatenate([x, source])
    x = up_conv(x, [256, 128])

    source = vgg16.get_layer('block3_conv3').output
    x = Interpolate(target_layer=source, name='resize_3')(x)
    x = concatenate([x, source])
    x = up_conv(x, [128, 64])

    source = vgg16.get_layer('block2_conv2').output
    x = Interpolate(target_layer=source, name='resize_4')(x)
    x = concatenate([x, source])
    feature = up_conv(x, [64, 32])

    x = conv_cls(feature, 2)

    region_score = Lambda(lambda layer: layer[:, :, :, 0])(x)
    affinity_score = Lambda(lambda layer: layer[:, :, :, 1])(x)

    return region_score, affinity_score


if __name__ == '__main__':
    input_image = Input(shape=(512, 512, 3))
    region, affinity = VGG16_UNet(input_tensor=input_image, weights=None)
    model = Model(input_image, [region, affinity], name='vgg16_unet')
    model.summary()
Ejemplo n.º 29
0
import imageio
from keras.optimizers import Adam
import time
from skimage.io import imread
import cv2
"""## **The Generator Network**"""

# Hyperparameters required for generator network

residual_blocks = 16
momentum = 0.8
input_shape = (64, 64, 3)

# Input layer to feed input to the network of a shape of (64,64,3)

input_layer = Input(shape=input_shape)
input_layer

# Adding the pre-residual block (2D convolution layer)
#filters: 64, kerel size: 9, strides: 1, Padding: same, Activation: relu

gen1 = Conv2D(filters=64,
              kernel_size=9,
              strides=1,
              padding='same',
              activation='relu')(input_layer)

# Method with the entire code for the residual block


def residual_block(x):
Ejemplo n.º 30
0
 def __init__(self, classes=6, dim=(224, 224, 3)):
     self.classes = classes
     self.dim = dim
     self.input_tensor = Input(shape=self.dim,
                               dtype='float32',
                               name='input')