def __init__(self):
        self.img_rows = 120
        self.img_cols = 96
        self.channels = 3

        # Following parameter and optimizer set as recommended in paper
        optimizer = Adam(0.0002, 0.5)

        # Build and compile the discriminator
        self.discriminator = self.build_discriminator()
        self.discriminator = multi_gpu_model(self.discriminator, gpus=2)
        self.discriminator.compile(loss='binary_crossentropy',
                                   optimizer=optimizer,
                                   metrics=['accuracy'])

        # Build the generator
        self.generator = self.build_generator()

        # The generator takes noise as input and generated imgs
        z = Input(shape=(100, ))
        img = self.generator(z)

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

        # The discriminator takes generated images as input and determines validity
        valid = self.discriminator(img)

        # The combined model  (stacked generator and discriminator) takes
        # noise as input => generates images => determines validity
        self.combined = Model(z, valid)
        self.combined = multi_gpu_model(self.combined, gpus=2)
        self.combined.compile(loss='binary_crossentropy',
                              optimizer=optimizer,
                              metrics=['accuracy'])
    def build_models(self):

        lr = 4e-4
        clip = 1.0
        decay = 1e-8

        # Initialize architectures
        self.disc = self.init_discriminator()
        self.gen = self.init_generator()

        if self.weights_path is not None:
            self.gen.load_weights(self.weights_path + 'generator_weights.h5')
            self.disc.load_weights(self.weights_path +
                                   'discriminator_weights.h5')

        # Create the model
        if self.gpu_list is not None:
            self.disc_model = multi_gpu_model(self.disc, gpus=self.gpu_list)
        else:
            self.disc_model = self.disc

        # Compile Discriminator Model
        #doptimizer = SGD(lr=lr, nesterov=True, clipvalue=clip)
        doptimizer = RMSprop(lr=lr, decay=decay, clipvalue=clip)
        self.disc_model.compile(loss='mse',
                                optimizer=doptimizer,
                                metrics=['accuracy'])

        # Compile Adversarial Model
        #goptimizer = Adam(clipvalue=clip)
        goptimizer = RMSprop(lr=lr / 2, decay=decay, clipvalue=clip)
        self.disc.trainable = False
        im_lr = Input(shape=(self.imlr_w, self.imlr_h, self.im_c))
        im_hr = Input(shape=(self.imhr_w, self.imhr_h, self.im_c))

        # Generated HR Images
        gen_hr = self.gen(im_lr)

        # Discriminator on Generator Output
        disc_gen_hr = self.disc(gen_hr)

        self.adv_model = Model(im_lr, disc_gen_hr)

        # Create the model
        if self.gpu_list is not None:
            self.adv_model = multi_gpu_model(Model(im_lr, disc_gen_hr),
                                             gpus=self.gpu_list)
        else:
            self.adv_model = Model(im_lr, disc_gen_hr)

        self.adv_model.compile(
            loss=['binary_crossentropy'],
            #loss_weights=[1e-3,1],
            optimizer=goptimizer,
            metrics=['accuracy'])
Пример #3
0
def DefineModel():
	
	# If GPUS==0 this will force use of CPU, even if GPUs are present
	# If GPUS>1 this will force the CPU to server as orchestrator
	# If GPUS==1 this will do nothing, allowing GPU to act as its own orchestrator
	if GPUS!=1: tf.device('/cpu:0')

	# Here we build the network model.
	model = Sequential()
	model.add(Conv2D(1, (1,1), activation="relu", kernel_initializer="glorot_uniform", strides=(1,1), input_shape=(height, width, 1), padding="same", data_format="channels_last") )
	model.add(Flatten())
	model.add(Dense(int(Nouts*5), activation='linear', kernel_initializer="glorot_uniform"))
	model.add(Dense(Nouts, activation='relu', kernel_initializer="glorot_uniform"))
	model.summary()

	if GPUS<=1 :
		parallel_model = model
	else:
		parallel_model = multi_gpu_model( model, gpus=GPUS )
	
	# Compile the model and print a summary of it
	opt = Adadelta(clipnorm=1.0)
	parallel_model.compile(loss=customLoss, optimizer=opt, metrics=['mae', 'mse', 'accuracy'])
	
	return parallel_model
Пример #4
0
def sensitivity_specificity(path_to_model_weights,
                            crop_shape=(64,64),
                            threshold=0.5,
                            batch_size=32,
                            nb_gpus=1,
                            ):
    path_to_validation_data = "../IntermediateData/validation_data.npy"
    path_to_validation_label = "../IntermediateData/validation_label.npy"
    data = np.load(path_to_validation_data)
    labels = np.load(path_to_validation_label)
    
    img_dims, output_dims = crop_shape+(3,), crop_shape+(1,)
    model_single_gpu = seunet_model.seunet(img_dims, output_dims)
    model_single_gpu.load_weights(path_to_model_weights)
    if int(nb_gpus) > 1:
        model_multi_gpu = multi_gpu_model(model_single_gpu, gpus=nb_gpus)
    else:
        model_multi_gpu = model_single_gpu
    
    predicted = model_multi_gpu.predict(data, batch_size=batch_size)
    predicted[predicted>threshold] = 1
    predicted[predicted<=threshold] = 0
    
    sensitivity = predicted[(predicted==1) & (labels==1)].size / float(labels[labels==1].size)
    specificity = predicted[(predicted==0) & (labels==0)].size / float(labels[labels==0].size)

    return sensitivity, specificity
Пример #5
0
def build_model(nb_words, max_length, embedding_size=200):
    #  with tf.device("/cpu:0"): # add
    inp = Input(shape=(max_length, ))
    #     x = Embedding(nb_words, embedding_size, weights=[embedding_matrix], trainable=False)(inp)
    x = Embedding(nb_words, embedding_size, input_length=max_length)(inp)
    x = SpatialDropout1D(0.3)(x)
    x1 = Bidirectional(CuDNNLSTM(256, return_sequences=True))(x)
    x2 = Bidirectional(CuDNNGRU(128, return_sequences=True))(x1)
    max_pool1 = GlobalMaxPooling1D()(x1)
    max_pool2 = GlobalMaxPooling1D()(x2)
    conc = Concatenate()([max_pool1, max_pool2])
    predictions = Dense(1, activation='sigmoid')(conc)
    model = Model(inputs=inp, outputs=predictions)
    #  adam = optimizers.SGD(lr=learning_rate)

    optm = AdaBound(lr=learning_rate,
                    final_lr=0.1,
                    gamma=1e-03,
                    weight_decay=0.,
                    amsbound=False)

    if is_multi:
        model = multi_gpu_model(model, gpus=gpu_count)
    model.compile(optimizer=optm,
                  loss='binary_crossentropy',
                  metrics=['accuracy'])
    return model
Пример #6
0
def NDUDE_CNN_model_5map(D, nb_x_classes, nb_z_classes, k, lr=0.001):
    unitN = 160
    # -----------------------------------------------------
    # Defining neural networks
    # -----------------------------------------------------
    inputs = layers.Input(shape=(D + 2 * k, nb_z_classes))
    layer = masked_CNN(unitN,
                       2 * k + 1,
                       kernel_initializer='he_normal',
                       padding='valid')(inputs)
    #layer = layers.Conv1D(unitN, 2*k+1, kernel_initializer = 'he_normal', padding='valid')(inputs)
    layer = layers.Activation('relu')(layer)
    layer = layers.Conv1D(unitN,
                          1,
                          kernel_initializer='he_normal',
                          padding='valid')(layer)
    layer = layers.Activation('relu')(layer)
    layer = layers.Conv1D(unitN,
                          1,
                          kernel_initializer='he_normal',
                          padding='valid')(layer)
    layer = layers.Activation('relu')(layer)
    layer = layers.Conv1D(nb_x_classes + 1,
                          1,
                          kernel_initializer='he_normal',
                          padding='valid')(layer)
    output = layers.Activation('softmax')(layer)
    model = models.Model(inputs=inputs, outputs=output)

    adam = optimizers.Adam(lr=lr)
    multi_model = multi_gpu_model(model, gpus=4)
    multi_model.compile(loss='poisson', optimizer=adam)
    return multi_model
Пример #7
0
def get_text_lstm_attention(sent_length, embeddings_weight):
    print("get_text_lstm_attention")
    content = Input(shape=(sent_length, ), dtype='int32')
    embedding = Embedding(name="word_embedding",
                          input_dim=embeddings_weight.shape[0],
                          weights=[embeddings_weight],
                          output_dim=embeddings_weight.shape[1],
                          trainable=False)

    embedded_sequences = SpatialDropout1D(0.2)(embedding(content))
    x = Dropout(0.25)(CuDNNLSTM(200,
                                return_sequences=True)(embedded_sequences))
    merged = Attention(sent_length)(x)
    merged = Dense(100, activation='relu')(merged)
    merged = Dropout(0.25)(merged)

    x = Dropout(0.2)(Activation(activation="relu")(BatchNormalization()(
        Dense(1000)(merged))))
    x = Activation(activation="relu")(BatchNormalization()(Dense(500)(x)))
    output = Dense(372, activation="softmax")(x)

    model = Model(inputs=content, outputs=output)
    model = multi_gpu_model(model, 2)
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Пример #8
0
def build_keras_clf(n_input_features=1,
                    hidden_layer_sizes=(10, 10),
                    learning_rate_init=0.01,
                    momentum=0.8,
                    n_gpus=0):
    """This function builds a Keras model for use with scikit's GridSearch"""
    if not isinstance(hidden_layer_sizes, tuple):
        hidden_layer_sizes = (hidden_layer_sizes, )

    model = Sequential()

    model.add(
        Dense(units=n_input_features,
              input_shape=(n_input_features, ),
              activation='relu'))

    for layer_size in hidden_layer_sizes:
        assert layer_size > 0
        model.add(Dense(units=layer_size, activation='relu'))

    # Add output layer
    model.add(Dense(units=1, activation='sigmoid'))

    # Set the number of GPUs
    if n_gpus > 1:
        model = multi_gpu_model(model, gpus=n_gpus)

    sgd = SGD(lr=learning_rate_init, momentum=momentum, nesterov=True)
    model.compile(loss='mean_squared_error',
                  optimizer=sgd,
                  metrics=["accuracy"])

    return model
Пример #9
0
def create_model(input_shape=(30, 120, 176, 1), rnn_units=128, cnn_units=32, num_gpu=1, nb_category=10):
    # define our time-distributed setup
    inp = Input(shape=input_shape)

    x = TimeDistributed(Conv2D(cnn_units, (3, 3), padding='same', activation='relu'))(inp)
    x = TimeDistributed(Conv2D(cnn_units, (3, 3), padding='same', activation='relu'))(x)
    x = TimeDistributed(MaxPooling2D(pool_size=(2, 2)))(x)
    x = Dropout(.5)(x)

    x = TimeDistributed(Conv2D(cnn_units, (3, 3), padding='same', activation='relu'))(x)
    x = TimeDistributed(Conv2D(cnn_units, (3, 3), padding='same', activation='relu'))(x)
    x = TimeDistributed(MaxPooling2D(pool_size=(2, 2)))(x)
    x = Dropout(.5)(x)

    x = TimeDistributed(Flatten())(x)
    x = GRU(units=rnn_units, return_sequences=True)(x)
    x = Dropout(.5)(x)

    x = TimeDistributed(Flatten())(x)
    x = GRU(units=rnn_units, return_sequences=True)(x)
    x = Dropout(.5)(x)

    x = GRU(units=rnn_units, return_sequences=False)(x)

    x = Dropout(.5)(x)
    x = Dense(nb_category, activation='softmax')(x)

    opt_adm = Adam()
    model = Model(inp, x)
    if num_gpu > 1:
        model = multi_gpu_model(model, gpus=num_gpu) # gpus
    model.compile(optimizer=opt_adm, loss='categorical_crossentropy', metrics=['accuracy'])
    print ("model paerms: %s"%model.count_params())
    return model
    def on_applymodel_clicked(self):
        if not self.checkBox_densenet.isChecked() and not self.checkBox_inceptionresnet.isChecked() \
                and not self.checkBox_nasnet.isChecked() and not self.checkBox_nasnetlarge.isChecked() \
                and self.checkBox_toyresnet.isChecked():
            # input shape to enter in parameters
            inputs = Input(shape=(256, 256, 3), name='img')
            x = Conv2D(32, 3, activation='relu')(inputs)
            x = Conv2D(64, 3, activation='relu')(x)
            block_1_output = MaxPooling2D(3)(x)

            x = Conv2D(64, 3, activation='relu',
                       padding='same')(block_1_output)
            x = Conv2D(64, 3, activation='relu', padding='same')(x)
            block_2_output = add([x, block_1_output])

            x = Conv2D(64, 3, activation='relu',
                       padding='same')(block_2_output)
            x = Conv2D(64, 3, activation='relu', padding='same')(x)
            block_3_output = add([x, block_2_output])

            x = Conv2D(64, 3, activation='relu')(block_3_output)
            x = GlobalAveragePooling2D()(x)
            x = Dense(256, activation='relu', name='dense_layer')(x)
            x = Dropout(0.5)(x)
            outputs = Dense(self.class_nb, activation='softmax')(x)

            model = Model(inputs, outputs, name='toy_resnet')
            model = multi_gpu_model(model, gpus=4)
Пример #11
0
    def train(self, gpus):
        # async datasets saver might be running, wait before training
        self.dataset.saver.wait()

        # train
        if self.model is None:
            self.model = self.logic.builder(True)

        to_train = self.model
        if gpus > 1:
            log.info("training with %d GPUs", gpus)
            to_train = multi_gpu_model(self.model, gpus=gpus)

        past = self.history.copy() if self.history is not None else None
        present = self.logic.trainer(to_train, self.dataset).history

        if past is None:
            self.history = present
        else:
            self.history = {}
            for name, past_values in past.items():
                self.history[name] = past_values + present[name]

        self.accu = self.accuracy()

        print("")
        self._emit_txt_stats(sys.stdout)

        # save model structure and weights
        self._save_model()
        # save training history
        self._save_history()
        # save model accuracy statistics
        self._save_stats()
Пример #12
0
def get_text_capsule(sent_length, embeddings_weight):
    print("get_text_capsule")
    content = Input(shape=(sent_length, ), dtype='int32')
    embedding = Embedding(name="word_embedding",
                          input_dim=embeddings_weight.shape[0],
                          weights=[embeddings_weight],
                          output_dim=embeddings_weight.shape[1],
                          trainable=False)
    embed = SpatialDropout1D(0.2)(embedding(content))

    x = Bidirectional(CuDNNGRU(128, return_sequences=True))(embed)
    capsule = Capsule(num_capsule=Num_capsule,
                      dim_capsule=Dim_capsule,
                      routings=Routings,
                      share_weights=True)(x)
    capsule = Flatten()(capsule)

    x = Dropout(0.2)(Activation(activation="relu")(BatchNormalization()(
        Dense(1000)(capsule))))
    x = Activation(activation="relu")(BatchNormalization()(Dense(500)(x)))
    output = Dense(372, activation="softmax")(x)

    model = Model(inputs=content, outputs=output)
    model = multi_gpu_model(model, 2)
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Пример #13
0
def main():
    #First need to load training data
    training, validation = yolo.loadData((224, 224))
    #print(training[0][0])
    #plt.imshow(training[0][0][0])
    #plt.show()

    #create pre-training model
    model = yolo.preTrainModel(224, 224)
    print(model.summary())
    model = multi_gpu_model(model, gpus=2)
    model.compile(optimizer=yolo.SGD(lr=0.01,
                                     decay=1e-6,
                                     momentum=0.9,
                                     nesterov=True),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    model.fit_generator(training,
                        steps_per_epoch=27644 // 128,
                        epochs=5000,
                        validation_data=validation,
                        validation_steps=2963 // 128)

    #save model
    model.save_weights('preTrainModel.h5')

    return
Пример #14
0
    def __init__(self, n_folds=5, name='BasicModel', config=None):
        if config is None:
            exit('请传入数值')
        self.name = name
        self.config = config
        self.n_class = config.n_class
        # char 特征
        self.char_max_len = config.CHAR_MAXLEN
        self.max_c_features = config.max_c_features
        # word 特征
        self.word_max_len = config.WORD_MAXLEN
        self.max_w_features = config.max_w_features
        self.char_mask_value = self.max_c_features - 2
        self.word_mask_value = self.max_w_features - 2
        self.batch_size = config.BATCH_SIZE

        self.char_embedding = config.char_init_embed
        self.word_embedding = config.word_init_embed
        self.char_embed_size = len(self.char_embedding[0])
        self.word_embed_size = len(self.word_embedding[0])
        self.n_folds = n_folds

        self.kf = KFold(n_splits=n_folds, shuffle=True, random_state=10)
        M = 3  # number of snapshots
        # alpha_zero = 5e-4  # initial learning rate
        # self.snap_epoch = NUM_EPOCHS
        # self.snapshot = SnapshotCallbackBuilder(self.snap_epoch, M, alpha_zero)
        self.last_val_acc = 0.

        self.init_lr = 0.001
        self.lr_schedule = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=1, min_lr=0.000001, verbose=1)

        #  if self.config.option == 6:
            #  self.init_lr = 1e-3
        #  elif self.config.option == 5:
            #  if 'attention' in self.config.model_name:
                #  self.wd = 0.001
            #  if 'textcnn' in self.config.model_name:
                #  self.init_lr = 0.001
                #  self.wd = 0.0015
            #  if 'capsule' in self.config.model_name:
                #  self.init_lr = 0.001
                #  self.wd = 0.003
            #  if 'lstmgru' in self.config.model_name:
                #  self.init_lr = 0.001
        #  elif self.config.option == 4:
            #  self.init_lr = 0.001
        #  elif self.config.option == 3:
            #  self.init_lr = 0.002
            #  # self.poly_decay = self.poly_decay_attention
        #  else:
            #  self.init_lr = 1e-3
        self.snapshot = SnapshotCallbackBuilder(NUM_EPOCHS, M, self.init_lr)
        self.early_stop_monitor = EarlyStopping(patience=5)
        print("[INFO] training with {} GPUs...".format(config.n_gpus))

        self.wd = config.wd
        self.model = self.create_model()
        if config.n_gpus > 1:
            self.model = multi_gpu_model(self.model, gpus=config.n_gpus)
Пример #15
0
def get_text_lstm2(sent_length, embeddings_weight):
    print("get_text_lstm2")
    content = Input(shape=(sent_length, ), dtype='int32')
    embedding = Embedding(name="word_embedding",
                          input_dim=embeddings_weight.shape[0],
                          weights=[embeddings_weight],
                          output_dim=embeddings_weight.shape[1],
                          trainable=False)

    embed = SpatialDropout1D(0.2)(embedding(content))
    x = Dropout(0.2)(Bidirectional(CuDNNLSTM(200,
                                             return_sequences=True))(embed))
    x = Dropout(0.2)(Bidirectional(CuDNNLSTM(100, return_sequences=True))(x))
    semantic = TimeDistributed(Dense(100, activation="tanh"))(x)
    pool_rnn = Lambda(lambda x: K.max(x, axis=1),
                      output_shape=(100, ))(semantic)

    x = Dropout(0.2)(Activation(activation="relu")(BatchNormalization()(
        Dense(1000)(pool_rnn))))
    x = Activation(activation="relu")(BatchNormalization()(Dense(500)(x)))
    output = Dense(372, activation="softmax")(x)

    model = Model(inputs=content, outputs=output)
    model = multi_gpu_model(model, 2)
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Пример #16
0
def get_text_rcnn2(sent_length, embeddings_weight):
    print("get_text_rcnn2")
    content = Input(shape=(None, ), dtype="int32")

    embedding = Embedding(name="word_embedding",
                          input_dim=embeddings_weight.shape[0],
                          weights=[embeddings_weight],
                          output_dim=embeddings_weight.shape[1],
                          trainable=False)

    x = SpatialDropout1D(0.2)(embedding(content))

    x = Convolution1D(filters=256,
                      kernel_size=3,
                      padding='same',
                      strides=1,
                      activation="relu")(x)
    x = MaxPooling1D(pool_size=2)(x)

    x = Dropout(0.2)(CuDNNGRU(units=200, return_sequences=True)(x))
    x = Dropout(0.2)(CuDNNGRU(units=100)(x))

    x = Dropout(0.2)(Activation(activation="relu")(BatchNormalization()(
        Dense(1000)(x))))
    x = Activation(activation="relu")(BatchNormalization()(Dense(500)(x)))
    output = Dense(372, activation="softmax")(x)

    model = Model(inputs=content, outputs=output)
    model = multi_gpu_model(model, 2)
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Пример #17
0
    def predict(self, img, n_bands, n_cls, num_gpus, hparams):
        # Create the model in keras
        if num_gpus == 1:
            model = self._create_inference(
                n_bands, n_cls, hparams)  # initialize the model on the CPU
            model.load_weights('models/Unet/weights/unet_recent.hdf5')
        else:
            with tf.device("/cpu:0"):
                model = self._create_inference(
                    n_bands, n_cls, hparams)  # initialize the model on the CPU
                model.load_weights('models/Unet/weights/unet_recent.hdf5')
            model = multi_gpu_model(
                model, gpus=num_gpus)  # Make it run on multiple GPUs

        # Predict batches of patches
        patches = np.shape(img)[0]  # Total number of patches
        patch_batch_size = 100

        # Do the prediction
        predicted = np.zeros(
            (patches, hparams.patch_size, hparams.patch_size, n_cls))
        for i in range(0, patches, patch_batch_size):
            #print('Processing patch nr.:', i, 'of', patches)
            predicted[i:i + patch_batch_size, :, :, :] = model.predict(
                img[i:i + patch_batch_size, :, :, :])

        return predicted
Пример #18
0
def get_text_gru4(sent_length, embeddings_weight):
    print("get_text_gru4")
    content = Input(shape=(sent_length, ), dtype='int32')
    embedding = Embedding(name="word_embedding",
                          input_dim=embeddings_weight.shape[0],
                          weights=[embeddings_weight],
                          output_dim=embeddings_weight.shape[1],
                          trainable=False)
    x = SpatialDropout1D(0.2)(embedding(content))

    x = Bidirectional(CuDNNLSTM(200, return_sequences=True))(x)
    x = Bidirectional(CuDNNGRU(200, return_sequences=True))(x)

    avg_pool = GlobalAveragePooling1D()(x)
    max_pool = GlobalMaxPooling1D()(x)

    x = concatenate([avg_pool, max_pool])

    x = Dropout(0.2)(Activation(activation="relu")(BatchNormalization()(
        Dense(1000)(x))))
    x = Activation(activation="relu")(BatchNormalization()(Dense(500)(x)))
    output = Dense(372, activation="softmax")(x)

    model = Model(inputs=content, outputs=output)
    model = multi_gpu_model(model, 2)
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Пример #19
0
def build_network(num_gpu=1, input_shape=None):
    inputs = Input(shape=input_shape, name='input')

    conv1 = Conv2D(4, kernel_size=(3, 3), activation='relu',
                   name='conv_1')(inputs)
    batch1 = BatchNormalization(name='batch_norm_1')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2), name='pool_1')(batch1)

    conv2 = Conv2D(4, kernel_size=(3, 3), activation='relu',
                   name='conv_2')(inputs)
    batch2 = BatchNormalization(name='batch_norm_2')(conv1)
    pool2 = MaxPooling2D(pool_size=(2, 2), name='pool_2')(batch1)

    flatten = Flatten()(pool2)
    fc1 = Dense(512, activation='relu', name='fc1')(flatten)
    d1 = Dropout(rate=0.2, name='dropout1')(fc1)
    fc2 = Dense(256, activation='relu', name='fc2')(d1)
    d2 = Dropout(rate=0.2, name='dropout2')(fc2)

    ouput = Dense(10, activation='softmax', name='softmax')

    model = Model(inputs=inputs, outputs=ouput)

    if num_gpu > 1:
        model = multi_gpu_model(model, num_gpu)
        model.compile(optimizer='adam',
                      loss='categorical_crossentropy',
                      metrice=['accuracy'])
    return model
    def setup_multi_gpu(model):

        import tensorflow as tf
        from keras.utils.training_utils import multi_gpu_model
        from tensorflow.python.client import device_lib

        # IMPORTANT: Tells tf to not occupy a specific amount of memory
        from keras.backend.tensorflow_backend import set_session

        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True  # dynamically grow the memory used on the GPU
        sess = tf.Session(config=config)
        set_session(
            sess
        )  # set this TensorFlow session as the default session for Keras.

        print('reading gpus avaliable..')
        local_device_protos = device_lib.list_local_devices()
        avail_gpus = [
            x.name for x in local_device_protos if x.device_type == 'GPU'
        ]
        num_gpu = len(avail_gpus)
        print('Amount of GPUs available: %s' % num_gpu)

        multi_model = multi_gpu_model(model, gpus=num_gpu)

        return multi_model
def compile_model(args, train_list, net_input_shape, uncomp_model):
    # Set optimizer to Adam
    try:
        opt = Adam(lr=args.initial_lr, beta_1=0.99, beta_2=0.999, decay=1e-6, amsgrad=True)
    except:
        opt = Adam(lr=args.initial_lr, beta_1=0.99, beta_2=0.999, decay=1e-6)

    # A set of useful metrics
    metrics = [tf.keras.metrics.Precision(), tf.keras.metrics.Recall(), tf.keras.metrics.AUC(curve='PR'),
               tf.keras.metrics.AUC(curve='ROC')]

    if args.num_classes > 2:
        metrics.append(categorical_accuracy)
    else:
        metrics.append(binary_accuracy)

    # Get the loss function and weights
    loss, loss_weighting = get_loss(training_list=train_list, net=args.net, choice=args.loss)

    # If using CPU or single GPU, compile the model with the chosen loss, optimizer, and metrics
    if args.gpus <= 1:
        uncomp_model.compile(optimizer=opt, loss=loss, metrics=metrics)
        return uncomp_model, loss_weighting
    # If using multiple GPUs, compile the model with the chosen loss, optimizer, and metrics
    else:
        from keras.utils.training_utils import multi_gpu_model
        with tf.device("/cpu:0"):
            uncomp_model.compile(optimizer=opt, loss=loss, metrics=metrics)
            model = multi_gpu_model(uncomp_model, gpus=args.gpus)
            model.__setattr__('callback_model', uncomp_model)
        model.compile(optimizer=opt, loss=loss, metrics=metrics)
        return model, loss_weighting
Пример #22
0
def load_model(config, model_section=None, weights_file=None, number_gpus=1):

    img_width, img_height,\
    batch_size, epochs, \
    training_steps_per_epoch, validation_steps_per_epoch,\
    positive_weight, model_name = get_metadata_model(config, model_section)

    model = pretrained_models.model_definition(model_name,
                                               (img_width, img_height, 3))

    #Adding custom Layers
    x = model.output
    x = Flatten()(x)
    x = Dense(1024,
              activation="relu",
              kernel_regularizer=regularizers.l2(0.01))(x)
    x = Dropout(0.5)(x)
    x = Dense(1024,
              activation="relu",
              kernel_regularizer=regularizers.l2(0.01))(x)
    predictions = Dense(2, activation="softmax")(x)

    # creating the final model
    model_final = Model(input=model.input, output=predictions)
    if (number_gpus > 1):
        model_final = multi_gpu_model(model, gpus=number_gpus)

    if weights_file is not None:
        model_final.load_weights(weights_file)

    return model_final
Пример #23
0
    def train(self, gpus):
        # async datasets saver might be running, wait before training
        self.dataset.saver.wait()

        # train
        if self.model is None:
            self.model = self.logic.builder(True)

        to_train = self.model
        if gpus > 1:
            log.info("training with %d GPUs", gpus)
            to_train = multi_gpu_model(self.model, gpus=gpus)

        self.history = self.logic.trainer(to_train, self.dataset).history
        self.accu = self.accuracy()

        print("")
        self._out_stats(sys.stdout)

        # save model structure and weights
        self._save_model()
        # save training history
        self._save_history()
        # save model accuracy statistics
        self._save_stats()
Пример #24
0
def get_text_rcnn5(sent_length, embeddings_weight):
    print("get_text_rcnn5")
    content = Input(shape=(sent_length, ), dtype='int32')
    embedding = Embedding(name="word_embedding",
                          input_dim=embeddings_weight.shape[0],
                          weights=[embeddings_weight],
                          output_dim=embeddings_weight.shape[1],
                          trainable=False)

    embed = SpatialDropout1D(0.2)(embedding(content))

    rnn_1 = Bidirectional(CuDNNGRU(200, return_sequences=True))(embed)
    rnn_2 = Bidirectional(CuDNNGRU(200, return_sequences=True))(rnn_1)
    x = concatenate([rnn_1, rnn_2], axis=2)

    last = Lambda(lambda t: t[:, -1], name='last')(x)
    maxpool = GlobalMaxPooling1D()(x)
    attn = AttentionWeightedAverage()(x)
    average = GlobalAveragePooling1D()(x)

    x = concatenate([last, maxpool, average, attn])

    x = Dropout(0.2)(Activation(activation="relu")(BatchNormalization()(
        Dense(1000)(x))))
    x = Activation(activation="relu")(BatchNormalization()(Dense(500)(x)))
    output = Dense(372, activation="softmax")(x)

    model = Model(inputs=content, outputs=output)
    model = multi_gpu_model(model, 2)
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Пример #25
0
def compile_model(args, net_input_shape, uncomp_model):
    # Set optimizer loss and metrics
    opt = Adam(lr=args.initial_lr, beta_1=0.99, beta_2=0.999, decay=1e-6)
    if args.net.find('caps') != -1:
        metrics = {'out_seg': dice_hard}
    else:
        metrics = [dice_hard]

    loss, loss_weighting = get_loss(root=args.data_root_dir,
                                    split=args.split_num,
                                    net=args.net,
                                    recon_wei=args.recon_wei,
                                    choice=args.loss)

    # If using CPU or single GPU
    if args.gpus <= 1:
        uncomp_model.compile(optimizer=opt,
                             loss=loss,
                             loss_weights=loss_weighting,
                             metrics=metrics)
        return uncomp_model
    # If using multiple GPUs
    else:
        with tf.device("/cpu:0"):
            uncomp_model.compile(optimizer=opt,
                                 loss=loss,
                                 loss_weights=loss_weighting,
                                 metrics=metrics)
            model = multi_gpu_model(uncomp_model, gpus=args.gpus)
            model.__setattr__('callback_model', uncomp_model)
        model.compile(optimizer=opt,
                      loss=loss,
                      loss_weights=loss_weighting,
                      metrics=metrics)
        return model
Пример #26
0
def get_text_rcnn1(sent_length, embeddings_weight):
    print("get_text_rcnn1")
    document = Input(shape=(None, ), dtype="int32")

    embedder = Embedding(name="word_embedding",
                         input_dim=embeddings_weight.shape[0],
                         weights=[embeddings_weight],
                         output_dim=embeddings_weight.shape[1],
                         trainable=False)

    doc_embedding = SpatialDropout1D(0.2)(embedder(document))
    forward = Bidirectional(CuDNNLSTM(200,
                                      return_sequences=True))(doc_embedding)
    together = concatenate([forward, doc_embedding], axis=2)

    semantic = Conv1D(100, 2, padding='same', strides=1,
                      activation='relu')(together)
    pool_rnn = Lambda(lambda x: K.max(x, axis=1),
                      output_shape=(100, ))(semantic)

    x = Dropout(0.2)(Activation(activation="relu")(BatchNormalization()(
        Dense(1000)(pool_rnn))))
    x = Activation(activation="relu")(BatchNormalization()(Dense(500)(x)))
    output = Dense(372, activation="softmax")(x)

    model = Model(inputs=document, outputs=output)
    model = multi_gpu_model(model, 2)
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
def create_model(lr,
                 hidden_size,
                 drop_prob,
                 conv_type='normal',
                 conv_depth1=32,
                 conv_depth2=32):

    K.clear_session()  ## necessary because otherwise GPU memory will run full
    drop_prob_1 = drop_prob
    drop_prob_2 = drop_prob

    # use the specified convolution building block
    conv_block = conv_blocks[conv_type]

    inp = Input(shape=(
        height, width,
        depth))  # depth goes last in TensorFlow back-end (first in Theano)

    conv_1 = conv_block(conv_depth_1, kernel_size, inp)
    conv_2 = conv_block(conv_depth_1, kernel_size, conv_1)

    pool_1 = MaxPooling2D(pool_size=(pool_size, pool_size))(conv_2)
    drop_1 = Dropout(drop_prob_1)(pool_1)

    conv_3 = conv_block(conv_depth_2, kernel_size, drop_1)
    conv_4 = conv_block(conv_depth_2, kernel_size, conv_3)

    pool_2 = MaxPooling2D(pool_size=(pool_size, pool_size))(conv_4)
    drop_2 = Dropout(drop_prob_1)(pool_2)

    flat = Flatten()(drop_2)

    # # add second input for dayofyear
    # input2_size=1
    # input2 = Input(shape=[input2_size])
    #
    # merged = concatenate([flat,input2])

    hidden = Dense(hidden_size, activation='softmax')(flat)
    drop_3 = Dropout(drop_prob_2)(hidden)

    ## our output layer has just one neuron (because we do regression, not classification
    ## the last neuron should be linear for regression (https://www.reddit.com/r/MachineLearning/comments/4ebh0f/question_neural_networks_for_regression/)
    out = Dense(1, activation='linear')(drop_3)

    # we will use parallel GPUS for the training, therefore the setup has to be done using
    # the cpu
    with tf.device("/cpu:0"):
        model = Model(inputs=inp, outputs=out)
        # convert the model to a model that can be trained with N_GPU GPUs

        model = multi_gpu_model(model, gpus=N_GPU)

    print('compiling model')
    model.compile(loss=loss,
                  optimizer=optimizers.adam(lr=lr),
                  metrics=[correlation_coefficient_loss, 'MSE'])

    return model
Пример #28
0
def main(argv=None):
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu_list

    # check if checkpoint path exists
    if not os.path.exists(FLAGS.checkpoint_path):
        os.mkdir(FLAGS.checkpoint_path)
    else:
        #if not FLAGS.restore:
        #    shutil.rmtree(FLAGS.checkpoint_path)
        #    os.mkdir(FLAGS.checkpoint_path)
        shutil.rmtree(FLAGS.checkpoint_path)
        os.mkdir(FLAGS.checkpoint_path)

    train_data_generator = data_processor.generator(FLAGS)
    train_samples_count = data_processor.count_samples(FLAGS)

    val_data = data_processor.load_data(FLAGS)

    if len(gpus) <= 1:
        print('Training with 1 GPU')

        if FLAGS.drn:
            east = EAST_DRN_model(input_size=FLAGS.input_size)
        else:
            east = EAST_model(FLAGS.input_size)
            
        parallel_model = east.model
    else:
        print('Training with %d GPUs' % len(gpus))
        with tf.device("/cpu:0"):
            east = EAST_model(FLAGS.input_size)
        if FLAGS.restore_model is not '':
            east.model.load_weights(FLAGS.restore_model)
        parallel_model = multi_gpu_model(east.model, gpus=len(gpus))

    score_map_loss_weight = K.variable(0.01, name='score_map_loss_weight')

    small_text_weight = K.variable(0., name='small_text_weight')

    lr_scheduler = LearningRateScheduler(lr_decay)
    ckpt = CustomModelCheckpoint(model=east.model, path=FLAGS.checkpoint_path + '/model-{epoch:02d}.h5', period=FLAGS.save_checkpoint_epochs, save_weights_only=True)
    tb = CustomTensorBoard(log_dir=FLAGS.checkpoint_path + '/train', score_map_loss_weight=score_map_loss_weight, small_text_weight=small_text_weight, data_generator=train_data_generator, write_graph=True)
    small_text_weight_callback = SmallTextWeight(small_text_weight)
    validation_evaluator = ValidationEvaluator(val_data, validation_log_dir=FLAGS.checkpoint_path + '/val')
    callbacks = [lr_scheduler, ckpt, tb, small_text_weight_callback, validation_evaluator]
    opt = AdamW(FLAGS.init_learning_rate)

    parallel_model.compile(loss=[dice_loss(east.overly_small_text_region_training_mask, east.text_region_boundary_training_mask, score_map_loss_weight, small_text_weight),
                                 rbox_loss(east.overly_small_text_region_training_mask, east.text_region_boundary_training_mask, small_text_weight, east.target_score_map)],
                           loss_weights=[1., 1.],
                           optimizer=opt)
    east.model.summary()

    model_json = east.model.to_json()
    with open(FLAGS.checkpoint_path + '/model.json', 'w') as json_file:
        json_file.write(model_json)

    history = parallel_model.fit_generator(train_data_generator, epochs=FLAGS.max_epochs, steps_per_epoch=train_samples_count/FLAGS.batch_size, workers=FLAGS.nb_workers, use_multiprocessing=True, callbacks=callbacks, verbose=1)
Пример #29
0
 def build_model(self, input_shape, opt, loss_fn):
     self.model = get_vannila_rnn_model()
     if self.gpus <= 1:
         self.model.summary()
         self.model.compile(optimizer=opt, loss=loss_fn)
     else:
         with tf.device("/cpu:0"):
             self.gpu_model = multi_gpu_model(self.model, gpus=self.gpus)
             self.gpu_model.compile(optimizer=opt, loss=loss_fn)
Пример #30
0
def get_text_cnn2(sent_length, embeddings_weight):
    print("get_text_cnn2")
    content = Input(shape=(sent_length, ), dtype='int32')
    embedding = Embedding(name="word_embedding",
                          input_dim=embeddings_weight.shape[0],
                          weights=[embeddings_weight],
                          output_dim=embeddings_weight.shape[1],
                          trainable=False)
    embed = embedding(content)
    filter_sizes = [1, 2, 3, 4]
    num_filters = 128
    embed_size = embeddings_weight.shape[1]

    x = SpatialDropout1D(0.2)(embed)
    x = Reshape((sent_length, embed_size, 1))(x)

    conv_0 = Conv2D(num_filters,
                    kernel_size=(filter_sizes[0], embed_size),
                    kernel_initializer='normal',
                    activation='elu')(x)
    conv_1 = Conv2D(num_filters,
                    kernel_size=(filter_sizes[1], embed_size),
                    kernel_initializer='normal',
                    activation='elu')(x)
    conv_2 = Conv2D(num_filters,
                    kernel_size=(filter_sizes[2], embed_size),
                    kernel_initializer='normal',
                    activation='elu')(x)
    conv_3 = Conv2D(num_filters,
                    kernel_size=(filter_sizes[3], embed_size),
                    kernel_initializer='normal',
                    activation='elu')(x)

    maxpool_0 = MaxPool2D(pool_size=(sent_length - filter_sizes[0] + 1,
                                     1))(conv_0)
    maxpool_1 = MaxPool2D(pool_size=(sent_length - filter_sizes[1] + 1,
                                     1))(conv_1)
    maxpool_2 = MaxPool2D(pool_size=(sent_length - filter_sizes[2] + 1,
                                     1))(conv_2)
    maxpool_3 = MaxPool2D(pool_size=(sent_length - filter_sizes[3] + 1,
                                     1))(conv_3)

    z = Concatenate(axis=1)([maxpool_0, maxpool_1, maxpool_2, maxpool_3])
    z = Flatten()(z)
    z = Dropout(0.1)(z)

    x = Dropout(0.2)(Activation(activation="relu")(BatchNormalization()(
        Dense(1000)(z))))
    x = Activation(activation="relu")(BatchNormalization()(Dense(500)(x)))
    output = Dense(372, activation="softmax")(x)

    model = Model(inputs=content, outputs=output)
    model = multi_gpu_model(model, 2)
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Пример #31
0
def compile_model(args, net_input_shape, uncomp_model):
    # Set optimizer loss and metrics
    opt = Adam(lr=args.initial_lr, beta_1=0.99, beta_2=0.999, decay=1e-6)
    if args.net.find('caps') != -1:
        metrics = {'out_seg': dice_hard}
    else:
        metrics = [dice_hard]

    loss, loss_weighting = get_loss(root=args.data_root_dir, split=args.split_num, net=args.net,
                                    recon_wei=args.recon_wei, choice=args.loss)

    # If using CPU or single GPU
    if args.gpus <= 1:
        uncomp_model.compile(optimizer=opt, loss=loss, metrics=metrics)
        return uncomp_model
    # If using multiple GPUs
    else:
        with tf.device("/cpu:0"):
            uncomp_model.compile(optimizer=opt, loss=loss, loss_weights=loss_weighting, metrics=metrics)
            model = multi_gpu_model(uncomp_model, gpus=args.gpus)
            model.__setattr__('callback_model', uncomp_model)
        model.compile(optimizer=opt, loss=loss, loss_weights=loss_weighting, metrics=metrics)
        return model
Пример #32
0
    target_size=SIZE, color_mode='grayscale',
    batch_size=BATCH_SIZE, shuffle=True
)

valid_datagen = SegmentationGenerator()

valid_iter = valid_datagen.flow_from_directory(
    DATA_PATHS['TEST']['IMAGES'],
    DATA_PATHS['TEST']['MASKS'],
    target_size=SIZE, color_mode='grayscale',
    batch_size=BATCH_SIZE, shuffle=False
)

model = segnet.build_model(*SIZE, n_channels=1)
if N_GPUS >= 2:
    model = multi_gpu_model(model, N_GPUS)
    
optimizer = SGD(lr=0.001, momentum=0.9, decay=0.0005, nesterov=False)
model.compile(loss=dice_coef_loss, optimizer=optimizer, metrics=[dice])

model_idx = 0

experiment_dir = 'segnet_{}x{}'.format(*SIZE)
models_dir = join(experiment_dir, 'models')
log_dir = join(experiment_dir, 'log')
if not exists(models_dir):
    makedirs(models_dir)
if not exists(log_dir):
    makedirs(log_dir)
modelname = 'segnet' + str(model_idx)
modelpath = join(models_dir, modelname)