# Confusion Matrix Calculation

In Red Analyze tools, the confusion matrix can be calculated based on a region or a view. The values of confusion matrix are clearly different from which basis is selected.

## Confusion Matrix Calculation - Views/Untrained Views

When the confusion matrix is based on views, the marking is made on each view, not each region. For a view, if there are defect pixels in this view whose score(s) are good enough to go over T2 threshold, then it is predicted as "Bad," otherwise, good or inter.

To mark a view as "Bad," the defect pixel with the highest defect probability (score) could be anywhere in a view, which doesn't necessarily be in the labeled defect region.

When it is view-basis, the label is made on each view, not each region. If you label a view as "Bad," that is said and it is labeled as bad, and otherwise, "Good."

### How to calculate "Actuals" - Views/Untrained Views

- When a view with "Good" label is given,
  → 1 "Good" Actual.

- When a view with "Bad" label is given,
  → 1 "Bad" Actual.

### How to calculate "Predicted" - Views/Untrained Views

When it is view-basis, the prediction is made on each "Actual" view, which is found above, not on each region. The prediction is calculated from T1, T2 and the representative score of the actual view.

The representative score of a view is the highest score of pixels in this view. Each score is the defect probability of each pixel. For example, for a view, if there are defect pixels in this view whose score(s) are good enough to go over T2 threshold, then it is predicted as "Bad," otherwise, "Good or Inter."

- When the highest score from all pixels in a view < T1,
  → 1 "Good" Predicted.

- When the highest score from all pixels in a view > T2,
  → 1 "Bad" Predicted.

- Otherwise,
  → 1 "Inter" Predicted.

## Confusion Matrix Calculation - Regions/Untrained Regions

When the confusion matrix is based on region, the labeling and marking are made on each region, not each view. This means that multiple marked regions (a 'marked region' stands for a set of pixels that are marked as "defective" with respect to T1 and T2) in a view are included in the confusion matrix calculation. In other words, a single view could be used to produce multiple counts (actual - predicted pairs) in the confusion matrix table, whereas a single view only produces a single count in the table in Views/Untrained Views.

### How to calculate "Actuals" - Regions/Untrained Regions

Compared to Views/Untrained Views, when it comes to counting "actual" for the confusion matrix for regions/untrained regions, there is a different logic for counting "actual."

- When a view with "Good" label is given,
  → 1 "Good" Actual region
  (The entire view itself is considered as a background region).

- When a view with "Bad" label and with N pixel-wise labeled defect regions is given,
  → N "Bad" Actuals corresponding to the number of pixel-wise labeled regions in this view (N "Bad" actual regions).
  → 1 "Good" Actual region corresponding to all the background pixels in this view.

- When a view with "Bad" label and without pixel-wise labeled defect regions is given,
  → 1 "Bad" Actual.

### How to calculate "Predicted" - Regions/Untrained Regions

In the confusion matrix for regions or untrained regions, the prediction is made on each "Actual" region, whose calculation was illustrated in the section right above. The prediction is calculated from T1, T2 and the representative score of an actual region.

The representative score of the actual region is the highest score of pixels in the "actual" region area. This is just the same as that the representative score of a view is the highest score of pixels in that view. Each score is the defect probability of each pixel.

In the confusion matrix for regions or untrained regions, the predicted result is determined by T1, T2 and the score of the actual region:

- The score of actual region < T1,
  → 1 "Good" Predicted

- T1 < The score of actual region < T2,
  → 1 "Inter" Predicted

- The score of actual region > T2,
  → 1 "Bad" Predicted

### Examples of Actual-Predicted pair calculation for Regions/Untrained Regions

In the examples below, we assume that the highest score found from an actual region is higher than T2.

- When N marked regions appear on the backgrounds (the good labeled pixels) and there are no pixel-wised labeled defects and this view itself was labeled as "Good":
  → 1 (Actual) Good - (Predicted) Bad pair
  (N marked regions are considered as a single marked region)

  Example of 3 marked regions appear on the backgrounds, no pixel-wise labeled defects, "Good" labeled view:

- When N marked regions appear on the backgrounds (the good labeled pixels) and there are no pixel-wised labeled defects but this whole view itself was just labeled as "Bad":
  → 1 (Actual) Bad - (Predicted) Bad pair
  (N marked regions are considered as a single marked region)

  Example of 4 marked regions appear on the backgrounds, no pixel-wise labeled defects, "Bad" labeled view:

- When marked regions appear on the pixel-wise labeled defect regions:
  The one important thing here to decide the outcome is, of the total, how many marked regions are overlapped with the pixel-wise labeled defect regions:

  - <Case 1>

    1 pixel-wise labeled region,
    1 marked region (overlapped with the pixel-wise labeled region),
    1 marked region (not overlapped with the pixel-wise labeled region and appear on the background):
    →
    1 (Actual) Bad - (Predicted) Bad pair (overlapped),
    1 (Actual) Good - (Predicted) Bad pair (not overlapped),

    Note: When there are one or more pixel-wise labeled defect regions in a view, all the pixels all of a lump except the labeled regions are considered as "a background," which counts for 1 Actual "Good" region." This expands to most cases but does not apply when there are no pixel-wise labeled regions in the view but the view itself was labeled as "Bad."

    Example of a marked region appear on the backgrounds, another marked region appear on a pixel-wise labeled region (overlapped), and this view itself is labeled as "Bad" as there are one or more pixel-wise labeled defects:

  - <Case 2>

    5 pixel-wise labeled regions,
    3 marked regions (each overlapped with each pixel-wise labeled region),
    2 marked regions (not overlapped with any pixel-wise labeled region and appear on the background):
    →
    3 (Actual) Bad - (Predicted) Bad pair (overlapped),
    2 (Actual) Bad - (Predicted) Good pair (not overlapped),
    1 (Actual) Good - (Predicted) Bad pair

    Note: The 2 marked regions appeared in the background are considered as a single marked region, which results in 1 "Bad" Predicted. This also expands to the N marked regions case.

  - <Case 3>

    3 pixel-wise labeled regions (A, B, C),
    3 marked regions (2 overlapped with A and 1 with B)
    1 marked region(not overlapped = appear on the background):
    →
    1 (Actual) Bad - (Predicted) Bad pair (2 overlapped with A),
    1 (Actual) Bad - (Predicted) Bad pair (1 overlapped with B),
    1 (Actual) Good - (Predicted) Bad pair (appear on the background),
    1 (Actual) Bad - (Predicted) Good pair (C)