Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -217,38 +217,12 @@ async def predict_single_dog(image):
|
|
217 |
# return iou
|
218 |
|
219 |
|
220 |
-
def
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
# 交換最大分數和當前索引
|
228 |
-
boxes[i], boxes[maxpos] = boxes[maxpos].clone(), boxes[i].clone()
|
229 |
-
scores[i], scores[maxpos] = scores[maxpos].clone(), scores[i].clone()
|
230 |
-
|
231 |
-
# 對剩餘的框應用 Soft NMS
|
232 |
-
for pos in range(i + 1, N):
|
233 |
-
iou = box_iou(boxes[i].unsqueeze(0), boxes[pos].unsqueeze(0)).squeeze()
|
234 |
-
|
235 |
-
if method == 'linear':
|
236 |
-
weight = 1 - iou if iou > thresh else 1
|
237 |
-
elif method == 'gaussian':
|
238 |
-
weight = torch.exp(-(iou * iou) / sigma)
|
239 |
-
else: # 'original'
|
240 |
-
weight = 1 if iou <= thresh else 0
|
241 |
-
|
242 |
-
scores[pos] *= weight
|
243 |
-
|
244 |
-
# 刪除得分低於閾值的框
|
245 |
-
keep = scores > thresh
|
246 |
-
boxes = boxes[keep.squeeze(-1)] # 確保 keep 的維度與 boxes 匹配
|
247 |
-
scores = scores[keep]
|
248 |
-
|
249 |
-
return boxes, scores
|
250 |
-
|
251 |
-
async def detect_multiple_dogs(image, conf_threshold=0.35, iou_threshold=0.55):
|
252 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
253 |
dogs = []
|
254 |
boxes = []
|
@@ -265,8 +239,8 @@ async def detect_multiple_dogs(image, conf_threshold=0.35, iou_threshold=0.55):
|
|
265 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
266 |
else:
|
267 |
boxes = torch.stack(boxes)
|
268 |
-
scores = torch.stack(scores).squeeze(-1)
|
269 |
-
nms_boxes, nms_scores =
|
270 |
|
271 |
for box, score in zip(nms_boxes, nms_scores):
|
272 |
x1, y1, x2, y2 = box.tolist()
|
|
|
217 |
# return iou
|
218 |
|
219 |
|
220 |
+
def weighted_nms(boxes, scores, iou_threshold=0.5, score_threshold=0.05):
|
221 |
+
keep = nms(boxes, scores, iou_threshold)
|
222 |
+
keep = keep[scores[keep] > score_threshold]
|
223 |
+
return boxes[keep], scores[keep]
|
224 |
+
|
225 |
+
async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.45):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
227 |
dogs = []
|
228 |
boxes = []
|
|
|
239 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
240 |
else:
|
241 |
boxes = torch.stack(boxes)
|
242 |
+
scores = torch.stack(scores).squeeze(-1)
|
243 |
+
nms_boxes, nms_scores = weighted_nms(boxes, scores, iou_threshold=iou_threshold, score_threshold=0.1)
|
244 |
|
245 |
for box, score in zip(nms_boxes, nms_scores):
|
246 |
x1, y1, x2, y2 = box.tolist()
|