              self.save()
          logger.debug("Replaced config. New config: %s", _CONFIG)
          logger.info("Using configuration saved in state file")
  
      def update_legacy_config(self):
          """ Update legacy state config files with the new loss formating
          """
          prior = "dssim_loss"
          new = "loss_function"
          if prior not in self.config:
              return False
 
</s> remove         prior = "dssim_loss"
        new = "loss_function"
        if prior not in self.config:
            return False
        self.config[new] = "ssim" if self.config[prior] else "mae"
        del self.config[prior]
        logger.info("Updated config from older dssim format. New config loss function: %s",
                    self.config[new])
        return True
 </s> add         logger.debug("Checking for legacy state file update")
        priors = ["dssim_loss", "penalized_mask_loss", "mask_type"]
        new_items = ["loss_function", "learn_mask", "mask_type"]
        updated = False
        for old, new in zip(priors, new_items):
            if old not in self.config:
                logger.debug("Legacy item '%s' not in config. Skipping update", old)
                continue

            # dssim_loss > loss_function
            if old == "dssim_loss":
                self.config[new] = "ssim" if self.config[old] else "mae"
                del self.config[old]
                updated = True
                logger.info("Updated config from legacy dssim format. New config loss "
                            "function: '%s'", self.config[new])
                continue

            # Add learn mask option and set to True if model has "penalized_mask_loss" specified
            if old == "penalized_mask_loss" and new not in self.config:
                self.config[new] = self.config["penalized_mask_loss"]
                updated = True
                logger.info("Added new 'learn_mask' config item for this model. Value set to: %s",
                            self.config[new])
                continue

            # Replace removed masks with most similar equivalent
            if old == "mask_type" and self.config[old] in ("facehull", "dfl_full"):
                old_mask = self.config[old]
                self.config[new] = "components"
                updated = True
                logger.info("Updated 'mask_type' from '%s' to '%s' for this model",
                            old_mask, self.config[new])

        logger.debug("State file updated for legacy config: %s", updated)
        return updated </s> remove         legacy_update = self.update_legacy_config()
 </s> add  </s> remove         """ Monkeypatch Memory Saving Gradients if requested """
 </s> add         """ Monkey-patch Memory Saving Gradients if requested """ </s> remove                      "training_opts: %s, landmarks: %s, config: %s)",
 </s> add                      "training_opts: %s, landmarks: %s, masks: %s, config: %s)",
    def _update_legacy_config(self):
        """ Legacy updates for new config additions.

        When new config items are added to the Faceswap code, existing model state files need to be
        updated to handle these new items.

        Current existing legacy update items:

            * loss - If old `dssim_loss` is ``true`` set new `loss_function` to `ssim` otherwise
            set it to `mae`. Remove old `dssim_loss` item

            * masks - If `penalized_mask_loss` exists but `learn_mask` does not, then add the
            latter and set to the same value as `penalized_mask_loss`.

            * masks type - Replace removed masks 'dfl_full' and 'facehull' with `components` mask

        Returns
        -------
        bool
            ``True`` if legacy items exist and state file has been updated, otherwise ``False``
              self.save()
          logger.debug("Replaced config. New config: %s", _CONFIG)
          logger.info("Using configuration saved in state file")
  
    def _update_legacy_config(self):
        """ Legacy updates for new config additions.

        When new config items are added to the Faceswap code, existing model state files need to be
        updated to handle these new items.

        Current existing legacy update items:

            * loss - If old `dssim_loss` is ``true`` set new `loss_function` to `ssim` otherwise
            set it to `mae`. Remove old `dssim_loss` item

            * masks - If `penalized_mask_loss` exists but `learn_mask` does not, then add the
            latter and set to the same value as `penalized_mask_loss`.

            * masks type - Replace removed masks 'dfl_full' and 'facehull' with `components` mask

        Returns
        -------
        bool
            ``True`` if legacy items exist and state file has been updated, otherwise ``False``
    def _update_legacy_config(self):
        """ Legacy updates for new config additions.

        When new config items are added to the Faceswap code, existing model state files need to be
        updated to handle these new items.

        Current existing legacy update items:

            * loss - If old `dssim_loss` is ``true`` set new `loss_function` to `ssim` otherwise
            set it to `mae`. Remove old `dssim_loss` item

            * masks - If `penalized_mask_loss` exists but `learn_mask` does not, then add the
            latter and set to the same value as `penalized_mask_loss`.

            * masks type - Replace removed masks 'dfl_full' and 'facehull' with `components` mask

        Returns
        -------
        bool
            ``True`` if legacy items exist and state file has been updated, otherwise ``False``
          """
          prior = "dssim_loss"
          new = "loss_function"
          if prior not in self.config:
              return False
keep keep keep keep replace replace keep keep keep keep keep
 Smart Masks - Training Implementation (#914)

* Smart Masks - Training

- Reinstate smart mask training code
- Reinstate mask_type back to model.config
- change 'replicate_input_mask to 'learn_mask'
- Add learn mask option
- Add mask loading from alignments to plugins.train.trainer
- Add mask_blur and mask threshold options
- _base.py - Pass mask options through training_opts dict
- plugins.train.model - check for mask_type not None for learn_mask and penalized_mask_loss
- Limit alignments loading to just those faces that appear in the training folder
- Raise error if not all training images have an alignment, and alignment file is required
- lib.training_data - Mask generation code
- lib.faces_detect - cv2 dimension stripping bugfix
- Remove cv2 linting code

* Update mask helptext in cli.py

* Fix Warp to Landmarks
Remove SHA1 hashing from training data

* Update mask training config

* Capture missing masks at training init

* lib.image.read_image_batch - Return filenames with batch for ordering

* scripts.train - Documentation

* plugins.train.trainer - documentation

* Ensure backward compatibility.
Fix convert for new predicted masks

* Update removed masks to components for legacy models. 
plugins/train/model/_base.py