Esempio n. 1
0
    def train_model(self,
                    x_train,
                    y_train,
                    learn_rate=0.001,
                    epoch=5,
                    batch_size=128,
                    validation_split_size=0.2,
                    train_callbacks=()):
        history = LossHistory()

        X_train, X_valid, y_train, y_valid = train_test_split(
            x_train, y_train, test_size=validation_split_size)

        opt = Adam(lr=learn_rate)

        self.classifier.compile(loss='binary_crossentropy',
                                optimizer=opt,
                                metrics=['accuracy'])

        # early stopping will auto-stop training process if model stops learning after 3 epochs
        earlyStopping = EarlyStopping(monitor='val_loss',
                                      patience=3,
                                      verbose=0,
                                      mode='auto')

        self.classifier.fit(
            X_train,
            y_train,
            batch_size=batch_size,
            epochs=epoch,
            verbose=1,
            validation_data=(X_valid, y_valid),
            callbacks=[history, *train_callbacks, earlyStopping])
        fbeta_score = self._get_fbeta_score(self.classifier, X_valid, y_valid)
        return [history.train_losses, history.val_losses, fbeta_score]
Esempio n. 2
0
    def fit(self,
            flow,
            epochs,
            lr,
            validation_data,
            train_callbacks=[],
            batches=300):
        history = LossHistory()
        fbeta = Fbeta(validation_data)
        opt = Adam(lr=lr)
        self.model.compile(loss='binary_crossentropy',
                           optimizer=opt,
                           metrics=['accuracy'])

        earlyStopping = EarlyStopping(monitor='val_loss',
                                      patience=3,
                                      verbose=0,
                                      mode='auto')
        self.model.fit_generator(flow,
                                 steps_per_epoch=batches,
                                 epochs=epochs,
                                 callbacks=[history, earlyStopping, fbeta] +
                                 train_callbacks,
                                 validation_data=validation_data)
        fb_score, thresholds = self.get_fbeta_score(validation_data,
                                                    verbose=False)
        return [
            fbeta.fbeta, history.train_losses, history.val_losses, fb_score,
            thresholds
        ]
    def train_inception(self, x_train, y_train, learn_rate=0.001, epoch=5, batch_size=128, validation_split_size=0.2, train_callbacks=()):

        history = LossHistory()

        X_train, X_valid, y_train, y_valid = train_test_split(x_train, y_train, test_size=validation_split_size)

        # this is the model we will train
        self.classifier = Model(inputs=self.base_model.input, outputs=self.predictions)

        # first: train only the top layers (which were randomly initialized)
        # i.e. freeze all convolutional InceptionV3 layers
        for layer in base_model.layers:
            layer.trainable = False

        opt = RMSprop(lr=learn_rate)

        # compile the model (should be done *after* setting layers to non-trainable)
        self.classifier.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])

        earlyStopping = EarlyStopping(monitor='val_loss', patience=3, verbose=0, mode='auto')

        # train the model on the new data for a few epochs
        self.classifier.fit_generator(...)

        # at this point, the top layers are well trained and we can start fine-tuning
        # convolutional layers from inception V3. We will freeze the bottom N layers
        # and train the remaining top layers.

        # let's visualize layer names and layer indices to see how many layers
        # we should freeze:
        for i, layer in enumerate(base_model.layers):
           print(i, layer.name)

        # we chose to train the top 2 inception blocks, i.e. we will freeze
        # the first 249 layers and unfreeze the rest:
        for layer in self.classifier.layers[:249]:
           layer.trainable = False
        for layer in self.classifier.layers[249:]:
           layer.trainable = True

        # we need to recompile the mode for these modifications to take effect
        # we use SGD with a low learning rate

        opt_sgd = SGD(lr=0.0001, momentum=0.9)
        
        self.classifier.compile(optimizer=opt_sgd, loss='categorical_crossentropy', metrics=['accuracy'])

        # we train our model again (this time fine-tuning the top 2 inception blocks
        # alongside the top Dense layers
        self.classifier.fit_generator(...)

        self.classifier.fit(X_train, y_train,
                            batch_size=batch_size,
                            epochs=epoch,
                            verbose=1,
                            validation_data=(X_valid, y_valid),
                            callbacks=[history, *train_callbacks, earlyStopping])
        fbeta_score = self._get_fbeta_score(self.classifier, X_valid, y_valid)
        return [history.train_losses, history.val_losses, fbeta_score]
Esempio n. 4
0
    def train_model(self,
                    x_train,
                    y_train,
                    x_valid,
                    y_valid,
                    learn_rate=0.001,
                    epoch=5,
                    batch_size=128,
                    w_sam_map=None,
                    train_callbacks=()):
        history = LossHistory()

        opt = Adam(lr=learn_rate)
        self.classifier.compile(loss='binary_crossentropy',
                                optimizer=opt,
                                metrics=['accuracy'])

        # early stopping will auto-stop training process if model stops learning after 3 epochs
        earlyStopping = EarlyStopping(monitor='val_loss',
                                      patience=3,
                                      verbose=2,
                                      mode='auto')

        #        datagen = ImageDataGenerator(
        #                width_shift_range=0.1,
        #                height_shift_range=0.1,
        #                fill_mode="reflect",
        #                horizontal_flip=True,
        #                vertical_flip=True)
        #
        #        # fits the model on batches with real-time data augmentation:
        #        self.classifier.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),
        #                            steps_per_epoch=len(x_train) / batch_size,
        #                            epochs=epoch,
        #                            verbose=2,
        #                            validation_data=(x_valid, y_valid),
        #                            callbacks=[history] + train_callbacks + [earlyStopping])

        # Fix AttributeError following https://github.com/fchollet/keras/pull/6502/files
        #self.classifier.fit(x_train, y_train, shuffle="batch", batch_size=batch_size)
        if w_sam_map is None:
            w_sam = None
        else:
            w_sam = np.vectorize(w_sam_map.get)(np.array(y_train))
            w_sam = w_sam.max(axis=1)
        self.classifier.fit(x_train,
                            y_train,
                            shuffle="batch",
                            batch_size=batch_size,
                            epochs=epoch,
                            verbose=2,
                            validation_data=(x_valid, y_valid),
                            sample_weight=w_sam,
                            callbacks=[history] + train_callbacks +
                            [earlyStopping])
        fbeta_score = self._get_fbeta_score(self.classifier, x_valid, y_valid)
        return [history.train_losses, history.val_losses, fbeta_score]
Esempio n. 5
0
def run():

    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

    x_valid = current_model.split.validation_images()
    y_valid = current_model.split.validation_some_hot()

    #x_train = current_model.split.training_images()
    #y_train = current_model.split.training_some_hot()


    print("*** Training ***")

    current_model.load_weights(models_path + '/model-old.h5')
    current_model.save_weights(models_path + '/model-old.h5')

    early_stopping = EarlyStopping(monitor='val_loss', patience=12, verbose=0, mode='auto')
    timestr = time.strftime("%Y%m%d_%H%M%S")
    checkpointer = ModelCheckpoint(filepath=models_path + '/checkpoint-model.h5', verbose=0, save_best_only=True)
    reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
                                  patience=5, min_lr=0.0001)
    #tensor_board = callbacks.TensorBoard(log_dir="logs/{}".format(timestr), histogram_freq=0, write_graph=True, write_images=True)

    train_datagen = ImageDataGenerator(
        fill_mode="reflect",
        horizontal_flip=True,
        zoom_range=0.30,
        width_shift_range=0.30,
        height_shift_range=0.30,
        rotation_range=360)

    #for n in range(100):

    x_train = current_model.split.training_images()
    y_train = current_model.split.training_some_hot()

    current_model.model.fit_generator(train_datagen.flow(x_train, y_train, batch_size=128),
              steps_per_epoch=len(x_train) / 128,
              epochs= 2000,
              verbose=1,
              validation_data=(x_valid, y_valid),
              callbacks=[checkpointer])



    current_model.save_weights(models_path + '/model.h5')
Esempio n. 6
0
def train():
    """
    train
    """
    model = get_model()

    x_train, y_train = load_data(PLANET_KAGGLE_JPEG_DIR)

    validation_split_size = 0.2
    learn_rate = 0.001
    epoch = 5
    batch_size = 128
    train_callbacks = ()
    history = LossHistory()

    X_train, X_valid, y_train, y_valid = train_test_split(
        x_train, y_train, test_size=validation_split_size)

    opt = Adam(lr=learn_rate)

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

    earlyStopping = EarlyStopping(monitor='val_loss',
                                  patience=3,
                                  verbose=0,
                                  mode='auto')
    model.fit(X_train,
              y_train,
              batch_size=batch_size,
              epochs=epoch,
              verbose=1,
              validation_data=(X_valid, y_valid),
              callbacks=[history, *train_callbacks, earlyStopping])

    model.save(MODEL_NAME + '_model.h5')

    p_valid = model.predict(X_valid)
    fbeta = fbeta_score(y_valid,
                        np.array(p_valid) > 0.2,
                        beta=2,
                        average='samples')
    print(fbeta)
Esempio n. 7
0
    def train_model_aug(self,
                        x_train,
                        y_train,
                        learn_rate=0.001,
                        epoch=5,
                        batch_size=128,
                        validation_split_size=0.15,
                        train_callbacks=()):
        history = LossHistory()

        X_train, X_valid, y_train, y_valid = train_test_split(
            x_train, y_train, test_size=validation_split_size)

        opt = Adam(lr=learn_rate)

        self.classifier.compile(loss='binary_crossentropy',
                                optimizer=opt,
                                metrics=['accuracy'])

        # early stopping will auto-stop training process if model stops learning after 3 epochs
        earlyStopping = EarlyStopping(monitor='val_loss',
                                      patience=3,
                                      verbose=0,
                                      mode='auto')

        datagen = ImageDataGenerator(rotation_range=10,
                                     width_shift_range=0.2,
                                     height_shift_range=0.2,
                                     zoom_range=0.1,
                                     horizontal_flip=True)

        datagen.fit(x_train)

        self.classifier.fit_generator(
            datagen.flow(X_train, y_train, batch_size=32),
            steps_per_epoch=len(x_train) / 32,
            epochs=epoch,
            verbose=1,
            validation_data=(X_valid, y_valid),
            callbacks=[history, *train_callbacks, earlyStopping])

        fbeta_score = self._get_fbeta_score(self.classifier, X_valid, y_valid)
        return [history.train_losses, history.val_losses, fbeta_score]
Esempio n. 8
0
    def _train_ckpt_on_dataset(
        self,
        dataset,
        train_size=0.67,
        batch_size=64,
        num_epochs=100,
        progress_cb=None,
        abnormal=None,
    ):
        self.current_eval = 0
        self.stat_dataset(dataset)

        dataset = self.scale_dataset(dataset)

        (X_miss, X_train), (X_miss_val, X_test) = self.train_test_split(
            dataset,
            train_size=train_size,
        )

        _stop = EarlyStopping(
            monitor='val_loss',
            patience=5,
            verbose=_verbose,
            mode='auto',
        )
        self._keras_model.fit(
            [X_train, X_miss],
            epochs=num_epochs,
            batch_size=batch_size,
            verbose=_verbose,
            validation_data=([X_test, X_miss_val], None),
            callbacks=[_stop],
        )

        # How well did it do?
        score = self._keras_model.evaluate(
            [X_test, X_miss_val],
            batch_size=batch_size,
            verbose=_verbose,
        )
        return score
    def train_model(self,
                    x_train,
                    y_train,
                    learn_rate=0.001,
                    epoch=5,
                    batch_size=128,
                    validation_split_size=0.2,
                    train_callbacks=()):
        history = LossHistory()

        X_train, X_valid, y_train, y_valid = train_test_split(
            x_train, y_train, test_size=validation_split_size)

        opt = Nadam(lr=learn_rate,
                    beta_1=0.9,
                    beta_2=0.999,
                    epsilon=1e-08,
                    schedule_decay=0.004)

        self.classifier.compile(loss='binary_crossentropy',
                                optimizer=opt,
                                metrics=['accuracy'])

        # early stopping will auto-stop training process if model stops learning after 3 epochs
        earlyStopping = EarlyStopping(monitor='val_loss',
                                      patience=3,
                                      verbose=0,
                                      mode='auto')
        for i in range(epoch):
            self.classifier.fit(
                X_train,
                y_train,
                batch_size=batch_size,
                epochs=1,
                verbose=2,
                validation_data=(X_valid, y_valid),
                callbacks=[history, *train_callbacks, earlyStopping])
            fbeta_score = self._get_fbeta_score(self.classifier, X_valid,
                                                y_valid)
            print('fbeta score: %s' % fbeta_score)
        return [history.train_losses, history.val_losses, fbeta_score]
Esempio n. 10
0
    def train_model_generator(self,
                              generator_train,
                              generator_valid,
                              learn_rate=0.001,
                              epoch=5,
                              batchSize=128,
                              steps=32383,
                              validation_steps=8096,
                              train_callbacks=()):
        history = LossHistory()
        #valid 8096  32383
        opt = Adam(lr=learn_rate)

        steps = steps / batchSize + 1 - 9
        validation_steps = validation_steps / batchSize + 1
        if steps % batchSize == 0:
            steps = steps / batchSize - 9
        if validation_steps % batchSize == 0:
            validation_steps = validation_steps / batchSize

        print(steps, validation_steps)
        self.classifier.compile(loss='binary_crossentropy',
                                optimizer=opt,
                                metrics=['accuracy'])

        earlyStopping = EarlyStopping(monitor='val_loss',
                                      patience=3,
                                      verbose=0,
                                      mode='auto')

        self.classifier.fit_generator(
            generator_train,
            steps_per_epoch=steps,
            epochs=epoch,
            verbose=1,
            validation_data=generator_valid,
            validation_steps=validation_steps,
            callbacks=[history, *train_callbacks, earlyStopping])
        fbeta_score = self._get_fbeta_score(self.classifier, X_valid, y_valid)
        return [history.train_losses, history.val_losses, fbeta_score]
Esempio n. 11
0
        def cross_val_model(params):
            keras_model = None
            # Destroys the current TF graph and creates a new one.
            # Useful to avoid clutter from old models / layers.
            K.clear_session()
            self._set_xpu_config(num_cpus, num_gpus)

            self.span = W = params.span
            (X_miss, X_train), (X_miss_val, X_test) = self.train_test_split(
                dataset,
                train_size=train_size,
                abnormal=abnormal,
            )
            if len(X_train) == 0:
                raise errors.NoData("insufficient training data")
            if len(X_test) == 0:
                raise errors.NoData("insufficient validation data")

            # expected input data shape: (batch_size, timesteps,)
            # network parameters
            input_shape = (W, )
            intermediate_dim = params.intermediate_dim
            latent_dim = params.latent_dim

            # VAE model = encoder + decoder
            # build encoder model
            main_input = Input(shape=input_shape)
            # bool vector to flag missing data points
            aux_input = Input(shape=input_shape)
            aux_output = Lambda(lambda x: x)(aux_input)
            x = Dense(intermediate_dim,
                      kernel_regularizer=regularizers.l2(0.01),
                      activation='relu')(main_input)
            z_mean = Dense(latent_dim, name='z_mean')(x)
            z_log_var = Dense(latent_dim, name='z_log_var')(x)

            # use reparameterization trick to push the sampling out as input
            # note that "output_shape" isn't necessary with the TensorFlow backend
            z = Lambda(sampling, output_shape=(latent_dim, ),
                       name='z')([z_mean, z_log_var])

            # build decoder model
            x = Dense(intermediate_dim,
                      kernel_regularizer=regularizers.l2(0.01),
                      activation='relu',
                      name='dense_1')(z)
            main_output = Dense(W, activation='linear', name='dense_2')(x)

            # instantiate Donut model
            keras_model = _Model([main_input, aux_input],
                                 [main_output, aux_output],
                                 name='donut')
            add_loss(keras_model, W)
            optimizer_cls = None
            if params.optimizer == 'adam':
                optimizer_cls = tf.keras.optimizers.Adam()

            keras_model.compile(optimizer=optimizer_cls, )

            _stop = EarlyStopping(
                monitor='val_loss',
                patience=5,
                verbose=_verbose,
                mode='auto',
            )
            keras_model.fit_generator(
                generator(X_train, X_miss, batch_size, keras_model),
                epochs=num_epochs,
                steps_per_epoch=len(X_train) / batch_size,
                verbose=_verbose,
                validation_data=([X_test, X_miss_val], None),
                callbacks=[_stop],
                workers=0,  # https://github.com/keras-team/keras/issues/5511
            )

            # How well did it do?
            score = keras_model.evaluate(
                [X_test, X_miss_val],
                batch_size=batch_size,
                verbose=_verbose,
            )

            self.current_eval += 1
            if progress_cb is not None:
                progress_cb(self.current_eval, max_evals)

            return score, keras_model
    def train_model(self,
                    x_train,
                    y_train,
                    epoch=5,
                    batch_size=128,
                    validation_split_size=0.2,
                    train_callbacks=()):
        history = LossHistory()

        X_train, X_valid, y_train, y_valid = train_test_split(
            x_train, y_train, test_size=validation_split_size)
        adam = Adam(lr=0.01, decay=1e-6)
        rms = RMSprop(lr=0.0001, decay=1e-6)
        self.classifier.compile(loss='binary_crossentropy',
                                optimizer='adam',
                                metrics=['accuracy'])

        datagen = ImageDataGenerator(
            featurewise_center=False,  # set input mean to 0 over the dataset
            samplewise_center=False,  # set each sample mean to 0
            featurewise_std_normalization=
            False,  # divide inputs by std of the dataset
            samplewise_std_normalization=False,  # divide each input by its std
            zca_whitening=False,  # apply ZCA whitening
            rotation_range=
            0,  # randomly rotate images in the range (degrees, 0 to 180)
            width_shift_range=
            0.1,  # randomly shift images horizontally (fraction of total width)
            height_shift_range=
            0.1,  # randomly shift images vertically (fraction of total height)
            horizontal_flip=True,  # randomly flip images
            vertical_flip=False)  # randomly flip images

        datagen.fit(X_train)
        print('X_train.shape[0]')
        print(X_train.shape[0])

        earlyStopping = EarlyStopping(monitor='val_loss',
                                      patience=2,
                                      verbose=0,
                                      mode='auto')

        self.classifier.fit_generator(datagen.flow(X_train,
                                                   y_train,
                                                   batch_size=batch_size),
                                      steps_per_epoch=X_train.shape[0] //
                                      batch_size,
                                      epochs=epoch,
                                      validation_data=(X_valid, y_valid))
        """
        self.classifier.fit(X_train, y_train,
                            batch_size=batch_size,
                            epochs=epoch,
                            verbose=1,
                            validation_data=(X_valid, y_valid),
                            callbacks=[history, *train_callbacks])
        """

        fbeta_score = self._get_fbeta_score(self.classifier, X_valid, y_valid)
        print(fbeta_score)
        return [history.train_losses, history.val_losses, fbeta_score]