  # <<< START: from Dfaker >>> #
  def PenalizedLoss(mask, loss_func, mask_prop=1.0, mask_scaling=1.0):  # pylint: disable=invalid-name
      """ Plaidml + tf Penalized loss function """
      mask_as_k_inv_prop = 1 - mask_prop
      mask = (mask * mask_prop) + mask_as_k_inv_prop
  
      def inner_loss(y_true, y_pred):
          # Branching because tensorflows broadcasting is wonky and
          # plaidmls concatenate is implemented ineficient.
 
</s> remove def PenalizedLoss(mask, loss_func, mask_prop=1.0):  # pylint: disable=invalid-name
 </s> add def PenalizedLoss(mask, loss_func, mask_prop=1.0, mask_scaling=1.0):  # pylint: disable=invalid-name </s> remove     def gaussian_blur(radius=2.0):
        def gaussian(var_x, radius, sigma):
            return np.exp(-(float(var_x) - float(radius)) ** 2 / (2 * sigma ** 2))

        def make_kernel(sigma):
            kernel_size = max(3, int(2 * 2 * sigma + 1))
            mean = np.floor(0.5 * kernel_size)
            kernel_1d = np.array([gaussian(x, mean, sigma) for x in range(kernel_size)])
            np_kernel = np.outer(kernel_1d, kernel_1d).astype(dtype=K.floatx())
            kernel = np_kernel / np.sum(np_kernel)
            return kernel

        gauss_kernel = make_kernel(radius)
        gauss_kernel = gauss_kernel[:, :, np.newaxis, np.newaxis]

        def func(input_):
            inputs = [input_[:, :, :, i:i + 1] for i in range(K.int_shape(input_)[-1])]
            outputs = [K.conv2d(inp, K.constant(gauss_kernel), strides=(1, 1), padding="same")
                       for inp in inputs]
            return K.concatenate(outputs, axis=-1)
        return func
 </s> add  </s> remove     def largest_output(self):
        """ Return the index of the largest face output """
        max_size = max(shape[0] for shape in self.loss_shapes if shape[2] == 3)
        largest_idx = [idx for idx, shape in enumerate(self.loss_shapes)
                       if shape[0] == max_size and shape[2] == 3][0]
        return largest_idx
 </s> add     def mask_input(self):
        """ Return the mask input or None """
        mask_inputs = [inp for inp in self.inputs if inp.name.startswith("mask")]
        if not mask_inputs:
            return None
        return mask_inputs[0]

    @property
    def mask_shape(self):
        """ Return the mask shape """
        if self.mask_input is None:
            return None
        return K.int_shape(self.mask_input)[1:] </s> remove     def loss_shapes(self):
 </s> add     def selected_loss(self):
        """ Return the selected loss function """
        retval = self.loss_dict[self.config.get("loss_function", "mae")]
        logger.debug(retval)
        return retval

    @property
    def selected_mask_loss(self):
        """ Return the selected mask loss function. Currently returns mse """
        retval = self.loss_dict["mse"]
        logger.debug(retval)
        return retval

    @property
    def output_shapes(self): </s> remove     def get_loss_functions(self, mask):
 </s> add     def get_loss_functions(self):
    def scale_mask(mask, scaling):
        """ Scale the input mask to be the same size as the input face """
        if scaling != 1.0:
            size = round(1 / scaling)
            mask = K.pool2d(mask,
                            pool_size=(size, size),
                            strides=(size, size),
                            padding="valid",
                            data_format=K.image_data_format(),
                            pool_mode="avg")
        logger.debug("resized tensor: %s", mask)
        return mask

    mask = scale_mask(mask, mask_scaling)
  # <<< START: from Dfaker >>> #
  def PenalizedLoss(mask, loss_func, mask_prop=1.0, mask_scaling=1.0):  # pylint: disable=invalid-name
      """ Plaidml + tf Penalized loss function """
    def scale_mask(mask, scaling):
        """ Scale the input mask to be the same size as the input face """
        if scaling != 1.0:
            size = round(1 / scaling)
            mask = K.pool2d(mask,
                            pool_size=(size, size),
                            strides=(size, size),
                            padding="valid",
                            data_format=K.image_data_format(),
                            pool_mode="avg")
        logger.debug("resized tensor: %s", mask)
        return mask

    mask = scale_mask(mask, mask_scaling)
      mask_as_k_inv_prop = 1 - mask_prop
      mask = (mask * mask_prop) + mask_as_k_inv_prop
  
      def inner_loss(y_true, y_pred):
          # Branching because tensorflows broadcasting is wonky and
          # plaidmls concatenate is implemented ineficient.
keep keep add keep keep keep keep keep keep
 Fix penalized loss for multiscale decoders 
lib/model/losses.py