              2 * covar_true_pred + self.c_2)
          denom = (K.square(u_true) + K.square(u_pred) + self.c_1) * (
              var_pred + var_true + self.c_2)
          ssim /= denom  # no need for clipping, c_1 + c_2 make the denorm non-zero
          return K.mean((1.0 - ssim) / 2.0)
  
      @staticmethod
      def _preprocess_padding(padding):
          """Convert keras padding to tensorflow padding.
  
 
</s> remove class PenalizedLoss():  # pylint:disable=too-few-public-methods
    """ Penalized Loss function.

    Applies the given loss function just to the masked area of the image.

    Parameters
    ----------
    loss_func: function
        The actual loss function to use
    mask_prop: float, optional
        The amount of mask propagation. Default: `1.0`
    """
    def __init__(self, loss_func, mask_prop=1.0):
        self._loss_func = loss_func
        self._mask_prop = mask_prop

    def __call__(self, y_true, y_pred):
        """ Apply the loss function to the masked area of the image.

        Parameters
        ----------
        y_true: tensor or variable
            The ground truth value. This should contain the mask in the 4th channel that will be
            split off for penalizing.
        y_pred: tensor or variable
            The predicted value

        Returns
        -------
        tensor
            The Loss value
        """
        mask = self._prepare_mask(K.expand_dims(y_true[..., -1], axis=-1))
        y_true = y_true[..., :-1]
        n_true = y_true * mask
        n_pred = y_pred * mask
        if isinstance(self._loss_func, DSSIMObjective):
            # Extract Image Patches in SSIM requires that y_pred be of a known shape, so
            # specifically reshape the tensor.
            n_pred = K.reshape(n_pred, K.int_shape(y_pred))
        return self._loss_func(n_true, n_pred)

    def _prepare_mask(self, mask):
        """ Prepare the masks for calculating loss

        Parameters
        ----------
        mask: :class:`numpy.ndarray`
            The masks for the current batch

        Returns
        -------
        tensor
            The prepared mask for applying to loss
        """
        mask_as_k_inv_prop = 1 - self._mask_prop
        mask = (mask * self._mask_prop) + mask_as_k_inv_prop
        return mask


 </s> add  </s> remove class PenalizedLoss(tf.keras.losses.Loss):
    """ Penalized Loss function.

    Applies the given loss function just to the masked area of the image.

    Parameters
    ----------
    loss_func: function
        The actual loss function to use
    mask_prop: float, optional
        The amount of mask propagation. Default: `1.0`
    """
    def __init__(self, loss_func, mask_prop=1.0):
        super().__init__(name="penalized_loss")
        self._loss_func = compile_utils.LossesContainer(loss_func)
        self._mask_prop = mask_prop

    def call(self, y_true, y_pred):
        """ Apply the loss function to the masked area of the image.

        Parameters
        ----------
        y_true: tensor or variable
            The ground truth value. This should contain the mask in the 4th channel that will be
            split off for penalizing.
        y_pred: tensor or variable
            The predicted value

        Returns
        -------
        tensor
            The Loss value
        """
        mask = self._prepare_mask(K.expand_dims(y_true[..., -1], axis=-1))
        y_true = y_true[..., :-1]
        n_true = K.concatenate([y_true[:, :, :, i:i+1] * mask for i in range(3)], axis=-1)
        n_pred = K.concatenate([y_pred[:, :, :, i:i+1] * mask for i in range(3)], axis=-1)
        return self._loss_func(n_true, n_pred)

    def _prepare_mask(self, mask):
        """ Prepare the masks for calculating loss

        Parameters
        ----------
        mask: :class:`numpy.ndarray`
            The masks for the current batch

        Returns
        -------
        tensor
            The prepared mask for applying to loss
        """
        mask_as_k_inv_prop = 1 - self._mask_prop
        mask = (mask * self._mask_prop) + mask_as_k_inv_prop
        return mask


 </s> add  </s> remove             side_inputs, side_targets = self._get_next(side)
            if self._model.config["penalized_mask_loss"]:
                side_targets = self._compile_masks(side_targets)
                if not self._model.config["learn_mask"]:  # Remove masks from the model targets
                    side_targets = side_targets[:-1]
 </s> add             batch = next(self._feeds[side])
            side_inputs = batch["feed"]
            side_targets = self._compile_mask_targets(batch["targets"],
                                                      batch["masks"],
                                                      batch.get("additional_masks", None))
            if self._model.config["learn_mask"]:
                side_targets = side_targets + [batch["masks"]] </s> remove     def _get_next(self, side):
        """ Return the next batch from the :class:`lib.training_data.TrainingDataGenerator` for
        this feeder ready for feeding into the model.

        Returns
        -------
        model_inputs: list
            A list of :class:`numpy.ndarray` for feeding into the model
        model_targets: list
            A list of :class:`numpy.ndarray` for comparing the output of the model
        """
        logger.trace("Generating targets")
        batch = next(self._feeds[side])
        targets_use_mask = (self._model.config["learn_mask"]
                            or self._model.config["penalized_mask_loss"])
        model_targets = batch["targets"] + batch["masks"] if targets_use_mask else batch["targets"]
        return batch["feed"], model_targets

    @classmethod
    def _compile_masks(cls, targets):
        """ Compile the masks into the targets for penalized loss.
 </s> add     def _compile_mask_targets(self, targets, masks, additional_masks):
        """ Compile the masks into the targets for penalized loss and for targeted learning. </s> remove         for func, weight in zip(self._loss_functions, self._loss_weights):
            loss += (K.mean(func(y_true, y_pred)) * weight)
 </s> add         for func, weight, mask_channel in zip(self._loss_functions,
                                              self._loss_weights,
                                              self._mask_channels):
            logger.debug("Processing loss function: (func: %s, weight: %s, mask_channel: %s)",
                         func, weight, mask_channel)
            n_true, n_pred = self._apply_mask(y_true, y_pred, mask_channel)
            if isinstance(func, DSSIMObjective):
                # Extract Image Patches in SSIM requires that y_pred be of a known shape, so
                # specifically reshape the tensor.
                n_pred = K.reshape(n_pred, K.int_shape(y_pred))
            this_loss = func(n_true, n_pred)
            loss_dims = K.ndim(this_loss)
            loss += (K.mean(this_loss, axis=list(range(1, loss_dims))) * weight)
        return (1.0 - ssim) / 2.0
              2 * covar_true_pred + self.c_2)
          denom = (K.square(u_true) + K.square(u_pred) + self.c_1) * (
              var_pred + var_true + self.c_2)
          ssim /= denom  # no need for clipping, c_1 + c_2 make the denorm non-zero
        return (1.0 - ssim) / 2.0
  
      @staticmethod
      def _preprocess_padding(padding):
          """Convert keras padding to tensorflow padding.
  
keep keep keep keep replace keep keep keep keep keep
 Add Mouth and Eye Priority to Loss options (#1054)

* Priority Training for Mouth and Eyes - Tensorflow

* Use chosen loss function for area multipliers

* loss multipliers for AMD

* Fix mask multipliers for plaid and roll PenalizedMaskLoss into LossWrapper

* losses_tf: roll PenalizedMaskLoss into LossWrapper 
lib/model/losses_plaid.py