Spaces:
Runtime error
Runtime error
update layout & use original method
Browse files
app.py
CHANGED
@@ -30,59 +30,6 @@ hourglass_args = {
|
|
30 |
},
|
31 |
}
|
32 |
|
33 |
-
def generate_mask(image, generator: SamAutomaticMaskGenerator):
|
34 |
-
generator.predictor.set_image(image)
|
35 |
-
|
36 |
-
image_size = image.shape[:2]
|
37 |
-
points_scale = np.array(image_size)[None, ::-1]
|
38 |
-
points_for_image = generator.point_grids[0] * points_scale
|
39 |
-
for (points,) in batch_iterator(generator.points_per_batch, points_for_image):
|
40 |
-
transformed_points = generator.predictor.transform.apply_coords(points, image_size)
|
41 |
-
in_points = torch.as_tensor(transformed_points, device=generator.predictor.device)
|
42 |
-
in_labels = torch.ones(in_points.shape[0], dtype=torch.int, device=in_points.device)
|
43 |
-
masks, iou_preds, _ = generator.predictor.predict_torch(
|
44 |
-
in_points[:, None, :],
|
45 |
-
in_labels[:, None],
|
46 |
-
multimask_output=True,
|
47 |
-
return_logits=True,
|
48 |
-
)
|
49 |
-
|
50 |
-
# Serialize predictions and store in MaskData
|
51 |
-
data = MaskData(
|
52 |
-
masks=masks.flatten(0, 1),
|
53 |
-
iou_preds=iou_preds.flatten(0, 1),
|
54 |
-
points=torch.as_tensor(points.repeat(masks.shape[1], axis=0)),
|
55 |
-
)
|
56 |
-
del masks
|
57 |
-
|
58 |
-
# Filter by predicted IoU
|
59 |
-
if generator.pred_iou_thresh > 0.0:
|
60 |
-
keep_mask = data["iou_preds"] > generator.pred_iou_thresh
|
61 |
-
data.filter(keep_mask)
|
62 |
-
|
63 |
-
# Calculate stability score
|
64 |
-
data["stability_score"] = calculate_stability_score(
|
65 |
-
data["masks"], generator.predictor.model.mask_threshold, generator.stability_score_offset
|
66 |
-
)
|
67 |
-
if generator.stability_score_thresh > 0.0:
|
68 |
-
keep_mask = data["stability_score"] >= generator.stability_score_thresh
|
69 |
-
data.filter(keep_mask)
|
70 |
-
|
71 |
-
# Threshold masks and calculate boxes
|
72 |
-
data["masks"] = data["masks"] > generator.predictor.model.mask_threshold
|
73 |
-
|
74 |
-
# Write mask records
|
75 |
-
curr_anns = []
|
76 |
-
for idx in range(len(data["masks"])):
|
77 |
-
ann = {
|
78 |
-
"segmentation": data["masks"][idx].numpy(),
|
79 |
-
"area": data["masks"][idx].sum().item(),
|
80 |
-
}
|
81 |
-
curr_anns.append(ann)
|
82 |
-
|
83 |
-
return curr_anns
|
84 |
-
|
85 |
-
|
86 |
def predict(image, speed_mode, points_per_side):
|
87 |
points_per_side = int(points_per_side)
|
88 |
mask_generator = SamAutomaticMaskGenerator(
|
@@ -92,8 +39,7 @@ def predict(image, speed_mode, points_per_side):
|
|
92 |
)
|
93 |
start = time.perf_counter()
|
94 |
with torch.no_grad():
|
95 |
-
|
96 |
-
masks = generate_mask(image, mask_generator)
|
97 |
eta = time.perf_counter() - start
|
98 |
eta_text = f"Time of generation: {eta:.2f} seconds"
|
99 |
|
@@ -136,7 +82,7 @@ def main():
|
|
136 |
label="Speed Mode",
|
137 |
multiselect=False,
|
138 |
)
|
139 |
-
with gr.
|
140 |
run_btn = gr.Button(label="Run", id="run", value="Run")
|
141 |
clear_btn = gr.Button(label="Clear", id="clear", value="Clear")
|
142 |
with gr.Column():
|
|
|
30 |
},
|
31 |
}
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
def predict(image, speed_mode, points_per_side):
|
34 |
points_per_side = int(points_per_side)
|
35 |
mask_generator = SamAutomaticMaskGenerator(
|
|
|
39 |
)
|
40 |
start = time.perf_counter()
|
41 |
with torch.no_grad():
|
42 |
+
masks = mask_generator.generate(image)
|
|
|
43 |
eta = time.perf_counter() - start
|
44 |
eta_text = f"Time of generation: {eta:.2f} seconds"
|
45 |
|
|
|
82 |
label="Speed Mode",
|
83 |
multiselect=False,
|
84 |
)
|
85 |
+
with gr.Column():
|
86 |
run_btn = gr.Button(label="Run", id="run", value="Run")
|
87 |
clear_btn = gr.Button(label="Clear", id="clear", value="Clear")
|
88 |
with gr.Column():
|