def yuNetDetection(self, frame): if self.init == 0: frameWidth, frameHeight = frame.shape[:2] self.pb = PriorBox(input_shape=(640, 480), output_shape=(frameHeight, frameWidth)) self.init = 1 blob = cv2.dnn.blobFromImage(frame, size=(640, 480)) outputNames = ['loc', 'conf', 'iou'] self.detector.setInput(blob) loc, conf, iou = self.detector.forward(outputNames) dets = self.pb.decode(np.squeeze(loc, axis=0), np.squeeze(conf, axis=0), np.squeeze(iou, axis=0)) idx = np.where(dets[:, -1] > self.confidence)[0] dets = dets[idx] if dets.shape[0]: facess = nms(dets, self.threshold) else: facess = () return facess faces = np.array(facess[:, :4]) faces = faces.astype(np.int) faceStartXY = faces[:, :2] faceEndXY = faces[:, 2:4] faceWH = faceEndXY - faceStartXY faces = np.hstack((faceStartXY, faceWH)) # scores = facess[:, -1] return faces
def __init__(self,args): if args.ctx and torch.cuda.is_available(): self.use_cuda = True else: self.use_cuda = False if self.use_cuda: torch.set_default_tensor_type('torch.cuda.FloatTensor') else: torch.set_default_tensor_type('torch.FloatTensor') self.loadmodel(args.headmodelpath) self.threshold = args.conf_thresh self.img_dir = args.img_dir self.detect = Detect(cfg) self.Prior = PriorBox(cfg) with torch.no_grad(): self.priors = self.Prior.forward()
def __init__(self, num_classes, num_blocks, top_k, conf_thresh, nms_thresh, variance): super(ASSD_ResNet101, self).__init__() self.num_classes = num_classes ############################################################################################ self.inplanes = 64 layers = [3, 4, 23, 3] self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = nn.BatchNorm2d(64) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(Bottleneck, 64, layers[0]) self.layer2 = self._make_layer(Bottleneck, 128, layers[1], stride=2) self.layer3 = self._make_layer(Bottleneck, 256, layers[2], stride=2) self.layer4 = self._make_layer(Bottleneck, 512, layers[3], stride=2) #self.L2Norm = L2Norm(n_channels=512, scale=20) self.extra_layers = nn.ModuleList( add_extras(layer_cfg['extra'], batch_norm=True)) self.conf_layers = nn.ModuleList( build_conf(layer_cfg['pred'], num_blocks, num_classes)) self.locs_layers = nn.ModuleList( build_locs(layer_cfg['pred'], num_blocks)) self.prior_boxes = PriorBox() self.prior_boxes = self.prior_boxes.forward() device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") self.prior_boxes = self.prior_boxes.to(device) self.fusion_layers = nn.ModuleList(fusionModule()) self.fusion_bn = nn.BatchNorm2d(768) #256*3 self.fusion_conv = nn.Conv2d(768, 512, kernel_size=1) self.att_layers = nn.ModuleList(make_attention()) self.softmax = nn.Softmax(dim=1) self.detect = Detect(num_classes=num_classes, top_k=top_k, conf_thresh=conf_thresh, nms_thresh=nms_thresh, variance=variance)
def __init__(self, phase, base, extras, head, num_classes): super(S3FD, self).__init__() self.phase = phase self.num_classes = num_classes ''' self.priorbox = PriorBox(size,cfg) self.priors = Variable(self.priorbox.forward(), volatile=True) ''' # SSD network self.vgg = nn.ModuleList(base) # Layer learns to scale the l2 normalized features from conv4_3 self.L2Norm3_3 = L2Norm(256, 10) self.L2Norm4_3 = L2Norm(512, 8) self.L2Norm5_3 = L2Norm(512, 5) self.extras = nn.ModuleList(extras) self.loc = nn.ModuleList(head[0]) self.conf = nn.ModuleList(head[1]) self.priorbox = PriorBox(cfg) with torch.no_grad(): self.priors = self.priorbox.forward()
img_resize = cv2.resize(img, dst=None, dsize=(input_shape), interpolation=cv2.INTER_LINEAR) hr, wr, _ = img_resize.shape print('Network input size: h={}, w={}'.format(hr, wr)) blob = cv2.dnn.blobFromImage(img_resize, size=input_shape) # run the net output_names = ['loc', 'conf'] net.setInput(blob) loc, conf = net.forward(output_names) # Decode bboxes and landmarks pb = PriorBox(input_shape=input_shape, output_shape=(w, h)) dets = pb.decode(np.squeeze(loc, axis=0), np.squeeze(conf, axis=0)) # Ignore low scores idx = np.where(dets[:, -1] > args.conf_thresh)[0] dets = dets[idx] # NMS if dets.shape[0] > 0: dets = nms(dets, args.nms_thresh) faces = dets[:args.keep_top_k, :] print('Detection results: {} faces found'.format(faces.shape[0])) print(faces) else: print('No faces found.') exit()
loc[:, 8:10] * self.variance[0] * self.priors[:, 2:4], self.priors[:, 0:2] + loc[:, 10:12] * self.variance[0] * self.priors[:, 2:4], self.priors[:, 0:2] + loc[:, 12:14] * self.variance[0] * self.priors[:, 2:4])) # scale recover landmark_scale = np.array([self.out_w, self.out_h] * 5) landmarks = landmarks * landmark_scale # get score cls_scores = conf[:, 1] iou_scores = iou[:, 0] scores = np.sqrt(cls_scores * iou_scores) scores = scores[:, np.newaxis] dets = np.hstack((bboxes, landmarks, scores)) return dets if __name__ == '__main__': from priorbox import PriorBox pb = PriorBox() print(pb.generate_priors().shape) loc = np.random.rand(1, 4385, 14) conf = np.random.rand(1, 4385, 2) iou = np.random.rand(1, 4385, 1) dets = pb.decode(np.squeeze(loc, axis=0), np.squeeze(conf, axis=0), np.squeeze(iou, axis=0)) print(dets.shape)