CodeJackR
commited on
Commit
·
316f7a6
1
Parent(s):
38a30a4
Manage image resizing
Browse files- handler.py +15 -44
handler.py
CHANGED
|
@@ -64,54 +64,25 @@ class EndpointHandler():
|
|
| 64 |
|
| 65 |
# 4. Process and select the best mask
|
| 66 |
try:
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
pred_masks = pred_masks.squeeze(1) # Remove extra dimension if present
|
| 78 |
-
|
| 79 |
-
# Select the best mask
|
| 80 |
best_mask_idx = torch.argmax(iou_scores)
|
| 81 |
-
best_mask =
|
| 82 |
-
|
| 83 |
-
# The mask is currently at the model's internal resolution
|
| 84 |
-
# We need to resize it to the reshaped input size first, then crop/pad to original size
|
| 85 |
-
|
| 86 |
-
# Step 1: Resize to reshaped input size
|
| 87 |
-
resized_mask = F.interpolate(
|
| 88 |
-
best_mask.unsqueeze(0).unsqueeze(0).float(),
|
| 89 |
-
size=reshaped_input_sizes,
|
| 90 |
-
mode='bilinear',
|
| 91 |
-
align_corners=False
|
| 92 |
-
).squeeze()
|
| 93 |
-
|
| 94 |
-
# Step 2: Handle padding/cropping to get back to original size
|
| 95 |
-
original_h, original_w = original_sizes
|
| 96 |
-
reshaped_h, reshaped_w = reshaped_input_sizes
|
| 97 |
-
|
| 98 |
-
# Calculate padding that was added during preprocessing
|
| 99 |
-
if reshaped_h > original_h or reshaped_w > original_w:
|
| 100 |
-
# There was padding, we need to crop
|
| 101 |
-
start_h = (reshaped_h - original_h) // 2
|
| 102 |
-
start_w = (reshaped_w - original_w) // 2
|
| 103 |
-
final_mask = resized_mask[start_h:start_h + original_h, start_w:start_w + original_w]
|
| 104 |
-
else:
|
| 105 |
-
# No padding or different scaling, just resize directly
|
| 106 |
-
final_mask = F.interpolate(
|
| 107 |
-
resized_mask.unsqueeze(0).unsqueeze(0),
|
| 108 |
-
size=original_sizes,
|
| 109 |
-
mode='bilinear',
|
| 110 |
-
align_corners=False
|
| 111 |
-
).squeeze()
|
| 112 |
|
| 113 |
-
# Convert to binary mask
|
| 114 |
-
mask_binary = (
|
| 115 |
|
| 116 |
except Exception as e:
|
| 117 |
print("Error processing masks: {}".format(e))
|
|
|
|
| 64 |
|
| 65 |
# 4. Process and select the best mask
|
| 66 |
try:
|
| 67 |
+
# Use the processor's post_process_masks method correctly
|
| 68 |
+
# This method expects the raw model outputs and the input metadata
|
| 69 |
+
post_processed_masks = self.processor.post_process_masks(
|
| 70 |
+
outputs.pred_masks,
|
| 71 |
+
inputs["original_sizes"],
|
| 72 |
+
inputs["reshaped_input_sizes"]
|
| 73 |
+
)
|
| 74 |
|
| 75 |
+
# post_processed_masks is a list with one element (for batch size 1)
|
| 76 |
+
# Each element has shape (num_masks, original_height, original_width)
|
| 77 |
+
masks = post_processed_masks[0] # Shape: (num_masks, H, W)
|
| 78 |
|
| 79 |
+
# Get IoU scores and select the best mask
|
| 80 |
+
iou_scores = outputs.iou_scores[0] # Shape: (num_masks,)
|
|
|
|
|
|
|
|
|
|
| 81 |
best_mask_idx = torch.argmax(iou_scores)
|
| 82 |
+
best_mask = masks[best_mask_idx] # Shape: (H, W)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# Convert to numpy and create binary mask
|
| 85 |
+
mask_binary = (best_mask > 0.0).cpu().numpy().astype(np.uint8) * 255
|
| 86 |
|
| 87 |
except Exception as e:
|
| 88 |
print("Error processing masks: {}".format(e))
|