Applying computer vision to image checking #727
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hackathon project: Applying computer vision to image checking
This is a prototype and is not meant to be merged. The pull request is for easy visibility/ability to comment.
Goal
The image checker can produce a great number of diffs. These are currenty organized by plot type.
This poses a problem if many of the diffs are similar -- it's not a great use of a developer's time to manually look through say 1,000 diffs only to realize every single one is just "the pixels shifted slightly left" or "the colorbar changed so of course the plots look different".
What would be ideal is to use computer vision/machine learning to group those diffs how we want them. E.g., "every diff in
cluster_0/is because the pixels shifted, every diff incluster_1/is because its colorbar changed, and so on".That would mean the developer only needs to look at a diff or two in each cluster to know what the issues are. There would be no need to manually review each diff, saving a lot of time.
Setting up
setup
Set up the directories to test on:
To run:
Explaining the code
The
cv_prototypefunction effectively replacesset_up_and_run_image_checkeras the caller ofcheck_images. If this was actually at the point of merging, we'd changein
set_up_and_run_image_checkerto:where
cv_dictis defined as something like what we have in themainblock now. With that change, the integration test suite could make use of this code. Thiscv_dictallows us to configure the pipeline with modularity. We can easily swap out different feature extraction, preprocessing, and clustering algorithms.The
check_imagesfunction is more-or-less unchanged, except for passing alongcv_dict.The
_check_mismatched_imagesfunction has 2 important additions, if we're in CV mode (i.e.,cv_dictis non-empty)_cv_compare_actual_and_expected, which is in fact substantially different than its counterpart_compare_actual_and_expected.group_diffs.Let's dive into
_cv_compare_actual_and_expected:get_images: we now usecv2.imreadinstead ofImage.opencompute_diff_image: before, we filtered based onif fraction >= 0.0002. That is, the fraction of mismatched pixels needed to be less than 0.02%. This is a little different. Here, we're seeing how high the grayscale value is for each pixel in the diff. If it's >gray_diff_threshold, we add it to the mask.write_images: more-or-less the same as what we were doing incompare_actual_and_expected, but usingcv2.imwriteand no longer drawing bounding boxes on the diffs. (I assume this is possible, but the cv2 images arendarrays notImages, so I didn't look into translating over the bounding box drawing.)update_features: this is totally new. This is finding the important features in the diffs. I.e., it's finding what to cluster the diffs (or, optionally, the actuals + the diffs) on. This currently has 3 options we can swap out: "simple_hist" is just a normalized color histogram, "combined_features" adds in spatial features, "sector_slice" is an attempt to slice up the image into relevant pieces.And dive into
group_diffs:preprocess_diffs: preprocess the features, as needed. We have options to scale features, reduce dimensions, and plot the features for a visual analysis.run_cluster_algorithm: run a clustering algorithm. There are options for DBSCAN, KMeans, and AgglomerativeClustering.Results
compute_diff_imagewith agray_diff_thresholdof30results in 12 more mismatches (57 => 69).I ran quite a few iterations of the code, tracked with the
try_num. The results can be seen in thediff_try#subdirectories of this pageInteresting iterations
Directories were moved as follows:
Best Result
The combination of parameters in this PR are what have given the best result so far:
Notably:
That produces these 4 clusters:
These results can be seen here.
Leveraging LivChat
LivChat was very useful in suggesting code snippets. It helped substantially in narrowing down areas to debug. For example, asked "is this issue because of the feature extraction or the clustering algorithm?" it explained how it was almost certainly the feature extraction that was the problem. It also suggested alternative clustering algorithms.
What would be needed to merge?
set_up_and_run_image_checkerset: