def _loss(self, x: "torch.Tensor") -> tuple["torch.Tensor", "torch.Tensor"]:
        """
        Compute the weight of each pixel and the overload loss for a given image.

        :param x: A given image
        :return: Overload loss and the weight of each pixel
        """

        import torch

        adv_logits = self.estimator.model.model(x)
        if isinstance(adv_logits, tuple):
            adv_logits = adv_logits[0]

        conf = adv_logits[..., 4]
        prob = adv_logits[..., 5:]
        prob = torch.where(conf[:, :, None] * prob > self.threshold, torch.ones_like(prob), prob)
        prob = torch.sum(prob, dim=2)
        conf = conf * prob

        ind_loss = -(1.0 - conf) * (1.0 - conf)
        ind_loss = torch.sum(ind_loss, dim=1)

        pixel_weight = torch.ones_like(x)
        pixel_weight.requires_grad_(False)
        with torch.inference_mode():
            stride_x = x.shape[-2] // self.num_grid
            stride_y = x.shape[-1] // self.num_grid
            grid_box = torch.zeros((0, 4), device=x.device)
            for i_i in range(self.num_grid):
                for j_j in range(self.num_grid):
                    x_1 = i_i * stride_x
                    y_1 = j_j * stride_y
                    x_2 = min(x_1 + stride_x, x.shape[-2])
                    y_2 = min(y_1 + stride_y, x.shape[-1])
                    b_b = torch.as_tensor([x_1, y_1, x_2, y_2], device=x.device)[None, :]
                    grid_box = torch.cat([grid_box, b_b], dim=0)

            for x_i in range(x.shape[0]):
                xyhw = adv_logits[x_i, :, :4]
                prob = torch.max(adv_logits[x_i, :, 5:], dim=1).values
                box_idx = adv_logits[x_i, :, 4] * prob > self.threshold
                xyhw = xyhw[box_idx]
                c_xyxy = self.xywh2xyxy(xyhw)
                scores = box_iou(grid_box, c_xyxy)
                scores = torch.where(scores > 0.0, torch.ones_like(scores), torch.zeros_like(scores))
                scores = torch.sum(scores, dim=1)

                # a native implementation:
                # Increase the weight of the grid with fewer objects
                idx_min = torch.argmin(scores)
                grid_min = grid_box[idx_min]
                x_1, y_1, x_2, y_2 = grid_min.int()
                pixel_weight[x_i, :, y_1:y_2, x_1:x_2] = pixel_weight[x_i, :, y_1:y_2, x_1:x_2] * 2
                pixel_weight = pixel_weight / torch.max(pixel_weight[x_i, :]) / 255.0

        return ind_loss, pixel_weight

        Compute the weight of each pixel and the overload loss for a given image.

        :param x: A given image
        :return: Overload loss and the weight of each pixel
        
art/attacks/evasion/overload/overload.py