Spaces:
Runtime error
Runtime error
Upload 6 files
Browse files- facelib/detection/yolov5face/utils/__init__.py +0 -0
- facelib/detection/yolov5face/utils/autoanchor.py +12 -0
- facelib/detection/yolov5face/utils/datasets.py +35 -0
- facelib/detection/yolov5face/utils/extract_ckpt.py +5 -0
- facelib/detection/yolov5face/utils/general.py +271 -0
- facelib/detection/yolov5face/utils/torch_utils.py +40 -0
facelib/detection/yolov5face/utils/__init__.py
ADDED
|
File without changes
|
facelib/detection/yolov5face/utils/autoanchor.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Auto-anchor utils
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def check_anchor_order(m):
|
| 5 |
+
# Check anchor order against stride order for YOLOv5 Detect() module m, and correct if necessary
|
| 6 |
+
a = m.anchor_grid.prod(-1).view(-1) # anchor area
|
| 7 |
+
da = a[-1] - a[0] # delta a
|
| 8 |
+
ds = m.stride[-1] - m.stride[0] # delta s
|
| 9 |
+
if da.sign() != ds.sign(): # same order
|
| 10 |
+
print("Reversing anchor order")
|
| 11 |
+
m.anchors[:] = m.anchors.flip(0)
|
| 12 |
+
m.anchor_grid[:] = m.anchor_grid.flip(0)
|
facelib/detection/yolov5face/utils/datasets.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scale_fill=False, scaleup=True):
|
| 6 |
+
# Resize image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232
|
| 7 |
+
shape = img.shape[:2] # current shape [height, width]
|
| 8 |
+
if isinstance(new_shape, int):
|
| 9 |
+
new_shape = (new_shape, new_shape)
|
| 10 |
+
|
| 11 |
+
# Scale ratio (new / old)
|
| 12 |
+
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
|
| 13 |
+
if not scaleup: # only scale down, do not scale up (for better test mAP)
|
| 14 |
+
r = min(r, 1.0)
|
| 15 |
+
|
| 16 |
+
# Compute padding
|
| 17 |
+
ratio = r, r # width, height ratios
|
| 18 |
+
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
|
| 19 |
+
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
|
| 20 |
+
if auto: # minimum rectangle
|
| 21 |
+
dw, dh = np.mod(dw, 64), np.mod(dh, 64) # wh padding
|
| 22 |
+
elif scale_fill: # stretch
|
| 23 |
+
dw, dh = 0.0, 0.0
|
| 24 |
+
new_unpad = (new_shape[1], new_shape[0])
|
| 25 |
+
ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
|
| 26 |
+
|
| 27 |
+
dw /= 2 # divide padding into 2 sides
|
| 28 |
+
dh /= 2
|
| 29 |
+
|
| 30 |
+
if shape[::-1] != new_unpad: # resize
|
| 31 |
+
img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
|
| 32 |
+
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
|
| 33 |
+
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
|
| 34 |
+
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
|
| 35 |
+
return img, ratio, (dw, dh)
|
facelib/detection/yolov5face/utils/extract_ckpt.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import sys
|
| 3 |
+
sys.path.insert(0,'./facelib/detection/yolov5face')
|
| 4 |
+
model = torch.load('facelib/detection/yolov5face/yolov5n-face.pt', map_location='cpu')['model']
|
| 5 |
+
torch.save(model.state_dict(),'weights/facelib/yolov5n-face.pth')
|
facelib/detection/yolov5face/utils/general.py
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
import torch
|
| 6 |
+
import torchvision
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def check_img_size(img_size, s=32):
|
| 10 |
+
# Verify img_size is a multiple of stride s
|
| 11 |
+
new_size = make_divisible(img_size, int(s)) # ceil gs-multiple
|
| 12 |
+
# if new_size != img_size:
|
| 13 |
+
# print(f"WARNING: --img-size {img_size:g} must be multiple of max stride {s:g}, updating to {new_size:g}")
|
| 14 |
+
return new_size
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def make_divisible(x, divisor):
|
| 18 |
+
# Returns x evenly divisible by divisor
|
| 19 |
+
return math.ceil(x / divisor) * divisor
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def xyxy2xywh(x):
|
| 23 |
+
# Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] where xy1=top-left, xy2=bottom-right
|
| 24 |
+
y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
|
| 25 |
+
y[:, 0] = (x[:, 0] + x[:, 2]) / 2 # x center
|
| 26 |
+
y[:, 1] = (x[:, 1] + x[:, 3]) / 2 # y center
|
| 27 |
+
y[:, 2] = x[:, 2] - x[:, 0] # width
|
| 28 |
+
y[:, 3] = x[:, 3] - x[:, 1] # height
|
| 29 |
+
return y
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def xywh2xyxy(x):
|
| 33 |
+
# Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
|
| 34 |
+
y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
|
| 35 |
+
y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x
|
| 36 |
+
y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y
|
| 37 |
+
y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x
|
| 38 |
+
y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y
|
| 39 |
+
return y
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None):
|
| 43 |
+
# Rescale coords (xyxy) from img1_shape to img0_shape
|
| 44 |
+
if ratio_pad is None: # calculate from img0_shape
|
| 45 |
+
gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new
|
| 46 |
+
pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding
|
| 47 |
+
else:
|
| 48 |
+
gain = ratio_pad[0][0]
|
| 49 |
+
pad = ratio_pad[1]
|
| 50 |
+
|
| 51 |
+
coords[:, [0, 2]] -= pad[0] # x padding
|
| 52 |
+
coords[:, [1, 3]] -= pad[1] # y padding
|
| 53 |
+
coords[:, :4] /= gain
|
| 54 |
+
clip_coords(coords, img0_shape)
|
| 55 |
+
return coords
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def clip_coords(boxes, img_shape):
|
| 59 |
+
# Clip bounding xyxy bounding boxes to image shape (height, width)
|
| 60 |
+
boxes[:, 0].clamp_(0, img_shape[1]) # x1
|
| 61 |
+
boxes[:, 1].clamp_(0, img_shape[0]) # y1
|
| 62 |
+
boxes[:, 2].clamp_(0, img_shape[1]) # x2
|
| 63 |
+
boxes[:, 3].clamp_(0, img_shape[0]) # y2
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def box_iou(box1, box2):
|
| 67 |
+
# https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py
|
| 68 |
+
"""
|
| 69 |
+
Return intersection-over-union (Jaccard index) of boxes.
|
| 70 |
+
Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
|
| 71 |
+
Arguments:
|
| 72 |
+
box1 (Tensor[N, 4])
|
| 73 |
+
box2 (Tensor[M, 4])
|
| 74 |
+
Returns:
|
| 75 |
+
iou (Tensor[N, M]): the NxM matrix containing the pairwise
|
| 76 |
+
IoU values for every element in boxes1 and boxes2
|
| 77 |
+
"""
|
| 78 |
+
|
| 79 |
+
def box_area(box):
|
| 80 |
+
return (box[2] - box[0]) * (box[3] - box[1])
|
| 81 |
+
|
| 82 |
+
area1 = box_area(box1.T)
|
| 83 |
+
area2 = box_area(box2.T)
|
| 84 |
+
|
| 85 |
+
inter = (torch.min(box1[:, None, 2:], box2[:, 2:]) - torch.max(box1[:, None, :2], box2[:, :2])).clamp(0).prod(2)
|
| 86 |
+
return inter / (area1[:, None] + area2 - inter)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def non_max_suppression_face(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, labels=()):
|
| 90 |
+
"""Performs Non-Maximum Suppression (NMS) on inference results
|
| 91 |
+
Returns:
|
| 92 |
+
detections with shape: nx6 (x1, y1, x2, y2, conf, cls)
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
nc = prediction.shape[2] - 15 # number of classes
|
| 96 |
+
xc = prediction[..., 4] > conf_thres # candidates
|
| 97 |
+
|
| 98 |
+
# Settings
|
| 99 |
+
# (pixels) maximum box width and height
|
| 100 |
+
max_wh = 4096
|
| 101 |
+
time_limit = 10.0 # seconds to quit after
|
| 102 |
+
redundant = True # require redundant detections
|
| 103 |
+
multi_label = nc > 1 # multiple labels per box (adds 0.5ms/img)
|
| 104 |
+
merge = False # use merge-NMS
|
| 105 |
+
|
| 106 |
+
t = time.time()
|
| 107 |
+
output = [torch.zeros((0, 16), device=prediction.device)] * prediction.shape[0]
|
| 108 |
+
for xi, x in enumerate(prediction): # image index, image inference
|
| 109 |
+
# Apply constraints
|
| 110 |
+
x = x[xc[xi]] # confidence
|
| 111 |
+
|
| 112 |
+
# Cat apriori labels if autolabelling
|
| 113 |
+
if labels and len(labels[xi]):
|
| 114 |
+
label = labels[xi]
|
| 115 |
+
v = torch.zeros((len(label), nc + 15), device=x.device)
|
| 116 |
+
v[:, :4] = label[:, 1:5] # box
|
| 117 |
+
v[:, 4] = 1.0 # conf
|
| 118 |
+
v[range(len(label)), label[:, 0].long() + 15] = 1.0 # cls
|
| 119 |
+
x = torch.cat((x, v), 0)
|
| 120 |
+
|
| 121 |
+
# If none remain process next image
|
| 122 |
+
if not x.shape[0]:
|
| 123 |
+
continue
|
| 124 |
+
|
| 125 |
+
# Compute conf
|
| 126 |
+
x[:, 15:] *= x[:, 4:5] # conf = obj_conf * cls_conf
|
| 127 |
+
|
| 128 |
+
# Box (center x, center y, width, height) to (x1, y1, x2, y2)
|
| 129 |
+
box = xywh2xyxy(x[:, :4])
|
| 130 |
+
|
| 131 |
+
# Detections matrix nx6 (xyxy, conf, landmarks, cls)
|
| 132 |
+
if multi_label:
|
| 133 |
+
i, j = (x[:, 15:] > conf_thres).nonzero(as_tuple=False).T
|
| 134 |
+
x = torch.cat((box[i], x[i, j + 15, None], x[:, 5:15], j[:, None].float()), 1)
|
| 135 |
+
else: # best class only
|
| 136 |
+
conf, j = x[:, 15:].max(1, keepdim=True)
|
| 137 |
+
x = torch.cat((box, conf, x[:, 5:15], j.float()), 1)[conf.view(-1) > conf_thres]
|
| 138 |
+
|
| 139 |
+
# Filter by class
|
| 140 |
+
if classes is not None:
|
| 141 |
+
x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)]
|
| 142 |
+
|
| 143 |
+
# If none remain process next image
|
| 144 |
+
n = x.shape[0] # number of boxes
|
| 145 |
+
if not n:
|
| 146 |
+
continue
|
| 147 |
+
|
| 148 |
+
# Batched NMS
|
| 149 |
+
c = x[:, 15:16] * (0 if agnostic else max_wh) # classes
|
| 150 |
+
boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores
|
| 151 |
+
i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS
|
| 152 |
+
|
| 153 |
+
if merge and (1 < n < 3e3): # Merge NMS (boxes merged using weighted mean)
|
| 154 |
+
# update boxes as boxes(i,4) = weights(i,n) * boxes(n,4)
|
| 155 |
+
iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix
|
| 156 |
+
weights = iou * scores[None] # box weights
|
| 157 |
+
x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True) # merged boxes
|
| 158 |
+
if redundant:
|
| 159 |
+
i = i[iou.sum(1) > 1] # require redundancy
|
| 160 |
+
|
| 161 |
+
output[xi] = x[i]
|
| 162 |
+
if (time.time() - t) > time_limit:
|
| 163 |
+
break # time limit exceeded
|
| 164 |
+
|
| 165 |
+
return output
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def non_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, labels=()):
|
| 169 |
+
"""Performs Non-Maximum Suppression (NMS) on inference results
|
| 170 |
+
|
| 171 |
+
Returns:
|
| 172 |
+
detections with shape: nx6 (x1, y1, x2, y2, conf, cls)
|
| 173 |
+
"""
|
| 174 |
+
|
| 175 |
+
nc = prediction.shape[2] - 5 # number of classes
|
| 176 |
+
xc = prediction[..., 4] > conf_thres # candidates
|
| 177 |
+
|
| 178 |
+
# Settings
|
| 179 |
+
# (pixels) maximum box width and height
|
| 180 |
+
max_wh = 4096
|
| 181 |
+
time_limit = 10.0 # seconds to quit after
|
| 182 |
+
redundant = True # require redundant detections
|
| 183 |
+
multi_label = nc > 1 # multiple labels per box (adds 0.5ms/img)
|
| 184 |
+
merge = False # use merge-NMS
|
| 185 |
+
|
| 186 |
+
t = time.time()
|
| 187 |
+
output = [torch.zeros((0, 6), device=prediction.device)] * prediction.shape[0]
|
| 188 |
+
for xi, x in enumerate(prediction): # image index, image inference
|
| 189 |
+
x = x[xc[xi]] # confidence
|
| 190 |
+
|
| 191 |
+
# Cat apriori labels if autolabelling
|
| 192 |
+
if labels and len(labels[xi]):
|
| 193 |
+
label_id = labels[xi]
|
| 194 |
+
v = torch.zeros((len(label_id), nc + 5), device=x.device)
|
| 195 |
+
v[:, :4] = label_id[:, 1:5] # box
|
| 196 |
+
v[:, 4] = 1.0 # conf
|
| 197 |
+
v[range(len(label_id)), label_id[:, 0].long() + 5] = 1.0 # cls
|
| 198 |
+
x = torch.cat((x, v), 0)
|
| 199 |
+
|
| 200 |
+
# If none remain process next image
|
| 201 |
+
if not x.shape[0]:
|
| 202 |
+
continue
|
| 203 |
+
|
| 204 |
+
# Compute conf
|
| 205 |
+
x[:, 5:] *= x[:, 4:5] # conf = obj_conf * cls_conf
|
| 206 |
+
|
| 207 |
+
# Box (center x, center y, width, height) to (x1, y1, x2, y2)
|
| 208 |
+
box = xywh2xyxy(x[:, :4])
|
| 209 |
+
|
| 210 |
+
# Detections matrix nx6 (xyxy, conf, cls)
|
| 211 |
+
if multi_label:
|
| 212 |
+
i, j = (x[:, 5:] > conf_thres).nonzero(as_tuple=False).T
|
| 213 |
+
x = torch.cat((box[i], x[i, j + 5, None], j[:, None].float()), 1)
|
| 214 |
+
else: # best class only
|
| 215 |
+
conf, j = x[:, 5:].max(1, keepdim=True)
|
| 216 |
+
x = torch.cat((box, conf, j.float()), 1)[conf.view(-1) > conf_thres]
|
| 217 |
+
|
| 218 |
+
# Filter by class
|
| 219 |
+
if classes is not None:
|
| 220 |
+
x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)]
|
| 221 |
+
|
| 222 |
+
# Check shape
|
| 223 |
+
n = x.shape[0] # number of boxes
|
| 224 |
+
if not n: # no boxes
|
| 225 |
+
continue
|
| 226 |
+
|
| 227 |
+
x = x[x[:, 4].argsort(descending=True)] # sort by confidence
|
| 228 |
+
|
| 229 |
+
# Batched NMS
|
| 230 |
+
c = x[:, 5:6] * (0 if agnostic else max_wh) # classes
|
| 231 |
+
boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores
|
| 232 |
+
i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS
|
| 233 |
+
if merge and (1 < n < 3e3): # Merge NMS (boxes merged using weighted mean)
|
| 234 |
+
# update boxes as boxes(i,4) = weights(i,n) * boxes(n,4)
|
| 235 |
+
iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix
|
| 236 |
+
weights = iou * scores[None] # box weights
|
| 237 |
+
x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True) # merged boxes
|
| 238 |
+
if redundant:
|
| 239 |
+
i = i[iou.sum(1) > 1] # require redundancy
|
| 240 |
+
|
| 241 |
+
output[xi] = x[i]
|
| 242 |
+
if (time.time() - t) > time_limit:
|
| 243 |
+
print(f"WARNING: NMS time limit {time_limit}s exceeded")
|
| 244 |
+
break # time limit exceeded
|
| 245 |
+
|
| 246 |
+
return output
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
def scale_coords_landmarks(img1_shape, coords, img0_shape, ratio_pad=None):
|
| 250 |
+
# Rescale coords (xyxy) from img1_shape to img0_shape
|
| 251 |
+
if ratio_pad is None: # calculate from img0_shape
|
| 252 |
+
gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new
|
| 253 |
+
pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding
|
| 254 |
+
else:
|
| 255 |
+
gain = ratio_pad[0][0]
|
| 256 |
+
pad = ratio_pad[1]
|
| 257 |
+
|
| 258 |
+
coords[:, [0, 2, 4, 6, 8]] -= pad[0] # x padding
|
| 259 |
+
coords[:, [1, 3, 5, 7, 9]] -= pad[1] # y padding
|
| 260 |
+
coords[:, :10] /= gain
|
| 261 |
+
coords[:, 0].clamp_(0, img0_shape[1]) # x1
|
| 262 |
+
coords[:, 1].clamp_(0, img0_shape[0]) # y1
|
| 263 |
+
coords[:, 2].clamp_(0, img0_shape[1]) # x2
|
| 264 |
+
coords[:, 3].clamp_(0, img0_shape[0]) # y2
|
| 265 |
+
coords[:, 4].clamp_(0, img0_shape[1]) # x3
|
| 266 |
+
coords[:, 5].clamp_(0, img0_shape[0]) # y3
|
| 267 |
+
coords[:, 6].clamp_(0, img0_shape[1]) # x4
|
| 268 |
+
coords[:, 7].clamp_(0, img0_shape[0]) # y4
|
| 269 |
+
coords[:, 8].clamp_(0, img0_shape[1]) # x5
|
| 270 |
+
coords[:, 9].clamp_(0, img0_shape[0]) # y5
|
| 271 |
+
return coords
|
facelib/detection/yolov5face/utils/torch_utils.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torch import nn
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def fuse_conv_and_bn(conv, bn):
|
| 6 |
+
# Fuse convolution and batchnorm layers https://tehnokv.com/posts/fusing-batchnorm-and-conv/
|
| 7 |
+
fusedconv = (
|
| 8 |
+
nn.Conv2d(
|
| 9 |
+
conv.in_channels,
|
| 10 |
+
conv.out_channels,
|
| 11 |
+
kernel_size=conv.kernel_size,
|
| 12 |
+
stride=conv.stride,
|
| 13 |
+
padding=conv.padding,
|
| 14 |
+
groups=conv.groups,
|
| 15 |
+
bias=True,
|
| 16 |
+
)
|
| 17 |
+
.requires_grad_(False)
|
| 18 |
+
.to(conv.weight.device)
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# prepare filters
|
| 22 |
+
w_conv = conv.weight.clone().view(conv.out_channels, -1)
|
| 23 |
+
w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var)))
|
| 24 |
+
fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.size()))
|
| 25 |
+
|
| 26 |
+
# prepare spatial bias
|
| 27 |
+
b_conv = torch.zeros(conv.weight.size(0), device=conv.weight.device) if conv.bias is None else conv.bias
|
| 28 |
+
b_bn = bn.bias - bn.weight.mul(bn.running_mean).div(torch.sqrt(bn.running_var + bn.eps))
|
| 29 |
+
fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn)
|
| 30 |
+
|
| 31 |
+
return fusedconv
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def copy_attr(a, b, include=(), exclude=()):
|
| 35 |
+
# Copy attributes from b to a, options to only include [...] and to exclude [...]
|
| 36 |
+
for k, v in b.__dict__.items():
|
| 37 |
+
if (include and k not in include) or k.startswith("_") or k in exclude:
|
| 38 |
+
continue
|
| 39 |
+
|
| 40 |
+
setattr(a, k, v)
|