Skip to content

Commit

Permalink
Fix the border of the Grad-CAM images with YAHOO preprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
bhky committed Aug 4, 2024
1 parent 7fd90b8 commit 8ade275
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions opennsfw2/_inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,12 @@ def make_and_save_nsfw_grad_cam(
preprocess_image(pil_image, preprocessing), open_nsfw_model,
"activation_stage3_block2", "fc_nsfw", 1
)
if preprocessing == Preprocessing.YAHOO:
border_reduction_factor = 224 / 256
w, h = pil_image.size
w_new, h_new = int(w * border_reduction_factor), int(h * border_reduction_factor)
left_new = (w - w_new) // 2
upper_new = (h - h_new) // 2
pil_image = pil_image.crop((left_new, upper_new, left_new + w_new, upper_new + h_new))

save_grad_cam(np.array(pil_image), heatmap, grad_cam_path, alpha)
Binary file modified tests/output_grad_cam_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/output_grad_cam_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/output_grad_cam_3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8ade275

Please sign in to comment.