parameters: (self, train_dataset, val_dataset, learning_rate, epochs, layers)
docstring: 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
docstring_summary: 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
docstring_tokens: 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
function: def train(self, train_dataset, val_dataset, learning_rate, epochs, layers):
          """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
              "heads": r"(mrcnn\_.*)|(rpn\_.*)|(fpn\_.*)",
              # From Resnet stage 4 layers and up
              "3+": r"(res3.*)|(bn3.*)|(res4.*)|(bn4.*)|(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)|(fpn\_.*)",
              "4+": r"(res4.*)|(bn4.*)|(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)|(fpn\_.*)",
              "5+": r"(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)|(fpn\_.*)",
              # All layers
              "all": ".*",
          }
          if layers in layer_regex.keys():
              layers = layer_regex[layers]
  
          # Data generators
          train_generator = data_generator(train_dataset, self.config, shuffle=True, 
                                                  batch_size=self.config.BATCH_SIZE)
          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,
                                              verbose=0, save_weights_only=True),
          ]
          
          # Common parameters to pass to fit_generator()
          fit_kwargs = {
              "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,
          }
          
          # 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,
              **fit_kwargs
              )
          self.epoch = max(self.epoch, epochs)
function_tokens: def train ( self , train_dataset , val_dataset , learning_rate , epochs , layers ) : assert self . mode == "training" , "Create model in training mode." # Pre-defined layer regular expressions layer_regex = { # all layers but the backbone "heads" : r"(mrcnn\_.*)|(rpn\_.*)|(fpn\_.*)" , # From Resnet stage 4 layers and up "3+" : r"(res3.*)|(bn3.*)|(res4.*)|(bn4.*)|(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)|(fpn\_.*)" , "4+" : r"(res4.*)|(bn4.*)|(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)|(fpn\_.*)" , "5+" : r"(res5.*)|(bn5.*)|(mrcnn\_.*)|(rpn\_.*)|(fpn\_.*)" , # All layers "all" : ".*" , } if layers in layer_regex . keys ( ) : layers = layer_regex [ layers ] # Data generators train_generator = data_generator ( train_dataset , self . config , shuffle = True , batch_size = self . config . BATCH_SIZE ) 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 , verbose = 0 , save_weights_only = True ) , ] # Common parameters to pass to fit_generator() fit_kwargs = { "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 , } # 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 , * * fit_kwargs ) self . epoch = max ( self . epoch , epochs )