Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -216,51 +216,33 @@ async def predict_single_dog(image):
|
|
216 |
# iou = intersection / float(area1 + area2 - intersection)
|
217 |
# return iou
|
218 |
|
|
|
219 |
def soft_nms(boxes, scores, sigma=0.5, thresh=0.001, method='gaussian'):
|
220 |
N = len(boxes)
|
221 |
|
222 |
for i in range(N):
|
223 |
-
maxscore = scores[i]
|
224 |
-
maxpos = i
|
225 |
-
|
226 |
-
tx1, ty1, tx2, ty2 = boxes[i]
|
227 |
-
ts = scores[i]
|
228 |
-
|
229 |
-
pos = i + 1
|
230 |
-
while pos < N:
|
231 |
-
if maxscore < scores[pos]:
|
232 |
-
maxscore = scores[pos]
|
233 |
-
maxpos = pos
|
234 |
-
pos += 1
|
235 |
-
|
236 |
# 交換最大分數和當前索引
|
237 |
-
boxes[i], boxes[maxpos] = boxes[maxpos].
|
238 |
-
scores[i], scores[maxpos] = scores[maxpos], scores[i]
|
239 |
|
240 |
# 對剩餘的框應用 Soft NMS
|
241 |
for pos in range(i + 1, N):
|
242 |
-
|
243 |
-
ts = scores[pos]
|
244 |
-
|
245 |
-
iou = calculate_iou(boxes[i], boxes[pos])
|
246 |
|
247 |
if method == 'linear':
|
248 |
weight = 1 - iou if iou > thresh else 1
|
249 |
elif method == 'gaussian':
|
250 |
weight = torch.exp(-(iou * iou) / sigma)
|
251 |
else: # 'original'
|
252 |
-
if iou
|
253 |
-
weight = 0
|
254 |
-
else:
|
255 |
-
weight = 1
|
256 |
|
257 |
-
scores[pos]
|
258 |
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
# 刪除得分為零的框
|
263 |
-
keep = scores > 0
|
264 |
boxes = boxes[keep]
|
265 |
scores = scores[keep]
|
266 |
|
@@ -274,16 +256,16 @@ async def detect_multiple_dogs(image, conf_threshold=0.35, iou_threshold=0.55):
|
|
274 |
|
275 |
for box in results.boxes:
|
276 |
if box.cls == 16: # COCO dataset class for dog is 16
|
277 |
-
xyxy = box.xyxy[0]
|
278 |
-
confidence = box.conf
|
279 |
boxes.append(xyxy)
|
280 |
scores.append(confidence)
|
281 |
|
282 |
if not boxes:
|
283 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
284 |
else:
|
285 |
-
boxes = torch.
|
286 |
-
scores = torch.
|
287 |
nms_boxes, nms_scores = soft_nms(boxes, scores, thresh=iou_threshold)
|
288 |
|
289 |
for box, score in zip(nms_boxes, nms_scores):
|
|
|
216 |
# iou = intersection / float(area1 + area2 - intersection)
|
217 |
# return iou
|
218 |
|
219 |
+
|
220 |
def soft_nms(boxes, scores, sigma=0.5, thresh=0.001, method='gaussian'):
|
221 |
N = len(boxes)
|
222 |
|
223 |
for i in range(N):
|
224 |
+
maxscore, maxpos = torch.max(scores[i:], dim=0)
|
225 |
+
maxpos = maxpos.item() + i
|
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]
|
247 |
scores = scores[keep]
|
248 |
|
|
|
256 |
|
257 |
for box in results.boxes:
|
258 |
if box.cls == 16: # COCO dataset class for dog is 16
|
259 |
+
xyxy = box.xyxy[0]
|
260 |
+
confidence = box.conf
|
261 |
boxes.append(xyxy)
|
262 |
scores.append(confidence)
|
263 |
|
264 |
if not boxes:
|
265 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
266 |
else:
|
267 |
+
boxes = torch.stack(boxes)
|
268 |
+
scores = torch.stack(scores)
|
269 |
nms_boxes, nms_scores = soft_nms(boxes, scores, thresh=iou_threshold)
|
270 |
|
271 |
for box, score in zip(nms_boxes, nms_scores):
|