parsing_rcnn_model.py
def train(self, train_dataset, val_dataset, learning_rate, epochs, layers, period):
        """Train the model.
        train_dataset, val_dataset: Training and validation Dataset objects.
        learning_rate: The learning rate to train with
        epochs: Number of training epochs. Note that previous training epochs
                are considered to be done alreay, so this actually determines
                the epochs to train in total rather than in this particaular
                call.
        layers: Allows selecting wich layers to train. It can be:
            - A regular expression to match layer names to train
            - One of these predefined values:
              heaads: The RPN, classifier and mask heads of the network
              all: All the layers
              3+: Train Resnet stage 3 and up
              4+: Train Resnet stage 4 and up
              5+: Train Resnet stage 5 and up
        """
        assert self.mode == "training", "Create model in training mode."

        # Pre-defined layer regular expressions
        layer_regex = {
            # all layers but the backbone
            "rpn": r"(rpn\_.*)",
            "roi_process_head": r"(mrcnn\_bbox\_.*)|(mrcnn\_class\_.*)|(mrcnn\_mask\_.*)|(mrcnn\_share\_.*)",
            "mask_heads": r"(mrcnn\_bbox\_.*)|(rpn\_.*)|(mrcnn\_class\_.*)|(mrcnn\_mask\_.*)|(mrcnn\_share\_.*)",
            "heads": r"(mrcnn\_.*)|(rpn\_.*)",
            # From a specific Resnet stage and up
            "3+": r"(res3.*)|(bn3.*)|(res4.*)|(bn4.*)|(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)",
            "4+": r"(res4.*)|(bn4.*)|(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)",
            "5+": r"(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)",
            # All layers
            "all": ".*",
            "first_stage": r"(mrcnn\_bbox\_.*)|(rpn\_.*)|(mrcnn\_class\_.*)|(mrcnn\_mask\_.*)|(mrcnn\_share\_.*)",
            "second_stage": r"(mrcnn\_.*)|(rpn\_.*)",
        }
        if layers in layer_regex.keys():
            layers = layer_regex[layers]

        # Data generators
        print("get train generator")
        train_generator = data_generator(train_dataset, self.config, shuffle=True, 
                                         batch_size=self.config.BATCH_SIZE)
        print("get val generator")
        val_generator = data_generator(val_dataset, self.config, shuffle=True, 
                                       batch_size=self.config.BATCH_SIZE)

        # Callbacks
        callbacks = [
            keras.callbacks.TensorBoard(log_dir=self.log_dir,
                                        histogram_freq=0, write_graph=True, write_images=False),
            keras.callbacks.ModelCheckpoint(self.checkpoint_path, period=period,
                                            verbose=0, save_weights_only=True),
        ]

        # Train
        log("\nStarting at epoch {}. LR={}\n".format(self.epoch, learning_rate))
        log("Checkpoint Path: {}".format(self.checkpoint_path))
        self.set_trainable(layers)
        self.compile(learning_rate, self.config.LEARNING_MOMENTUM)

        self.keras_model.fit_generator(
            train_generator,
            initial_epoch=self.epoch,
            epochs=epochs,
            steps_per_epoch=self.config.STEPS_PER_EPOCH,
            callbacks=callbacks,
            validation_data=next(val_generator),
            validation_steps=self.config.VALIDATION_STEPS,
            max_queue_size=100,
            workers=max(self.config.BATCH_SIZE // 2, 2),
            use_multiprocessing=True,
        )
        self.epoch = max(self.epoch, epochs)
Train the model.
        train_dataset, val_dataset: Training and validation Dataset objects.
        learning_rate: The learning rate to train with
        epochs: Number of training epochs. Note that previous training epochs
                are considered to be done alreay, so this actually determines
                the epochs to train in total rather than in this particaular
                call.
        layers: Allows selecting wich layers to train. It can be:
            - A regular expression to match layer names to train
            - One of these predefined values:
              heaads: The RPN, classifier and mask heads of the network
              all: All the layers
              3+: Train Resnet stage 3 and up
              4+: Train Resnet stage 4 and up
              5+: Train Resnet stage 5 and up
        
Train the model.
train_dataset, val_dataset: Training and validation Dataset objects.
learning_rate: The learning rate to train with
epochs: Number of training epochs. Note that previous training epochs
are considered to be done alreay, so this actually determines
the epochs to train in total rather than in this particaular
call.
layers: Allows selecting wich layers to train.
Train the model . train_dataset val_dataset : Training and validation Dataset objects . learning_rate : The learning rate to train with epochs : Number of training epochs . Note that previous training epochs are considered to be done alreay so this actually determines the epochs to train in total rather than in this particaular call . layers : Allows selecting wich layers to train .
def train(self, train_dataset, val_dataset, learning_rate, epochs, layers, period):
        assert self.mode == "training", "Create model in training mode."
        layer_regex = {
            "rpn": r"(rpn\_.*)",
            "roi_process_head": r"(mrcnn\_bbox\_.*)|(mrcnn\_class\_.*)|(mrcnn\_mask\_.*)|(mrcnn\_share\_.*)",
            "mask_heads": r"(mrcnn\_bbox\_.*)|(rpn\_.*)|(mrcnn\_class\_.*)|(mrcnn\_mask\_.*)|(mrcnn\_share\_.*)",
            "heads": r"(mrcnn\_.*)|(rpn\_.*)",
            "3+": r"(res3.*)|(bn3.*)|(res4.*)|(bn4.*)|(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)",
            "4+": r"(res4.*)|(bn4.*)|(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)",
            "5+": r"(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)",
            "all": ".*",
            "first_stage": r"(mrcnn\_bbox\_.*)|(rpn\_.*)|(mrcnn\_class\_.*)|(mrcnn\_mask\_.*)|(mrcnn\_share\_.*)",
            "second_stage": r"(mrcnn\_.*)|(rpn\_.*)",
        }
        if layers in layer_regex.keys():
            layers = layer_regex[layers]
        print("get train generator")
        train_generator = data_generator(train_dataset, self.config, shuffle=True, 
                                         batch_size=self.config.BATCH_SIZE)
        print("get val generator")
        val_generator = data_generator(val_dataset, self.config, shuffle=True, 
                                       batch_size=self.config.BATCH_SIZE)
        callbacks = [
            keras.callbacks.TensorBoard(log_dir=self.log_dir,
                                        histogram_freq=0, write_graph=True, write_images=False),
            keras.callbacks.ModelCheckpoint(self.checkpoint_path, period=period,
                                            verbose=0, save_weights_only=True),
        ]
        log("\nStarting at epoch {}. LR={}\n".format(self.epoch, learning_rate))
        log("Checkpoint Path: {}".format(self.checkpoint_path))
        self.set_trainable(layers)
        self.compile(learning_rate, self.config.LEARNING_MOMENTUM)
        self.keras_model.fit_generator(
            train_generator,
            initial_epoch=self.epoch,
            epochs=epochs,
            steps_per_epoch=self.config.STEPS_PER_EPOCH,
            callbacks=callbacks,
            validation_data=next(val_generator),
            validation_steps=self.config.VALIDATION_STEPS,
            max_queue_size=100,
            workers=max(self.config.BATCH_SIZE // 2, 2),
            use_multiprocessing=True,
        )
        self.epoch = max(self.epoch, epochs)
def train ( self , train_dataset , val_dataset , learning_rate , epochs , layers , period ) : assert self . mode == "training" , "Create model in training mode." layer_regex = { "rpn" : r"(rpn\_.*)" , "roi_process_head" : r"(mrcnn\_bbox\_.*)|(mrcnn\_class\_.*)|(mrcnn\_mask\_.*)|(mrcnn\_share\_.*)" , "mask_heads" : r"(mrcnn\_bbox\_.*)|(rpn\_.*)|(mrcnn\_class\_.*)|(mrcnn\_mask\_.*)|(mrcnn\_share\_.*)" , "heads" : r"(mrcnn\_.*)|(rpn\_.*)" , "3+" : r"(res3.*)|(bn3.*)|(res4.*)|(bn4.*)|(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)" , "4+" : r"(res4.*)|(bn4.*)|(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)" , "5+" : r"(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)" , "all" : ".*" , "first_stage" : r"(mrcnn\_bbox\_.*)|(rpn\_.*)|(mrcnn\_class\_.*)|(mrcnn\_mask\_.*)|(mrcnn\_share\_.*)" , "second_stage" : r"(mrcnn\_.*)|(rpn\_.*)" , } if layers in layer_regex . keys ( ) : layers = layer_regex [ layers ] print ( "get train generator" ) train_generator = data_generator ( train_dataset , self . config , shuffle = True , batch_size = self . config . BATCH_SIZE ) print ( "get val generator" ) val_generator = data_generator ( val_dataset , self . config , shuffle = True , batch_size = self . config . BATCH_SIZE ) callbacks = [ keras . callbacks . TensorBoard ( log_dir = self . log_dir , histogram_freq = 0 , write_graph = True , write_images = False ) , keras . callbacks . ModelCheckpoint ( self . checkpoint_path , period = period , verbose = 0 , save_weights_only = True ) , ] log ( "\nStarting at epoch {}. LR={}\n" . format ( self . epoch , learning_rate ) ) log ( "Checkpoint Path: {}" . format ( self . checkpoint_path ) ) self . set_trainable ( layers ) self . compile ( learning_rate , self . config . LEARNING_MOMENTUM ) self . keras_model . fit_generator ( train_generator , initial_epoch = self . epoch , epochs = epochs , steps_per_epoch = self . config . STEPS_PER_EPOCH , callbacks = callbacks , validation_data = next ( val_generator ) , validation_steps = self . config . VALIDATION_STEPS , max_queue_size = 100 , workers = max ( self . config . BATCH_SIZE // 2 , 2 ) , use_multiprocessing = True , ) self . epoch = max ( self . epoch , epochs )
Train the model.
Train the model .
"""Train the model.
        train_dataset, val_dataset: Training and validation Dataset objects.
        learning_rate: The learning rate to train with
        epochs: Number of training epochs. Note that previous training epochs
                are considered to be done alreay, so this actually determines
                the epochs to train in total rather than in this particaular
                call.
        layers: Allows selecting wich layers to train. It can be:
            - A regular expression to match layer names to train
            - One of these predefined values:
              heaads: The RPN, classifier and mask heads of the network
              all: All the layers
              3+: Train Resnet stage 3 and up
              4+: Train Resnet stage 4 and up
              5+: Train Resnet stage 5 and up
        """ # Pre-defined layer regular expressions # all layers but the backbone # From a specific Resnet stage and up # All layers # Data generators # Callbacks # Train
self
train_dataset
val_dataset
learning_rate
epochs
layers
period