---
title: Inner-MPDIoU Loss for PCB Defect Detection
url: https://www.emergentmind.com/topics/inner-mpdiou-loss
type: topic
---

# Inner-MPDIoU Loss for PCB Defect Detection

Inner-MPDIoU Loss is a boundary-based loss function developed to improve the localization accuracy of tiny objects in object detection tasks, particularly in printed circuit board (PCB) defect detection. It modifies traditional IoU-based losses by focusing the overlap computation on a scale-adjustable inner region of the ground-truth bounding box and incorporating penalties for drift in both box center and corner alignment, normalized for scale invariance. The method is introduced and evaluated in “Multi-Scale PCB Defect Detection with YOLOv8 Network Improved via Pruning and Lightweight Network” [2507.17176] to address challenges in high-density, small-object detection scenarios.

## 1. Mathematical Formulation

The Inner-MPDIoU loss is constructed through several key steps, utilizing both the predicted bounding box $B$ and the ground-truth box $B^{gt}$ represented by their edges. It introduces an auxiliary ground-truth box $B^t$, generated by scaling $B^{gt}$ about its center by ratio $r \in [0.5, 1.5]$. The steps are:

1. **Auxiliary Box Construction**:
   $$
   \begin{align*}
   b_l^t &= x^{gt} - W^{gt} \cdot r \\
   b_r^t &= x^{gt} + W^{gt} \cdot r \\
   b_t^t &= y^{gt} - H^{gt} \cdot r \\
   b_b^t &= y^{gt} + H^{gt} \cdot r
   \end{align*}
   $$
   Here, $(x^{gt}, y^{gt})$ is the center, and $W^{gt}, H^{gt}$ are width and height.

2. **Intersection Area**:
   $$
   I = \max(0, \min(b_r, b_r^t)-\max(b_l, b_l^t)) \cdot \max(0, \min(b_b, b_b^t)-\max(b_t, b_t^t))
   $$

3. **Area Calculations**:
   $$
   A^t = 4r^2 W^{gt} H^{gt} \\
   A = (b_r - b_l) \cdot (b_b - b_t)
   $$

4. **Union**:
   $$
   U = A + A^t - I
   $$

5. **Corner-Point Drift Penalty**:
   $$
   d_1 = (b_l^t - b_l)^2 + (b_t^t - b_t)^2 \\
   d_2 = (b_r^t - b_r)^2 + (b_b^t - b_b)^2
   $$
   These terms measure discrepancies at the top-left and bottom-right corners.

6. **Image-Scale Normalization**:
   $$
   \rho^2 = W_{img}^2 + H_{img}^2
   $$

7. **Inner-MPDIoU Metric**:
   $$
   \text{InnerMPDIoU} = \frac{I}{U} - \frac{d_1 + d_2}{\rho^2}
   $$

8. **Loss Function**:
   $$
   \mathcal{L}_{\text{Inner-MPDIoU}} = 1 - \text{InnerMPDIoU}
   $$

This formulation encourages not only overlap, but also precise matching in box location and aspect under a scale-invariant normalization.

## 2. Intuitive Rationale and Components

Each component of Inner-MPDIoU serves a specific purpose for robust performance on tiny and dense targets:

- The auxiliary box $B^t$ focuses the metric on a controllable region of the ground-truth box via $r$, allowing finer control over localization emphasis. For $r < 1$, the loss prioritizes the core; for $r > 1$, it relaxes localization to a larger region, compensating for slight labeling uncertainty or missing center alignment.
- The intersection-over-union between $B$ and $B^t$ directs optimization to achieve high overlap specifically in the scaled region.
- The corner distance penalties ($d_1, d_2$) penalize misalignments in both position and aspect, targeting failures that standard IoU or DIoU loss cannot fully capture.
- Normalization by $\rho^2$ renders penalty strength consistent regardless of image scale.

A plausible implication is that this construction is more sensitive to the kinds of small misalignments that are critical for defect detection in high-resolution, high-density PCB imagery.

## 3. Comparison with Related IoU-based Losses

Inner-MPDIoU is distinct from several established IoU-family losses:

| Loss           | Overlap Term     | Drift/Aspect Penalty        |
|----------------|------------------|----------------------------|
| IoU            | Yes (IoU)        | None                       |
| GIoU           | Yes              | Enclosing box area         |
| DIoU           | Yes              | Center-point distance      |
| CIoU           | Yes              | Center + aspect penalty    |
| MPDIoU         | Yes              | Multiple key-point drifts  |
| Inner-MPDIoU   | Yes (aux. IoU)   | Two corners, img normalized|

Standard IoU focuses solely on area. GIoU, DIoU, and CIoU incrementally introduce penalties for enclosure, center, and aspect, but do not control for error concentration within a specific region of the box. MPDIoU generalizes center drift to multiple corners, but still lacks tunable focus. Inner-MPDIoU uniquely concentrates the overlap metric on an adjustable inner (or slightly expanded) region, and applies a pair of corner penalties tailored for high-density, tiny-object contexts [2507.17176].

## 4. Hyperparameters and Control

The primary hyperparameter of Inner-MPDIoU is the inner-region ratio $r$:

- Valid range: $0.5 \leq r \leq 1.5$
- $r \approx 1.0$ leaves the auxiliary box the same as ground-truth.
- $r < 1.0$ restricts the focus, exerting greater pressure on core alignment—effective for remedying box overshoot.
- $r > 1.0$ introduces tolerance, helpful if predicted boxes are systematically off-center.

In the referenced work, no additional term weighting is introduced; implicit equal contribution is made by IoU and the corner-penalty components. Tuning $r$ is typically performed on a small validation subset. No explicit balance hyperparameter is required between the loss' two terms.

## 5. Implementation Considerations

Integrating Inner-MPDIoU into object detection frameworks such as YOLOv8 involves minimal modification to the bounding box regression loss:

- Replace CIoU or similar loss with the Inner-MPDIoU loss function.
- For each forward pass and for each positive sample, compute auxiliary box coordinates, overlap and union areas, the two corner distances, normalization by $\rho^2$, and form the final loss.
- The computational overhead is negligible—element-wise min/max and squared distance operations account for <1% increase in FLOPs.

A PyTorch-style function is provided in the primary reference:

```python
def inner_mpdiou_loss(pred_boxes, gt_boxes, img_wh, ratio):
    # pred_boxes, gt_boxes: (N,4) in (l, t, r, b) format
    W_img, H_img = img_wh
    rho2 = W_img*W_img + H_img*H_img

    # 1) auxiliary inner GT box
    x_gt = (gt_boxes[:,0]+gt_boxes[:,2]) * 0.5
    y_gt = (gt_boxes[:,1]+gt_boxes[:,3]) * 0.5
    W_gt = gt_boxes[:,2] - gt_boxes[:,0]
    H_gt = gt_boxes[:,3] - gt_boxes[:,1]
    lt = torch.stack([x_gt - W_gt*ratio,
                      y_gt - H_gt*ratio], dim=1)
    rb = torch.stack([x_gt + W_gt*ratio,
                      y_gt + H_gt*ratio], dim=1)

    # 2) intersection with pred_boxes
    lt_min = torch.max(pred_boxes[:,:2], lt)
    rb_min = torch.min(pred_boxes[:,2:], rb)
    wh_int = (rb_min - lt_min).clamp(min=0)
    I = wh_int[:,0] * wh_int[:,1]

    # 3) areas
    A_pred = (pred_boxes[:,2]-pred_boxes[:,0]) * (pred_boxes[:,3]-pred_boxes[:,1])
    A_aux  = (rb[:,0]-lt[:,0]) * (rb[:,1]-lt[:,1])
    U = A_pred + A_aux - I + 1e-7

    # 4) corner‐point distances
    d1 = (lt[:,0] - pred_boxes[:,0])**2 + (lt[:,1] - pred_boxes[:,1])**2
    d2 = (rb[:,0] - pred_boxes[:,2])**2 + (rb[:,1] - pred_boxes[:,3])**2

    # 5) Inner-MPDIoU metric
    inner_mpdiou = (I / U) - ((d1 + d2) / rho2)

    # 6) loss
    loss = 1.0 - inner_mpdiou.clamp(min=-1.0, max=1.0)
    return loss.mean()
```

No modifications are needed elsewhere in the network pipeline. The additional cost is compatible with real-time requirements in industrial applications.

## 6. Empirical Performance and Impact

In ablation studies on the PKU-Market-PCB dataset, Inner-MPDIoU yields superior localization metrics, especially for small targets:

| Stage              | P (%)  | R (%)  | mAP₀.₅ (%) | mAP₀.₅:₀.₉₅ (%) |
|--------------------|--------|--------|------------|-----------------|
| A+B+C (no loss)    | 98.25  | 98.85  | 99.06      | 63.51           |
| +Inner-MPDIoU      | 98.39  | 99.26  | 99.20      | 66.34           |

A net gain of +0.41 pp in recall and +2.83 pp mAP₀.₅:₀.₉₅ (over 4% relative) is achieved [2507.17176]. Qualitatively, on challenging backgrounds or for very small defect regions (e.g., 1–2 px missing-hole/spur), Inner-MPDIoU facilitates tighter bounding, mitigates overextension, and reduces missed tiny objects. Application within a YOLOv8-based detector demonstrates improvements without requiring additional model complexity or computational resources.

## 7. Application Scope and Limitations

Inner-MPDIoU is especially well-suited for domains where precise delineation of small, high-density, or high-value regions is central, such as PCB defect detection. The tunable focus via $r$ provides flexibility across data regimes, allowing calibration for varying levels of annotation confidence or object granularity.

A plausible implication is that while Inner-MPDIoU demonstrates improvements in PCB defect benchmarks, the utility of the inner-region penalty may depend on the degree of annotation quality, object density, and specific small-object localization challenges present in other domains. Since only two corner-points are penalized (rather than all four as in some variants), certain forms of skew or rotational error may not be fully penalized unless $r$ is appropriately chosen.

Overall, Inner-MPDIoU characterized by its scale-controlled overlap focus and multi-point corner penalties, provides an effective and low-overhead approach for improving tiny-object localization in contemporary object detection frameworks [2507.17176].

Source: https://www.emergentmind.com/topics/inner-mpdiou-loss