Ejemplo n.º 1
0
# Pre-compute propagation
for i in range(K - 1):
    fltr = fltr.dot(fltr)
fltr.sort_indices()

# Model definition
X_in = Input(shape=(F, ))
fltr_in = Input((N, ), sparse=True)
output = GraphConv(n_classes,
                   activation='softmax',
                   kernel_regularizer=l2(l2_reg),
                   use_bias=False)([X_in, fltr_in])

# Build model
model = Model(inputs=[X_in, fltr_in], outputs=output)
optimizer = Adam(lr=learning_rate)
model.compile(optimizer=optimizer,
              loss='categorical_crossentropy',
              weighted_metrics=['acc'])
model.summary()

# Train model
validation_data = ([X, fltr], y, val_mask)
model.fit(
    [X, fltr],
    y,
    sample_weight=train_mask,
    epochs=epochs,
    batch_size=N,
    validation_data=validation_data,
    shuffle=False,  # Shuffling data means shuffling the whole graph
Ejemplo n.º 2
0
    def train(self):
        train_img_list = []
        train_label_list = []
        for file in os.listdir(self.train_folder):
            files_img_in_array = self.read_train_images(filename=file)
            train_img_list.append(files_img_in_array)  #Image list add up
            train_label_list.append(int(file.split('_')[0]))
        print(train_label_list)  #lable list addup
        train_label_list = to_categorical(train_label_list, 4)
        train_img_list = np.array(train_img_list)
        train_label_list = np.array(train_label_list)
        print(train_label_list)

        #train_label_list = to_categorical(train_label_list,4) #format into binary [0,0,0,0,1,0,0]

        train_img_list = train_img_list.astype('float32')
        train_img_list /= 255

        #-- setup Neural network CNN
        model = Sequential()
        #CNN Layer - 1
        model.add(
            Convolution2D(
                filters=32,  #Output for next later layer output (100,100,32)
                kernel_size=(5, 5),  #size of each filter in pixel
                padding='same',  #边距处理方法 padding method
                input_shape=(100, 100,
                             3),  #input shape ** channel last(TensorFlow)
            ))
        model.add(Activation('relu'))
        model.add(
            MaxPooling2D(
                pool_size=(2, 2),  #Output for next layer (50,50,32)
                strides=(2, 2),
                padding='same',
            ))

        #CNN Layer - 2
        model.add(
            Convolution2D(
                filters=64,  #Output for next layer (50,50,64)
                kernel_size=(2, 2),
                padding='same',
            ))
        model.add(Activation('relu'))
        model.add(
            MaxPooling2D(  #Output for next layer (25,25,64)
                pool_size=(2, 2),
                strides=(2, 2),
                padding='same',
            ))

        #Fully connected Layer -1
        model.add(Flatten())
        model.add(Dense(1024))
        model.add(Activation('relu'))
        # Fully connected Layer -2
        model.add(Dense(512))
        model.add(Activation('relu'))
        # Fully connected Layer -3
        model.add(Dense(256))
        model.add(Activation('relu'))
        # Fully connected Layer -4
        model.add(Dense(self.categories))
        model.add(Activation('softmax'))
        # Define Optimizer
        adam = Adam(lr=0.0001)
        #Compile the model
        model.compile(optimizer=adam,
                      loss="categorical_crossentropy",
                      metrics=['accuracy'])
        # Fire up the network
        model.fit(
            train_img_list,
            train_label_list,
            epochs=self.number_batch,
            batch_size=self.batch_size,
            verbose=1,
        )
        #SAVE your work -model
        model.save('./cellfinder.h5')
Ejemplo n.º 3
0
    x = Dense(units)(x)
    x = LeakyReLU(alpha=0.2)(x)
    x = Reshape((32, 5, 8))(x)
    x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
    x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
    x = UpSampling2D((2, 2))(x)
    x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)
    x = Conv2D(128, (3, 3), activation='relu', padding='same')(x)
    x = UpSampling2D((2, 2))(x)
    x = Conv2D(256, (3, 3), activation='relu', padding='same')(x)
    x = Conv2D(1, (3, 3), activation='tanh', padding='same')(x)
    return x

# Define model training hyperparameters
autoencoder_train = Model(input_img, autoencoder(input_img))
autoencoder_train.compile(loss='mean_squared_error', optimizer = Adam())
autoencoder_train.summary()

# Create a file to save parameter weights
weightname = 'model_weights.h5'
model_saver = tf.keras.callbacks.ModelCheckpoint(weightname ,monitor='val_loss', verbose=1,save_best_only=True, save_weights_only=False, mode='auto')

# Train the Autoencoder
autoencoder_train.load_weights(weightname)
H = autoencoder_train.fit(x_train, x_train, validation_data=(x_test, x_test), epochs=EPOCHS, batch_size=BS, callbacks = [model_saver])

"""#Plotting original and extracted images"""

# Make a prediction using the validation set
extract = autoencoder_train.predict(x_test)
loss = H.history['loss']
Ejemplo n.º 4
0
movie = Input(shape=(1,))
m = EmbeddingLayer(n_movies, n_factors)(movie)

x = Concatenate()([u, m])
x = Dropout(0.05)(x)

x = Dense(10, kernel_initializer='he_normal')(x)
x = Activation('relu')(x)
x = Dropout(0.5)(x)

x = Dense(1, kernel_initializer='he_normal')(x)
x = Activation('sigmoid')(x)
x = Lambda(lambda x: x * (max_rating - min_rating) + min_rating)(x)

model = Model(inputs=[user, movie], outputs=x)
opt = Adam(lr=0.001)
model.compile(loss='mean_squared_error', optimizer=opt)

print(model.summary())

history = model.fit(x=X_train_array, y=y_train, batch_size=64, epochs=5, verbose=1,
                    validation_data=(X_test_array, y_test))

plt.xlabel("Epoch Number")
plt.ylabel("Loss Magnidute")
plt.plot(history.history['loss'])
plt.show()

print('user: '******'movie: ', X_test[:10, 1])
print('rate: ', y_test[:10])
Ejemplo n.º 5
0
# Set channel
channel = x_train.shape[-1]

# It is suggested to use [-1, 1] input for GAN training
x_train = (x_train.astype('float32') - 127.5) / 127.5
x_test = (x_test.astype('float32') - 127.5) / 127.5

# Get image size
img_size = x_train[0].shape

# Get number of classes
n_classes = len(np.unique(y_train))

# %% ---------------------------------- Hyperparameters ----------------------------------------------------------------

optimizer = Adam(lr=0.0002, beta_1=0.5, beta_2=0.9)
latent_dim = 32

# trainRatio === times(Train D) / times(Train G)
# trainRatio = 5


# %% ---------------------------------- Models Setup -------------------------------------------------------------------
# Build Generator with mlp
def generator_fc():
    noise = Input(shape=(latent_dim, ))
    x = Dense(128, kernel_initializer=weight_init)(noise)
    x = BatchNormalization()(x)
    x = LeakyReLU(0.2)(x)
    x = Dense(256, kernel_initializer=weight_init)(x)
    x = BatchNormalization()(x)
Ejemplo n.º 6
0
model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(26, activation='softmax'))

# If you want to train the same model or try other models, go for this
if mode == "train":
    model.compile(loss='categorical_crossentropy',
                  optimizer=Adam(lr=0.0001, decay=1e-6),
                  metrics=['accuracy'])

    model_info = model.fit_generator(train_generator,
                                     steps_per_epoch=num_train // batch_size,
                                     epochs=num_epoch,
                                     validation_data=validation_generator,
                                     validation_steps=num_val // batch_size)

    model.save_weights('model_mnist.h5')

# emotions will be displayed on your face from the webcam feed
elif mode == "display":
    model.load_weights('model_mnist.h5')

    # prevents openCL usage and unnecessary logging messages
print(model.summary())

#Parâmetros:
#kernel_size: indica o tamanho da matriz de detector de características (feature detector)
#input_shape: indica o tamanho da imagem a ser utilizada no treinamento
#data_format='channels_last': parâmetro padrão que indica que será adicionada mais uma dimensão da imagem no final da matriz
#kernel_regularizer: indica que irá adicionar uma penalidade quando o erro tiver um determinado valo
"""## Etapa 8 - Compilando o modelo
*   Parâmetros Adam:  https://arxiv.org/abs/1412.6980
*   Artigo sobre parâmetros Adam: https://machinelearningmastery.com/adam-optimization-algorithm-for-deep-learning/ 
*   Parâmetros beta: representam a taxa de decaimento exponencial (por exemplo, 0.9) que está relacionado com a taxa de aprendizagem (learning rate - lr)
"""

#Compilando o modelo
model.compile(loss=categorical_crossentropy,
              optimizer=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-7),
              metrics=['accuracy'])
arquivo_modelo = 'modelo_02_expressoes.h5'
arquivo_modelo_json = 'modelo_02_expressoes.json'
lr_reducer = ReduceLROnPlateau(monitor='val_loss',
                               factor=0.9,
                               patience=3,
                               verbose=1)
early_stopper = EarlyStopping(monitor='val_loss',
                              min_delta=0,
                              patience=8,
                              verbose=1,
                              mode='auto')
checkpointer = ModelCheckpoint(arquivo_modelo,
                               monitor='val_loss',
                               verbose=1,
Ejemplo n.º 8
0
def main(go_file, train_data_file, test_data_file, terms_file, model_file,
         out_file, split, batch_size, epochs, load, logger_file, threshold,
         device, params_index):
    params = {
        'max_kernel': 129,
        'initializer': 'glorot_normal',
        'dense_depth': 0,
        'nb_filters': 512,
        'optimizer': Adam(lr=3e-4),
        'loss': 'binary_crossentropy'
    }
    # SLURM JOB ARRAY INDEX
    pi = params_index
    if params_index != -1:
        kernels = [33, 65, 129, 257, 513]
        dense_depths = [0, 1, 2]
        nb_filters = [32, 64, 128, 256, 512]
        params['max_kernel'] = kernels[pi % 5]
        pi //= 5
        params['dense_depth'] = dense_depths[pi % 3]
        pi //= 3
        params['nb_filters'] = nb_filters[pi % 5]
        pi //= 5
        out_file = f'data/predictions_{params_index}.pkl'
        logger_file = f'data/training_{params_index}.csv'
        model_file = f'data/model_{params_index}.h5'
    print('Params:', params)

    go = Ontology(go_file, with_rels=True)
    terms_df = pd.read_pickle(terms_file)
    terms = terms_df['terms'].values.flatten()

    train_df, valid_df = load_data(train_data_file, terms, split)
    test_df = pd.read_pickle(test_data_file)
    terms_dict = {v: i for i, v in enumerate(terms)}
    nb_classes = len(terms)

    with tf.device('/' + device):
        test_steps = int(math.ceil(len(test_df) / batch_size))
        test_generator = DFGenerator(test_df, terms_dict, nb_classes,
                                     batch_size)
        if load:
            logging.info('Loading pretrained model')
            model = load_model(model_file)
        else:
            logging.info('Creating a new model')
            model = create_model(nb_classes, params)

            logging.info("Training data size: %d" % len(train_df))
            logging.info("Validation data size: %d" % len(valid_df))
            checkpointer = ModelCheckpoint(filepath=model_file,
                                           verbose=1,
                                           save_best_only=True)
            earlystopper = EarlyStopping(monitor='val_loss',
                                         patience=6,
                                         verbose=1)
            logger = CSVLogger(logger_file)

            logging.info('Starting training the model')

            valid_steps = int(math.ceil(len(valid_df) / batch_size))
            train_steps = int(math.ceil(len(train_df) / batch_size))
            train_generator = DFGenerator(train_df, terms_dict, nb_classes,
                                          batch_size)
            valid_generator = DFGenerator(valid_df, terms_dict, nb_classes,
                                          batch_size)

            model.summary()
            model.fit_generator(train_generator,
                                steps_per_epoch=train_steps,
                                epochs=epochs,
                                validation_data=valid_generator,
                                validation_steps=valid_steps,
                                max_queue_size=batch_size,
                                workers=12,
                                callbacks=[logger, checkpointer, earlystopper])
            logging.info('Loading best model')
            model = load_model(model_file)

        logging.info('Evaluating model')
        loss = model.evaluate_generator(test_generator, steps=test_steps)
        logging.info('Test loss %f' % loss)
        logging.info('Predicting')
        valid_generator.reset()
        preds = model.predict_generator(valid_generator, steps=valid_steps)
labels = [
          'airplane',
          'automobile',
          'bird',
          'cat',
          'deer',
          'dog',
          'frog',
          'horse',
          'ship',
          'truck'
          ]


### Cell 8 ###
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=LR, decay=DECAY), metrics=['accuracy'])

print(type(X_train))
print(type(Y_train))
print(type(X_valid))
print(type(Y_valid))


print(len(Y_train[0]))
Y_train[0]


### Cell 10 ###
history = model.fit(X_train / 255.0 , to_categorical(Y_train), batch_size=BATCH_SIZE, shuffle=True, epochs=250,
                    validation_data=(X_valid/255.0, to_categorical(Y_valid)))
    def build_model(self, hp):
        """
            Model is responsible of building an RNN model with the decided parameters
        :param hp: Keras Tuner Parameters
        :return: Sequential Recurrent Neural Network Model
        """
        self.log_event('    -> Creating a model.')
        model = Sequential()

        model.add(self.add_mask_layer())

        # Add One LSTM Layer with Batch Normalization
        model.add(
            LSTM(units=hp.Int(
                'first_layer',
                min_value=self.hyper_parameters['lstm_units']['min'],
                max_value=self.hyper_parameters['lstm_units']['max'],
                step=self.hyper_parameters['lstm_units']['step']),
                 return_sequences=True,
                 dropout=self.hyper_parameters['lstm_layer_dropout'],
                 recurrent_dropout=0.1,
                 activation=self.hyper_parameters['lstm_layer_activation']))

        model.add(BatchNormalization())

        # Add Dropout
        model.add(
            Dropout(
                hp.Choice('dropout_one',
                          values=self.hyper_parameters['dropout'])))

        # Add the second LSTM Layer with Batch Normalization
        model.add(
            LSTM(units=hp.Int(
                'second_layer',
                min_value=self.hyper_parameters['lstm_units']['min'],
                max_value=self.hyper_parameters['lstm_units']['max'],
                step=self.hyper_parameters['lstm_units']['step']),
                 return_sequences=False,
                 dropout=self.hyper_parameters['lstm_layer_dropout'],
                 recurrent_dropout=0.1,
                 activation=self.hyper_parameters['lstm_layer_activation']))

        model.add(BatchNormalization())

        # Add Dropout
        model.add(
            Dropout(
                hp.Choice('dropout_one',
                          values=self.hyper_parameters['dropout'])))

        # Add Output Layer
        model.add(
            Dense(self.number_of_distinct_items,
                  activation=self.hyper_parameters['dense_activation']))

        # Compile the model
        opt = Adam(
            hp.Choice('learning_rate',
                      values=self.hyper_parameters['learning_rate']))

        model.compile(loss=self.hyper_parameters['loss'],
                      optimizer=opt,
                      metrics=self.hyper_parameters['metric'])

        self.log_event('    -> Returning the model.')
        return model
Ejemplo n.º 11
0
                decay_rate=0.92,
                staircase=True)
            optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)

            for epoch in range(Init_Epoch, Freeze_Epoch):
                fit_one_epoch(model, loss, optimizer, epoch, epoch_size,
                              epoch_size_val, gen, gen_val, Freeze_Epoch,
                              get_train_step_fn())
        else:
            gen = Generator(Batch_size, train_lines, inputs_size, num_classes,
                            dataset_path).generate()
            gen_val = Generator(Batch_size, val_lines, inputs_size,
                                num_classes, dataset_path).generate(False)

            model.compile(loss=dice_loss_with_CE() if dice_loss else CE(),
                          optimizer=Adam(lr=lr),
                          metrics=[f_score()])

            model.fit_generator(gen,
                                steps_per_epoch=epoch_size,
                                validation_data=gen_val,
                                validation_steps=epoch_size_val,
                                epochs=Freeze_Epoch,
                                initial_epoch=Init_Epoch,
                                callbacks=[
                                    checkpoint_period, reduce_lr,
                                    early_stopping, tensorboard, loss_history
                                ])

    for i in range(freeze_layers):
        model.layers[i].trainable = True
Ejemplo n.º 12
0
    def create_model(self,
                     filters_number,
                     network_depth=6,
                     use_residual=True,
                     use_bottleneck=True,
                     max_kernel_size=20,
                     learning_rate=0.01,
                     regularization_rate=0.0):
        """
        Generate a InceptionTime model. See Fawaz et al. 2019.

        The compiled Keras model is returned.

        Parameters
        ----------
        input_shape : tuple
            Shape of the input dataset: (num_samples, num_timesteps, num_channels)
        class_number : int
            Number of classes for classification task
        filters_number : int
            number of filters for each convolutional layer
        network_depth : int
            Depth of network, i.e. number of Inception modules to stack.
        use_residual: bool
            If =True, then residual connections are used. Default is True.
        use_bottleneck: bool
            If=True, bottleneck layer is used at the entry of Inception modules.
            Default is true.
        max_kernel_size: int,
            Maximum kernel size for convolutions within Inception module.
        learning_rate : float
            learning rate
        regularization_rate: float
            regularization rate

        Returns
        -------
        model : Keras model
            The compiled Keras model
        """
        dim_length = self.x_shape[1]  # number of samples in a time series
        dim_channels = self.x_shape[2]  # number of channels
        weightinit = 'lecun_uniform'  # weight initialization
        bottleneck_size = 32

        def inception_module(input_tensor, stride=1, activation='linear'):

            if use_bottleneck and int(input_tensor.shape[-1]) > 1:
                input_inception = Conv1D(filters=bottleneck_size,
                                         kernel_size=1,
                                         padding='same',
                                         activation=activation,
                                         kernel_initializer=weightinit,
                                         use_bias=False)(input_tensor)
            else:
                input_inception = input_tensor

            kernel_sizes = [max_kernel_size // (2**i) for i in range(3)]
            conv_list = []

            for kernel_size in kernel_sizes:
                conv_list.append(
                    Conv1D(filters=filters_number,
                           kernel_size=kernel_size,
                           strides=stride,
                           padding='same',
                           activation=activation,
                           kernel_initializer=weightinit,
                           use_bias=False)(input_inception))

            max_pool_1 = MaxPool1D(pool_size=3, strides=stride,
                                   padding='same')(input_tensor)

            conv_last = Conv1D(filters=filters_number,
                               kernel_size=1,
                               padding='same',
                               activation=activation,
                               kernel_initializer=weightinit,
                               use_bias=False)(max_pool_1)

            conv_list.append(conv_last)

            x = Concatenate(axis=2)(conv_list)
            x = BatchNormalization()(x)
            x = Activation(activation='relu')(x)
            return x

        def shortcut_layer(input_tensor, out_tensor):
            shortcut_y = Conv1D(filters=int(out_tensor.shape[-1]),
                                kernel_size=1,
                                padding='same',
                                kernel_initializer=weightinit,
                                use_bias=False)(input_tensor)
            shortcut_y = BatchNormalization()(shortcut_y)

            x = Add()([shortcut_y, out_tensor])
            x = Activation('relu')(x)
            return x

        # Build the actual model:
        input_layer = Input((dim_length, dim_channels))
        x = BatchNormalization()(
            input_layer)  # Added batchnorm (not in original paper)
        input_res = x

        for depth in range(network_depth):
            x = inception_module(x)

            if use_residual and depth % 3 == 2:
                x = shortcut_layer(input_res, x)
                input_res = x

        gap_layer = GlobalAveragePooling1D()(x)

        # Final classification layer
        output_layer = Dense(self.number_of_classes,
                             activation='relu')(gap_layer)

        # Create model and compile
        model = Model(inputs=input_layer, outputs=output_layer)

        model.compile(loss='mean_absolute_error',
                      optimizer=Adam(lr=learning_rate),
                      metrics=self.metrics)

        return model
class myCallback(Callback):
    def on_epoch_end(self, epoch, logs={}):
        if logs.get('loss') < 0.1:
            print("\nReached loss less than 0.4 so cancelling training!")
            self.model.stop_training = True


model = Sequential([
                    Flatten(input_shape=(28, 28), name="input_layer"),
                    Dense(512, activation=tf.nn.relu, name="layer1"),
                    Dense(512, activation=tf.nn.relu, name="layer2"),
                    Dense(10, activation=tf.nn.softmax, name="output_layer")
])

print(model.weights)

model.compile(optimizer=Adam(),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

print(model.summary())

model.fit(training_images, training_labels, epochs=20, callbacks=myCallback())

print(model.evaluate(test_images, test_labels))

classifications = model.predict(test_images)
print(classifications[0])
print(test_labels[0])
Ejemplo n.º 14
0
def BuildModel():
    field = Input((10, 10, 11), name='field')
    #bots = Input((4*MaxNrOfBots,), name='bots')
    bots = Input((3,), name='bots')

    # f = field
    # f = Conv2D(32, 1, strides=1, padding='same', activation='relu', kernel_initializer='he_uniform', name='conv1a')(f)
    # f = Conv2D(32, 3, strides=1, padding='same', activation='relu', kernel_initializer='he_uniform', name='conv1b')(f)
    # f = Conv2D(32, 3, strides=2, padding='same', activation='relu', kernel_initializer='he_uniform', name='conv1c')(f)

    # f = Conv2D(64, 1, strides=1, padding='same', activation='relu', kernel_initializer='he_uniform', name='conv2a')(f)
    # f = Conv2D(64, 3, strides=1, padding='same', activation='relu', kernel_initializer='he_uniform', name='conv2b')(f)
    # f = Conv2D(64, 3, strides=2, padding='same', activation='relu', kernel_initializer='he_uniform', name='conv2c')(f)

    # f = Conv2D(128, 1, strides=1, padding='same', activation='relu', kernel_initializer='he_uniform', name='conv3a')(f)
    # f = Conv2D(128, 3, strides=1, padding='same', activation='relu', kernel_initializer='he_uniform', name='conv3b')(f)
    # f = Conv2D(128, 3, strides=2, padding='same', activation='relu', kernel_initializer='he_uniform', name='conv3c')(f)

    # f = GlobalAveragePooling2D(name='avg')(f)

    # b = bots
    # b = Dense(128, activation='relu', kernel_initializer='he_uniform', name='pre1')(b)
    
    # h = Concatenate(name='concat')([f, b])
    # h = Dense(512, activation='relu', kernel_initializer='he_uniform', name='dense1')(h)
    # h = Dense(128, activation='relu', kernel_initializer='he_uniform', name='dense2')(h)
    # h = Dense(3, activation='softmax', name='dense3')(h)
    
    f = Flatten(name='flatten')(field)
    h = Concatenate(name='concat')([f, bots])
    h = Dense(2048, activation='elu', kernel_initializer='he_uniform', name='dense1')(h)
    h = Dense(2048, activation='elu', kernel_initializer='he_uniform', name='dense2')(h)
    h = Dense(1024, activation='elu', kernel_initializer='he_uniform', name='dense3')(h)
    h = Dense(1024, activation='elu', kernel_initializer='he_uniform', name='dense4')(h)
    h = Dense(1024, activation='elu', kernel_initializer='he_uniform', name='dense5')(h)
    h = Dense(1024, activation='elu', kernel_initializer='he_uniform', name='dense6')(h)
    h = Dense(512, activation='elu', kernel_initializer='he_uniform', name='dense7')(h)
    h = Dense(3, activation='softmax', name='dense8')(h)

    action = h
    model_play = Model([field, bots], action)


    advantage = Input((1,), name='advantage')
    model_train = Model([field, bots, advantage], action)
    model_train.compile(loss=CategoricalVanillaPolicyGradientLoss(advantage), optimizer=Adam(lr=LearningRate))

    return model_play, model_train
Ejemplo n.º 15
0
    def train(self):
        self.logger.info("DnnOptimizer::train")

        training_generator = FluctuationDataGenerator(
            self.partition['train'],
            data_dir=self.dirinput_train,
            **self.params)
        validation_generator = FluctuationDataGenerator(
            self.partition['validation'],
            data_dir=self.dirinput_test,
            **self.params)
        model = u_net(
            (self.grid_phi, self.grid_r, self.grid_z, self.dim_input),
            depth=self.depth,
            batchnorm=self.batch_normalization,
            pool_type=self.pooling,
            start_channels=self.filters,
            dropout=self.dropout)
        model.compile(loss=self.lossfun,
                      optimizer=Adam(lr=self.adamlr),
                      metrics=[self.metrics])  # Mean squared error

        model.summary()
        plot_model(model,
                   to_file='plots/model_%s_nEv%d.png' %
                   (self.suffix, self.train_events),
                   show_shapes=True,
                   show_layer_names=True)

        #log_dir = "logs/" + datetime.datetime.now(datetime.timezone.utc).strftime("%Y%m%d_%H%M%S")
        log_dir = 'logs/' + '%s_nEv%d' % (self.suffix, self.train_events)
        tensorboard_callback = TensorBoard(log_dir=log_dir, histogram_freq=1)

        model._get_distribution_strategy = lambda: None
        his = model.fit(training_generator,
                        validation_data=validation_generator,
                        use_multiprocessing=False,
                        epochs=self.epochs,
                        callbacks=[tensorboard_callback])

        plt.style.use("ggplot")
        plt.figure()
        plt.yscale('log')
        plt.plot(np.arange(0, self.epochs),
                 his.history["loss"],
                 label="train_loss")
        plt.plot(np.arange(0, self.epochs),
                 his.history["val_loss"],
                 label="val_loss")
        plt.plot(np.arange(0, self.epochs),
                 his.history[self.metrics],
                 label="train_" + self.metrics)
        plt.plot(np.arange(0, self.epochs),
                 his.history["val_" + self.metrics],
                 label="val_" + self.metrics)
        plt.title("Training Loss and Accuracy on Dataset")
        plt.xlabel("Epoch #")
        plt.ylabel("Loss/Accuracy")
        plt.legend(loc="lower left")
        plt.savefig("plots/plot_%s_nEv%d.png" %
                    (self.suffix, self.train_events))

        model_json = model.to_json()
        with open("%s/model_%s_nEv%d.json" % (self.dirmodel, self.suffix, self.train_events), "w") \
            as json_file:
            json_file.write(model_json)
        model.save_weights("%s/model_%s_nEv%d.h5" %
                           (self.dirmodel, self.suffix, self.train_events))
        self.logger.info("Saved trained model to disk")
Ejemplo n.º 16
0
    model = utils.load_model(args.out)
elif args.mode == "train":
    model = getattr(utils.models, args.model)()
else:
    raise ValueError("Aucun modèle à l'emplacement donné par --out")

# Affiche une description du réseau : couches, nombres de paramètres
model.summary()

# Instructions exécutées dans le mode 'train'
if args.mode == "train":
    # Charge le minimiseur
    if args.minimizer == "sgd":
        optimizer = SGD(lr=args.lr)
    elif args.minimizer == "adam":
        optimizer = Adam(lr=args.lr)
    else:
        raise ValueError("Nom d'optimiseur inconnu")

    # Charge les données d'entraînement
    x_train, y_train = utils.get_train_data(num_train_examples=args.examples)

    # Entraîne le réseau pendant un certain nombre d'epochs
    model.compile(loss="categorical_crossentropy",
                  optimizer=optimizer,
                  metrics=["accuracy"])
    model.fit(x=x_train,
              y=y_train,
              epochs=args.epochs,
              batch_size=args.batch_size)
Ejemplo n.º 17
0
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy

#%% Set up model

model = Sequential([
    Dense(units=16, input_shape=(1, ), activation='relu'),
    # Dense(units=8, activation='relu'),
    Dense(units=32, activation='relu'),
    # Dense(units=2048, activation='relu'),
    Dense(units=2, activation='softmax')
])

model.summary()

model.compile(optimizer=Adam(learning_rate=0.0001),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

import datetime
import os
# log_dir = os.getcwd() + "\\logs\\fit\\"
# log_dir = "logs\fit"
# os.makedirs(log_dir)

# tensorboard_callback = keras.callbacks.TensorBoard(log_dir='./Graph', histogram_freq=1)

# logdir=mylogs:C:\path\to\output\folder
# tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
    def CreateModel(self):
        '''
		定义CNN/LSTM/CTC模型,使用函数式模型
		输入层:200维的特征值序列,一条语音数据的最大长度设为1600(大约16s)
		隐藏层:卷积池化层,卷积核大小为3x3,池化窗口大小为2
		隐藏层:全连接层
		输出层:全连接层,神经元数量为self.MS_OUTPUT_SIZE,使用softmax作为激活函数,
		CTC层:使用CTC的loss作为损失函数,实现连接性时序多输出
		
		'''

        input_data = Input(name='the_input',
                           shape=(self.AUDIO_LENGTH, self.AUDIO_FEATURE_LENGTH,
                                  1))

        layer_h1 = Conv2D(32, (3, 3),
                          use_bias=False,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(input_data)  # 卷积层
        #layer_h1 = Dropout(0.05)(layer_h1)
        layer_h2 = Conv2D(32, (3, 3),
                          use_bias=True,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(layer_h1)  # 卷积层
        layer_h3 = MaxPooling2D(pool_size=2, strides=None,
                                padding="valid")(layer_h2)  # 池化层

        #layer_h3 = Dropout(0.05)(layer_h3) # 随机中断部分神经网络连接,防止过拟合
        layer_h4 = Conv2D(64, (3, 3),
                          use_bias=True,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(layer_h3)  # 卷积层
        #layer_h4 = Dropout(0.1)(layer_h4)
        layer_h5 = Conv2D(64, (3, 3),
                          use_bias=True,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(layer_h4)  # 卷积层
        layer_h6 = MaxPooling2D(pool_size=2, strides=None,
                                padding="valid")(layer_h5)  # 池化层

        #layer_h6 = Dropout(0.1)(layer_h6)
        layer_h7 = Conv2D(128, (3, 3),
                          use_bias=True,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(layer_h6)  # 卷积层
        #layer_h7 = Dropout(0.15)(layer_h7)
        layer_h8 = Conv2D(128, (3, 3),
                          use_bias=True,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(layer_h7)  # 卷积层
        layer_h9 = MaxPooling2D(pool_size=2, strides=None,
                                padding="valid")(layer_h8)  # 池化层

        #layer_h9 = Dropout(0.15)(layer_h9)
        layer_h10 = Conv2D(128, (3, 3),
                           use_bias=True,
                           activation='relu',
                           padding='same',
                           kernel_initializer='he_normal')(layer_h9)  # 卷积层
        #layer_h10 = Dropout(0.2)(layer_h10)
        layer_h11 = Conv2D(128, (3, 3),
                           use_bias=True,
                           activation='relu',
                           padding='same',
                           kernel_initializer='he_normal')(layer_h10)  # 卷积层
        layer_h12 = MaxPooling2D(pool_size=1, strides=None,
                                 padding="valid")(layer_h11)  # 池化层

        #layer_h12 = Dropout(0.2)(layer_h12)
        layer_h13 = Conv2D(128, (3, 3),
                           use_bias=True,
                           activation='relu',
                           padding='same',
                           kernel_initializer='he_normal')(layer_h12)  # 卷积层
        #layer_h13 = Dropout(0.3)(layer_h13)
        layer_h14 = Conv2D(128, (3, 3),
                           use_bias=True,
                           activation='relu',
                           padding='same',
                           kernel_initializer='he_normal')(layer_h13)  # 卷积层
        layer_h15 = MaxPooling2D(pool_size=1, strides=None,
                                 padding="valid")(layer_h14)  # 池化层

        #test=Model(inputs = input_data, outputs = layer_h12)
        #test.summary()

        layer_h16 = Reshape((200, 3200))(layer_h15)  #Reshape层

        #layer_h16 = Dropout(0.3)(layer_h16) # 随机中断部分神经网络连接,防止过拟合
        layer_h17 = Dense(128,
                          activation="relu",
                          use_bias=True,
                          kernel_initializer='he_normal')(layer_h16)  # 全连接层

        inner = layer_h17
        #layer_h5 = LSTM(256, activation='relu', use_bias=True, return_sequences=True)(layer_h4) # LSTM层

        rnn_size = 128
        gru_1 = GRU(rnn_size,
                    return_sequences=True,
                    kernel_initializer='he_normal',
                    name='gru1')(inner)
        gru_1b = GRU(rnn_size,
                     return_sequences=True,
                     go_backwards=True,
                     kernel_initializer='he_normal',
                     name='gru1_b')(inner)
        gru1_merged = add([gru_1, gru_1b])
        gru_2 = GRU(rnn_size,
                    return_sequences=True,
                    kernel_initializer='he_normal',
                    name='gru2')(gru1_merged)
        gru_2b = GRU(rnn_size,
                     return_sequences=True,
                     go_backwards=True,
                     kernel_initializer='he_normal',
                     name='gru2_b')(gru1_merged)

        gru2 = concatenate([gru_2, gru_2b])

        layer_h20 = gru2
        #layer_h20 = Dropout(0.4)(gru2)
        layer_h21 = Dense(128,
                          activation="relu",
                          use_bias=True,
                          kernel_initializer='he_normal')(layer_h20)  # 全连接层

        #layer_h17 = Dropout(0.3)(layer_h17)
        layer_h22 = Dense(self.MS_OUTPUT_SIZE,
                          use_bias=True,
                          kernel_initializer='he_normal')(layer_h21)  # 全连接层

        y_pred = Activation('softmax', name='Activation0')(layer_h22)
        model_data = Model(inputs=input_data, outputs=y_pred)
        #model_data.summary()

        labels = Input(name='the_labels',
                       shape=[self.label_max_string_length],
                       dtype='float32')
        input_length = Input(name='input_length', shape=[1], dtype='int64')
        label_length = Input(name='label_length', shape=[1], dtype='int64')
        # Keras doesn't currently support loss funcs with extra parameters
        # so CTC loss is implemented in a lambda layer

        #layer_out = Lambda(ctc_lambda_func,output_shape=(self.MS_OUTPUT_SIZE, ), name='ctc')([y_pred, labels, input_length, label_length])#(layer_h6) # CTC
        loss_out = Lambda(self.ctc_lambda_func, output_shape=(1, ),
                          name='ctc')(
                              [y_pred, labels, input_length, label_length])

        model = Model(inputs=[input_data, labels, input_length, label_length],
                      outputs=loss_out)

        model.summary()

        # clipnorm seems to speeds up convergence
        #sgd = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5)
        #ada_d = Adadelta(lr = 0.01, rho = 0.95, epsilon = 1e-06)
        opt = Adam(lr=0.001,
                   beta_1=0.9,
                   beta_2=0.999,
                   decay=0.0,
                   epsilon=10e-8)
        #model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd)

        model.build((self.AUDIO_LENGTH, self.AUDIO_FEATURE_LENGTH, 1))
        model = ParallelModel(model, NUM_GPU)

        model.compile(loss={
            'ctc': lambda y_true, y_pred: y_pred
        },
                      optimizer=opt)

        # captures output of softmax so we can decode the output during visualization
        test_func = K.function([input_data], [y_pred])

        #print('[*提示] 创建模型成功,模型编译成功')
        print('[*Info] Create Model Successful, Compiles Model Successful. ')
        return model, model_data
Ejemplo n.º 19
0
print('x_data.shape: {}, y_data.shape: {}'.format(x_data.shape, y_data.shape))

#input_visualization()
#plt.show()

train_ds = tf.data.Dataset.from_tensor_slices((x_data, y_data))
train_ds = train_ds.shuffle(1000).batch(8)

# Model
model = Sequential()
model.add(Dense(n_class))
model.add(Activation('softmax'))
#model.summary()

loss_object = SparseCategoricalCrossentropy()
optimizer = Adam(learning_rate=0.01)

train_loss = Mean()
train_acc = SparseCategoricalAccuracy()

EPOCHS = 10

## Training
for epoch in range(EPOCHS):
    trainer()
    training_reporter()

#
x1_test = np.linspace(-1.2, 1.2, 100).astype(np.float32)
x2_test = np.linspace(-1.2, 1.2, 100).astype(np.float32)
Ejemplo n.º 20
0
def main():
    # Gets currect directory path
    cdir = os.getcwd()

    # Parameters
    epochs = 100
    batch_size = 1
    depth = 3
    loss_label = 'GDL'
    loss_func = generalized_dice_loss

    learning_rate = 1e-4

    datasets = ["ds0", "ds1", "ds2", "ds3"]
    datasets_label = [
        "EvaLady", "AosugiruHaru", "JijiBabaFight", "MariaSamaNihaNaisyo"
    ]

    df = pd.DataFrame(columns=[
        'Trained on', 'Tested on', 'Loss', 'Accuracy', 'Precision', 'Recall'
    ])

    for m, ml in enumerate(datasets):
        model = UNet(depth)

        model.load_weights("unet_{0}.hdf5".format(ml))

        for d, ds in enumerate(datasets):
            if ds == m:
                pass

            # Gets all files .jpg
            inputs_train = glob.glob(
                str(cdir) + "/../../datasets/D1_" + ds + "/input/*.jpg")
            # Gets all files .png
            targets_train = glob.glob(
                str(cdir) + "/../../datasets/D1_" + ds + "/target/*.png")

            inputs_val = glob.glob(
                str(cdir) + "/../../datasets/TT_" + ds + "/input/*.jpg")
            # Gets all files .png
            targets_val = glob.glob(
                str(cdir) + "/../../datasets/TT_" + ds + "/target/*.png")

            # Sort paths
            inputs_train.sort()
            targets_train.sort()
            inputs_val.sort()
            targets_val.sort()

            opt = Adam(lr=learning_rate)

            # Fixes a initial seed for randomness
            np.random.seed(RANDOM_SEED)
            set_random_seed(RANDOM_SEED)

            X_train = []
            Y_train = []
            X_val = []
            Y_val = []
            # Iterates through files and extract the patches for training, validation and testing
            for i, _ in enumerate(inputs_val):
                x = plt.imread(inputs_val[i])
                if len(x.shape) == 3:
                    x = x[:, :, 0]
                X_val.append(fix_size(x, depth))
                Y_val.append(fix_size(plt.imread(targets_val[i]), depth))

            X_val = img_to_normal(np.array(X_val)[..., np.newaxis])
            Y_val = img_to_ohe(np.array(Y_val))

            # Shuffles both the inputs and targets set
            indexes = list(range(0, len(inputs_val)))
            np.random.shuffle(indexes)
            X_val = X_val[indexes]
            Y_val = Y_val[indexes]
            inputs_val = np.array(inputs_val)[indexes]

            X_val2 = X_val[5:]
            Y_val2 = Y_val[5:]

            Y_pred = model.predict(X_val2)

            test_loss = loss_func(K.constant(Y_val2),
                                  K.constant(Y_pred)).numpy()

            Y_pred = ohe_to_img(Y_pred)
            Y_val2 = ohe_to_img(Y_val2)

            metrics = calc_metrics(Y_val2, Y_pred)
            df = df.append(
                pd.DataFrame(
                    data={
                        'Trained on': [datasets_label[m]],
                        'Tested on': [datasets_label[d]],
                        'Loss': [test_loss],
                        'Accuracy': [metrics['accuracy']],
                        'Precision': [metrics['precision']],
                        'Recall': [metrics['recall']]
                    }))

            df.to_csv('results.csv', index=False)

    clear_session()
Ejemplo n.º 21
0
classTotals = trainY.sum(axis=0)
classWeight = classTotals.max() / classTotals

# construct the image generator for data augmentation
aug = ImageDataGenerator(rotation_range=10,
                         zoom_range=0.15,
                         width_shift_range=0.1,
                         height_shift_range=0.1,
                         shear_range=0.15,
                         horizontal_flip=False,
                         vertical_flip=False,
                         fill_mode="nearest")

# initialize the optimizer and model
print("[INFO] compiling model...")
opt = Adam(lr=INIT_LR, decay=INIT_LR / (NUM_EPOCHS * 0.5))
model = TrafficSignNet.build(width=32, height=32, depth=3, classes=numLabels)
model.compile(loss="categorical_crossentropy",
              optimizer=opt,
              metrics=["accuracy"])

# compile the model and train the network
print("[INFO] training network...")
H = model.fit_generator(aug.flow(trainX, trainY, batch_size=BS),
                        validation_data=(testX, testY),
                        steps_per_epoch=trainX.shape[0] // BS,
                        epochs=NUM_EPOCHS,
                        class_weight=classWeight,
                        verbose=1)

# evaluate the network
# Set the seed for reproducible result
np.random.seed(1000)

# RandomDim, conventionally 100, author decided 10
randomDim = 10 
# Load MNIST data
(X_train, _), (_, _) = mnist.load_data()
X_train = (X_train.astype(np.float32) - 127.5)/127.5
X_train = X_train.reshape(60000, 784)


# In[3]:


# Optimizer
adam = Adam(lr=0.0002, beta_1=0.5)

generator = Sequential()
generator.add(Dense(256, input_dim=randomDim)) #, kernel_initializer=initializers.RandomNormal(stddev=0.02)))
generator.add(LeakyReLU(0.2))
generator.add(Dense(512))
generator.add(LeakyReLU(0.2))
generator.add(Dense(1024))
generator.add(LeakyReLU(0.2))
generator.add(Dense(784, activation='tanh'))
#generator.compile(loss='binary_crossentropy', optimizer=adam)

# Generator:
# Total params: 1,463,312
# Trainable params: 1,463,312
# Non-trainable params: 0
Ejemplo n.º 23
0
    # v1 does not use BN after last shortcut connection-ReLU
    x = AveragePooling2D(pool_size=8)(x)
    y = Flatten()(x)
    outputs = Dense(num_classes,
                    activation='softmax',
                    kernel_initializer='he_normal')(y)

    # Instantiate model.
    model = Model(inputs=inputs, outputs=outputs)
    return model


model = resnet_v1(input_shape=input_shape, depth=20)

model.compile(loss='categorical_crossentropy',
              optimizer=Adam(lr=lr_schedule(0)),
              metrics=['accuracy'])
model.summary()

# Prepare model model saving directory.
save_dir = os.path.join(os.getcwd(), 'saved_models')
model_name = 'cifar10_%s_model.{epoch:03d}.h5' % model_type
if not os.path.isdir(save_dir):
    os.makedirs(save_dir)
filepath = os.path.join(save_dir, model_name)

checkpoint = ModelCheckpoint(filepath=filepath,
                             monitor='val_acc',
                             verbose=1,
                             save_best_only=True)
Ejemplo n.º 24
0
    idx_char[num_chars] = MU
    idx_char[num_chars + 1] = END
    char_idx = {c: i for i, c in enumerate(wl_chars)}
    char_idx[MU] = num_chars
    char_idx[END] = num_chars + 1
    # モデル構築
    if os.path.exists('/content/drive/My Drive/colab/lstm_set.h5'):
        # loading the model
        print('load model...')
        model = load_model('/content/drive/My Drive/colab/lstm_set.h5')
    else:
        model = lstm_model()
    model.summary()

    model.compile(loss='categorical_crossentropy',
                  optimizer=Adam())  # mean_squared_error
    m = model
    # テキスト文章取得
    toots = list([
        tmp.strip() for tmp in open(
            "/content/drive/My Drive/colab/toot_merge_n.txt").readlines()
    ])
    # d2vモデル ベクトル取得
    d2v_vecs = Doc2Vec.load(
        "/content/drive/My Drive/colab/d2v.model").docvecs.vectors_docs
    # データセット構築
    dataset = build_tf_ds(batch_size=batch_size)
    # コールバック設定
    print_callback = LambdaCallback(on_epoch_end=on_epoch_end)
    ES = EarlyStopping(monitor='loss',
                       min_delta=0.001,
Ejemplo n.º 25
0
    plt.legend()

    # Task 5

    # ---- Parameters ---- #
    img_w = 128  # Witdh of input images
    img_h = 128  # Height of input images
    img_ch = 1  # Number of channels
    base = 32  # Number of neurons in first layer

    # Creating a CNN Network
    clf = lenet(img_ch, img_w, img_h, base)

    # Compiling the layers and setting loss & optimizer function
    clf.compile(loss='binary_crossentropy',
                optimizer=Adam(lr=0.0001),
                metrics=['binary_accuracy'])

    # Trains the model for 200 epochs with 8 images in each batch
    clf_hist = clf.fit(
        x_train,
        y_train,
        batch_size=8,
        epochs=200,
        validation_data=(x_test,
                         y_test))  # Validation data is training data shuffled

    # Prints a summary of the network
    clf.summary()

    # ---- Plotting ---- #
Ejemplo n.º 26
0
model.add(Dense(1024))
model.add(Activation("relu"))
model.add(Dropout(0.5))

model.add(Dense(256))
model.add(Activation("relu"))

model.add(Dense(128))
model.add(Activation("relu"))

model.add(Dense(2))
model.add(Activation("softmax"))

model.compile(loss='categorical_crossentropy',
              optimizer=Adam(learning_rate=1e-3),
              metrics=["accuracy"])
model.summary()

checkpoint = ModelCheckpoint(filepath=os.path.join('saved_models', filename,
                                                   "model_best.h5"),
                             monitor='val_accuracy',
                             verbose=1,
                             save_best_only=True,
                             mode='auto',
                             save_weights_only=False)
csv_logger = CSVLogger(os.path.join('saved_models', filename,
                                    "model_history_log.csv"),
                       append=True)

steps_per_epoch = n_files_train * (n_events_per_file // batch_size)
Ejemplo n.º 27
0
def entrenamiento(kfold, etapa, datos, arquitectura, train_epochs,
                  batch_epochs, early_stopping, iteracion, models_info,
                  pipeline):
    import time
    start_model = time.time()
    base_model, preprocess_input = get_model(arquitectura, iteracion,
                                             models_info, pipeline)
    model_performance = {}

    if dataset == 'gleasson':
        datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
                                     rotation_range=40,
                                     width_shift_range=0.1,
                                     height_shift_range=0.1,
                                     shear_range=0.01,
                                     zoom_range=[0.9, 1.25],
                                     horizontal_flip=True,
                                     vertical_flip=False,
                                     fill_mode='reflect',
                                     data_format='channels_last')

    if etapa == 'train':
        train_generator = datagen.flow_from_dataframe(
            dataframe=datos['df_train'],
            x_col=x_col_name,
            y_col=y_col_name,
            target_size=(pipeline['img_height'], pipeline['img_width']),
            class_mode='categorical',
            batch_size=pipeline["batch_size"],
            seed=42,
            shuffle=True)

    if etapa == 'train_EL':
        train_generator = datagen.flow_from_dataframe(
            dataframe=datos['df_train_EL'],
            x_col=x_col_name,
            y_col=y_col_name,
            target_size=(pipeline['img_height'], pipeline['img_width']),
            class_mode='categorical',
            batch_size=pipeline["batch_size"],
            seed=42,
            shuffle=True)

    if len(datos['df_val']) > 0:
        val_datagen = ImageDataGenerator(
            preprocessing_function=preprocess_input)

        valid_generator = val_datagen.flow_from_dataframe(
            dataframe=datos['df_val'],
            x_col=x_col_name,
            y_col=y_col_name,
            batch_size=pipeline["batch_size"],
            seed=42,
            shuffle=True,
            class_mode="categorical",
            target_size=(pipeline['img_height'], pipeline['img_width']))

    test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)

    if dataset == 'gleasson':
        test1_generator = test_datagen.flow_from_dataframe(
            dataframe=datos['df_test1'],
            x_col="patch_name1",
            y_col="grade_1",
            batch_size=pipeline["batch_size"],
            seed=42,
            shuffle=False,
            class_mode="categorical",
            target_size=(pipeline['img_height'], pipeline['img_width']))

        test2_generator = test_datagen.flow_from_dataframe(
            dataframe=datos['df_test2'],
            x_col="patch_name2",
            y_col="grade_2",
            batch_size=pipeline["batch_size"],
            seed=42,
            shuffle=False,
            class_mode="categorical",
            target_size=(pipeline['img_height'], pipeline['img_width']))

    if etapa == 'train' or etapa == 'train_EL':
        finetune_model = transfer_learning_soft(
            base_model, pipeline["class_num"],
            pipeline["stage_config"][iteracion])

    #entrenar modelo
    from tensorflow.keras.optimizers import SGD, Adam, RMSprop

    if etapa == 'train':
        NUM_EPOCHS = train_epochs
        num_train_images = len(
            datos['df_train']) * pipeline["augmenting_factor"]
        datos_entrenamiento = datos['df_train'].copy()
    if etapa == 'train_EL':
        NUM_EPOCHS = batch_epochs
        num_train_images = len(
            datos['df_train_EL']) * pipeline["augmenting_factor"]
        datos_entrenamiento = datos['df_train_EL'].copy()

    STEP_SIZE_TRAIN = num_train_images // train_generator.batch_size
    if len(datos['df_val']) > 0:
        STEP_SIZE_VALID = valid_generator.n // valid_generator.batch_size
    STEP_SIZE_TEST1 = test1_generator.n // test1_generator.batch_size

    if dataset == 'gleasson':
        STEP_SIZE_TEST2 = test2_generator.n // test2_generator.batch_size

    if len(datos['df_val']) > 0:
        generator_seguimiento = valid_generator
        pasos_seguimiento = STEP_SIZE_VALID
    else:
        generator_seguimiento = test1_generator
        pasos_seguimiento = STEP_SIZE_TEST1

    if pipeline["class_num"] > 3:
        metrics = ['accuracy']
        loss = 'categorical_crossentropy'
        peso_clases = {}
        total = datos_entrenamiento.shape[0]
        weights = (total /
                   datos_entrenamiento.groupby(y_col_name).count().values) / 4
        peso_clases = {
            0: weights[0][0],
            1: weights[1][0],
            2: weights[2][0],
            3: weights[3][0]
        }

        #print(datos_entrenamiento.groupby(y_col_name))
        print(datos_entrenamiento.groupby(y_col_name).count())
        print(datos_entrenamiento.groupby(y_col_name).count().values)
        max_class_num = np.argmax(
            datos_entrenamiento.groupby(y_col_name).count().values)
        print('id_maximo', max_class_num)

        peso_clases = {}
        class_num = datos_entrenamiento.groupby(y_col_name).count().values
        print('num_maximo', class_num[max_class_num])
        weights = class_num[max_class_num] / datos_entrenamiento.groupby(
            y_col_name).count().values
        print(weights)
        peso_clases = {
            0: weights[0][0],
            1: weights[1][0],
            2: weights[2][0],
            3: weights[3][0]
        }
        print(peso_clases)

    if pipeline["class_num"] == 3:
        metrics = ['accuracy']
        # calcular pesos de cada clase
        total = datos_entrenamiento.shape[0]
        weights = (total /
                   datos_entrenamiento.groupby(y_col_name).count().values) / 3
        peso_clases = {0: weights[0][0], 1: weights[1][0], 2: weights[2][0]}
    elif pipeline["class_num"] == 2:
        metrics = ['accuracy']
        # calcular pesos de cada clase
        total = datos_entrenamiento.shape[0]
        weights = (total /
                   datos_entrenamiento.groupby(y_col_name).count().values) / 2
        peso_clases = {0: weights[0][0], 1: weights[1][0]}
        loss = 'binary_crossentropy'

    adam = Adam(lr=pipeline["stage_config"][iteracion]['LR'])
    finetune_model.compile(adam, loss=loss, metrics=metrics)
    early = EarlyStopping(monitor='val_loss',
                          min_delta=1e-3,
                          patience=5,
                          verbose=1,
                          restore_best_weights=True)

    if len(peso_clases) > 0:
        print("\n")
        print("ESTOY USANDO PESADO DE CLASES!")
        print(peso_clases)
        print("##############################")
        print("\n")
        history = finetune_model.fit(train_generator,
                                     epochs=NUM_EPOCHS,
                                     workers=1,
                                     steps_per_epoch=STEP_SIZE_TRAIN,
                                     validation_data=generator_seguimiento,
                                     validation_steps=pasos_seguimiento,
                                     callbacks=[early],
                                     verbose=1,
                                     class_weight=peso_clases)
    else:
        history = finetune_model.fit(train_generator,
                                     epochs=NUM_EPOCHS,
                                     workers=1,
                                     steps_per_epoch=STEP_SIZE_TRAIN,
                                     validation_data=generator_seguimiento,
                                     validation_steps=pasos_seguimiento,
                                     verbose=1,
                                     callbacks=[early])

    if len(datos['df_val']) > 0:
        score1 = finetune_model.evaluate(valid_generator,
                                         verbose=0,
                                         steps=STEP_SIZE_VALID)

    score2 = finetune_model.evaluate(test1_generator,
                                     verbose=0,
                                     steps=STEP_SIZE_TEST1)

    if dataset == 'gleasson':
        score3 = finetune_model.evaluate_generator(generator=test2_generator,
                                                   verbose=1,
                                                   steps=STEP_SIZE_TEST2)

    if len(datos['df_val']) > 0:
        print("Val  Loss      : ", score1[0])
        print("Val  Accuracy  : ", score1[1])
        print("\n")
    print("Test1 Loss     : ", score2[0])
    print("Test1 Accuracy : ", score2[1])
    print("\n")
    if dataset == 'gleasson':
        print("Test2 Loss     : ", score3[0])
        print("Test2 Accuracy : ", score3[1])
        logs.append([
            kfold, iteracion, arquitectura, score1[0], score1[1], score2[0],
            score2[1], score3[0], score3[1]
        ])

    #guardar_logs(ruta,[logs[-1]])
    save_logs(logs, 'train', pipeline)

    # Plot training & validation accuracy values
    #print(history.history)
    #print('--- Val acc ---')
    #print(history.history['val_acc'])
    plt.plot(history.history['acc'])
    if len(datos['df_val']) > 0:
        plt.plot(history.history['val_acc'])
    plt.title('Model accuracy - {}'.format(arquitectura))
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Test'], loc='upper left')
    plt.show()

    # Plot training & validation loss values
    plt.plot(history.history['loss'])
    if len(datos['df_val']) > 0:
        plt.plot(history.history['val_loss'])
    plt.title('Model loss - {}'.format(arquitectura))
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Test'], loc='upper left')
    plt.show()

    end_model = time.time()
    time_training = end_model - start_model
    print(f"training {arquitectura}", time_training)

    if pipeline['save_model']:
        save_path_model = os.path.join(
            pipeline['save_path_model'],
            f'{kfold}_{iteracion}_{arquitectura}.h5')
        finetune_model.save(save_path_model)
        model_performance['val_acc'] = score1[1]
        return save_path_model, model_performance

    logs_time.append([kfold, iteracion, arquitectura, time_training])
    save_logs(logs_time, 'time', pipeline)

    #pipeline['logs_model']
    #time_training
    model_performance['val_acc'] = score1[1]
    model_performance['test1_acc'] = score2[1]
    model_performance['test2_acc'] = score3[1]
    return finetune_model, model_performance
def SiameseNet(image_shape):
    '''
    left_input = Input(image_shape)
    right_input = Input(image_shape)

    
    convnet = Sequential([
        Conv2D(5,3, input_shape=image_shape),
        Activation('relu'),
        MaxPooling2D(),
        Conv2D(5,3),
        Activation('relu'),
        MaxPooling2D(),
        Conv2D(7,2),
        Activation('relu'),
        MaxPooling2D(),
        Conv2D(7,2),
        Activation('relu'),
        Dropout(.20),
        Flatten(),
        Dense(18) , 
              kernel_regularizer=regularizers.l2(1e-4),
              bias_regularizer=regularizers.l2(1e-4),
              activity_regularizer=regularizers.l2(1e-5)),
        Activation('sigmoid')
        ])
    '''

    left_input = Input(image_shape)
    right_input = Input(image_shape)

    # Embedding Net
    convnet = Sequential([
        Conv2D(50, 3, input_shape=image_shape),
        Activation('relu'),
        MaxPooling2D(3),
        Conv2D(80, 3),
        Activation('relu'),
        MaxPooling2D(3),
        Conv2D(80, 2),
        Activation('relu'),
        MaxPooling2D(2),
        Conv2D(100, 2),
        Activation('relu'),
        Dropout(.25),
        Flatten(),
        Dense(256),
        Dense(128,
              kernel_regularizer=regularizers.l2(1e-4),
              bias_regularizer=regularizers.l2(1e-4),
              activity_regularizer=regularizers.l2(1e-5)),
        Activation('relu')  #linear
    ])

    ###################################### --OPTION 2-- ##########################################

    def euclidean_distance(vects):
        x, y = vects
        sum_square = K.sum(K.square(x - y), axis=1, keepdims=True)
        return K.sqrt(K.maximum(sum_square, K.epsilon()))

    def eucl_dist_output_shape(shapes):
        shape1, shape2 = shapes

        return (shape1[0], 1)

    encoded_l = convnet(left_input)
    encoded_r = convnet(right_input)

    distance = Lambda(euclidean_distance, output_shape=eucl_dist_output_shape)(
        [encoded_l, encoded_r])
    predictions = Dense(1, activation='sigmoid')(distance)
    siamese_net = Model(inputs=[left_input, right_input], outputs=predictions)

    def contrastive_loss(y_true, y_pred):
        '''Contrastive loss from Hadsell-et-al.'06
        http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
        '''
        margin = 1
        square_pred = K.square(y_pred)
        margin_square = K.square(K.maximum(margin - y_pred, 0))
        return K.mean(y_true * square_pred + (1 - y_true) * margin_square)

    def accuracy(y_true, y_pred):
        '''Compute classification accuracy with a fixed threshold on distances.
        '''
        return K.mean(K.equal(y_true, K.cast(y_pred < 0.5, y_true.dtype)))

    optimizer = Adam(0.0001, decay=2.5e-4)
    siamese_net.compile(loss=contrastive_loss,
                        optimizer=optimizer,
                        metrics=[accuracy])

    return siamese_net
Ejemplo n.º 29
0
headModel = Dense(128, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(2, activation="softmax")(headModel)

# place the head FC model on top of the base model (this will become
# the actual model we will train)
model = Model(inputs=baseModel.input, outputs=headModel)

# loop over all layers in the base model and freeze them so they will
# *not* be updated during the first training process
for layer in baseModel.layers:
    layer.trainable = False

# compile our model
print("[INFO] compiling model...")
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"])
# train the head of the network
print("[INFO] training head...")

H = model.fit(aug.flow(trainX, trainY, batch_size=BS),
              steps_per_epoch=len(trainX) // BS,
              validation_data=(testX, testY),
              validation_steps=len(testX) // BS,
              epochs=EPOCHS)
# make predictions on the testing set
print("[INFO] evaluating network...")
predIdxs = model.predict(testX, batch_size=BS)

# for each image in the testing set we need to find the index of the
# label with corresponding largest predicted probability
Ejemplo n.º 30
0
            # 预热期
            warmup_epoch = int((Freeze_epoch - Init_epoch) * 0.2)
            # 总共的步长
            total_steps = int(
                (Freeze_epoch - Init_epoch) * num_train / batch_size)
            # 预热步长
            warmup_steps = int(warmup_epoch * num_train / batch_size)
            # 学习率
            reduce_lr = WarmUpCosineDecayScheduler(
                learning_rate_base=learning_rate_base,
                total_steps=total_steps,
                warmup_learning_rate=1e-4,
                warmup_steps=warmup_steps,
                hold_base_rate_steps=num_train,
                min_learn_rate=1e-6)
            model.compile(optimizer=Adam(),
                          loss={
                              'yolo_loss': lambda y_true, y_pred: y_pred
                          })
        else:
            reduce_lr = ReduceLROnPlateau(monitor='val_loss',
                                          factor=0.5,
                                          patience=2,
                                          verbose=1)
            model.compile(optimizer=Adam(learning_rate_base),
                          loss={
                              'yolo_loss': lambda y_true, y_pred: y_pred
                          })

        print('Train on {} samples, val on {} samples, with batch size {}.'.
              format(num_train, num_val, batch_size))