Skip to content

Commit

Permalink
quality_control: correct the per-class accuracy formula (#7640)
Browse files Browse the repository at this point in the history
<!-- Raise an issue to propose your change
(https://github.com/opencv/cvat/issues).
It helps to avoid duplication of efforts from multiple independent
contributors.
Discuss your ideas with maintainers to be sure that changes will be
approved and merged.
Read the [Contribution
guide](https://opencv.github.io/cvat/docs/contributing/). -->

<!-- Provide a general summary of your changes in the Title above -->

### Motivation and context
<!-- Why is this change required? What problem does it solve? If it
fixes an open
issue, please link to the issue here. Describe your changes in detail,
add
screenshots. -->
The current formula used to calculate `ConfusionMatrix.accuracy` is, in
fact, not accuracy, but the Jaccard index. Replace it with the correct
formula.

Since the Jaccard index is a useful metric in its own right, calculate
it too, but save it in another attribute of `ConfusionMatrix`.

### How has this been tested?
<!-- Please describe in detail how you tested your changes.
Include details of your testing environment, and the tests you ran to
see how your change affects other areas of the code, etc. -->
Manual testing.

### Checklist
<!-- Go over all the following points, and put an `x` in all the boxes
that apply.
If an item isn't applicable for some reason, then ~~explicitly
strikethrough~~ the whole
line. If you don't do that, GitHub will show incorrect progress for the
pull request.
If you're unsure about any of these, don't hesitate to ask. We're here
to help! -->
- [x] I submit my changes into the `develop` branch
- [x] I have created a changelog fragment <!-- see top comment in
CHANGELOG.md -->
- ~~[ ] I have updated the documentation accordingly~~
- ~~[ ] I have added tests to cover my changes~~
- ~~[ ] I have linked related issues (see [GitHub docs](

https://help.github.com/en/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword))~~
- ~~[ ] I have increased versions of npm packages if it is necessary

([cvat-canvas](https://github.com/opencv/cvat/tree/develop/cvat-canvas#versioning),

[cvat-core](https://github.com/opencv/cvat/tree/develop/cvat-core#versioning),

[cvat-data](https://github.com/opencv/cvat/tree/develop/cvat-data#versioning)
and

[cvat-ui](https://github.com/opencv/cvat/tree/develop/cvat-ui#versioning))~~

### License

- [x] I submit _my code changes_ under the same [MIT License](
https://github.com/opencv/cvat/blob/develop/LICENSE) that covers the
project.
  Feel free to contact the maintainers if that's a concern.

---------

Co-authored-by: Maxim Zhiltsov <[email protected]>
  • Loading branch information
SpecLad and zhiltsov-max authored Mar 21, 2024
1 parent 1749002 commit fc54c47
Show file tree
Hide file tree
Showing 6 changed files with 2,189 additions and 330 deletions.
5 changes: 5 additions & 0 deletions changelog.d/20240319_183656_roman_accuracy_jaccard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Fixed

- Corrected the formula for per-class accuracy in quality reports;
the old formula is now exposed as the `jaccard_index` key
(<https://github.com/opencv/cvat/pull/7640>)
15 changes: 13 additions & 2 deletions cvat/apps/quality_control/quality_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ class ConfusionMatrix(_Serializable):
precision: np.array
recall: np.array
accuracy: np.array
jaccard_index: Optional[np.array]

@property
def axes(self):
Expand All @@ -240,6 +241,9 @@ def from_dict(cls, d: dict):
precision=np.asarray(d["precision"]),
recall=np.asarray(d["recall"]),
accuracy=np.asarray(d["accuracy"]),
# This field didn't exist at first, so it might not be present
# in old serialized instances.
jaccard_index=np.asarray(d["jaccard_index"]) if "jaccard_index" in d else None,
)


Expand Down Expand Up @@ -1934,17 +1938,23 @@ def _generate_annotations_summary(
matched_ann_counts = np.diag(confusion_matrix)
ds_ann_counts = np.sum(confusion_matrix, axis=1)
gt_ann_counts = np.sum(confusion_matrix, axis=0)
total_annotations_count = np.sum(confusion_matrix)

label_accuracies = _arr_div(
label_jaccard_indices = _arr_div(
matched_ann_counts, ds_ann_counts + gt_ann_counts - matched_ann_counts
)
label_precisions = _arr_div(matched_ann_counts, ds_ann_counts)
label_recalls = _arr_div(matched_ann_counts, gt_ann_counts)
label_accuracies = (
total_annotations_count # TP + TN + FP + FN
- (ds_ann_counts - matched_ann_counts) # - FP
- (gt_ann_counts - matched_ann_counts) # - FN
# ... = TP + TN
) / (total_annotations_count or 1)

valid_annotations_count = np.sum(matched_ann_counts)
missing_annotations_count = np.sum(confusion_matrix[cls._UNMATCHED_IDX, :])
extra_annotations_count = np.sum(confusion_matrix[:, cls._UNMATCHED_IDX])
total_annotations_count = np.sum(confusion_matrix)
ds_annotations_count = np.sum(ds_ann_counts[: cls._UNMATCHED_IDX])
gt_annotations_count = np.sum(gt_ann_counts[: cls._UNMATCHED_IDX])

Expand All @@ -1961,6 +1971,7 @@ def _generate_annotations_summary(
precision=label_precisions,
recall=label_recalls,
accuracy=label_accuracies,
jaccard_index=label_jaccard_indices,
),
)

Expand Down
11 changes: 11 additions & 0 deletions tests/python/rest_api/test_quality_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,3 +1202,14 @@ def test_settings_affect_metrics(

new_report = self.create_quality_report(admin_user, task_id)
assert new_report["summary"]["conflict_count"] != old_report["summary"]["conflict_count"]

def test_old_report_can_be_loaded(self, admin_user, quality_reports):
report = min((r for r in quality_reports if r["task_id"]), key=lambda r: r["id"])
assert report["created_date"] < "2024"

with make_api_client(admin_user) as api_client:
(report_data, _) = api_client.quality_api.retrieve_report_data(report["id"])

# This report should have been created before the Jaccard index was included.
for d in [report_data["comparison_summary"], *report_data["frame_results"].values()]:
assert d["annotations"]["confusion_matrix"]["jaccard_index"] is None
1,716 changes: 1,390 additions & 326 deletions tests/python/shared/assets/cvat_db/data.json

Large diffs are not rendered by default.

Loading

0 comments on commit fc54c47

Please sign in to comment.