Beispiel #1
0
    def train(self,
              x_train,
              batch_size,
              epochs,
              run_folder,
              print_every_n_batches=100,
              initial_epoch=0,
              lr_decay=1):

        custom_callback = CustomCallback(run_folder, print_every_n_batches,
                                         initial_epoch, self)
        lr_sched = step_decay_schedule(initial_lr=self.learning_rate,
                                       decay_factor=lr_decay,
                                       step_size=1)

        checkpoint2 = ModelCheckpoint(os.path.join(run_folder,
                                                   'weights/weights.h5'),
                                      save_weights_only=True,
                                      verbose=1)

        callbacks_list = [checkpoint2, custom_callback, lr_sched]

        self.model.fit(x_train,
                       x_train,
                       batch_size=batch_size,
                       shuffle=True,
                       epochs=epochs,
                       initial_epoch=initial_epoch,
                       callbacks=callbacks_list)
Beispiel #2
0
    def train(self,
              x_train,
              batch_size,
              epochs,
              run_folder,
              print_every_n_batches=100,
              initial_epoch=0,
              lr_decay=1):
        ## Custom callback to print every n batches and squeeze images to a particular marker
        custom_callback = CustomCallback(run_folder, print_every_n_batches,
                                         initial_epoch, self)
        ## Custom Step delay scheduler for reducing the learning rate
        lr_sched = step_decay_schedule(initial_lr=self.learning_rate,
                                       decay_factor=lr_decay,
                                       step_size=1)
        ## For saving the weight parameters every epoc
        checkpoint2 = ModelCheckpoint(filepath=os.path.join(
            run_folder, 'weights/weights.h5'),
                                      save_weights_only=True,
                                      verbose=1)

        callbacks_list = [checkpoint2, custom_callback, lr_sched]

        self.model.fit(x=x_train,
                       y=x_train,
                       batch_size=batch_size,
                       shuffle=True,
                       epochs=epochs,
                       initial_epoch=initial_epoch,
                       callbacks=callbacks_list)
    def train(self,
              batch_size,
              epochs,
              run_folder,
              directory,
              print_every_n_batches=100,
              initial_epoch=0,
              lr_decay=1):

        x_train = keras.preprocessing.image_dataset_from_directory(
            directory,
            labels="inferred",
            label_mode="int",
            class_names=None,
            color_mode="rgb",
            batch_size=32,
            image_size=(128, 128),
            shuffle=True,
            seed=None,
            validation_split=None,
            subset=None,
            interpolation="bilinear",
            follow_links=False,
        )

        custom_callback = CustomCallback(run_folder, print_every_n_batches,
                                         initial_epoch, self)
        lr_sched = step_decay_schedule(initial_lr=self.learning_rate,
                                       decay_factor=lr_decay,
                                       step_size=1)

        checkpoint_filepath = os.path.join(
            run_folder, "weights/weights-{epoch:03d}-{loss:.2f}.h5")
        checkpoint1 = ModelCheckpoint(checkpoint_filepath,
                                      save_weights_only=True,
                                      verbose=1)
        checkpoint2 = ModelCheckpoint(os.path.join(run_folder,
                                                   'weights/weights.h5'),
                                      save_weights_only=True,
                                      verbose=1)

        callbacks_list = [checkpoint1, checkpoint2, custom_callback, lr_sched]

        self.model.fit(x_train,
                       x_train,
                       batch_size=batch_size,
                       shuffle=True,
                       epochs=epochs,
                       initial_epoch=initial_epoch,
                       callbacks=callbacks_list)
Beispiel #4
0
    def train_with_generator(self, data_flow, epochs, run_folder, print_every_n_batches = 100, initial_epoch = 0, lr_decay = 1):

        custom_callback = CustomCallback(run_folder, print_every_n_batches, initial_epoch, self)
        lr_sched = step_decay_schedule(initial_lr=self.learning_rate, decay_factor=lr_decay, step_size=1)

        checkpoint_filepath=os.path.join(run_folder, "weights/weights-{epoch:03d}-{loss:.2f}.h5")
        checkpoint1 = ModelCheckpoint(checkpoint_filepath, save_weights_only = True, verbose=1)
        checkpoint2 = ModelCheckpoint(os.path.join(run_folder, 'weights/weights.h5'), save_weights_only = True, verbose=1)

        callbacks_list = [checkpoint1, checkpoint2, custom_callback, lr_sched]
                
        self.model.fit_generator(
            data_flow
            , shuffle = True
            , epochs = epochs
            , initial_epoch = initial_epoch
            , callbacks = callbacks_list
            )
Beispiel #5
0
    def train(self,
              x_train,
              batch_size,
              epochs,
              run_folder,
              print_every_n_batches=100,
              initial_epoch=0,
              lr_decay=1):

        custom_callback = CustomCallback(run_folder, print_every_n_batches,
                                         initial_epoch, self)
        lr_sched = step_decay_schedule(initial_lr=self.learning_rate,
                                       decay_factor=lr_decay,
                                       step_size=1)

        checkpoint_filepath = os.path.join(
            run_folder, "weights/weights.h5"
        )  # https://github.com/davidADSP/GDL_code/issues/73#issuecomment-725401490
        # checkpoint_filepath=os.path.join(run_folder, "weights/weights-{epoch:03d}-{loss:.2f}.h5")
        checkpoint1 = ModelCheckpoint(checkpoint_filepath,
                                      save_weights_only=True,
                                      verbose=1)
        checkpoint2 = ModelCheckpoint(os.path.join(run_folder,
                                                   'weights/weights.h5'),
                                      save_weights_only=True,
                                      verbose=1)

        callbacks_list = [checkpoint1, checkpoint2, custom_callback, lr_sched]

        self.model.fit(x_train,
                       x_train,
                       batch_size=batch_size,
                       shuffle=True,
                       epochs=epochs,
                       initial_epoch=initial_epoch,
                       callbacks=callbacks_list)