Run this model with HF Jobs
Run SnapOtter/deepsafe-services on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/SnapOtter/deepsafe-services. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/SnapOtter/deepsafe-services on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use SnapOtter/deepsafe-services with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use SnapOtter/deepsafe-services with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("SnapOtter/deepsafe-services", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
DeepSafe commited on
chore: remove non-weight files (batch 7)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- video/pwtf-dvd/model_code/inference/test_tools/__init__.pyDeleted
- video/pwtf-dvd/model_code/inference/test_tools/common.pyDeleted-122
- video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__init__.pyDeleted-56
- video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/__init__.cpython-310.pycDeleted
- video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/__init__.cpython-39.pycDeleted
- video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/alignment.cpython-310.pycDeleted
- video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/alignment.cpython-39.pycDeleted
- video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/detector.cpython-310.pycDeleted
- video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/detector.cpython-39.pycDeleted
- video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/utils.cpython-310.pycDeleted
- video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/utils.cpython-39.pycDeleted
- video/pwtf-dvd/model_code/inference/test_tools/ct/detection/alignment.pyDeleted-608
- video/pwtf-dvd/model_code/inference/test_tools/ct/detection/detector.pyDeleted-46
- video/pwtf-dvd/model_code/inference/test_tools/ct/detection/utils.pyDeleted-146
- video/pwtf-dvd/model_code/inference/test_tools/ct/face_alignment/__init__.pyDeleted-1
- video/pwtf-dvd/model_code/inference/test_tools/ct/face_alignment/basenet.pyDeleted-107
- video/pwtf-dvd/model_code/inference/test_tools/ct/face_alignment/predictor.pyDeleted-143
- video/pwtf-dvd/model_code/inference/test_tools/ct/face_alignment/utils.pyDeleted-17
- video/pwtf-dvd/model_code/inference/test_tools/ct/operations.pyDeleted-79
- video/pwtf-dvd/model_code/inference/test_tools/ct/tracking/__init__.pyDeleted
- video/pwtf-dvd/model_code/inference/test_tools/ct/tracking/sort.pyDeleted-285
- video/pwtf-dvd/model_code/inference/test_tools/ct/tracking/tracker.pyDeleted-27
- video/pwtf-dvd/model_code/inference/test_tools/ct/utils.pyDeleted-5
- video/pwtf-dvd/model_code/inference/test_tools/faster_crop_align_xray.pyDeleted-73
- video/pwtf-dvd/model_code/inference/test_tools/supply_writer.pyDeleted-49
- video/pwtf-dvd/model_code/inference/test_tools/utils.pyDeleted-115
- video/pwtf-dvd/model_code/inference/test_tools/warp_for_xray.pyDeleted-574
- video/pwtf-dvd/model_code/inference/utils/__init__.pyDeleted-7
- video/pwtf-dvd/model_code/inference/utils/common.pyDeleted-80
- video/pwtf-dvd/model_code/inference/utils/logger.pyDeleted-182
- video/pwtf-dvd/model_code/inference/utils/model_loader.pyDeleted-117
- video/pwtf-dvd/model_code/inference/utils/plugin_loader.pyDeleted-69
- video/pwtf-dvd/model_code/inference/utils/torch_save.pyDeleted-9
- video/pwtf-dvd/model_code/preprocessing/preprocess.pyDeleted-258
- video/pwtf-dvd/model_code/preprocessing/test_tools/__init__.pyDeleted
- video/pwtf-dvd/model_code/preprocessing/test_tools/common.pyDeleted-122
- video/pwtf-dvd/model_code/preprocessing/test_tools/ct/detection/__init__.pyDeleted-56
- video/pwtf-dvd/model_code/preprocessing/test_tools/ct/detection/alignment.pyDeleted-608
- video/pwtf-dvd/model_code/preprocessing/test_tools/ct/detection/detector.pyDeleted-46
- video/pwtf-dvd/model_code/preprocessing/test_tools/ct/detection/utils.pyDeleted-147
- video/pwtf-dvd/model_code/preprocessing/test_tools/ct/face_alignment/__init__.pyDeleted-1
- video/pwtf-dvd/model_code/preprocessing/test_tools/ct/face_alignment/basenet.pyDeleted-107
- video/pwtf-dvd/model_code/preprocessing/test_tools/ct/face_alignment/predictor.pyDeleted-143
- video/pwtf-dvd/model_code/preprocessing/test_tools/ct/face_alignment/utils.pyDeleted-17
- video/pwtf-dvd/model_code/preprocessing/test_tools/ct/operations.pyDeleted-79
- video/pwtf-dvd/model_code/preprocessing/test_tools/ct/tracking/__init__.pyDeleted
- video/pwtf-dvd/model_code/preprocessing/test_tools/ct/tracking/sort.pyDeleted-285
- video/pwtf-dvd/model_code/preprocessing/test_tools/ct/tracking/tracker.pyDeleted-27
- video/pwtf-dvd/model_code/preprocessing/test_tools/ct/utils.pyDeleted-5
- video/pwtf-dvd/model_code/preprocessing/test_tools/faster_crop_align_xray.pyDeleted-73
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
from .ct.detection.utils import grab_all_frames, get_valid_faces, sample_chunksfrom .ct.operations import multiple_trackingimport numpy as npfrom .ct.face_alignment import LandmarkPredictorfrom .ct.detection import FaceDetectorimport cv2from .utils import flatten,partition
detector = FaceDetector(0)predictor = LandmarkPredictor(0)
def get_five(ldm68): groups = [range(36, 42), range(42, 48), [30], [48], [54]] points = [] for group in groups: points.append(ldm68[group].mean(0)) return np.array(points)
def get_bbox(mask): try: y, x = np.nonzero(mask[..., 0]) return x.min() - 1, y.min() - 1, x.max() + 1, y.max() + 1 except: return None
def get_bigger_box(image, box, scale=0.5): height, width = image.shape[:2] box = np.rint(box).astype(np.int) new_box = box.reshape(2, 2) size = new_box[1] - new_box[0] diff = scale * size diff = diff[None, :] * np.array([-1, 1])[:, None] new_box = new_box + diff new_box[:, 0] = np.clip(new_box[:, 0], 0, width - 1) new_box[:, 1] = np.clip(new_box[:, 1], 0, height - 1) new_box = np.rint(new_box).astype(np.int) return new_box.reshape(-1)
def process_bigger_clips(clips, dete_res, clip_size, step, scale=0.5): assert len(clips) % clip_size == 0 detect_results = sample_chunks(dete_res, clip_size, step) clips = sample_chunks(clips, clip_size, step) new_clips = [] for i, (frame_clip, record_clip) in enumerate(zip(clips, detect_results)): tracks = multiple_tracking(record_clip) for j, track in enumerate(tracks): new_images = [] for (box, ldm, _), frame in zip(track, frame_clip): big_box = get_bigger_box(frame, box, scale) x1, y1, x2, y2 = big_box top_left = big_box[:2][None, :] new_ldm5 = ldm - top_left box = np.rint(box).astype(np.int) new_box = (box.reshape(2, 2) - top_left).reshape(-1) feed = LandmarkPredictor.prepare_feed(frame, box) ldm68 = predictor(feed) - top_left new_images.append( (frame[y1:y2, x1:x2], big_box, new_box, new_ldm5, ldm68) ) new_clips.append(new_images) return new_clips
def post(detected_faces): return [[face[:4], None, face[-1]] for face in detected_faces]
def check(detect_res): return min([len(faces) for faces in detect_res]) != 0
def detect_all(file, sfd_only=False, return_frames=False, max_size=None): frames = grab_all_frames(file, max_size=max_size, cvt=True) if not sfd_only: detect_res = flatten( [detector.detect(item) for item in partition(frames, 50)] ) detect_res = get_valid_faces(detect_res, thres=0.5) else: raise NotImplementedError
all_68 = get_lm68(frames, detect_res) if not return_frames: return detect_res, all_68 else: return detect_res, all_68, frames
def get_lm68(frames, detect_res): assert len(frames) == len(detect_res) frame_count = len(frames) all_68 = [] for i in range(frame_count): frame = frames[i] faces = detect_res[i] if len(faces) == 0: res_68 = [] else: feeds = [] for face in faces: assert len(face) == 3 box = face[0] feed = LandmarkPredictor.prepare_feed(frame, box) feeds.append(feed) res_68 = predictor(feeds) assert len(res_68) == len(faces) for face, l_68 in zip(faces, res_68): if face[1] is None: face[1] = get_five(l_68) all_68.append(res_68)
assert len(all_68) == len(detect_res) return all_681234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import cv2from .detector import RetinaFacefrom .utils import *
def assert_bounded(val, low, up): return val >= low and val < up
def check_valid(face, w, h): box = face[0] if box[0] > box[2]: return False if box[1] > box[3]: return False for idx, bound in zip([0, 1, 2, 3], [w, h, w, h]): if not assert_bounded(box[idx], 0, bound): return False pts = face[1] for p in pts: for idx, bound in zip([0, 1], [w, h]): if not assert_bounded(p[idx], 0, bound): return False return True
def post_detect(detect_results, scale, w, h): new_results = [] for frame_faces in detect_results: new_frame_faces = [] for box, ldm, score in frame_faces: box = box * scale ldm = ldm * scale face = (box, ldm, score) if check_valid(face, w=w, h=h): new_frame_faces.append(face) new_results.append(new_frame_faces) return new_results
class FaceDetector(RetinaFace): def scale_detect(self, images): max_res = 1920 h, w = images[0].shape[:2] if max(h, w) > max_res: init_scale = max(h, w) / max_res else: init_scale = 1 resize_scale = 2 * init_scale resize_w = int(w / resize_scale) resize_h = int(h / resize_scale) detect_input = [cv2.resize(frame, (resize_w, resize_h)) for frame in images] detect_results = post_detect( self.detect(detect_input), scale=resize_scale, w=w, h=h, ) return detect_results123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608from itertools import product as productfrom math import ceil
import numpy as npimport torchimport torch.backends.cudnn as cudnnimport torch.nn as nnimport torch.nn.functional as Fimport torchvision.models._utils as _utils
def conv_bn(inp, oup, stride=1, leaky=0): return nn.Sequential( nn.Conv2d(inp, oup, 3, stride, 1, bias=False), nn.BatchNorm2d(oup), nn.LeakyReLU(negative_slope=leaky, inplace=True), )
def conv_bn_no_relu(inp, oup, stride): return nn.Sequential( nn.Conv2d(inp, oup, 3, stride, 1, bias=False), nn.BatchNorm2d(oup), )
def conv_bn1X1(inp, oup, stride, leaky=0): return nn.Sequential( nn.Conv2d(inp, oup, 1, stride, padding=0, bias=False), nn.BatchNorm2d(oup), nn.LeakyReLU(negative_slope=leaky, inplace=True), )
def conv_dw(inp, oup, stride, leaky=0.1): return nn.Sequential( nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False), nn.BatchNorm2d(inp), nn.LeakyReLU(negative_slope=leaky, inplace=True), nn.Conv2d(inp, oup, 1, 1, 0, bias=False), nn.BatchNorm2d(oup), nn.LeakyReLU(negative_slope=leaky, inplace=True), )
class SSH(nn.Module): def __init__(self, in_channel, out_channel): super(SSH, self).__init__() assert out_channel % 4 == 0 leaky = 0 if out_channel <= 64: leaky = 0.1 self.conv3X3 = conv_bn_no_relu(in_channel, out_channel // 2, stride=1)
self.conv5X5_1 = conv_bn(in_channel, out_channel // 4, stride=1, leaky=leaky) self.conv5X5_2 = conv_bn_no_relu(out_channel // 4, out_channel // 4, stride=1)
self.conv7X7_2 = conv_bn( out_channel // 4, out_channel // 4, stride=1, leaky=leaky ) self.conv7x7_3 = conv_bn_no_relu(out_channel // 4, out_channel // 4, stride=1)
def forward(self, input): conv3X3 = self.conv3X3(input)
conv5X5_1 = self.conv5X5_1(input) conv5X5 = self.conv5X5_2(conv5X5_1)
conv7X7_2 = self.conv7X7_2(conv5X5_1) conv7X7 = self.conv7x7_3(conv7X7_2)
out = torch.cat([conv3X3, conv5X5, conv7X7], dim=1) out = F.relu(out) return out
class FPN(nn.Module): def __init__(self, in_channels_list, out_channels): super(FPN, self).__init__() leaky = 0 if out_channels <= 64: leaky = 0.1 self.output1 = conv_bn1X1( in_channels_list[0], out_channels, stride=1, leaky=leaky ) self.output2 = conv_bn1X1( in_channels_list[1], out_channels, stride=1, leaky=leaky ) self.output3 = conv_bn1X1( in_channels_list[2], out_channels, stride=1, leaky=leaky )
self.merge1 = conv_bn(out_channels, out_channels, leaky=leaky) self.merge2 = conv_bn(out_channels, out_channels, leaky=leaky)
def forward(self, input): # names = list(input.keys()) input = list(input.values())
output1 = self.output1(input[0]) output2 = self.output2(input[1]) output3 = self.output3(input[2])
up3 = F.interpolate( output3, size=[output2.size(2), output2.size(3)], mode="nearest" ) output2 = output2 + up3 output2 = self.merge2(output2)
up2 = F.interpolate( output2, size=[output1.size(2), output1.size(3)], mode="nearest" ) output1 = output1 + up2 output1 = self.merge1(output1)
out = [output1, output2, output3] return out
class MobileNetV1(nn.Module): def __init__(self): super(MobileNetV1, self).__init__() self.stage1 = nn.Sequential( conv_bn(3, 8, 2, leaky=0.1), # 3 conv_dw(8, 16, 1), # 7 conv_dw(16, 32, 2), # 11 conv_dw(32, 32, 1), # 19 conv_dw(32, 64, 2), # 27 conv_dw(64, 64, 1), # 43 ) self.stage2 = nn.Sequential( conv_dw(64, 128, 2), # 43 + 16 = 59 conv_dw(128, 128, 1), # 59 + 32 = 91 conv_dw(128, 128, 1), # 91 + 32 = 123 conv_dw(128, 128, 1), # 123 + 32 = 155 conv_dw(128, 128, 1), # 155 + 32 = 187 conv_dw(128, 128, 1), # 187 + 32 = 219 ) self.stage3 = nn.Sequential( conv_dw(128, 256, 2), # 219 +3 2 = 241 conv_dw(256, 256, 1), # 241 + 64 = 301 ) self.avg = nn.AdaptiveAvgPool2d((1, 1)) self.fc = nn.Linear(256, 1000)
def forward(self, x): x = self.stage1(x) x = self.stage2(x) x = self.stage3(x) x = self.avg(x) # x = self.model(x) x = x.view(-1, 256) x = self.fc(x) return x
class ClassHead(nn.Module): def __init__(self, inchannels=512, num_anchors=3): super(ClassHead, self).__init__() self.num_anchors = num_anchors self.conv1x1 = nn.Conv2d( inchannels, self.num_anchors * 2, kernel_size=(1, 1), stride=1, padding=0 )
def forward(self, x): out = self.conv1x1(x) out = out.permute(0, 2, 3, 1).contiguous()
return out.view(out.shape[0], -1, 2)
class BboxHead(nn.Module): def __init__(self, inchannels=512, num_anchors=3): super(BboxHead, self).__init__() self.conv1x1 = nn.Conv2d( inchannels, num_anchors * 4, kernel_size=(1, 1), stride=1, padding=0 )
def forward(self, x): out = self.conv1x1(x) out = out.permute(0, 2, 3, 1).contiguous()
return out.view(out.shape[0], -1, 4)
class LandmarkHead(nn.Module): def __init__(self, inchannels=512, num_anchors=3): super(LandmarkHead, self).__init__() self.conv1x1 = nn.Conv2d( inchannels, num_anchors * 10, kernel_size=(1, 1), stride=1, padding=0 )
def forward(self, x): out = self.conv1x1(x) out = out.permute(0, 2, 3, 1).contiguous()
return out.view(out.shape[0], -1, 10)
class RetinaFace(nn.Module): def __init__(self, cfg=None, phase="train"): """ :param cfg: Network related settings. :param phase: train or test. """ super(RetinaFace, self).__init__() self.phase = phase backbone = None if cfg["name"] == "mobilenet0.25": backbone = MobileNetV1() elif cfg["name"] == "Resnet50": import torchvision.models as models
backbone = models.resnet50(pretrained=cfg["pretrain"])
self.body = _utils.IntermediateLayerGetter(backbone, cfg["return_layers"]) in_channels_stage2 = cfg["in_channel"] in_channels_list = [ in_channels_stage2 * 2, in_channels_stage2 * 4, in_channels_stage2 * 8, ] out_channels = cfg["out_channel"] self.fpn = FPN(in_channels_list, out_channels) self.ssh1 = SSH(out_channels, out_channels) self.ssh2 = SSH(out_channels, out_channels) self.ssh3 = SSH(out_channels, out_channels)
self.ClassHead = self._make_class_head(fpn_num=3, inchannels=cfg["out_channel"]) self.BboxHead = self._make_bbox_head(fpn_num=3, inchannels=cfg["out_channel"]) self.LandmarkHead = self._make_landmark_head( fpn_num=3, inchannels=cfg["out_channel"] )
def _make_class_head(self, fpn_num=3, inchannels=64, anchor_num=2): classhead = nn.ModuleList() for i in range(fpn_num): classhead.append(ClassHead(inchannels, anchor_num)) return classhead
def _make_bbox_head(self, fpn_num=3, inchannels=64, anchor_num=2): bboxhead = nn.ModuleList() for i in range(fpn_num): bboxhead.append(BboxHead(inchannels, anchor_num)) return bboxhead
def _make_landmark_head(self, fpn_num=3, inchannels=64, anchor_num=2): landmarkhead = nn.ModuleList() for i in range(fpn_num): landmarkhead.append(LandmarkHead(inchannels, anchor_num)) return landmarkhead
def forward(self, inputs): out = self.body(inputs)
# FPN fpn = self.fpn(out)
# SSH feature1 = self.ssh1(fpn[0]) feature2 = self.ssh2(fpn[1]) feature3 = self.ssh3(fpn[2]) features = [feature1, feature2, feature3]
bbox_regressions = torch.cat( [self.BboxHead[i](feature) for i, feature in enumerate(features)], dim=1 ) classifications = torch.cat( [self.ClassHead[i](feature) for i, feature in enumerate(features)], dim=1 ) ldm_regressions = torch.cat( [self.LandmarkHead[i](feature) for i, feature in enumerate(features)], dim=1 )
if self.phase == "train": output = (bbox_regressions, classifications, ldm_regressions) else: output = ( bbox_regressions, F.softmax(classifications, dim=-1), ldm_regressions, ) return output
# Adapted from https://github.com/Hakuyume/chainer-ssddef decode(loc, priors, variances): boxes = torch.cat( ( priors[:, :2] + loc[:, :2] * variances[0] * priors[:, 2:], priors[:, 2:] * torch.exp(loc[:, 2:] * variances[1]), ), 1, ) boxes[:, :2] -= boxes[:, 2:] / 2 boxes[:, 2:] += boxes[:, :2] return boxes
def decode_landm(pre, priors, variances): landms = torch.cat( ( priors[:, :2] + pre[:, :2] * variances[0] * priors[:, 2:], priors[:, :2] + pre[:, 2:4] * variances[0] * priors[:, 2:], priors[:, :2] + pre[:, 4:6] * variances[0] * priors[:, 2:], priors[:, :2] + pre[:, 6:8] * variances[0] * priors[:, 2:], priors[:, :2] + pre[:, 8:10] * variances[0] * priors[:, 2:], ), dim=1, ) return landms
def py_cpu_nms(dets, thresh): """Pure Python NMS baseline.""" x1 = dets[:, 0] y1 = dets[:, 1] x2 = dets[:, 2] y2 = dets[:, 3] scores = dets[:, 4]
areas = (x2 - x1 + 1) * (y2 - y1 + 1) order = scores.argsort()[::-1]
keep = [] while order.size > 0: i = order[0] keep.append(i) xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2 = np.minimum(x2[i], x2[order[1:]]) yy2 = np.minimum(y2[i], y2[order[1:]])
w = np.maximum(0.0, xx2 - xx1 + 1) h = np.maximum(0.0, yy2 - yy1 + 1) inter = w * h ovr = inter / (areas[i] + areas[order[1:]] - inter)
inds = np.where(ovr <= thresh)[0] order = order[inds + 1]
return keep
class PriorBox(object): def __init__(self, cfg, image_size=None, phase="train"): super(PriorBox, self).__init__() self.min_sizes = cfg["min_sizes"] self.steps = cfg["steps"] self.clip = cfg["clip"] self.image_size = image_size self.feature_maps = [ [ceil(self.image_size[0] / step), ceil(self.image_size[1] / step)] for step in self.steps ] self.name = "s"
def forward(self): anchors = [] for k, f in enumerate(self.feature_maps): min_sizes = self.min_sizes[k] for i, j in product(range(f[0]), range(f[1])): for min_size in min_sizes: s_kx = min_size / self.image_size[1] s_ky = min_size / self.image_size[0] dense_cx = [ x * self.steps[k] / self.image_size[1] for x in [j + 0.5] ] dense_cy = [ y * self.steps[k] / self.image_size[0] for y in [i + 0.5] ] for cy, cx in product(dense_cy, dense_cx): anchors += [cx, cy, s_kx, s_ky]
# back to torch land output = torch.Tensor(anchors).view(-1, 4) if self.clip: output.clamp_(max=1, min=0) return output
cfg_mnet = { "name": "mobilenet0.25", "min_sizes": [[16, 32], [64, 128], [256, 512]], "steps": [8, 16, 32], "variance": [0.1, 0.2], "clip": False, "loc_weight": 2.0, "gpu_train": True, "batch_size": 32, "ngpu": 1, "epoch": 250, "decay1": 190, "decay2": 220, "image_size": 640, "pretrain": True, "return_layers": {"stage1": 1, "stage2": 2, "stage3": 3}, "in_channel": 32, "out_channel": 64,}
cfg_re50 = { "name": "Resnet50", "min_sizes": [[16, 32], [64, 128], [256, 512]], "steps": [8, 16, 32], "variance": [0.1, 0.2], "clip": False, "loc_weight": 2.0, "gpu_train": True, "batch_size": 24, "ngpu": 4, "epoch": 100, "decay1": 70, "decay2": 90, "image_size": 840, "pretrain": False, "return_layers": {"layer2": 1, "layer3": 2, "layer4": 3}, "in_channel": 256, "out_channel": 256,}
def check_keys(model, pretrained_state_dict): ckpt_keys = set(pretrained_state_dict.keys()) model_keys = set(model.state_dict().keys()) used_pretrained_keys = model_keys & ckpt_keys assert len(used_pretrained_keys) > 0, "load NONE from pretrained checkpoint" return True
def remove_prefix(state_dict, prefix): """ Old style model is stored with all names of parameters sharing common prefix 'module.' """ f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x return {f(key): value for key, value in state_dict.items()}
def load_model(model, pretrained_path, load_to_cpu): if load_to_cpu: if pretrained_path is None: url = "https://github.com/yinglinzheng/face_weights/releases/download/v1/mobilenet0.25_Final.pth" pretrained_dict = torch.utils.model_zoo.load_url(url) else: pretrained_dict = torch.load( pretrained_path, map_location=lambda storage, loc: storage ) else: device = torch.cuda.current_device() pretrained_dict = torch.load( pretrained_path, map_location=lambda storage, loc: storage.cuda(device) ) if "state_dict" in pretrained_dict.keys(): pretrained_dict = remove_prefix(pretrained_dict["state_dict"], "module.") else: pretrained_dict = remove_prefix(pretrained_dict, "module.") check_keys(model, pretrained_dict) model.load_state_dict(pretrained_dict, strict=False) return model
def load_net(model_path, device, network="mobilenet"): if network == "mobilenet": cfg = cfg_mnet elif network == "resnet50": cfg = cfg_re50 # net and model net = RetinaFace(cfg=cfg, phase="test") net = load_model(net, model_path, True) net.eval() cudnn.benchmark = True net = net.to(device) return net
def parse_det(det): landmarks = det[5:].reshape(5, 2) box = det[:4] score = det[4] return box, landmarks, score
def post_process( loc, conf, landms, prior_data, cfg, scale, scale1, resize, confidence_threshold, top_k, nms_threshold, keep_top_k,): boxes = decode(loc, prior_data, cfg["variance"]) boxes = boxes * scale / resize boxes = boxes.cpu().numpy() scores = conf.cpu().numpy()[:, 1] landms_copy = decode_landm(landms, prior_data, cfg["variance"])
landms_copy = landms_copy * scale1 / resize landms_copy = landms_copy.cpu().numpy()
# ignore low scores inds = np.where(scores > confidence_threshold)[0] boxes = boxes[inds] landms_copy = landms_copy[inds] scores = scores[inds]
# keep top-K before NMS order = scores.argsort()[::-1][:top_k] boxes = boxes[order] landms_copy = landms_copy[order] scores = scores[order]
# do NMS dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False) keep = py_cpu_nms(dets, nms_threshold) # keep = nms(dets, args.nms_threshold,force_cpu=args.cpu) dets = dets[keep, :] landms_copy = landms_copy[keep]
# keep top-K faster NMS dets = dets[:keep_top_k, :] landms_copy = landms_copy[:keep_top_k, :]
dets = np.concatenate((dets, landms_copy), axis=1) # show image dets = sorted(dets, key=lambda x: x[4], reverse=True) dets = [parse_det(x) for x in dets]
return dets
def batch_detect(net, images, device, is_tensor=False, normalized=False): with torch.no_grad(): confidence_threshold = 0.02 cfg = cfg_mnet top_k = 5000 nms_threshold = 0.4 keep_top_k = 750 resize = 1 if not is_tensor: try: img = np.float32(images) except ValueError: raise NotImplementedError("Input images must of same size") img = torch.from_numpy(img) else: img = images.float() img = img.to(device) mean = ( torch.as_tensor([104, 117, 123], dtype=img.dtype, device=img.device) .unsqueeze(0) .unsqueeze(0) .unsqueeze(0) ) img -= mean img = img.permute(0, 3, 1, 2) (batch_size, _, im_height, im_width,) = img.shape scale = torch.as_tensor( [im_width, im_height, im_width, im_height], dtype=img.dtype, device=img.device, ) scale = scale.to(device)
loc, conf, landms = net(img) # forward pass
priorbox = PriorBox(cfg, image_size=(im_height, im_width)) priors = priorbox.forward() prior_data = priors.to(device) scale1 = torch.as_tensor( [ img.shape[3], img.shape[2], img.shape[3], img.shape[2], img.shape[3], img.shape[2], img.shape[3], img.shape[2], img.shape[3], img.shape[2], ], dtype=img.dtype, device=img.device, ) scale1 = scale1.to(device)
all_dets = [ post_process( loc_i, conf_i, landms_i, prior_data, cfg, scale, scale1, resize, confidence_threshold, top_k, nms_threshold, keep_top_k, ) for loc_i, conf_i, landms_i in zip(loc, conf, landms) ]
return all_dets12345678910111213141516171819202122232425262728293031323334353637383940414243444546import os
import numpy as npimport torch
from .alignment import load_net, batch_detect
def get_project_dir(): current_path = os.path.abspath(os.path.join(__file__, "../")) return current_path
def relative(path): path = os.path.join(get_project_dir(), path) return os.path.abspath(path)
class RetinaFace: def __init__( self, gpu_id=-1, model_path=None, network="mobilenet", ): self.gpu_id = gpu_id self.device = ( torch.device("cpu") if gpu_id == -1 else torch.device("cuda", gpu_id) ) self.model = load_net(model_path, self.device, network)
def detect(self, images): if isinstance(images, np.ndarray): if len(images.shape) == 3: return batch_detect(self.model, [images], self.device)[0] elif len(images.shape) == 4: return batch_detect(self.model, images, self.device) elif isinstance(images, list): return batch_detect(self.model, np.array(images), self.device) elif isinstance(images, torch.Tensor): if len(images.shape) == 3: return batch_detect(self.model, images.unsqueeze(0), self.device)[0] elif len(images.shape) == 4: return batch_detect(self.model, images, self.device) else: raise NotImplementedError()
def __call__(self, images): return self.detect(images)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146import cv2from test_tools.utils import flattenimport numpy as np
def chunks(l, n, step=None): if step is None: step = n return [l[i : i + n] for i in range(0, len(l), step)]
def sample_chunks(l, n, step=None): return [l[i : i + n] for i in range(0, len(l), step) if i + n <= len(l)]
def grab_all_frames(path, max_size, cvt=False): capture = cv2.VideoCapture(path) ret = True frames = [] while ret: ret, frame = capture.read() if ret: if cvt: frame = frame[..., ::-1] frames.append(frame) if len(frames) == max_size: break capture.release() return frames
def get_clips_uniform(path, count, clip_size): capture = cv2.VideoCapture(path) n_frames = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)) max_clip_available = n_frames + 1 - clip_size if count > max_clip_available: count = max_clip_available final_start = max_clip_available - 1 start_indices = np.linspace(0, final_start, count, endpoint=True, dtype=np.int) all_clip_idx = [list(range(start, start + clip_size)) for start in start_indices] valid = set(flatten(all_clip_idx)) max_idx = max(valid)
frames = {} for idx in range(max_idx + 1): # Get the next frame, but don't decode if we're not using it. ret = capture.grab() if not ret: continue
if idx in valid: ret, frame = capture.retrieve() if not ret or frame is None: continue else: # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frames[idx] = frame
capture.release() clips = [] for clip_idx in all_clip_idx: clip = [] flag = True for idx in clip_idx: if idx not in frames: flag = False break clip.append(frames[idx]) if flag: clips.append(clip) return clips
def get_valid_faces(detect_results, max_count=10, thres=0.5, at_least=False): new_results = [] for i, faces in enumerate(detect_results): if len(faces) > max_count: faces = faces[:max_count] l = [] for j, face in enumerate(faces): if face[-1] < thres and not (j == 0 and at_least): continue box, lm, score = face box = box.astype(np.float32) lm = lm.astype(np.float32) l.append((box, lm, score)) new_results.append(l) return new_results
def scale_box(box, scale_h, scale_w, h, w): x1, y1, x2, y2 = box.astype(np.int32) center_x = (x1 + x2) // 2 center_y = (y1 + y2) // 2 box_h = int((y2 - y1) * scale_h) box_w = int((x2 - x1) * scale_w) new_x1 = center_x - box_w // 2 new_x2 = new_x1 + box_w new_y1 = center_y - box_h // 2 new_y2 = new_y1 + box_h new_x1 = max(new_x1, 0) new_y1 = max(new_y1, 0) new_y2 = min(new_y2, h) new_x2 = min(new_x2, w) return new_x1, new_y1, new_x2, new_y2
def get_bbox(detect_res): tmp_detect_res = get_valid_faces(detect_res, max_count=4, thres=0.5) all_face_bboxs = [] for faces in tmp_detect_res: all_face_bboxs.extend([face[0] for face in faces]) all_face_bboxs = np.array(all_face_bboxs).astype(np.int) x1 = all_face_bboxs[:, 0].min() x2 = all_face_bboxs[:, 2].max() y1 = all_face_bboxs[:, 1].min() y2 = all_face_bboxs[:, 3].max()
return x1, y1, x2, y2
def delta_detect_res(detect_res, x1, y1): diff = np.array([[x1, y1]]) new_detect_res = [] for faces in detect_res: f = [] for face in faces: box, lm, score = face box = box.astype(np.float) box[[0, 2]] -= x1 box[[1, 3]] -= y1 lm = lm.astype(np.float) - diff f.append((box, lm, score)) new_detect_res.append(f) return new_detect_res
def pre_crop(clips, detect_res): box = np.array(get_bbox(detect_res)) w = box[2] - box[0] h = box[3] - box[1] x1, y1, x2, y2 = scale_box( box, 1.5, 1.2 if w > 2 * h else 1.5, clips[0].shape[0], clips[0].shape[1] ) clips = np.array(clips) return clips[:, y1:y2, x1:x2], delta_detect_res(detect_res, x1, y1)1from .predictor import LandmarkPredictor123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107# Backbone networks used for face landmark detection# Cunjian Chen (cunjian@msu.edu)
import torch.nn as nnimport torchvision.models as models
class ConvBlock(nn.Module): def __init__(self, inp, oup, k, s, p, dw=False, linear=False): super(ConvBlock, self).__init__() self.linear = linear if dw: self.conv = nn.Conv2d(inp, oup, k, s, p, groups=inp, bias=False) else: self.conv = nn.Conv2d(inp, oup, k, s, p, bias=False) self.bn = nn.BatchNorm2d(oup) if not linear: self.prelu = nn.PReLU(oup)
def forward(self, x): x = self.conv(x) x = self.bn(x) if self.linear: return x else: return self.prelu(x)
# SE module# https://github.com/wujiyang/Face_Pytorch/blob/master/backbone/cbam.pyclass SEModule(nn.Module): """Squeeze and Excitation Module"""
def __init__(self, channels, reduction): super(SEModule, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.fc1 = nn.Conv2d( channels, channels // reduction, kernel_size=1, padding=0, bias=False ) self.relu = nn.ReLU(inplace=True) self.fc2 = nn.Conv2d( channels // reduction, channels, kernel_size=1, padding=0, bias=False ) self.sigmoid = nn.Sigmoid()
def forward(self, x): input = x x = self.avg_pool(x) x = self.fc1(x) x = self.relu(x) x = self.fc2(x) x = self.sigmoid(x)
return input * x
# USE global depthwise convolution layer. Compatible with MobileNetV2 (224×224), MobileNetV2_ExternalData (224×224)class MobileNet_GDConv(nn.Module): def __init__(self, num_classes): super(MobileNet_GDConv, self).__init__() self.pretrain_net = models.mobilenet_v2(pretrained=False) self.base_net = nn.Sequential(*list(self.pretrain_net.children())[:-1]) self.linear7 = ConvBlock(1280, 1280, (7, 7), 1, 0, dw=True, linear=True) self.linear1 = ConvBlock(1280, num_classes, 1, 1, 0, linear=True)
def forward(self, x): x = self.base_net(x) x = self.linear7(x) x = self.linear1(x) x = x.view(x.size(0), -1) return x
# USE global depthwise convolution layer. Compatible with MobileNetV2 (56×56)class MobileNet_GDConv_56(nn.Module): def __init__(self, num_classes): super(MobileNet_GDConv_56, self).__init__() self.pretrain_net = models.mobilenet_v2(pretrained=False) self.base_net = nn.Sequential(*list(self.pretrain_net.children())[:-1]) self.linear7 = ConvBlock(1280, 1280, (2, 2), 1, 0, dw=True, linear=True) self.linear1 = ConvBlock(1280, num_classes, 1, 1, 0, linear=True)
def forward(self, x): x = self.base_net(x) x = self.linear7(x) x = self.linear1(x) x = x.view(x.size(0), -1) return x
# MobileNetV2 with SE; Compatible with MobileNetV2_SE (224×224) and MobileNetV2_SE_RE (224×224)class MobileNet_GDConv_SE(nn.Module): def __init__(self, num_classes): super(MobileNet_GDConv_SE, self).__init__() self.pretrain_net = models.mobilenet_v2(pretrained=True) self.base_net = nn.Sequential(*list(self.pretrain_net.children())[:-1]) self.linear7 = ConvBlock(1280, 1280, (7, 7), 1, 0, dw=True, linear=True) self.linear1 = ConvBlock(1280, num_classes, 1, 1, 0, linear=True) self.attention = SEModule(1280, 8)
def forward(self, x): x = self.base_net(x) x = self.attention(x) x = self.linear7(x) x = self.linear1(x) x = x.view(x.size(0), -1) return x123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143# Face alignment demo# Uses MTCNN as face detector# Cunjian Chen (ccunjian@gmail.com)import torchimport cv2import numpy as npfrom torch.utils.data import DataLoaderfrom .basenet import MobileNet_GDConv
def get_device(gpu_id): if gpu_id > -1: return torch.device(f"cuda:{str(gpu_id)}") else: return torch.device("cpu")
def load_model(file): model = MobileNet_GDConv(136) if file is not None: model.load_state_dict(torch.load(file, map_location="cpu")) else: url = "https://github.com/yinglinzheng/face_weights/releases/download/v1/mobilenet_224_model_best_gdconv_external.pth" model.load_state_dict(torch.utils.model_zoo.load_url(url)) return model
# landmark of (5L, 2L) from [0,1] to real rangedef reproject(bbox, landmark): landmark_ = landmark.clone() x1, y1, x2, y2 = bbox w = x2 - x1 h = y2 - y1 landmark_[:, 0] *= w landmark_[:, 0] += x1 landmark_[:, 1] *= h landmark_[:, 1] += y1 return landmark_
def prepare_feed(img, face): height, width, _ = img.shape mean = np.asarray([0.485, 0.456, 0.406]) std = np.asarray([0.229, 0.224, 0.225]) out_size = 224 x1, y1, x2, y2 = face[:4]
w = x2 - x1 + 1 h = y2 - y1 + 1 size = int(min([w, h]) * 1.2) cx = x1 + w // 2 cy = y1 + h // 2 x1 = cx - size // 2 x2 = x1 + size y1 = cy - size // 2 y2 = y1 + size
dx = max(0, -x1) dy = max(0, -y1) x1 = max(0, x1) y1 = max(0, y1)
edx = max(0, x2 - width) edy = max(0, y2 - height) x2 = min(width, x2) y2 = min(height, y2) new_bbox = torch.Tensor([x1, y1, x2, y2]).int() x1, y1, x2, y2 = new_bbox cropped = img[y1:y2, x1:x2] if dx > 0 or dy > 0 or edx > 0 or edy > 0: cropped = cv2.copyMakeBorder( cropped, int(dy), int(edy), int(dx), int(edx), cv2.BORDER_CONSTANT, 0 ) cropped_face = cv2.resize(cropped, (out_size, out_size))
if cropped_face.shape[0] <= 0 or cropped_face.shape[1] <= 0: return None test_face = cropped_face.copy() test_face = test_face / 255.0 test_face = (test_face - mean) / std test_face = test_face.transpose((2, 0, 1)) data = torch.from_numpy(test_face).float() return dict(data=data, bbox=new_bbox)
@torch.no_grad()def single_predict(model, feed, device): landmark = model(feed["data"].unsqueeze(0).to(device)).cpu() landmark = landmark.reshape(-1, 2) landmark = reproject(feed["bbox"], landmark) return landmark.numpy()
@torch.no_grad()def batch_predict(model, feeds, device): if not isinstance(feeds, list): feeds = [feeds] # loader = DataLoader(FeedDataset(feeds), batch_size=50, shuffle=False) data = [] for feed in feeds: data.append(feed["data"].unsqueeze(0)) data = torch.cat(data, 0).to(device) results = []
landmarks = model(data).cpu() for landmark, feed in zip(landmarks, feeds): landmark = landmark.reshape(-1, 2) landmark = reproject(feed["bbox"], landmark) results.append(landmark.numpy()) return results
@torch.no_grad()def batch_predict2(model, feeds, device, batch_size=None): if not isinstance(feeds, list): feeds = [feeds] if batch_size is None: batch_size = len(feeds) loader = DataLoader(feeds, batch_size=len(feeds), shuffle=False) results = [] for feed in loader: landmarks = model(feed["data"].to(device)).cpu() for landmark, bbox in zip(landmarks, feed["bbox"]): landmark = landmark.reshape(-1, 2) landmark = reproject(bbox, landmark) results.append(landmark.numpy()) return results
class LandmarkPredictor: def __init__(self, gpu_id=0, file=None): self.device = get_device(gpu_id) self.model = load_model(file).to(self.device).eval()
def __call__(self, feeds): results = batch_predict2(self.model, feeds, self.device) if not isinstance(feeds, list): results = results[0] return results
@staticmethod def prepare_feed(img, face): return prepare_feed(img, face)1234567891011121314151617import cv2
def drawLandmark_multiple(img, bbox, landmark): """ Input: - img: gray or RGB - bbox: type of BBox - landmark: reproject landmark of (5L, 2L) Output: - img marked with landmark and bbox """ x1, y1, x2, y2 = bbox cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2) for x, y in landmark: cv2.circle(img, (int(x), int(y)), 2, (0, 255, 0), -1) return img12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import os
import osimport cv2import numpy as npfrom .tracking.sort import iou
def face_iou(f1, f2): return iou(f1[0], f2[0])
def simple_tracking(batch_landmarks, index=0, thres=0.5): track = []
for i, faces in enumerate(batch_landmarks): if i == 0: if len(faces) <= index or faces[index][-1] < 0.8: return None if index != 0: for idx in range(index): if face_iou(faces[idx], faces[index]) > thres: return None track.append(faces[index]) else: last = track[i - 1] if len(faces) == 0: return None sorted_faces = sorted(faces, key=lambda x: face_iou(x, last), reverse=True) if face_iou(sorted_faces[0], last) < thres: return None track.append(sorted_faces[0]) return track
def multiple_tracking(batch_landmarks): tracks = [] for i in range(len(batch_landmarks[0])): track = simple_tracking(batch_landmarks, index=i) if track is None: continue tracks.append(track) return tracks
def find_longest(detect_res): fc = len(detect_res) tuples = [] start = 0 end = 0 previous_count = -1 all_tracks = [] # start 取得到,end 取不到 while start < (fc - 1): for end in range(start + 2, fc + 1): tracks = multiple_tracking(detect_res[start:end]) if (len(tracks) != previous_count and previous_count != -1) or len( tracks ) == 0: break previous_count = len(tracks) if end - start > 2: if end != fc: un_reach_end = end - 1 else: un_reach_end = end sub_tracks = multiple_tracking(detect_res[start:un_reach_end]) if end == fc and len(sub_tracks) == 0: un_reach_end = end - 1 sub_tracks = multiple_tracking(detect_res[start:un_reach_end]) if len(sub_tracks) > 0: tpl = (start, un_reach_end) tuples.append(tpl) all_tracks.append(sub_tracks[0]) else: raise NotImplementedError previous_count = -1 end = un_reach_end start = end return tuples, all_tracks123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285""" SORT: A Simple, Online and Realtime Tracker Copyright (C) 2016 Alex Bewley alex@dynamicdetection.com
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>."""from __future__ import print_functionimport os.pathimport numpy as npimport matplotlib.pyplot as pltimport matplotlib.patches as patchesfrom scipy.optimize import linear_sum_assignmentimport globimport timeimport argparsefrom filterpy.kalman import KalmanFilter
def iou(bb_test, bb_gt): """ Computes IUO between two bboxes in the form [x1,y1,x2,y2] """ xx1 = np.maximum(bb_test[0], bb_gt[0]) yy1 = np.maximum(bb_test[1], bb_gt[1]) xx2 = np.minimum(bb_test[2], bb_gt[2]) yy2 = np.minimum(bb_test[3], bb_gt[3]) w = np.maximum(0.0, xx2 - xx1) h = np.maximum(0.0, yy2 - yy1) wh = w * h o = wh / ( (bb_test[2] - bb_test[0]) * (bb_test[3] - bb_test[1]) + (bb_gt[2] - bb_gt[0]) * (bb_gt[3] - bb_gt[1]) - wh ) return o
def convert_bbox_to_z(bbox): """ Takes a bounding box in the form [x1,y1,x2,y2] and returns z in the form [x,y,s,r] where x,y is the centre of the box and s is the scale/area and r is the aspect ratio """ w = bbox[2] - bbox[0] h = bbox[3] - bbox[1] x = bbox[0] + w / 2.0 y = bbox[1] + h / 2.0 s = w * h # scale is just area r = w / float(h) return np.array([x, y, s, r]).reshape((4, 1))
def convert_x_to_bbox(x, score=None): """ Takes a bounding box in the centre form [x,y,s,r] and returns it in the form [x1,y1,x2,y2] where x1,y1 is the top left and x2,y2 is the bottom right """ w = np.sqrt(x[2] * x[3]) h = x[2] / w if score == None: return np.array( [x[0] - w / 2.0, x[1] - h / 2.0, x[0] + w / 2.0, x[1] + h / 2.0] ).reshape((1, 4)) else: return np.array( [x[0] - w / 2.0, x[1] - h / 2.0, x[0] + w / 2.0, x[1] + h / 2.0, score] ).reshape((1, 5))
class KalmanBoxTracker(object): """ This class represents the internel state of individual tracked objects observed as bbox. """
count = 0
def __init__(self, bbox): """ Initialises a tracker using initial bounding box. """ # define constant velocity model self.kf = KalmanFilter(dim_x=7, dim_z=4) self.kf.F = np.array( [ [1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1], ] ) self.kf.H = np.array( [ [1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], ] )
self.kf.R[2:, 2:] *= 10.0 self.kf.P[ 4:, 4: ] *= 1000.0 # give high uncertainty to the unobservable initial velocities self.kf.P *= 10.0 self.kf.Q[-1, -1] *= 0.01 self.kf.Q[4:, 4:] *= 0.01
self.kf.x[:4] = convert_bbox_to_z(bbox) self.time_since_update = 0 self.id = KalmanBoxTracker.count KalmanBoxTracker.count += 1 self.history = [] self.hits = 0 self.hit_streak = 0 self.age = 0
def update(self, bbox): """ Updates the state vector with observed bbox. """ self.time_since_update = 0 self.history = [] self.hits += 1 self.hit_streak += 1 self.kf.update(convert_bbox_to_z(bbox))
def predict(self): """ Advances the state vector and returns the predicted bounding box estimate. """ if (self.kf.x[6] + self.kf.x[2]) <= 0: self.kf.x[6] *= 0.0 self.kf.predict() self.age += 1 if self.time_since_update > 0: self.hit_streak = 0 self.time_since_update += 1 self.history.append(convert_x_to_bbox(self.kf.x)) return self.history[-1]
def get_state(self): """ Returns the current bounding box estimate. """ return convert_x_to_bbox(self.kf.x)
def associate_detections_to_trackers(detections, trackers, iou_threshold=0.3): """ Assigns detections to tracked object (both represented as bounding boxes)
Returns 3 lists of matches, unmatched_detections and unmatched_trackers """ if len(trackers) == 0: return ( np.empty((0, 2), dtype=int), np.arange(len(detections)), np.empty((0, 5), dtype=int), ) iou_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)
for d, det in enumerate(detections): for t, trk in enumerate(trackers): iou_matrix[d, t] = iou(det, trk)
matched_indices = linear_sum_assignment(-iou_matrix) matched_indices = np.array(list(zip(*matched_indices)), dtype=np.int) matched_indices.shape = (-1, 2) # print(matched_indices) # print(type(matched_indices))
unmatched_detections = [] for d, det in enumerate(detections): if d not in matched_indices[:, 0]: unmatched_detections.append(d) unmatched_trackers = [] for t, trk in enumerate(trackers): if t not in matched_indices[:, 1]: unmatched_trackers.append(t)
# filter out matched with low IOU matches = [] for m in matched_indices: if iou_matrix[m[0], m[1]] < iou_threshold: unmatched_detections.append(m[0]) unmatched_trackers.append(m[1]) else: matches.append(m.reshape(1, 2)) if len(matches) == 0: matches = np.empty((0, 2), dtype=int) else: matches = np.concatenate(matches, axis=0)
return matches, np.array(unmatched_detections), np.array(unmatched_trackers)
class Sort(object): def __init__(self, max_age=1, min_hits=3): """ Sets key parameters for SORT """ self.max_age = max_age self.min_hits = min_hits self.trackers = [] self.frame_count = 0
def update(self, dets): """ Params: dets - a numpy array of detections in the format [[x1,y1,x2,y2,score],[x1,y1,x2,y2,score],...] Requires: this method must be called once for each frame even with empty detections. Returns the a similar array, where the last column is the object ID.
NOTE: The number of objects returned may differ from the number of detections provided. """ self.frame_count += 1 # get predicted locations from existing trackers. trks = np.zeros((len(self.trackers), 5)) to_del = [] ret = [] for t, trk in enumerate(trks): pos = self.trackers[t].predict()[0] trk[:] = [pos[0], pos[1], pos[2], pos[3], 0] if np.any(np.isnan(pos)): to_del.append(t) trks = np.ma.compress_rows(np.ma.masked_invalid(trks)) for t in reversed(to_del): self.trackers.pop(t) matched, unmatched_dets, unmatched_trks = associate_detections_to_trackers( dets, trks )
# update matched trackers with assigned detections for t, trk in enumerate(self.trackers): if t not in unmatched_trks: d = matched[np.where(matched[:, 1] == t)[0], 0] trk.update(dets[d, :][0])
# create and initialise new trackers for unmatched detections for i in unmatched_dets: trk = KalmanBoxTracker(dets[i, :]) self.trackers.append(trk) i = len(self.trackers) for trk in reversed(self.trackers): d = trk.get_state()[0] if (trk.time_since_update < 1) and ( trk.hit_streak >= self.min_hits or self.frame_count <= self.min_hits ): ret.append( np.concatenate((d, [trk.id + 1])).reshape(1, -1) ) # +1 as MOT benchmark requires positive i -= 1 # remove dead tracklet if trk.time_since_update > self.max_age: self.trackers.pop(i) if len(ret) > 0: return np.concatenate(ret) return np.empty((0, 5))
def parse_args(): """Parse input arguments.""" parser = argparse.ArgumentParser(description="SORT demo") parser.add_argument( "--display", dest="display", help="Display online tracker output (slow) [False]", action="store_true", ) args = parser.parse_args() return args123456789101112131415161718192021222324252627from .sort import Sortimport numpy as np
def get_detections(faces): detections = [] for face in faces: x1, y1, x2, y2 = face[0] detections.append((x1, y1, x2, y2, face[-1])) return np.array(detections)
def get_tracks(detect_results): tracks = {} mot_tracker = Sort() for faces in detect_results: detections = get_detections(faces) track_bbs_ids = mot_tracker.update(detections) for track in track_bbs_ids: # 单独框出每一张人脸 id = int(track[-1]) box = track[:4] if id in tracks: tracks[id].append(box) else: tracks[id] = [box]
return [track for id, track in tracks.items() if len(track) == len(detect_results)]12345import cv2
def write_img(file, img): cv2.imwrite(file, img, [cv2.IMWRITE_PNG_COMPRESSION, 0])12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273import numpy as npimport cv2from .warp_for_xray import ( estimiate_batch_transform, transform_landmarks, std_points_256,)import numpy as np
class FasterCropAlignXRay: """ 修正到统一坐标系,统一图像大小到标准尺寸 """
def __init__(self, size=256): self.image_size = size self.std_points = std_points_256 * size / 256.0
def __call__(self, landmarks, images=None, jitter=False): landmarks = [landmark[:4] for landmark in landmarks] ori_boxes = np.array([ori_box for _, _, _, ori_box in landmarks]) five_landmarks = np.array([ldm5 for _, ldm5, _, _ in landmarks]) landmarks68 = np.array([ldm68 for _, _, ldm68, _ in landmarks]) # assert landmarks68.min() > 0
left_top = ori_boxes[:, :2].min(0)
right_bottom = ori_boxes[:, 2:].max(0)
size = right_bottom - left_top
w, h = size
diff = ori_boxes[:, :2] - left_top[None, ...]
new_five_landmarks = five_landmarks + diff[:, None, :] new_landmarks68 = landmarks68 + diff[:, None, :]
landmark_for_estimiate = new_five_landmarks.copy() if jitter: landmark_for_estimiate += np.random.uniform( -4, 4, landmark_for_estimiate.shape )
tfm, trans = estimiate_batch_transform( landmark_for_estimiate, tgt_pts=self.std_points )
transformed_landmarks68 = np.array( [transform_landmarks(ldm68, trans) for ldm68 in new_landmarks68] )
if images is not None: transformed_images = [ self.process_sinlge(tfm, image, d, h, w) for image, d in zip(images, diff) ] # 拼接 func 的参数 transformed_images = np.stack(transformed_images) return transformed_landmarks68, transformed_images else: return transformed_landmarks68
def process_sinlge(self, tfm, image, d, h, w): assert isinstance(image, np.ndarray) new_image = np.zeros((h, w, 3), dtype=np.uint8) x, y = d ih, iw, _ = image.shape new_image[y : y + ih, x : x + iw] = image transformed_image = cv2.warpAffine( new_image, tfm, (self.image_size, self.image_size) ) return transformed_image12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import cv2
class SupplyWriter: def __init__(self, intput_video, output_video, opt_thres, rgb_input=True): reader = cv2.VideoCapture(intput_video) fourcc = cv2.VideoWriter_fourcc(*"XVID") fps = reader.get(cv2.CAP_PROP_FPS) width = int(reader.get(3)) height = int(reader.get(4)) reader.release() self.padding = 40
self.writer = cv2.VideoWriter(output_video, fourcc, fps, (height, width)[::-1]) self.rgb_input = rgb_input self.opt_thres = opt_thres
def run(self, images, scores, boxes): # Text variables font_face = cv2.FONT_HERSHEY_SIMPLEX thickness = 5 font_scale = 3
for image, score, box in zip(images, scores, boxes): if self.rgb_input: image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) if box is not None: label = "fake" if score > self.opt_thres else "real" x1, y1, x2, y2 = box x = int(x1) y = int(y1) w = int(x2 - x1) h = int(y2 - y1) color = ( (255, 255, 0) if label == "real" else (0, 255, 255) ) # BGR 255 0 cv2.putText( image, label, (x, y + h + 68), font_face, font_scale, color, thickness, 2, ) # draw box over face cv2.rectangle(image, (x, y), (x + w, y + h), color, 10) self.writer.write(image) self.writer.release()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115import numpy as npimport cv2import osimport platformimport jsonimport errno
def weak_check(detect_res): return sum([len(faces) for faces in detect_res]) > len(detect_res) * 0.75
def get_crop_box(shape, box, scale=0.5): height, width = shape box = np.rint(box).astype(np.int32) new_box = box.reshape(2, 2) size = new_box[1] - new_box[0] diff = scale * size diff = diff[None, :] * np.array([-1, 1])[:, None] new_box = new_box + diff new_box[:, 0] = np.clip(new_box[:, 0], 0, width - 1) new_box[:, 1] = np.clip(new_box[:, 1], 0, height - 1) new_box = np.rint(new_box).astype(np.int32) return new_box.reshape(-1)
def get_fps(input_file): reader = cv2.VideoCapture(input_file) fps = reader.get(cv2.CAP_PROP_FPS) reader.release() return fps
def mkdir_p(dirname): """Like "mkdir -p", make a dir recursively, but do nothing if the dir exists 这个是线程安全的, from Lingzhi Li Args: dirname(str): """ assert dirname is not None if dirname == "" or os.path.isdir(dirname): return try: os.makedirs(dirname) except OSError as e: if e.errno != errno.EEXIST: raise e
def mkdir(*args): for folder in args: if not os.path.isdir(folder): mkdir_p(folder)
def make_join(*args): folder = os.path.join(*args) mkdir(folder) return folder
def list_dir(folder, condition=None, key=lambda x: x, reverse=False, co_join=[]): files = os.listdir(folder) if condition is not None: files = filter(condition, files) co_join = [folder] + co_join if key is not None: files = sorted(files, key=key, reverse=reverse) files = [(file, *[os.path.join(fold, file) for fold in co_join]) for file in files] return files
def get_jointer(file): def jointer(folder): return os.path.join(folder, file)
return jointer
def flatten(l): return [item for sublist in l for item in sublist]
def is_win(): return platform.system() == "Windows"
def get_postfix(post_fix): return lambda x: x.endswith(post_fix)
def partition(images, size): """ Returns a new list with elements of which is a list of certain size.
>>> partition([1, 2, 3, 4], 3) [[1, 2, 3], [4]] """ return [ images[i : i + size] if i + size <= len(images) else images[i:] for i in range(0, len(images), size) ]
def load_json(file): with open(file, "r") as f: res = json.load(f) return res
def save_json(file, obj): with open(file, "w", encoding="utf-8") as f: json.dump(obj, f, indent=4, ensure_ascii=False)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574import numpy as npimport cv2
# -*- coding: utf-8 -*-"""Created on Tue Jul 11 06:54:28 2017
@author: zhaoyafei"""
import numpy as npfrom numpy.linalg import inv, norm, lstsqfrom numpy.linalg import matrix_rank as rank
"""Introduction:----------numpy implemetation form matlab function CP2TFORM(...)with 'transformtype': 1) 'nonreflective similarity' 2) 'similarity'
MATLAB code:----------%--------------------------------------% Function findNonreflectiveSimilarity%function [trans, output] = findNonreflectiveSimilarity(uv,xy,options)%% For a nonreflective similarity:%% let sc = s*cos(theta)% let ss = s*sin(theta)%% [ sc -ss% [u v] = [x y 1] * ss sc% tx ty]%% There are 4 unknowns: sc,ss,tx,ty.%% Another way to write this is:%% u = [x y 1 0] * [sc% ss% tx% ty]%% v = [y -x 0 1] * [sc% ss% tx% ty]%% With 2 or more correspondence points we can combine the u equations and% the v equations for one linear system to solve for sc,ss,tx,ty.%% [ u1 ] = [ x1 y1 1 0 ] * [sc]% [ u2 ] [ x2 y2 1 0 ] [ss]% [ ... ] [ ... ] [tx]% [ un ] [ xn yn 1 0 ] [ty]% [ v1 ] [ y1 -x1 0 1 ]% [ v2 ] [ y2 -x2 0 1 ]% [ ... ] [ ... ]% [ vn ] [ yn -xn 0 1 ]%% Or rewriting the above matrix equation:% U = X * r, where r = [sc ss tx ty]'% so r = X\ U.%
K = options.K;M = size(xy,1);x = xy(:,1);y = xy(:,2);X = [x y ones(M,1) zeros(M,1); y -x zeros(M,1) ones(M,1) ];
u = uv(:,1);v = uv(:,2);U = [u; v];
% We know that X * r = Uif rank(X) >= 2*K r = X \ U;else error(message('images:cp2tform:twoUniquePointsReq'))end
sc = r(1);ss = r(2);tx = r(3);ty = r(4);
Tinv = [sc -ss 0; ss sc 0; tx ty 1];
T = inv(Tinv);T(:,3) = [0 0 1]';
trans = maketform('affine', T);output = [];
%-------------------------% Function findSimilarity%function [trans, output] = findSimilarity(uv,xy,options)%% The similarities are a superset of the nonreflective similarities as they may% also include reflection.%% let sc = s*cos(theta)% let ss = s*sin(theta)%% [ sc -ss% [u v] = [x y 1] * ss sc% tx ty]%% OR%% [ sc ss% [u v] = [x y 1] * ss -sc% tx ty]%% Algorithm:% 1) Solve for trans1, a nonreflective similarity.% 2) Reflect the xy data across the Y-axis,% and solve for trans2r, also a nonreflective similarity.% 3) Transform trans2r to trans2, undoing the reflection done in step 2.% 4) Use TFORMFWD to transform uv using both trans1 and trans2,% and compare the results, Returnsing the transformation corresponding% to the smaller L2 norm.
% Need to reset options.K to prepare for calls to findNonreflectiveSimilarity.% This is safe because we already checked that there are enough point pairs.options.K = 2;
% Solve for trans1[trans1, output] = findNonreflectiveSimilarity(uv,xy,options);
% Solve for trans2
% manually reflect the xy data across the Y-axisxyR = xy;xyR(:,1) = -1*xyR(:,1);
trans2r = findNonreflectiveSimilarity(uv,xyR,options);
% manually reflect the tform to undo the reflection done on xyRTreflectY = [-1 0 0; 0 1 0; 0 0 1];trans2 = maketform('affine', trans2r.tdata.T * TreflectY);
% Figure out if trans1 or trans2 is betterxy1 = tformfwd(trans1,uv);norm1 = norm(xy1-xy);
xy2 = tformfwd(trans2,uv);norm2 = norm(xy2-xy);
if norm1 <= norm2 trans = trans1;else trans = trans2;end"""
class MatlabCp2tormException(Exception): def __str__(self): return "In File {}:{}".format(__file__, super.__str__(self))
def tformfwd(trans, uv): """ Function: ---------- apply affine transform 'trans' to uv
Parameters: ---------- @trans: 3x3 np.array transform matrix @uv: Kx2 np.array each row is a pair of coordinates (x, y)
Returns: ---------- @xy: Kx2 np.array each row is a pair of transformed coordinates (x, y) """ uv = np.hstack((uv, np.ones((uv.shape[0], 1)))) xy = np.dot(uv, trans) xy = xy[:, 0:-1] return xy
def tforminv(trans, uv): """ Function: ---------- apply the inverse of affine transform 'trans' to uv
Parameters: ---------- @trans: 3x3 np.array transform matrix @uv: Kx2 np.array each row is a pair of coordinates (x, y)
Returns: ---------- @xy: Kx2 np.array each row is a pair of inverse-transformed coordinates (x, y) """ Tinv = inv(trans) xy = tformfwd(Tinv, uv) return xy
def findNonreflectiveSimilarity(uv, xy, options=None): """ Function: ---------- Find Non-reflective Similarity Transform Matrix 'trans': u = uv[:, 0] v = uv[:, 1] x = xy[:, 0] y = xy[:, 1] [x, y, 1] = [u, v, 1] * trans
Parameters: ---------- @uv: Kx2 np.array source points each row is a pair of coordinates (x, y) @xy: Kx2 np.array each row is a pair of inverse-transformed @option: not used, keep it as None
Returns: @trans: 3x3 np.array transform matrix from uv to xy @trans_inv: 3x3 np.array inverse of trans, transform matrix from xy to uv
Matlab: ---------- % For a nonreflective similarity: % % let sc = s*cos(theta) % let ss = s*sin(theta) % % [ sc -ss % [u v] = [x y 1] * ss sc % tx ty] % % There are 4 unknowns: sc,ss,tx,ty. % % Another way to write this is: % % u = [x y 1 0] * [sc % ss % tx % ty] % % v = [y -x 0 1] * [sc % ss % tx % ty] % % With 2 or more correspondence points we can combine the u equations and % the v equations for one linear system to solve for sc,ss,tx,ty. % % [ u1 ] = [ x1 y1 1 0 ] * [sc] % [ u2 ] [ x2 y2 1 0 ] [ss] % [ ... ] [ ... ] [tx] % [ un ] [ xn yn 1 0 ] [ty] % [ v1 ] [ y1 -x1 0 1 ] % [ v2 ] [ y2 -x2 0 1 ] % [ ... ] [ ... ] % [ vn ] [ yn -xn 0 1 ] % % Or rewriting the above matrix equation: % U = X * r, where r = [sc ss tx ty]' % so r = X\ U. % """ options = {"K": 2}
K = options["K"] M = xy.shape[0] x = xy[:, 0].reshape((-1, 1)) # use reshape to keep a column vector y = xy[:, 1].reshape((-1, 1)) # use reshape to keep a column vector # print '--->x, y:\n', x, y
tmp1 = np.hstack((x, y, np.ones((M, 1)), np.zeros((M, 1)))) tmp2 = np.hstack((y, -x, np.zeros((M, 1)), np.ones((M, 1)))) X = np.vstack((tmp1, tmp2)) # print '--->X.shape: ', X.shape # print 'X:\n', X
u = uv[:, 0].reshape((-1, 1)) # use reshape to keep a column vector v = uv[:, 1].reshape((-1, 1)) # use reshape to keep a column vector U = np.vstack((u, v)) # print '--->U.shape: ', U.shape # print 'U:\n', U
# We know that X * r = U if rank(X) >= 2 * K: r, _, _, _ = lstsq(X, U, rcond=-1) r = np.squeeze(r) else: raise Exception("cp2tform:twoUniquePointsReq")
# print '--->r:\n', r
sc = r[0] ss = r[1] tx = r[2] ty = r[3]
Tinv = np.array([[sc, -ss, 0], [ss, sc, 0], [tx, ty, 1]])
# print '--->Tinv:\n', Tinv
T = inv(Tinv) # print '--->T:\n', T
T[:, 2] = np.array([0, 0, 1])
return T, Tinv
def findSimilarity(uv, xy, options=None): """ Function: ---------- Find Reflective Similarity Transform Matrix 'trans': u = uv[:, 0] v = uv[:, 1] x = xy[:, 0] y = xy[:, 1] [x, y, 1] = [u, v, 1] * trans
Parameters: ---------- @uv: Kx2 np.array source points each row is a pair of coordinates (x, y) @xy: Kx2 np.array each row is a pair of inverse-transformed @option: not used, keep it as None
Returns: ---------- @trans: 3x3 np.array transform matrix from uv to xy @trans_inv: 3x3 np.array inverse of trans, transform matrix from xy to uv
Matlab: ---------- % The similarities are a superset of the nonreflective similarities as they may % also include reflection. % % let sc = s*cos(theta) % let ss = s*sin(theta) % % [ sc -ss % [u v] = [x y 1] * ss sc % tx ty] % % OR % % [ sc ss % [u v] = [x y 1] * ss -sc % tx ty] % % Algorithm: % 1) Solve for trans1, a nonreflective similarity. % 2) Reflect the xy data across the Y-axis, % and solve for trans2r, also a nonreflective similarity. % 3) Transform trans2r to trans2, undoing the reflection done in step 2. % 4) Use TFORMFWD to transform uv using both trans1 and trans2, % and compare the results, Returnsing the transformation corresponding % to the smaller L2 norm.
% Need to reset options.K to prepare for calls to findNonreflectiveSimilarity. % This is safe because we already checked that there are enough point pairs. """ options = {"K": 2}
# uv = np.array(uv) # xy = np.array(xy)
# Solve for trans1 trans1, trans1_inv = findNonreflectiveSimilarity(uv, xy, options)
# Solve for trans2
# manually reflect the xy data across the Y-axis xyR = xy xyR[:, 0] = -1 * xyR[:, 0]
trans2r, trans2r_inv = findNonreflectiveSimilarity(uv, xyR, options)
# manually reflect the tform to undo the reflection done on xyR TreflectY = np.array([[-1, 0, 0], [0, 1, 0], [0, 0, 1]])
trans2 = np.dot(trans2r, TreflectY)
# Figure out if trans1 or trans2 is better xy1 = tformfwd(trans1, uv) norm1 = norm(xy1 - xy)
xy2 = tformfwd(trans2, uv) norm2 = norm(xy2 - xy)
if norm1 <= norm2: return trans1, trans1_inv else: trans2_inv = inv(trans2) return trans2, trans2_inv
def get_similarity_transform(src_pts, dst_pts, reflective=True): """ Function: ---------- Find Similarity Transform Matrix 'trans': u = src_pts[:, 0] v = src_pts[:, 1] x = dst_pts[:, 0] y = dst_pts[:, 1] [x, y, 1] = [u, v, 1] * trans
Parameters: ---------- @src_pts: Kx2 np.array source points, each row is a pair of coordinates (x, y) @dst_pts: Kx2 np.array destination points, each row is a pair of transformed coordinates (x, y) @reflective: True or False if True: use reflective similarity transform else: use non-reflective similarity transform
Returns: ---------- @trans: 3x3 np.array transform matrix from uv to xy trans_inv: 3x3 np.array inverse of trans, transform matrix from xy to uv """
if reflective: trans, trans_inv = findSimilarity(src_pts, dst_pts) else: trans, trans_inv = findNonreflectiveSimilarity(src_pts, dst_pts)
return trans, trans_inv
def cvt_tform_mat_for_cv2(trans): """ Function: ---------- Convert Transform Matrix 'trans' into 'cv2_trans' which could be directly used by cv2.warpAffine(): u = src_pts[:, 0] v = src_pts[:, 1] x = dst_pts[:, 0] y = dst_pts[:, 1] [x, y].T = cv_trans * [u, v, 1].T
Parameters: ---------- @trans: 3x3 np.array transform matrix from uv to xy
Returns: ---------- @cv2_trans: 2x3 np.array transform matrix from src_pts to dst_pts, could be directly used for cv2.warpAffine() """ cv2_trans = trans[:, 0:2].T
return cv2_trans
def get_similarity_transform_for_cv2(src_pts, dst_pts, reflective=True): """ Function: ---------- Find Similarity Transform Matrix 'cv2_trans' which could be directly used by cv2.warpAffine(): u = src_pts[:, 0] v = src_pts[:, 1] x = dst_pts[:, 0] y = dst_pts[:, 1] [x, y].T = cv_trans * [u, v, 1].T
Parameters: ---------- @src_pts: Kx2 np.array source points, each row is a pair of coordinates (x, y) @dst_pts: Kx2 np.array destination points, each row is a pair of transformed coordinates (x, y) reflective: True or False if True: use reflective similarity transform else: use non-reflective similarity transform
Returns: ---------- @cv2_trans: 2x3 np.array transform matrix from src_pts to dst_pts, could be directly used for cv2.warpAffine() """ trans, trans_inv = get_similarity_transform(src_pts, dst_pts, reflective) cv2_trans = cvt_tform_mat_for_cv2(trans) return cv2_trans, trans
std_points_317 = np.array( [ [85.82991, 115.7792], [169.0532, 114.3381], [127.574, 167.0006], [90.6964, 204.7014], [167.3069, 203.3733], ])
padding = 30
std_points_317 = std_points_317 + padding
std_points_256=std_points_317.copy()std_points_256[..., 0] -= 30std_points_256[..., 1] -= 60
def warp_as_face_x_ray(img, src_pts, tgt_pts=std_points_317): tfm, trans = get_similarity_transform_for_cv2(src_pts.copy(), tgt_pts.copy()) return cv2.warpAffine(img, tfm, (317, 317)), trans
def estimiate_batch_transform(all_src_pts, tgt_pts=std_points_317): tgt_pts = np.repeat(tgt_pts[None, ...], len(all_src_pts), 0).reshape(-1, 2) src_pts = np.array(all_src_pts).reshape(-1, 2) tfm, trans = get_similarity_transform_for_cv2(src_pts, tgt_pts) return tfm, trans
def batch_warp_as_face_x_ray(images, all_src_pts, tgt_pts=std_points_317): tfm, trans = estimiate_batch_transform(all_src_pts, tgt_pts) return [cv2.warpAffine(img, tfm, (317, 317)) for img in images], trans
def transform_landmarks(landmarks, trans): transformed = np.hstack((landmarks, np.ones((landmarks.shape[0], 1)))) transformed = np.dot(transformed, trans) return transformed[:, :2]
def compute_reverse_trans(trans): return np.linalg.inv(trans)1234567
__all__ = [] # do not use ' from utils import *'
from .common import *from .plugin_loader import PluginLoader#from .plugin_loaderv2 import PluginLoader as PluginLoaderV2from .model_loader import add_loader1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980#!/usr/bin/python# -*- coding: UTF-8 -*-
import osimport torchfrom torch.autograd import Variableimport errnoimport torch.distributed as distimport mathfrom functools import reducedef make_folder(path, version): if not os.path.exists(os.path.join(path, version)): print(os.path.join(path, version)) os.makedirs(os.path.join(path, version))
def tensor2var(x, grad=False): if torch.cuda.is_available(): x = x.cuda() return Variable(x, requires_grad=grad)
def var2tensor(x): return x.data.cpu()
def var2numpy(x): return x.data.cpu().numpy()
def denorm(x): out = (x + 1) / 2 return out.clamp_(0, 1)
def mkdir_p(dirname): """ Like "mkdir -p", make a dir recursively, but do nothing if the dir exists Args: dirname(str): """ assert dirname is not None if dirname == '' or os.path.isdir(dirname): return try: os.makedirs(dirname) except OSError as e: if e.errno != errno.EEXIST: raise e
def skipShardSplit(aList, drop_last=False, num_replicas=None, rank=None): if not isinstance(aList, list) and not isinstance(aList, tuple): aList = List
if num_replicas is None: num_replicas = dist.get_world_size() if dist.is_initialized() else 1 if rank is None: rank = dist.get_rank() if dist.is_initialized() else 0
num_replicas = num_replicas rank = rank drop_last = drop_last
if drop_last: aList = aList[0: (len(aList) // num_replicas) * num_replicas]
# subsample aList = aList[rank::num_replicas]
return aList
def mixb2a(a,b): if len(b) > len(a): a,b = b,a if len(b) == 0: return a chunk_num = (len(b)) a_chunk = splitIntoChunk(a, chunk_num) b_chunk = list(map(lambda x:[x],b)) return reduce(lambda x, y: x+y, [_a+_b for _a,_b in zip(a_chunk, b_chunk)])
def splitIntoChunk(aList, chunk_num): return [aList[math.ceil(k * (len(aList) / chunk_num)):math.ceil((k + 1) * (len(aList) / chunk_num)):] for k in range(chunk_num)]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182#!/usr/bin/python# -*- coding: UTF-8 -*-# Modified by: algohunt# Microsoft Research & Peking University # lilingzhi@pku.edu.cn# Copyright (c) 2019
# -*- coding: utf-8 -*-
"""Borrow from tensorpack credit goes to yuxin wuThe logger module itself has the common logging functions of Python's:class:`logging.Logger`. For example:
.. code-block:: python
from utils import logger logger.set_logger_dir('train_log/test') logger.info("Test") logger.error("Error happened!")"""
import loggingimport osimport os.pathimport shutilimport sysfrom datetime import datetime, timedeltafrom six.moves import inputfrom termcolor import coloredimport time
__all__ = ['set_logger_dir', 'auto_set_dir', 'get_logger_dir']
class _MyFormatter(logging.Formatter): def format(self, record): date = colored('[%(asctime)s @%(filename)s:%(lineno)d]', 'green') msg = '%(message)s' if record.levelno == logging.WARNING: fmt = date + ' ' + colored('WRN', 'red', attrs=['blink']) + ' ' + msg elif record.levelno == logging.ERROR or record.levelno == logging.CRITICAL: fmt = date + ' ' + colored('ERR', 'red', attrs=['blink', 'underline']) + ' ' + msg elif record.levelno == logging.DEBUG: fmt = date + ' ' + colored('DBG', 'yellow', attrs=['blink']) + ' ' + msg else: fmt = date + ' ' + msg if hasattr(self, '_style'): # Python3 compatibility self._style._fmt = fmt self._fmt = fmt return super(_MyFormatter, self).format(record)
def _getlogger(): logger = logging.getLogger('tensorpack') logger.propagate = False logger.setLevel(logging.INFO) handler = logging.StreamHandler(sys.stdout) handler.setFormatter(_MyFormatter(datefmt='%m%d %H:%M:%S')) logger.addHandler(handler) return logger
_logger = _getlogger()_LOGGING_METHOD = ['info', 'warning', 'error', 'critical', 'exception', 'debug', 'setLevel']# export logger functionsfor func in _LOGGING_METHOD: locals()[func] = getattr(_logger, func) __all__.append(func)# 'warn' is deprecated in logging modulewarn = _logger.warning__all__.append('warn')
def _get_time_str(): utc_time = datetime.utcfromtimestamp(time.time()) beijing_time = utc_time- timedelta(hours=8) return beijing_time.strftime('%m%d-%H%M%S')
# globals: logger file and directory:LOG_DIR = None_FILE_HANDLER = None
def _set_file(path): global _FILE_HANDLER if os.path.isfile(path): backup_name = path + '.' + _get_time_str() shutil.move(path, backup_name) _logger.info("Existing log file '{}' backuped to '{}'".format(path, backup_name)) # noqa: F821 hdl = logging.FileHandler( filename=path, encoding='utf-8', mode='w') hdl.setFormatter(_MyFormatter(datefmt='%m%d %H:%M:%S'))
_FILE_HANDLER = hdl _logger.addHandler(hdl) _logger.info("Argv: " + ' '.join(sys.argv))
def set_logger_dir(dirname, action=None): """ Set the directory for global logging.
Args: dirname(str): log directory action(str): an action of ["k","d","q"] to be performed when the directory exists. Will ask user by default.
"d": delete the directory. Note that the deletion may fail when the directory is used by tensorboard.
"k": keep the directory. This is useful when you resume from a previous training and want the directory to look as if the training was not interrupted. Note that this option does not load old models or any other old states for you. It simply does nothing.
""" global LOG_DIR, _FILE_HANDLER if _FILE_HANDLER: # unload and close the old file handler, so that we may safely delete the logger directory _logger.removeHandler(_FILE_HANDLER) del _FILE_HANDLER
def dir_nonempty(dirname): # If directory exists and nonempty (ignore hidden files), prompt for action return os.path.isdir(dirname) and len([x for x in os.listdir(dirname) if x[0] != '.'])
if dir_nonempty(dirname): if not action: _logger.warn("""\Log directory {} exists! Use 'd' to delete it. """.format(dirname)) _logger.warn("""\If you're resuming from a previous run, you can choose to keep it.Press any other key to exit. """) while not action: action = input("Select Action: k (keep) / d (delete) / q (quit):").lower().strip() act = action if act == 'b': backup_name = dirname + _get_time_str() shutil.move(dirname, backup_name) info("Directory '{}' backuped to '{}'".format(dirname, backup_name)) # noqa: F821 elif act == 'd': shutil.rmtree(dirname, ignore_errors=True) if dir_nonempty(dirname): shutil.rmtree(dirname, ignore_errors=False) elif act == 'n': dirname = dirname + _get_time_str() info("Use a new log directory {}".format(dirname)) # noqa: F821 elif act == 'k': pass else: raise OSError("Directory {} exits!".format(dirname)) LOG_DIR = dirname from . import mkdir_p mkdir_p(dirname) _set_file(os.path.join(dirname, 'log.log'))
def auto_set_dir(action=None, name=None): """ Use :func:`logger.set_logger_dir` to set log directory to "./train_log/{scriptname}:{name}". "scriptname" is the name of the main python file currently running""" mod = sys.modules['__main__'] basename = os.path.basename(mod.__file__) auto_dirname = os.path.join('train_log', basename[:basename.rfind('.')]) if name: auto_dirname += '_%s' % name if os.name == 'nt' else ':%s' % name set_logger_dir(auto_dirname, action=action)
def get_logger_dir(): """ Returns: The logger directory, or None if not set. The directory is used for general logging, tensorboard events, checkpoints, etc. """ return LOG_DIR123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117#!/usr/bin/python# -*- coding: UTF-8 -*-
import typesfrom utils import loggerfrom config import config as cfgimport osimport sysimport globimport torchimport tracebackimport typesimport torch.distributed as distimport copyfrom .torch_save import torch_save
def add_loader(target, name,max_to_keep=2):
def get_rank(self): return dist.get_rank() if dist.is_initialized() else 0
def save_models(self, epoch): """ Backup and save the models """ if self.get_rank() == 0: logger.debug("Backing up and saving models") if not os.path.exists(self.model_dir): os.mkdir(self.model_dir) torch_save(self.state_dict(), self.get_checkpoint_path(epoch)) if os.path.exists(self.get_checkpoint_path(epoch - self.max_to_keep)): os.remove(self.get_checkpoint_path(epoch - self.max_to_keep)) logger.info("{} models saved".format(self.name))
def load(self, fullpath=None, epoch=-1): """ Force Loading a model, or load the latest model""" if fullpath is None: fullpath, loaded_epoch = self.find_last(epoch) else: loaded_epoch = epoch
if fullpath is None: logger.info("No existing {} model found".format(self.name)) return False, -1 logger.debug("Loading model: '%s'", fullpath) try: saved_state_dict = torch.load(fullpath, map_location='cpu') self.load_state_dict(saved_state_dict) logger.info(" consume training from {}".format(fullpath)) except ValueError as err: logger.warning("Failed loading existing training data for {}. Generating new models".format(self.name)) logger.debug("Exception: %s", str(err)) return False, -1 except OSError as err: logger.warning("Failed loading existing training data for {}. Generating new models".format(self.name)) logger.debug("Exception: %s", str(err)) return False, -1 except RuntimeError as err: logger.warning("{} model has corrupted, try to load earlier one".format(self.name)) logger.debug("Exception: %s", str(err)) return False, -1 except: logger.error(traceback.format_exc()) raise
return True, loaded_epoch
def get_checkpoint_path(self, epoch): """" returning the checkpoint path w.r.t epoch which should be {name}_{epoch}.pth""" return os.path.join(self.model_dir, self.name + '_' +str(epoch) + '.pth')
def find_last(self, epoch=-1, model_dir=None): """Finds the last checkpoint file of the last trained model in the model directory. Returns: checkpoint :The path of the last checkpoint file
""" if model_dir is None: model_dir = self.model_dir if not os.path.exists(model_dir): logger.info("model dir not exists {} ".format(model_dir)) return None, -1 #assert os.path.exists(self.model_dir), "model dir not exists {}".format(self.model_dir) checkpoints = glob.glob(os.path.join(model_dir, '*.pth'))
checkpoints = list(filter(lambda x: os.path.basename(x).startswith(self.name), checkpoints)) if len(checkpoints) == 0: return None, -1 checkpoints = {int(os.path.basename(x).split('.')[0].split('_')[-1]):x for x in checkpoints}
start = min(checkpoints.keys()) end = max(checkpoints.keys())
if epoch == -1: return checkpoints[end], end elif epoch < start : raise RuntimeError( "model for epoch {} has been deleted as we only keep {} models".format(epoch,self.max_to_keep)) elif epoch > end: raise RuntimeError( "epoch {} is bigger than all exist checkpoints".format(epoch)) else: return checkpoints[epoch], epoch
target.find_last = types.MethodType(find_last, target) target.get_checkpoint_path = types.MethodType(get_checkpoint_path, target) target.load = types.MethodType(load, target) target.save_models = types.MethodType(save_models, target) target.get_rank = types.MethodType(get_rank, target)
target.max_to_keep = max_to_keep target.name = name target.model_dir = os.path.join(cfg.path.model_dir, cfg.setting_name) return target123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869#!/usr/bin/python# -*- coding: UTF-8 -*-
""" Plugin loader for extract, training and model tasks """
from utils import loggerimport osfrom importlib import import_modulefrom typing import Typefrom trainer._base import TrainerBasefrom torch.utils.data import Datasetfrom model._base import ModelBase
class PluginLoader(): """ Plugin loader for extract, training and model tasks function: get_{model_type} args: {model_name} will return the a class named model_type under model_type.model_name.py
as it return a class you should also annotate the returning classtype to make code linting avaliable in some IDE """ @staticmethod def get_classifier(name) -> Type[ModelBase]: """ Return requested attribute encoder plugin """ return PluginLoader._import("model.classifier", name)
@staticmethod def get_trainer(name) -> Type[TrainerBase]: """ Return requested trainer plugin """ return PluginLoader._import("trainer", name)
@staticmethod def get_dataset(name) -> Type[Dataset]: """ Return requested trainer plugin """ return PluginLoader._import("dataset", name)
@staticmethod def _import(attr, name): """ Import the plugin's module """ name = name.replace("-", "_") ttl = attr.split(".")[-1].title() logger.info("Loading %s from %s plugin...", ttl, name.title()) attr = "model" if attr == "Trainer" else attr.lower() mod = ".".join((attr, name)) module = import_module(mod) logger.info(str(module) + str(ttl)) return getattr(module, ttl)
@staticmethod def get_available_trainer(): """ Return a list of available models """ modelpath = os.path.join(os.path.dirname(__file__), "trainer") models = sorted(item.name.replace(".py", "").replace("_", "-") for item in os.scandir(modelpath) if not item.name.startswith("_") and item.name.endswith(".py")) return models
@staticmethod def get_default_model(): """ Return the default model """ models = PluginLoader.get_available_models() return 'original' if 'original' in models else models[0]
123456789import torch
def torch_save(arr,file): if torch.__version__>="1.6.0": torch.save(arr, file, _use_new_zipfile_serialization=False) else: torch.save(arr, file)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258import osfrom os.path import joinimport argparseimport globimport subprocessimport cv2from tqdm import tqdmimport numpy as npimport loggingimport torchfrom test_tools.common import detect_all, grab_all_framesfrom test_tools.faster_crop_align_xray import FasterCropAlignXRayfrom test_tools.warp_for_xray import ( estimiate_batch_transform, transform_landmarks, std_points_256,)from test_tools.ct.operations import find_longest, multiple_trackingfrom test_tools.utils import get_crop_boximport datetime# from FaceForensics.face_detection_save import get_boundingbox
os.environ['CUDA_LAUNCH_BLOCKING'] = "1"os.environ["CUDA_VISIBLE_DEVICES"] = "0"
device=torch.device('cuda')#Datenow = datetime.datetime.now()logger = logging.getLogger("main") #Logger 선언stream_handler = logging.StreamHandler() # Logger output 방법 선언formatter = logging.Formatter('[%(asctime)s][%(levelname)s|%(filename)s:%(lineno)s] >> %(message)s')stream_handler.setFormatter(formatter)logger.addHandler(stream_handler)logger.setLevel(logging.DEBUG)
crop_align_func = FasterCropAlignXRay(256)max_frame= 10000
### video path를 받으면 crop된 face를 저장하는 함수 ###def crop_face_from_video(video_path,cache_path,crop_path,clip_size): # mp4 파일이 아니면 return if 'mp4' not in video_path : return # video name video_name = video_path.split('/')[-1].replace('.mp4','') # 만약 crop image path에 crop된 이미지가 110개 이상이면 return
if os.path.exists(crop_path): if len(os.listdir(crop_path))>clip_limit: logger.info(f'{video_name} already exists') return ########################################## # detect_res : list, 전체 frame, whole frame # detect_res [] : list, len = 사람 수로 예상 the number of detected face in a frame # detect_res [] [] : tuple, length = 3 # detect_res [] [] 의 각 요소는 각각 box, lm5 : landmark (5,2) , score ########################################## # all_lm68 : list, 전체 frame, whole frame # all_lm68 : list, len = 사람 수로 예상, the number of detected face in a frame # all_lm68 : np.array : landmark 68개 (68,2) ########################################## # frames : each frame's np.array # cache_file : cache file path # landmark와 box를 저장하는 cache file cache_file = f"{cache_path}.pth" if os.path.exists(cache_file): # cache file이 존재하면 load하고 frame만 불러옴 detect_res, all_lm68 = torch.load(cache_file) frames = grab_all_frames(video_path, max_size=max_frame, cvt=True) logger.info("detection result loaded from cache") else: # cache file이 존재하지 않으면 detect_all 함수를 통해 detect_res, all_lm68, frames를 불러옴 # detection_all 함수는 retina_face를 이용해서 box와 landmark를 찾는 함수 detect_res, all_lm68, frames = detect_all( video_path, return_frames=True, max_size=10000 ) torch.save((detect_res, all_lm68), cache_file) try: shape = frames[0].shape[:2] except IndexError: # if there is no frame in the video, error list에 저장 f = open("./indexerror.txt", 'a') f.write("{}\n".format(video_path)) f.close() return # 모든 detect_res all_detect_res = []
assert len(all_lm68) == len(detect_res) # in each frame, save the detected face's bounding box, landmark(5, 68), score as a tuple and save it in a list for faces, faces_lm68 in zip(detect_res, all_lm68): new_faces = [] for (box, lm5, score), face_lm68 in zip(faces, faces_lm68): new_face = (box, lm5, face_lm68, score) new_faces.append(new_face) all_detect_res.append(new_faces) detect_res = all_detect_res # SORT tracking # tracks : list, len = 사람 수로 예상, the number of detected face in a frame # tracks [] : list, len = 프레임 수, the number of frames # tracks [] [] : tuple, length = 4, 각각 box, lm5 : landmark (5,2) , lm68 : landmark (68,2), score tracks = multiple_tracking(detect_res) # tuples : list, len = 사람 수로 예상, the number of detected face in a frame # tuples [] : tuple, length = 2, 각각 0, 프레임 수 the number of frames tuples = [(0, len(detect_res))] * len(tracks) # if there is no face detected, find the longest face in the video if len(tracks) == 0: tuples, tracks = find_longest(detect_res) data_storage = {} frame_boxes = {} super_clips = [] frame_res = {} super_clips_start_end = [] # super_clips : tracking된 face들을 의미하는 것으로 보임 for track_i, ((start, end), track) in enumerate(zip(tuples, tracks)): # each track(=face) # if detect_res's length is not equal to track's length, raise error assert len(detect_res[start:end]) == len(track) super_clips.append(len(track)) super_clips_start_end.append((start, end)) for face, frame_idx, j in zip(track, range(start, end), range(len(track))): # frame에서 각각의 face box,lm5,lm68 = face[:3] # box, lm5, lm68 big_box = get_crop_box(shape, box, scale=0.5) # get crop box
top_left = big_box[:2][None, :] # top left point
new_lm5 = lm5 - top_left new_lm68 = lm68 - top_left
new_box = (box.reshape(2, 2) - top_left).reshape(-1)
info = (new_box, new_lm5, new_lm68, big_box) # face info
x1, y1, x2, y2 = big_box cropped = frames[frame_idx][y1:y2, x1:x2] # cropped = cv2.resize(cropped, (512, 512)) # face들을 tracking한 박스들로 crop함 # landmark들도 box에 맞게 변환 # data_storage에 저장 i는 face id, j는 frame을 의미 base_key = f"{track_i}_{j}_" # i : face, j : frame data_storage[base_key + "img"] = cropped data_storage[base_key + "ldm"] = info data_storage[base_key + "idx"] = frame_idx frame_boxes[frame_idx] = np.rint(box).astype(np.int64) # 총 crop된 face들과 그 face들의 frame 수를 알려줌 logger.info(f"{crop_path} : sampling clips from super clips {super_clips}") clips_for_video = [] clip_size = clip_size pad_length = clip_size - 1 # 각 face id 별로 clip을 만듦 # 아래의 영어 표기로는 8clip을 의미하지만 정확하겐 clip size 만큼 함 for super_clip_idx, super_clip_size in enumerate(super_clips): # cut the super clip into clips, overlap 7frames, 8frames per clip inner_index = list(range(super_clip_size)) if super_clip_size < clip_size: # if there is not enough frames to make a clip, pad the frames # to do : how to operate the padding # 정확하게 이 코드가 어떻게 동작하는지 모르겠지만 # 대략적으로 frame들을 clipsize로 나눌때 부족하면 # clip size만큼의 frame이 되도록 padding을 함 if super_clip_size < clip_size//2 : continue post_module = inner_index[1:-1][::-1] + inner_index
l_post = len(post_module) post_module = post_module * (pad_length // l_post + 1) post_module = post_module[:pad_length] assert len(post_module) == pad_length
pre_module = inner_index + inner_index[1:-1][::-1] l_pre = len(post_module) pre_module = pre_module * (pad_length // l_pre + 1) pre_module = pre_module[-pad_length:] assert len(pre_module) == pad_length
inner_index = pre_module + inner_index + post_module
super_clip_size = len(inner_index)
frame_range = [ inner_index[i : i + clip_size] for i in range(super_clip_size) if i + clip_size <= super_clip_size ] for indices in frame_range: clip = [(super_clip_idx, t) for t in indices] clips_for_video.append(clip) # landmarks, images = crop_align_func(landmarks, images) # i : face, j : frame processed_clips = 0 # Track number of processed clips for clip in clips_for_video: # Check if we've reached the clip limit if processed_clips >= clip_limit: logger.info(f"Reached clip limit of {clip_limit}, stopping processing") break # 각 자른 clip에 대해서 진행 images = [data_storage[f"{i}_{j}_img"] for i, j in clip] # call cropped face images from data_storage, i : face, j : frame landmarks = [data_storage[f"{i}_{j}_ldm"] for i, j in clip] # call landmarks from data_storage, i : face, j : frame # landmark를 기준으로 crop align func을 진행 # 해당 함수가 clip에 있는 얼굴들의 landmark 평균을 기준으로 박스를 설정하고 # 박스를 기준으로 crop align을 진행 # 다르게 말하면, landmark 평균을 기준으로 박스의 geometry를 설정하고 # 박스의 geometry는 고정한체로 얼굴이 움직이는 걸 찍었다 생각하면 됨 # 다시 또 말하면, 카메라를 고정하고 사람이 움직이는 것을 찍은것처럼 # PPT 참조 landmarks, images = crop_align_func(landmarks, images) # align the face images by landmarks in the clip i, j = clip[-1] k = super_clips[i]%clip_size ########################################################################## # 코드 변경시 이 함수에서는 이부분만 변경할 것을 권고 !!!!!!!!!!!!!!!!!!!!!!!! # 특히, cv2.imwrite함수만 변경할 것을 추천 ########################################################################## if (j+1)%clip_size==0: # if last frame number of the clip is multiple of clip_size, save all images in the clip # it means save face alignments in 8 frames in the video so that they don't overlap for f, (i,j) in enumerate(clip) : cv2.imwrite(join(crop_path, f'{i:02}_{j:04}.png'), cv2.cvtColor(images[f], cv2.COLOR_BGR2RGB)) if j == super_clips[i]-1: # if the clip have last frame image, save all images in the clip if k!=0 : # if the clip is not multiple of clip_size, save the last k images in the clip # k is the number of frames that are not overlapped for l in range(clip_size-k,clip_size): ci,cj = clip[l] cv2.imwrite(join(crop_path, f'{ci:02}_{cj:04}.png'), cv2.cvtColor(images[l], cv2.COLOR_BGR2RGB)) # clip means face alignment images in 8 frames, non-overlap processed_clips += 1 # Increment processed clip counter ##########################################################################
if __name__ == '__main__': p = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter ) p.add_argument('--video_path','-i', type=str, default='/videos.mp4', help='path to input video') p.add_argument('--save_path','-s', type=str, default='/data/crop_face', help='path to save cropped faces') p.add_argument('--cachepath', '-c', type=str, default='/data/cache', help='path to cache detection results') p.add_argument('--clipsize','-l',type=int,default=32, help='number of frames in a clip') args = p.parse_args() video_path = args.video_path save_path = args.save_path cache_path = args.cachepath clip_size = args.clipsize crop_face_from_video(video_path, cache_path, crop_path, clip_size) ###################### reference ######################
# this code reference from FTCN Official Code in git hub# link is https://github.com/yinglinzheng/FTCN
# - Zheng, Y., Bao, J., Chen, D., Zeng, M., & Wen, F. (2021). Exploring Temporal Coherence for More General Video Face Forgery Detection. In Proceedings of the IEEE/CVF International Conference on Computer Vision (pp. 15044–15054).
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
from .ct.detection.utils import grab_all_frames, get_valid_faces, sample_chunksfrom .ct.operations import multiple_trackingimport numpy as npfrom .ct.face_alignment import LandmarkPredictorfrom .ct.detection import FaceDetectorimport cv2from .utils import flatten,partition
detector = FaceDetector(0)predictor = LandmarkPredictor(0)
def get_five(ldm68): groups = [range(36, 42), range(42, 48), [30], [48], [54]] points = [] for group in groups: points.append(ldm68[group].mean(0)) return np.array(points)
def get_bbox(mask): try: y, x = np.nonzero(mask[..., 0]) return x.min() - 1, y.min() - 1, x.max() + 1, y.max() + 1 except: return None
def get_bigger_box(image, box, scale=0.5): height, width = image.shape[:2] box = np.rint(box).astype(np.int) new_box = box.reshape(2, 2) size = new_box[1] - new_box[0] diff = scale * size diff = diff[None, :] * np.array([-1, 1])[:, None] new_box = new_box + diff new_box[:, 0] = np.clip(new_box[:, 0], 0, width - 1) new_box[:, 1] = np.clip(new_box[:, 1], 0, height - 1) new_box = np.rint(new_box).astype(np.int) return new_box.reshape(-1)
def process_bigger_clips(clips, dete_res, clip_size, step, scale=0.5): assert len(clips) % clip_size == 0 detect_results = sample_chunks(dete_res, clip_size, step) clips = sample_chunks(clips, clip_size, step) new_clips = [] for i, (frame_clip, record_clip) in enumerate(zip(clips, detect_results)): tracks = multiple_tracking(record_clip) for j, track in enumerate(tracks): new_images = [] for (box, ldm, _), frame in zip(track, frame_clip): big_box = get_bigger_box(frame, box, scale) x1, y1, x2, y2 = big_box top_left = big_box[:2][None, :] new_ldm5 = ldm - top_left box = np.rint(box).astype(np.int) new_box = (box.reshape(2, 2) - top_left).reshape(-1) feed = LandmarkPredictor.prepare_feed(frame, box) ldm68 = predictor(feed) - top_left new_images.append( (frame[y1:y2, x1:x2], big_box, new_box, new_ldm5, ldm68) ) new_clips.append(new_images) return new_clips
def post(detected_faces): return [[face[:4], None, face[-1]] for face in detected_faces]
def check(detect_res): return min([len(faces) for faces in detect_res]) != 0
def detect_all(file, sfd_only=False, return_frames=False, max_size=None): frames = grab_all_frames(file, max_size=max_size, cvt=True) if not sfd_only: detect_res = flatten( [detector.detect(item) for item in partition(frames, 50)] ) detect_res = get_valid_faces(detect_res, thres=0.5) else: raise NotImplementedError
all_68 = get_lm68(frames, detect_res) if not return_frames: return detect_res, all_68 else: return detect_res, all_68, frames
def get_lm68(frames, detect_res): assert len(frames) == len(detect_res) frame_count = len(frames) all_68 = [] for i in range(frame_count): frame = frames[i] faces = detect_res[i] if len(faces) == 0: res_68 = [] else: feeds = [] for face in faces: assert len(face) == 3 box = face[0] feed = LandmarkPredictor.prepare_feed(frame, box) feeds.append(feed) res_68 = predictor(feeds) assert len(res_68) == len(faces) for face, l_68 in zip(faces, res_68): if face[1] is None: face[1] = get_five(l_68) all_68.append(res_68)
assert len(all_68) == len(detect_res) return all_681234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import cv2from .detector import RetinaFacefrom .utils import *
def assert_bounded(val, low, up): return val >= low and val < up
def check_valid(face, w, h): box = face[0] if box[0] > box[2]: return False if box[1] > box[3]: return False for idx, bound in zip([0, 1, 2, 3], [w, h, w, h]): if not assert_bounded(box[idx], 0, bound): return False pts = face[1] for p in pts: for idx, bound in zip([0, 1], [w, h]): if not assert_bounded(p[idx], 0, bound): return False return True
def post_detect(detect_results, scale, w, h): new_results = [] for frame_faces in detect_results: new_frame_faces = [] for box, ldm, score in frame_faces: box = box * scale ldm = ldm * scale face = (box, ldm, score) if check_valid(face, w=w, h=h): new_frame_faces.append(face) new_results.append(new_frame_faces) return new_results
class FaceDetector(RetinaFace): def scale_detect(self, images): max_res = 1920 h, w = images[0].shape[:2] if max(h, w) > max_res: init_scale = max(h, w) / max_res else: init_scale = 1 resize_scale = 2 * init_scale resize_w = int(w / resize_scale) resize_h = int(h / resize_scale) detect_input = [cv2.resize(frame, (resize_w, resize_h)) for frame in images] detect_results = post_detect( self.detect(detect_input), scale=resize_scale, w=w, h=h, ) return detect_results123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608from itertools import product as productfrom math import ceil
import numpy as npimport torchimport torch.backends.cudnn as cudnnimport torch.nn as nnimport torch.nn.functional as Fimport torchvision.models._utils as _utils
def conv_bn(inp, oup, stride=1, leaky=0): return nn.Sequential( nn.Conv2d(inp, oup, 3, stride, 1, bias=False), nn.BatchNorm2d(oup), nn.LeakyReLU(negative_slope=leaky, inplace=True), )
def conv_bn_no_relu(inp, oup, stride): return nn.Sequential( nn.Conv2d(inp, oup, 3, stride, 1, bias=False), nn.BatchNorm2d(oup), )
def conv_bn1X1(inp, oup, stride, leaky=0): return nn.Sequential( nn.Conv2d(inp, oup, 1, stride, padding=0, bias=False), nn.BatchNorm2d(oup), nn.LeakyReLU(negative_slope=leaky, inplace=True), )
def conv_dw(inp, oup, stride, leaky=0.1): return nn.Sequential( nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False), nn.BatchNorm2d(inp), nn.LeakyReLU(negative_slope=leaky, inplace=True), nn.Conv2d(inp, oup, 1, 1, 0, bias=False), nn.BatchNorm2d(oup), nn.LeakyReLU(negative_slope=leaky, inplace=True), )
class SSH(nn.Module): def __init__(self, in_channel, out_channel): super(SSH, self).__init__() assert out_channel % 4 == 0 leaky = 0 if out_channel <= 64: leaky = 0.1 self.conv3X3 = conv_bn_no_relu(in_channel, out_channel // 2, stride=1)
self.conv5X5_1 = conv_bn(in_channel, out_channel // 4, stride=1, leaky=leaky) self.conv5X5_2 = conv_bn_no_relu(out_channel // 4, out_channel // 4, stride=1)
self.conv7X7_2 = conv_bn( out_channel // 4, out_channel // 4, stride=1, leaky=leaky ) self.conv7x7_3 = conv_bn_no_relu(out_channel // 4, out_channel // 4, stride=1)
def forward(self, input): conv3X3 = self.conv3X3(input)
conv5X5_1 = self.conv5X5_1(input) conv5X5 = self.conv5X5_2(conv5X5_1)
conv7X7_2 = self.conv7X7_2(conv5X5_1) conv7X7 = self.conv7x7_3(conv7X7_2)
out = torch.cat([conv3X3, conv5X5, conv7X7], dim=1) out = F.relu(out) return out
class FPN(nn.Module): def __init__(self, in_channels_list, out_channels): super(FPN, self).__init__() leaky = 0 if out_channels <= 64: leaky = 0.1 self.output1 = conv_bn1X1( in_channels_list[0], out_channels, stride=1, leaky=leaky ) self.output2 = conv_bn1X1( in_channels_list[1], out_channels, stride=1, leaky=leaky ) self.output3 = conv_bn1X1( in_channels_list[2], out_channels, stride=1, leaky=leaky )
self.merge1 = conv_bn(out_channels, out_channels, leaky=leaky) self.merge2 = conv_bn(out_channels, out_channels, leaky=leaky)
def forward(self, input): # names = list(input.keys()) input = list(input.values())
output1 = self.output1(input[0]) output2 = self.output2(input[1]) output3 = self.output3(input[2])
up3 = F.interpolate( output3, size=[output2.size(2), output2.size(3)], mode="nearest" ) output2 = output2 + up3 output2 = self.merge2(output2)
up2 = F.interpolate( output2, size=[output1.size(2), output1.size(3)], mode="nearest" ) output1 = output1 + up2 output1 = self.merge1(output1)
out = [output1, output2, output3] return out
class MobileNetV1(nn.Module): def __init__(self): super(MobileNetV1, self).__init__() self.stage1 = nn.Sequential( conv_bn(3, 8, 2, leaky=0.1), # 3 conv_dw(8, 16, 1), # 7 conv_dw(16, 32, 2), # 11 conv_dw(32, 32, 1), # 19 conv_dw(32, 64, 2), # 27 conv_dw(64, 64, 1), # 43 ) self.stage2 = nn.Sequential( conv_dw(64, 128, 2), # 43 + 16 = 59 conv_dw(128, 128, 1), # 59 + 32 = 91 conv_dw(128, 128, 1), # 91 + 32 = 123 conv_dw(128, 128, 1), # 123 + 32 = 155 conv_dw(128, 128, 1), # 155 + 32 = 187 conv_dw(128, 128, 1), # 187 + 32 = 219 ) self.stage3 = nn.Sequential( conv_dw(128, 256, 2), # 219 +3 2 = 241 conv_dw(256, 256, 1), # 241 + 64 = 301 ) self.avg = nn.AdaptiveAvgPool2d((1, 1)) self.fc = nn.Linear(256, 1000)
def forward(self, x): x = self.stage1(x) x = self.stage2(x) x = self.stage3(x) x = self.avg(x) # x = self.model(x) x = x.view(-1, 256) x = self.fc(x) return x
class ClassHead(nn.Module): def __init__(self, inchannels=512, num_anchors=3): super(ClassHead, self).__init__() self.num_anchors = num_anchors self.conv1x1 = nn.Conv2d( inchannels, self.num_anchors * 2, kernel_size=(1, 1), stride=1, padding=0 )
def forward(self, x): out = self.conv1x1(x) out = out.permute(0, 2, 3, 1).contiguous()
return out.view(out.shape[0], -1, 2)
class BboxHead(nn.Module): def __init__(self, inchannels=512, num_anchors=3): super(BboxHead, self).__init__() self.conv1x1 = nn.Conv2d( inchannels, num_anchors * 4, kernel_size=(1, 1), stride=1, padding=0 )
def forward(self, x): out = self.conv1x1(x) out = out.permute(0, 2, 3, 1).contiguous()
return out.view(out.shape[0], -1, 4)
class LandmarkHead(nn.Module): def __init__(self, inchannels=512, num_anchors=3): super(LandmarkHead, self).__init__() self.conv1x1 = nn.Conv2d( inchannels, num_anchors * 10, kernel_size=(1, 1), stride=1, padding=0 )
def forward(self, x): out = self.conv1x1(x) out = out.permute(0, 2, 3, 1).contiguous()
return out.view(out.shape[0], -1, 10)
class RetinaFace(nn.Module): def __init__(self, cfg=None, phase="train"): """ :param cfg: Network related settings. :param phase: train or test. """ super(RetinaFace, self).__init__() self.phase = phase backbone = None if cfg["name"] == "mobilenet0.25": backbone = MobileNetV1() elif cfg["name"] == "Resnet50": import torchvision.models as models
backbone = models.resnet50(pretrained=cfg["pretrain"])
self.body = _utils.IntermediateLayerGetter(backbone, cfg["return_layers"]) in_channels_stage2 = cfg["in_channel"] in_channels_list = [ in_channels_stage2 * 2, in_channels_stage2 * 4, in_channels_stage2 * 8, ] out_channels = cfg["out_channel"] self.fpn = FPN(in_channels_list, out_channels) self.ssh1 = SSH(out_channels, out_channels) self.ssh2 = SSH(out_channels, out_channels) self.ssh3 = SSH(out_channels, out_channels)
self.ClassHead = self._make_class_head(fpn_num=3, inchannels=cfg["out_channel"]) self.BboxHead = self._make_bbox_head(fpn_num=3, inchannels=cfg["out_channel"]) self.LandmarkHead = self._make_landmark_head( fpn_num=3, inchannels=cfg["out_channel"] )
def _make_class_head(self, fpn_num=3, inchannels=64, anchor_num=2): classhead = nn.ModuleList() for i in range(fpn_num): classhead.append(ClassHead(inchannels, anchor_num)) return classhead
def _make_bbox_head(self, fpn_num=3, inchannels=64, anchor_num=2): bboxhead = nn.ModuleList() for i in range(fpn_num): bboxhead.append(BboxHead(inchannels, anchor_num)) return bboxhead
def _make_landmark_head(self, fpn_num=3, inchannels=64, anchor_num=2): landmarkhead = nn.ModuleList() for i in range(fpn_num): landmarkhead.append(LandmarkHead(inchannels, anchor_num)) return landmarkhead
def forward(self, inputs): out = self.body(inputs)
# FPN fpn = self.fpn(out)
# SSH feature1 = self.ssh1(fpn[0]) feature2 = self.ssh2(fpn[1]) feature3 = self.ssh3(fpn[2]) features = [feature1, feature2, feature3]
bbox_regressions = torch.cat( [self.BboxHead[i](feature) for i, feature in enumerate(features)], dim=1 ) classifications = torch.cat( [self.ClassHead[i](feature) for i, feature in enumerate(features)], dim=1 ) ldm_regressions = torch.cat( [self.LandmarkHead[i](feature) for i, feature in enumerate(features)], dim=1 )
if self.phase == "train": output = (bbox_regressions, classifications, ldm_regressions) else: output = ( bbox_regressions, F.softmax(classifications, dim=-1), ldm_regressions, ) return output
# Adapted from https://github.com/Hakuyume/chainer-ssddef decode(loc, priors, variances): boxes = torch.cat( ( priors[:, :2] + loc[:, :2] * variances[0] * priors[:, 2:], priors[:, 2:] * torch.exp(loc[:, 2:] * variances[1]), ), 1, ) boxes[:, :2] -= boxes[:, 2:] / 2 boxes[:, 2:] += boxes[:, :2] return boxes
def decode_landm(pre, priors, variances): landms = torch.cat( ( priors[:, :2] + pre[:, :2] * variances[0] * priors[:, 2:], priors[:, :2] + pre[:, 2:4] * variances[0] * priors[:, 2:], priors[:, :2] + pre[:, 4:6] * variances[0] * priors[:, 2:], priors[:, :2] + pre[:, 6:8] * variances[0] * priors[:, 2:], priors[:, :2] + pre[:, 8:10] * variances[0] * priors[:, 2:], ), dim=1, ) return landms
def py_cpu_nms(dets, thresh): """Pure Python NMS baseline.""" x1 = dets[:, 0] y1 = dets[:, 1] x2 = dets[:, 2] y2 = dets[:, 3] scores = dets[:, 4]
areas = (x2 - x1 + 1) * (y2 - y1 + 1) order = scores.argsort()[::-1]
keep = [] while order.size > 0: i = order[0] keep.append(i) xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2 = np.minimum(x2[i], x2[order[1:]]) yy2 = np.minimum(y2[i], y2[order[1:]])
w = np.maximum(0.0, xx2 - xx1 + 1) h = np.maximum(0.0, yy2 - yy1 + 1) inter = w * h ovr = inter / (areas[i] + areas[order[1:]] - inter)
inds = np.where(ovr <= thresh)[0] order = order[inds + 1]
return keep
class PriorBox(object): def __init__(self, cfg, image_size=None, phase="train"): super(PriorBox, self).__init__() self.min_sizes = cfg["min_sizes"] self.steps = cfg["steps"] self.clip = cfg["clip"] self.image_size = image_size self.feature_maps = [ [ceil(self.image_size[0] / step), ceil(self.image_size[1] / step)] for step in self.steps ] self.name = "s"
def forward(self): anchors = [] for k, f in enumerate(self.feature_maps): min_sizes = self.min_sizes[k] for i, j in product(range(f[0]), range(f[1])): for min_size in min_sizes: s_kx = min_size / self.image_size[1] s_ky = min_size / self.image_size[0] dense_cx = [ x * self.steps[k] / self.image_size[1] for x in [j + 0.5] ] dense_cy = [ y * self.steps[k] / self.image_size[0] for y in [i + 0.5] ] for cy, cx in product(dense_cy, dense_cx): anchors += [cx, cy, s_kx, s_ky]
# back to torch land output = torch.Tensor(anchors).view(-1, 4) if self.clip: output.clamp_(max=1, min=0) return output
cfg_mnet = { "name": "mobilenet0.25", "min_sizes": [[16, 32], [64, 128], [256, 512]], "steps": [8, 16, 32], "variance": [0.1, 0.2], "clip": False, "loc_weight": 2.0, "gpu_train": True, "batch_size": 32, "ngpu": 1, "epoch": 250, "decay1": 190, "decay2": 220, "image_size": 640, "pretrain": True, "return_layers": {"stage1": 1, "stage2": 2, "stage3": 3}, "in_channel": 32, "out_channel": 64,}
cfg_re50 = { "name": "Resnet50", "min_sizes": [[16, 32], [64, 128], [256, 512]], "steps": [8, 16, 32], "variance": [0.1, 0.2], "clip": False, "loc_weight": 2.0, "gpu_train": True, "batch_size": 24, "ngpu": 4, "epoch": 100, "decay1": 70, "decay2": 90, "image_size": 840, "pretrain": False, "return_layers": {"layer2": 1, "layer3": 2, "layer4": 3}, "in_channel": 256, "out_channel": 256,}
def check_keys(model, pretrained_state_dict): ckpt_keys = set(pretrained_state_dict.keys()) model_keys = set(model.state_dict().keys()) used_pretrained_keys = model_keys & ckpt_keys assert len(used_pretrained_keys) > 0, "load NONE from pretrained checkpoint" return True
def remove_prefix(state_dict, prefix): """ Old style model is stored with all names of parameters sharing common prefix 'module.' """ f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x return {f(key): value for key, value in state_dict.items()}
def load_model(model, pretrained_path, load_to_cpu): if load_to_cpu: if pretrained_path is None: url = "https://github.com/yinglinzheng/face_weights/releases/download/v1/mobilenet0.25_Final.pth" pretrained_dict = torch.utils.model_zoo.load_url(url) else: pretrained_dict = torch.load( pretrained_path, map_location=lambda storage, loc: storage ) else: device = torch.cuda.current_device() pretrained_dict = torch.load( pretrained_path, map_location=lambda storage, loc: storage.cuda(device) ) if "state_dict" in pretrained_dict.keys(): pretrained_dict = remove_prefix(pretrained_dict["state_dict"], "module.") else: pretrained_dict = remove_prefix(pretrained_dict, "module.") check_keys(model, pretrained_dict) model.load_state_dict(pretrained_dict, strict=False) return model
def load_net(model_path, device, network="mobilenet"): if network == "mobilenet": cfg = cfg_mnet elif network == "resnet50": cfg = cfg_re50 # net and model net = RetinaFace(cfg=cfg, phase="test") net = load_model(net, model_path, True) net.eval() cudnn.benchmark = True net = net.to(device) return net
def parse_det(det): landmarks = det[5:].reshape(5, 2) box = det[:4] score = det[4] return box, landmarks, score
def post_process( loc, conf, landms, prior_data, cfg, scale, scale1, resize, confidence_threshold, top_k, nms_threshold, keep_top_k,): boxes = decode(loc, prior_data, cfg["variance"]) boxes = boxes * scale / resize boxes = boxes.cpu().numpy() scores = conf.cpu().numpy()[:, 1] landms_copy = decode_landm(landms, prior_data, cfg["variance"])
landms_copy = landms_copy * scale1 / resize landms_copy = landms_copy.cpu().numpy()
# ignore low scores inds = np.where(scores > confidence_threshold)[0] boxes = boxes[inds] landms_copy = landms_copy[inds] scores = scores[inds]
# keep top-K before NMS order = scores.argsort()[::-1][:top_k] boxes = boxes[order] landms_copy = landms_copy[order] scores = scores[order]
# do NMS dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False) keep = py_cpu_nms(dets, nms_threshold) # keep = nms(dets, args.nms_threshold,force_cpu=args.cpu) dets = dets[keep, :] landms_copy = landms_copy[keep]
# keep top-K faster NMS dets = dets[:keep_top_k, :] landms_copy = landms_copy[:keep_top_k, :]
dets = np.concatenate((dets, landms_copy), axis=1) # show image dets = sorted(dets, key=lambda x: x[4], reverse=True) dets = [parse_det(x) for x in dets]
return dets
def batch_detect(net, images, device, is_tensor=False, normalized=False): with torch.no_grad(): confidence_threshold = 0.02 cfg = cfg_mnet top_k = 5000 nms_threshold = 0.4 keep_top_k = 750 resize = 1 if not is_tensor: try: img = np.float32(images) except ValueError: raise NotImplementedError("Input images must of same size") img = torch.from_numpy(img) else: img = images.float() img = img.to(device) mean = ( torch.as_tensor([104, 117, 123], dtype=img.dtype, device=img.device) .unsqueeze(0) .unsqueeze(0) .unsqueeze(0) ) img -= mean img = img.permute(0, 3, 1, 2) (batch_size, _, im_height, im_width,) = img.shape scale = torch.as_tensor( [im_width, im_height, im_width, im_height], dtype=img.dtype, device=img.device, ) scale = scale.to(device)
loc, conf, landms = net(img) # forward pass
priorbox = PriorBox(cfg, image_size=(im_height, im_width)) priors = priorbox.forward() prior_data = priors.to(device) scale1 = torch.as_tensor( [ img.shape[3], img.shape[2], img.shape[3], img.shape[2], img.shape[3], img.shape[2], img.shape[3], img.shape[2], img.shape[3], img.shape[2], ], dtype=img.dtype, device=img.device, ) scale1 = scale1.to(device)
all_dets = [ post_process( loc_i, conf_i, landms_i, prior_data, cfg, scale, scale1, resize, confidence_threshold, top_k, nms_threshold, keep_top_k, ) for loc_i, conf_i, landms_i in zip(loc, conf, landms) ]
return all_dets12345678910111213141516171819202122232425262728293031323334353637383940414243444546import os
import numpy as npimport torch
from .alignment import load_net, batch_detect
def get_project_dir(): current_path = os.path.abspath(os.path.join(__file__, "../")) return current_path
def relative(path): path = os.path.join(get_project_dir(), path) return os.path.abspath(path)
class RetinaFace: def __init__( self, gpu_id=-1, model_path=None, network="mobilenet", ): self.gpu_id = gpu_id self.device = ( torch.device("cpu") if gpu_id == -1 else torch.device("cuda", gpu_id) ) self.model = load_net(model_path, self.device, network)
def detect(self, images): if isinstance(images, np.ndarray): if len(images.shape) == 3: return batch_detect(self.model, [images], self.device)[0] elif len(images.shape) == 4: return batch_detect(self.model, images, self.device) elif isinstance(images, list): return batch_detect(self.model, np.array(images), self.device) elif isinstance(images, torch.Tensor): if len(images.shape) == 3: return batch_detect(self.model, images.unsqueeze(0), self.device)[0] elif len(images.shape) == 4: return batch_detect(self.model, images, self.device) else: raise NotImplementedError()
def __call__(self, images): return self.detect(images)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147import cv2# from test_tools.utils import flattenimport numpy as npdef flatten(l): return [item for sublist in l for item in sublist]
def chunks(l, n, step=None): if step is None: step = n return [l[i : i + n] for i in range(0, len(l), step)]
def sample_chunks(l, n, step=None): return [l[i : i + n] for i in range(0, len(l), step) if i + n <= len(l)]
def grab_all_frames(path, max_size, cvt=False): capture = cv2.VideoCapture(path) ret = True frames = [] while ret: ret, frame = capture.read() if ret: if cvt: frame = frame[..., ::-1] frames.append(frame) if len(frames) == max_size: break capture.release() return frames
def get_clips_uniform(path, count, clip_size): capture = cv2.VideoCapture(path) n_frames = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)) max_clip_available = n_frames + 1 - clip_size if count > max_clip_available: count = max_clip_available final_start = max_clip_available - 1 start_indices = np.linspace(0, final_start, count, endpoint=True, dtype=np.int) all_clip_idx = [list(range(start, start + clip_size)) for start in start_indices] valid = set(flatten(all_clip_idx)) max_idx = max(valid)
frames = {} for idx in range(max_idx + 1): # Get the next frame, but don't decode if we're not using it. ret = capture.grab() if not ret: continue
if idx in valid: ret, frame = capture.retrieve() if not ret or frame is None: continue else: # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frames[idx] = frame
capture.release() clips = [] for clip_idx in all_clip_idx: clip = [] flag = True for idx in clip_idx: if idx not in frames: flag = False break clip.append(frames[idx]) if flag: clips.append(clip) return clips
def get_valid_faces(detect_results, max_count=10, thres=0.5, at_least=False): new_results = [] for i, faces in enumerate(detect_results): if len(faces) > max_count: faces = faces[:max_count] l = [] for j, face in enumerate(faces): if face[-1] < thres and not (j == 0 and at_least): continue box, lm, score = face box = box.astype(np.float64) lm = lm.astype(np.float64) l.append((box, lm, score)) new_results.append(l) return new_results
def scale_box(box, scale_h, scale_w, h, w): x1, y1, x2, y2 = box.astype(np.int32) center_x = (x1 + x2) // 2 center_y = (y1 + y2) // 2 box_h = int((y2 - y1) * scale_h) box_w = int((x2 - x1) * scale_w) new_x1 = center_x - box_w // 2 new_x2 = new_x1 + box_w new_y1 = center_y - box_h // 2 new_y2 = new_y1 + box_h new_x1 = max(new_x1, 0) new_y1 = max(new_y1, 0) new_y2 = min(new_y2, h) new_x2 = min(new_x2, w) return new_x1, new_y1, new_x2, new_y2
def get_bbox(detect_res): tmp_detect_res = get_valid_faces(detect_res, max_count=4, thres=0.5) all_face_bboxs = [] for faces in tmp_detect_res: all_face_bboxs.extend([face[0] for face in faces]) all_face_bboxs = np.array(all_face_bboxs).astype(np.int) x1 = all_face_bboxs[:, 0].min() x2 = all_face_bboxs[:, 2].max() y1 = all_face_bboxs[:, 1].min() y2 = all_face_bboxs[:, 3].max()
return x1, y1, x2, y2
def delta_detect_res(detect_res, x1, y1): diff = np.array([[x1, y1]]) new_detect_res = [] for faces in detect_res: f = [] for face in faces: box, lm, score = face box = box.astype(np.float64) box[[0, 2]] -= x1 box[[1, 3]] -= y1 lm = lm.astype(np.float64) - diff f.append((box, lm, score)) new_detect_res.append(f) return new_detect_res
def pre_crop(clips, detect_res): box = np.array(get_bbox(detect_res)) w = box[2] - box[0] h = box[3] - box[1] x1, y1, x2, y2 = scale_box( box, 1.5, 1.2 if w > 2 * h else 1.5, clips[0].shape[0], clips[0].shape[1] ) clips = np.array(clips) return clips[:, y1:y2, x1:x2], delta_detect_res(detect_res, x1, y1)1from .predictor import LandmarkPredictor123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107# Backbone networks used for face landmark detection# Cunjian Chen (cunjian@msu.edu)
import torch.nn as nnimport torchvision.models as models
class ConvBlock(nn.Module): def __init__(self, inp, oup, k, s, p, dw=False, linear=False): super(ConvBlock, self).__init__() self.linear = linear if dw: self.conv = nn.Conv2d(inp, oup, k, s, p, groups=inp, bias=False) else: self.conv = nn.Conv2d(inp, oup, k, s, p, bias=False) self.bn = nn.BatchNorm2d(oup) if not linear: self.prelu = nn.PReLU(oup)
def forward(self, x): x = self.conv(x) x = self.bn(x) if self.linear: return x else: return self.prelu(x)
# SE module# https://github.com/wujiyang/Face_Pytorch/blob/master/backbone/cbam.pyclass SEModule(nn.Module): """Squeeze and Excitation Module"""
def __init__(self, channels, reduction): super(SEModule, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.fc1 = nn.Conv2d( channels, channels // reduction, kernel_size=1, padding=0, bias=False ) self.relu = nn.ReLU(inplace=True) self.fc2 = nn.Conv2d( channels // reduction, channels, kernel_size=1, padding=0, bias=False ) self.sigmoid = nn.Sigmoid()
def forward(self, x): input = x x = self.avg_pool(x) x = self.fc1(x) x = self.relu(x) x = self.fc2(x) x = self.sigmoid(x)
return input * x
# USE global depthwise convolution layer. Compatible with MobileNetV2 (224×224), MobileNetV2_ExternalData (224×224)class MobileNet_GDConv(nn.Module): def __init__(self, num_classes): super(MobileNet_GDConv, self).__init__() self.pretrain_net = models.mobilenet_v2(pretrained=False) self.base_net = nn.Sequential(*list(self.pretrain_net.children())[:-1]) self.linear7 = ConvBlock(1280, 1280, (7, 7), 1, 0, dw=True, linear=True) self.linear1 = ConvBlock(1280, num_classes, 1, 1, 0, linear=True)
def forward(self, x): x = self.base_net(x) x = self.linear7(x) x = self.linear1(x) x = x.view(x.size(0), -1) return x
# USE global depthwise convolution layer. Compatible with MobileNetV2 (56×56)class MobileNet_GDConv_56(nn.Module): def __init__(self, num_classes): super(MobileNet_GDConv_56, self).__init__() self.pretrain_net = models.mobilenet_v2(pretrained=False) self.base_net = nn.Sequential(*list(self.pretrain_net.children())[:-1]) self.linear7 = ConvBlock(1280, 1280, (2, 2), 1, 0, dw=True, linear=True) self.linear1 = ConvBlock(1280, num_classes, 1, 1, 0, linear=True)
def forward(self, x): x = self.base_net(x) x = self.linear7(x) x = self.linear1(x) x = x.view(x.size(0), -1) return x
# MobileNetV2 with SE; Compatible with MobileNetV2_SE (224×224) and MobileNetV2_SE_RE (224×224)class MobileNet_GDConv_SE(nn.Module): def __init__(self, num_classes): super(MobileNet_GDConv_SE, self).__init__() self.pretrain_net = models.mobilenet_v2(pretrained=True) self.base_net = nn.Sequential(*list(self.pretrain_net.children())[:-1]) self.linear7 = ConvBlock(1280, 1280, (7, 7), 1, 0, dw=True, linear=True) self.linear1 = ConvBlock(1280, num_classes, 1, 1, 0, linear=True) self.attention = SEModule(1280, 8)
def forward(self, x): x = self.base_net(x) x = self.attention(x) x = self.linear7(x) x = self.linear1(x) x = x.view(x.size(0), -1) return x123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143# Face alignment demo# Uses MTCNN as face detector# Cunjian Chen (ccunjian@gmail.com)import torchimport cv2import numpy as npfrom torch.utils.data import DataLoaderfrom .basenet import MobileNet_GDConv
def get_device(gpu_id): if gpu_id > -1: return torch.device(f"cuda:{str(gpu_id)}") else: return torch.device("cpu")
def load_model(file): model = MobileNet_GDConv(136) if file is not None: model.load_state_dict(torch.load(file, map_location="cpu")) else: url = "https://github.com/yinglinzheng/face_weights/releases/download/v1/mobilenet_224_model_best_gdconv_external.pth" model.load_state_dict(torch.utils.model_zoo.load_url(url)) return model
# landmark of (5L, 2L) from [0,1] to real rangedef reproject(bbox, landmark): landmark_ = landmark.clone() x1, y1, x2, y2 = bbox w = x2 - x1 h = y2 - y1 landmark_[:, 0] *= w landmark_[:, 0] += x1 landmark_[:, 1] *= h landmark_[:, 1] += y1 return landmark_
def prepare_feed(img, face): height, width, _ = img.shape mean = np.asarray([0.485, 0.456, 0.406]) std = np.asarray([0.229, 0.224, 0.225]) out_size = 224 x1, y1, x2, y2 = face[:4]
w = x2 - x1 + 1 h = y2 - y1 + 1 size = int(min([w, h]) * 1.2) cx = x1 + w // 2 cy = y1 + h // 2 x1 = cx - size // 2 x2 = x1 + size y1 = cy - size // 2 y2 = y1 + size
dx = max(0, -x1) dy = max(0, -y1) x1 = max(0, x1) y1 = max(0, y1)
edx = max(0, x2 - width) edy = max(0, y2 - height) x2 = min(width, x2) y2 = min(height, y2) new_bbox = torch.Tensor([x1, y1, x2, y2]).int() x1, y1, x2, y2 = new_bbox cropped = img[y1:y2, x1:x2] if dx > 0 or dy > 0 or edx > 0 or edy > 0: cropped = cv2.copyMakeBorder( cropped, int(dy), int(edy), int(dx), int(edx), cv2.BORDER_CONSTANT, 0 ) cropped_face = cv2.resize(cropped, (out_size, out_size))
if cropped_face.shape[0] <= 0 or cropped_face.shape[1] <= 0: return None test_face = cropped_face.copy() test_face = test_face / 255.0 test_face = (test_face - mean) / std test_face = test_face.transpose((2, 0, 1)) data = torch.from_numpy(test_face).float() return dict(data=data, bbox=new_bbox)
@torch.no_grad()def single_predict(model, feed, device): landmark = model(feed["data"].unsqueeze(0).to(device)).cpu() landmark = landmark.reshape(-1, 2) landmark = reproject(feed["bbox"], landmark) return landmark.numpy()
@torch.no_grad()def batch_predict(model, feeds, device): if not isinstance(feeds, list): feeds = [feeds] # loader = DataLoader(FeedDataset(feeds), batch_size=50, shuffle=False) data = [] for feed in feeds: data.append(feed["data"].unsqueeze(0)) data = torch.cat(data, 0).to(device) results = []
landmarks = model(data).cpu() for landmark, feed in zip(landmarks, feeds): landmark = landmark.reshape(-1, 2) landmark = reproject(feed["bbox"], landmark) results.append(landmark.numpy()) return results
@torch.no_grad()def batch_predict2(model, feeds, device, batch_size=None): if not isinstance(feeds, list): feeds = [feeds] if batch_size is None: batch_size = len(feeds) loader = DataLoader(feeds, batch_size=len(feeds), shuffle=False) results = [] for feed in loader: landmarks = model(feed["data"].to(device)).cpu() for landmark, bbox in zip(landmarks, feed["bbox"]): landmark = landmark.reshape(-1, 2) landmark = reproject(bbox, landmark) results.append(landmark.numpy()) return results
class LandmarkPredictor: def __init__(self, gpu_id=0, file=None): self.device = get_device(gpu_id) self.model = load_model(file).to(self.device).eval()
def __call__(self, feeds): results = batch_predict2(self.model, feeds, self.device) if not isinstance(feeds, list): results = results[0] return results
@staticmethod def prepare_feed(img, face): return prepare_feed(img, face)1234567891011121314151617import cv2
def drawLandmark_multiple(img, bbox, landmark): """ Input: - img: gray or RGB - bbox: type of BBox - landmark: reproject landmark of (5L, 2L) Output: - img marked with landmark and bbox """ x1, y1, x2, y2 = bbox cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2) for x, y in landmark: cv2.circle(img, (int(x), int(y)), 2, (0, 255, 0), -1) return img12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import os
import osimport cv2import numpy as npfrom .tracking.sort import iou
def face_iou(f1, f2): return iou(f1[0], f2[0])
def simple_tracking(batch_landmarks, index=0, thres=0.5): track = []
for i, faces in enumerate(batch_landmarks): if i == 0: if len(faces) <= index or faces[index][-1] < 0.8: return None if index != 0: for idx in range(index): if face_iou(faces[idx], faces[index]) > thres: return None track.append(faces[index]) else: last = track[i - 1] if len(faces) == 0: return None sorted_faces = sorted(faces, key=lambda x: face_iou(x, last), reverse=True) if face_iou(sorted_faces[0], last) < thres: return None track.append(sorted_faces[0]) return track
def multiple_tracking(batch_landmarks): tracks = [] for i in range(len(batch_landmarks[0])): track = simple_tracking(batch_landmarks, index=i) if track is None: continue tracks.append(track) return tracks
def find_longest(detect_res): fc = len(detect_res) tuples = [] start = 0 end = 0 previous_count = -1 all_tracks = [] # start 取得到,end 取不到 while start < (fc - 1): for end in range(start + 2, fc + 1): tracks = multiple_tracking(detect_res[start:end]) if (len(tracks) != previous_count and previous_count != -1) or len( tracks ) == 0: break previous_count = len(tracks) if end - start > 2: if end != fc: un_reach_end = end - 1 else: un_reach_end = end sub_tracks = multiple_tracking(detect_res[start:un_reach_end]) if end == fc and len(sub_tracks) == 0: un_reach_end = end - 1 sub_tracks = multiple_tracking(detect_res[start:un_reach_end]) if len(sub_tracks) > 0: tpl = (start, un_reach_end) tuples.append(tpl) all_tracks.append(sub_tracks[0]) else: raise NotImplementedError previous_count = -1 end = un_reach_end start = end return tuples, all_tracks123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285""" SORT: A Simple, Online and Realtime Tracker Copyright (C) 2016 Alex Bewley alex@dynamicdetection.com
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>."""from __future__ import print_functionimport os.pathimport numpy as npimport matplotlib.pyplot as pltimport matplotlib.patches as patchesfrom scipy.optimize import linear_sum_assignmentimport globimport timeimport argparsefrom filterpy.kalman import KalmanFilter
def iou(bb_test, bb_gt): """ Computes IUO between two bboxes in the form [x1,y1,x2,y2] """ xx1 = np.maximum(bb_test[0], bb_gt[0]) yy1 = np.maximum(bb_test[1], bb_gt[1]) xx2 = np.minimum(bb_test[2], bb_gt[2]) yy2 = np.minimum(bb_test[3], bb_gt[3]) w = np.maximum(0.0, xx2 - xx1) h = np.maximum(0.0, yy2 - yy1) wh = w * h o = wh / ( (bb_test[2] - bb_test[0]) * (bb_test[3] - bb_test[1]) + (bb_gt[2] - bb_gt[0]) * (bb_gt[3] - bb_gt[1]) - wh ) return o
def convert_bbox_to_z(bbox): """ Takes a bounding box in the form [x1,y1,x2,y2] and returns z in the form [x,y,s,r] where x,y is the centre of the box and s is the scale/area and r is the aspect ratio """ w = bbox[2] - bbox[0] h = bbox[3] - bbox[1] x = bbox[0] + w / 2.0 y = bbox[1] + h / 2.0 s = w * h # scale is just area r = w / float(h) return np.array([x, y, s, r]).reshape((4, 1))
def convert_x_to_bbox(x, score=None): """ Takes a bounding box in the centre form [x,y,s,r] and returns it in the form [x1,y1,x2,y2] where x1,y1 is the top left and x2,y2 is the bottom right """ w = np.sqrt(x[2] * x[3]) h = x[2] / w if score == None: return np.array( [x[0] - w / 2.0, x[1] - h / 2.0, x[0] + w / 2.0, x[1] + h / 2.0] ).reshape((1, 4)) else: return np.array( [x[0] - w / 2.0, x[1] - h / 2.0, x[0] + w / 2.0, x[1] + h / 2.0, score] ).reshape((1, 5))
class KalmanBoxTracker(object): """ This class represents the internel state of individual tracked objects observed as bbox. """
count = 0
def __init__(self, bbox): """ Initialises a tracker using initial bounding box. """ # define constant velocity model self.kf = KalmanFilter(dim_x=7, dim_z=4) self.kf.F = np.array( [ [1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1], ] ) self.kf.H = np.array( [ [1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], ] )
self.kf.R[2:, 2:] *= 10.0 self.kf.P[ 4:, 4: ] *= 1000.0 # give high uncertainty to the unobservable initial velocities self.kf.P *= 10.0 self.kf.Q[-1, -1] *= 0.01 self.kf.Q[4:, 4:] *= 0.01
self.kf.x[:4] = convert_bbox_to_z(bbox) self.time_since_update = 0 self.id = KalmanBoxTracker.count KalmanBoxTracker.count += 1 self.history = [] self.hits = 0 self.hit_streak = 0 self.age = 0
def update(self, bbox): """ Updates the state vector with observed bbox. """ self.time_since_update = 0 self.history = [] self.hits += 1 self.hit_streak += 1 self.kf.update(convert_bbox_to_z(bbox))
def predict(self): """ Advances the state vector and returns the predicted bounding box estimate. """ if (self.kf.x[6] + self.kf.x[2]) <= 0: self.kf.x[6] *= 0.0 self.kf.predict() self.age += 1 if self.time_since_update > 0: self.hit_streak = 0 self.time_since_update += 1 self.history.append(convert_x_to_bbox(self.kf.x)) return self.history[-1]
def get_state(self): """ Returns the current bounding box estimate. """ return convert_x_to_bbox(self.kf.x)
def associate_detections_to_trackers(detections, trackers, iou_threshold=0.3): """ Assigns detections to tracked object (both represented as bounding boxes)
Returns 3 lists of matches, unmatched_detections and unmatched_trackers """ if len(trackers) == 0: return ( np.empty((0, 2), dtype=int), np.arange(len(detections)), np.empty((0, 5), dtype=int), ) iou_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)
for d, det in enumerate(detections): for t, trk in enumerate(trackers): iou_matrix[d, t] = iou(det, trk)
matched_indices = linear_sum_assignment(-iou_matrix) matched_indices = np.array(list(zip(*matched_indices)), dtype=np.int) matched_indices.shape = (-1, 2) # print(matched_indices) # print(type(matched_indices))
unmatched_detections = [] for d, det in enumerate(detections): if d not in matched_indices[:, 0]: unmatched_detections.append(d) unmatched_trackers = [] for t, trk in enumerate(trackers): if t not in matched_indices[:, 1]: unmatched_trackers.append(t)
# filter out matched with low IOU matches = [] for m in matched_indices: if iou_matrix[m[0], m[1]] < iou_threshold: unmatched_detections.append(m[0]) unmatched_trackers.append(m[1]) else: matches.append(m.reshape(1, 2)) if len(matches) == 0: matches = np.empty((0, 2), dtype=int) else: matches = np.concatenate(matches, axis=0)
return matches, np.array(unmatched_detections), np.array(unmatched_trackers)
class Sort(object): def __init__(self, max_age=1, min_hits=3): """ Sets key parameters for SORT """ self.max_age = max_age self.min_hits = min_hits self.trackers = [] self.frame_count = 0
def update(self, dets): """ Params: dets - a numpy array of detections in the format [[x1,y1,x2,y2,score],[x1,y1,x2,y2,score],...] Requires: this method must be called once for each frame even with empty detections. Returns the a similar array, where the last column is the object ID.
NOTE: The number of objects returned may differ from the number of detections provided. """ self.frame_count += 1 # get predicted locations from existing trackers. trks = np.zeros((len(self.trackers), 5)) to_del = [] ret = [] for t, trk in enumerate(trks): pos = self.trackers[t].predict()[0] trk[:] = [pos[0], pos[1], pos[2], pos[3], 0] if np.any(np.isnan(pos)): to_del.append(t) trks = np.ma.compress_rows(np.ma.masked_invalid(trks)) for t in reversed(to_del): self.trackers.pop(t) matched, unmatched_dets, unmatched_trks = associate_detections_to_trackers( dets, trks )
# update matched trackers with assigned detections for t, trk in enumerate(self.trackers): if t not in unmatched_trks: d = matched[np.where(matched[:, 1] == t)[0], 0] trk.update(dets[d, :][0])
# create and initialise new trackers for unmatched detections for i in unmatched_dets: trk = KalmanBoxTracker(dets[i, :]) self.trackers.append(trk) i = len(self.trackers) for trk in reversed(self.trackers): d = trk.get_state()[0] if (trk.time_since_update < 1) and ( trk.hit_streak >= self.min_hits or self.frame_count <= self.min_hits ): ret.append( np.concatenate((d, [trk.id + 1])).reshape(1, -1) ) # +1 as MOT benchmark requires positive i -= 1 # remove dead tracklet if trk.time_since_update > self.max_age: self.trackers.pop(i) if len(ret) > 0: return np.concatenate(ret) return np.empty((0, 5))
def parse_args(): """Parse input arguments.""" parser = argparse.ArgumentParser(description="SORT demo") parser.add_argument( "--display", dest="display", help="Display online tracker output (slow) [False]", action="store_true", ) args = parser.parse_args() return args123456789101112131415161718192021222324252627from .sort import Sortimport numpy as np
def get_detections(faces): detections = [] for face in faces: x1, y1, x2, y2 = face[0] detections.append((x1, y1, x2, y2, face[-1])) return np.array(detections)
def get_tracks(detect_results): tracks = {} mot_tracker = Sort() for faces in detect_results: detections = get_detections(faces) track_bbs_ids = mot_tracker.update(detections) for track in track_bbs_ids: # 单独框出每一张人脸 id = int(track[-1]) box = track[:4] if id in tracks: tracks[id].append(box) else: tracks[id] = [box]
return [track for id, track in tracks.items() if len(track) == len(detect_results)]12345import cv2
def write_img(file, img): cv2.imwrite(file, img, [cv2.IMWRITE_PNG_COMPRESSION, 0])12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273import numpy as npimport cv2from .warp_for_xray import ( estimiate_batch_transform, transform_landmarks, std_points_256,)import numpy as np
class FasterCropAlignXRay: """ 修正到统一坐标系,统一图像大小到标准尺寸 """
def __init__(self, size=256): self.image_size = size self.std_points = std_points_256 * size / 256.0
def __call__(self, landmarks, images=None, jitter=False): landmarks = [landmark[:4] for landmark in landmarks] ori_boxes = np.array([ori_box for _, _, _, ori_box in landmarks]) five_landmarks = np.array([ldm5 for _, ldm5, _, _ in landmarks]) landmarks68 = np.array([ldm68 for _, _, ldm68, _ in landmarks]) # assert landmarks68.min() > 0
left_top = ori_boxes[:, :2].min(0)
right_bottom = ori_boxes[:, 2:].max(0)
size = right_bottom - left_top
w, h = size
diff = ori_boxes[:, :2] - left_top[None, ...]
new_five_landmarks = five_landmarks + diff[:, None, :] new_landmarks68 = landmarks68 + diff[:, None, :]
landmark_for_estimiate = new_five_landmarks.copy() if jitter: landmark_for_estimiate += np.random.uniform( -4, 4, landmark_for_estimiate.shape )
tfm, trans = estimiate_batch_transform( landmark_for_estimiate, tgt_pts=self.std_points )
transformed_landmarks68 = np.array( [transform_landmarks(ldm68, trans) for ldm68 in new_landmarks68] )
if images is not None: transformed_images = [ self.process_sinlge(tfm, image, d, h, w) for image, d in zip(images, diff) ] # 拼接 func 的参数 transformed_images = np.stack(transformed_images) return transformed_landmarks68, transformed_images else: return transformed_landmarks68
def process_sinlge(self, tfm, image, d, h, w): assert isinstance(image, np.ndarray) new_image = np.zeros((h, w, 3), dtype=np.uint8) x, y = d ih, iw, _ = image.shape new_image[y : y + ih, x : x + iw] = image transformed_image = cv2.warpAffine( new_image, tfm, (self.image_size, self.image_size) ) return transformed_imagevideo/pwtf-dvd/model_code/inference/test_tools/__init__.pyDeleted
video/pwtf-dvd/model_code/inference/test_tools/common.pyDeleted-122
video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__init__.pyDeleted-56
video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/__init__.cpython-310.pycDeleted
video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/__init__.cpython-39.pycDeleted
video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/alignment.cpython-310.pycDeleted
video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/alignment.cpython-39.pycDeleted
video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/detector.cpython-310.pycDeleted
video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/detector.cpython-39.pycDeleted
video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/utils.cpython-310.pycDeleted
video/pwtf-dvd/model_code/inference/test_tools/ct/detection/__pycache__/utils.cpython-39.pycDeleted
video/pwtf-dvd/model_code/inference/test_tools/ct/detection/alignment.pyDeleted-608
video/pwtf-dvd/model_code/inference/test_tools/ct/detection/detector.pyDeleted-46
video/pwtf-dvd/model_code/inference/test_tools/ct/detection/utils.pyDeleted-146
video/pwtf-dvd/model_code/inference/test_tools/ct/face_alignment/__init__.pyDeleted-1
video/pwtf-dvd/model_code/inference/test_tools/ct/face_alignment/basenet.pyDeleted-107
video/pwtf-dvd/model_code/inference/test_tools/ct/face_alignment/predictor.pyDeleted-143
video/pwtf-dvd/model_code/inference/test_tools/ct/face_alignment/utils.pyDeleted-17
video/pwtf-dvd/model_code/inference/test_tools/ct/operations.pyDeleted-79
video/pwtf-dvd/model_code/inference/test_tools/ct/tracking/__init__.pyDeleted
video/pwtf-dvd/model_code/inference/test_tools/ct/tracking/sort.pyDeleted-285
video/pwtf-dvd/model_code/inference/test_tools/ct/tracking/tracker.pyDeleted-27
video/pwtf-dvd/model_code/inference/test_tools/ct/utils.pyDeleted-5
video/pwtf-dvd/model_code/inference/test_tools/faster_crop_align_xray.pyDeleted-73
video/pwtf-dvd/model_code/inference/test_tools/supply_writer.pyDeleted-49
video/pwtf-dvd/model_code/inference/test_tools/utils.pyDeleted-115
video/pwtf-dvd/model_code/inference/test_tools/warp_for_xray.pyDeleted-574
video/pwtf-dvd/model_code/inference/utils/__init__.pyDeleted-7
video/pwtf-dvd/model_code/inference/utils/common.pyDeleted-80
video/pwtf-dvd/model_code/inference/utils/logger.pyDeleted-182
video/pwtf-dvd/model_code/inference/utils/model_loader.pyDeleted-117
video/pwtf-dvd/model_code/inference/utils/plugin_loader.pyDeleted-69
video/pwtf-dvd/model_code/inference/utils/torch_save.pyDeleted-9
video/pwtf-dvd/model_code/preprocessing/preprocess.pyDeleted-258
video/pwtf-dvd/model_code/preprocessing/test_tools/__init__.pyDeleted
video/pwtf-dvd/model_code/preprocessing/test_tools/common.pyDeleted-122
video/pwtf-dvd/model_code/preprocessing/test_tools/ct/detection/__init__.pyDeleted-56
video/pwtf-dvd/model_code/preprocessing/test_tools/ct/detection/alignment.pyDeleted-608
video/pwtf-dvd/model_code/preprocessing/test_tools/ct/detection/detector.pyDeleted-46
video/pwtf-dvd/model_code/preprocessing/test_tools/ct/detection/utils.pyDeleted-147
video/pwtf-dvd/model_code/preprocessing/test_tools/ct/face_alignment/__init__.pyDeleted-1
video/pwtf-dvd/model_code/preprocessing/test_tools/ct/face_alignment/basenet.pyDeleted-107
video/pwtf-dvd/model_code/preprocessing/test_tools/ct/face_alignment/predictor.pyDeleted-143
video/pwtf-dvd/model_code/preprocessing/test_tools/ct/face_alignment/utils.pyDeleted-17
video/pwtf-dvd/model_code/preprocessing/test_tools/ct/operations.pyDeleted-79
video/pwtf-dvd/model_code/preprocessing/test_tools/ct/tracking/__init__.pyDeleted
video/pwtf-dvd/model_code/preprocessing/test_tools/ct/tracking/sort.pyDeleted-285
video/pwtf-dvd/model_code/preprocessing/test_tools/ct/tracking/tracker.pyDeleted-27
video/pwtf-dvd/model_code/preprocessing/test_tools/ct/utils.pyDeleted-5
video/pwtf-dvd/model_code/preprocessing/test_tools/faster_crop_align_xray.pyDeleted-73