要求格式[x1,y1,x2,y2]
if __name__ == '__main__':
import torch
a=torch.Tensor([[1,1,2,2],[1,1,3.100001,3],[1,1,3.1,3]])
b=torch.Tensor([[0.9],[0.98],[0.980005]])
from torchvision.ops import nms
ccc=nms(a,b,0.4)
print(ccc)
print(a[ccc])
from __future__ import print_function
import os
import argparse
import torch
import torch.backends.cudnn as cudnn
import numpy as np
from torchvision.ops import nms
from data import cfg_mnet, cfg_re50, cfg_peleenet
from layers.functions.prior_box import PriorBox
from utils.nms.py_cpu_nms import py_cpu_nms
import cv2
from models.retinaface import RetinaFace
from utils.box_utils import decode, decode_landm
import time
parser = argparse.ArgumentParser(description='Retinaface')
# parser.add_argument('-m', '--trained_model', default='weights/mobilenet0.25_Final.pth',type=str)
# parser.add_argument('-m', '--trained_model', default='weights/0.8296_0.9090_0.7629_12.5676_1.0e-06_8.pth',type=str)
parser.add_argument('-m', '--trained_model', default='158/0.8289_0.9060_0.7638_23.3821_1.0e-04_22.pth',type=str)
parser.add_argument('--network', default='peleenet', help='Backbone network mobile0.25 or resnet50')
parser.add_argument('--cpu', action="store_true", default=False, help='Use cpu inference')
parser.add_argument('--confidence_threshold', default=0.9, type=float, help='confidence_threshold')
parser.add_argument('--top_k', default=20, type=int, help='top_k')
parser.add_argument('--nms_threshold', default=0.3, type=float, help='nms_threshold')
parser.add_argument('--keep_top_k', default=12, type=int, help='keep_top_k')
parser.add_argument('--save_image', action="store_true", default=True, help='show detection results')
parser.add_argument('--vis_thres', default=0.6, type=float, help='visualization_threshold')
args = parser.parse_args()
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
unused_pretrained_keys = ckpt_keys - model_keys
missing_keys = model_keys - ckpt_keys
print('Missing keys:{}'.format(len(missing_keys)))
print('Unused checkpoint keys:{}'.format(len(unused_pretrained_keys)))
print('Used keys:{}'.format(len(used_pretrained_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.' '''
print('remove prefix \'{}\''.format(prefix))
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):
print('Loading pretrained model from {}'.format(pretrained_path))
if load_to_cpu:
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
if __name__ == '__main__':
torch.set_grad_enabled(False)
cfg = None
if args.network == "mobile0.25":
cfg = cfg_mnet
elif args.network == "peleenet":
cfg = cfg_peleenet
elif args.network == "resnet50":
cfg = cfg_re50
# net and model
cfg['num_classes'] = 1
net = RetinaFace(cfg=cfg, phase = 'test')
net = load_model(net, args.trained_model, args.cpu)
net.eval()
print('Finished loading model!')
# print(net)
cudnn.benchmark = True
device = torch.device("cpu" if args.cpu else "cuda")
net = net.to(device)
# vc = cv2.VideoCapture(0) # 讀入視頻文件
d = 0
exit_flag = False
c = 0
images = []
images_origin = []
time1 = time.time()
im_width=320*2
im_height=180*2
# img_raw = cv2.resize(img_raw, (width, height))
# im_height, im_width, _ = img_raw.shape
scale = torch.Tensor([im_width, im_height, im_width, im_height])
scale = scale.to(device)
priorbox = PriorBox(cfg, image_size=(im_height, im_width))
priors = priorbox.forward()
priors = priors.to(device)
path=r"\\192.168.25.73\Team-CV\dataset\chumao_train\video\20190528_124414\JPEGImages/"
list_path=r"F:\data\VOCdevkit2007\VOC2007/"
# path=r"\\192.168.25.73\Team-CV\dataset\chumao_train\coco_0719\JPEGImages/"
# path=r"\\192.168.25.73\Team-CV\dataset\chumao_train_1024\head_big\0806\JPEGImages/"
# path=r"D:\Team-CV\dataset\chumao_train_1024\chumao_head\JPEGImages/"
# testset_list =path[:-7] + "wider_val.txt"
#
# with open(testset_list, 'r') as fr:
# test_dataset = fr.read().split()
g = os.walk(list_path)
test_dataset = ['%s/%s' % (i[0], j) for i in g if i[0].endswith('JPEGImages') for j in i[-1] if
j.endswith('jpg')]
num_images = len(test_dataset)
for i, file in enumerate(test_dataset):
img_raw=cv2.imread(file)
img_raw=cv2.resize(img_raw,(im_width, im_height))
img = np.float32(img_raw)
tic = time.time()
img -= (104, 117, 123)
img = img.transpose(2, 0, 1)
img = torch.from_numpy(img).unsqueeze(0)
img = img.to(device)
loc, conf = net(img) # forward pass
prior_data = priors.data
boxes = decode(loc.data.squeeze(0), prior_data, cfg['variance'])
boxes = boxes * scale
# boxes = boxes.cpu().numpy()
scores = torch.sigmoid(conf.squeeze(0).squeeze(1))#.data.cpu().numpy()
# ignore low scores
inds =scores >= args.confidence_threshold
boxes = boxes[inds]
# landms = landms[inds]
scores = scores[inds]
scores,order= scores.topk(min((scores.size(0), args.top_k)), 0, True, True)
boxes = boxes[order]
# landms = landms[order]
# do NMS
# dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
# dets = soft_nms(dets, score_thresh=args.confidence_threshold)
keep = nms(boxes, scores,args.nms_threshold)
# keep top-K faster NMS
boxes = boxes[keep]
scores = scores[keep]
dets = boxes[:args.keep_top_k, :]
scores = scores[:args.keep_top_k]
# landms = landms[:args.keep_top_k, :]
# dets = np.concatenate((dets, landms), axis=1)
print('net forward time: {:.4f}'.format(time.time() - tic))
# show image
if args.save_image:
for index, b in enumerate(dets):
if scores[index] < args.vis_thres:
continue
text = "{:.4f}".format(scores[index])
b = list(map(int, b))
cv2.rectangle(img_raw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 1)
cx = b[0]
cy = b[1] + 12
cv2.putText(img_raw, text, (cx, cy),
cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255))
# save image
cv2.imshow("sdf",img_raw)
cv2.waitKey()
# name = "test.jpg"
# cv2.imwrite(name, img_raw)