Spaces:
Sleeping
Sleeping
Fix Resize Error
Browse files
main.py
CHANGED
|
@@ -68,26 +68,41 @@ def compute_semantic_similarity(img_a: Image.Image, img_b: Image.Image) -> float
|
|
| 68 |
return round(score.item(), 4)
|
| 69 |
|
| 70 |
def compute_structural_similarity(img_a: Image.Image, img_b: Image.Image):
|
|
|
|
| 71 |
arr_a = np.array(img_a.convert('L'))
|
| 72 |
arr_b = np.array(img_b.convert('L'))
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
#
|
|
|
|
| 81 |
edges_a_thick = cv2.dilate(edges_a, kernel, iterations=1)
|
| 82 |
edges_b_thick = cv2.dilate(edges_b, kernel, iterations=1)
|
| 83 |
|
|
|
|
| 84 |
edges_b_resized = cv2.resize(edges_b_thick, (edges_a_thick.shape[1], edges_a_thick.shape[0]))
|
| 85 |
|
| 86 |
-
#
|
| 87 |
intersection = np.logical_and(edges_a_thick > 0, edges_b_resized > 0).sum()
|
| 88 |
union = np.logical_or(edges_a_thick > 0, edges_b_resized > 0).sum()
|
| 89 |
|
| 90 |
iou_score = intersection / union if union != 0 else 0.0
|
|
|
|
|
|
|
| 91 |
return round(iou_score, 4), Image.fromarray(edges_a), Image.fromarray(edges_b)
|
| 92 |
|
| 93 |
def compute_patch_similarity(img_a: Image.Image, img_b: Image.Image):
|
|
@@ -95,8 +110,9 @@ def compute_patch_similarity(img_a: Image.Image, img_b: Image.Image):
|
|
| 95 |
img_a_resized = img_a.resize(target_size)
|
| 96 |
img_b_resized = img_b.resize(target_size)
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
|
|
|
| 100 |
|
| 101 |
with torch.no_grad():
|
| 102 |
out_a = dino_model(**inputs_a)
|
|
|
|
| 68 |
return round(score.item(), 4)
|
| 69 |
|
| 70 |
def compute_structural_similarity(img_a: Image.Image, img_b: Image.Image):
|
| 71 |
+
# Convert to grayscale numpy arrays
|
| 72 |
arr_a = np.array(img_a.convert('L'))
|
| 73 |
arr_b = np.array(img_b.convert('L'))
|
| 74 |
|
| 75 |
+
# 1. Aggressive Gaussian Blur to eliminate stylistic texture
|
| 76 |
+
# A kernel size of (11, 11) is strong enough to blur out noise but keep main shapes
|
| 77 |
+
blur_a = cv2.GaussianBlur(arr_a, (11, 11), 0)
|
| 78 |
+
blur_b = cv2.GaussianBlur(arr_b, (11, 11), 0)
|
| 79 |
+
|
| 80 |
+
# 2. Dynamic Auto-Canny Helper Function
|
| 81 |
+
def auto_canny(image, sigma=0.33):
|
| 82 |
+
v = np.median(image)
|
| 83 |
+
lower = int(max(0, (1.0 - sigma) * v))
|
| 84 |
+
upper = int(min(255, (1.0 + sigma) * v))
|
| 85 |
+
return cv2.Canny(image, lower, upper)
|
| 86 |
+
|
| 87 |
+
# Extract structural edges using the blurred images and dynamic thresholds
|
| 88 |
+
edges_a = auto_canny(blur_a)
|
| 89 |
+
edges_b = auto_canny(blur_b)
|
| 90 |
|
| 91 |
+
# 3. Dilate the edges to give them a "margin of error" for spatial overlap
|
| 92 |
+
kernel = np.ones((7,7), np.uint8) # Thicker kernel for better overlap
|
| 93 |
edges_a_thick = cv2.dilate(edges_a, kernel, iterations=1)
|
| 94 |
edges_b_thick = cv2.dilate(edges_b, kernel, iterations=1)
|
| 95 |
|
| 96 |
+
# Resize B to match A for matrix math
|
| 97 |
edges_b_resized = cv2.resize(edges_b_thick, (edges_a_thick.shape[1], edges_a_thick.shape[0]))
|
| 98 |
|
| 99 |
+
# Calculate Intersection over Union (IoU)
|
| 100 |
intersection = np.logical_and(edges_a_thick > 0, edges_b_resized > 0).sum()
|
| 101 |
union = np.logical_or(edges_a_thick > 0, edges_b_resized > 0).sum()
|
| 102 |
|
| 103 |
iou_score = intersection / union if union != 0 else 0.0
|
| 104 |
+
|
| 105 |
+
# Return the clean (non-thickened) edges for a prettier UI visualization
|
| 106 |
return round(iou_score, 4), Image.fromarray(edges_a), Image.fromarray(edges_b)
|
| 107 |
|
| 108 |
def compute_patch_similarity(img_a: Image.Image, img_b: Image.Image):
|
|
|
|
| 110 |
img_a_resized = img_a.resize(target_size)
|
| 111 |
img_b_resized = img_b.resize(target_size)
|
| 112 |
|
| 113 |
+
# FIX: Add do_resize=False so it doesn't crush our 518x518 images back to 224x224
|
| 114 |
+
inputs_a = dino_processor(images=img_a_resized, return_tensors="pt", do_resize=False).to(device)
|
| 115 |
+
inputs_b = dino_processor(images=img_b_resized, return_tensors="pt", do_resize=False).to(device)
|
| 116 |
|
| 117 |
with torch.no_grad():
|
| 118 |
out_a = dino_model(**inputs_a)
|