Beispiel #1
0
    def forward(self, predictions):
        """
        Args:
            loc_data: (tensor) Loc preds from loc layers
                Shape: [batch,num_priors*4]
            conf_data: (tensor) Shape: Conf preds from conf layers
                Shape: [batch*num_priors,num_classes]
            prior_data: (tensor) Prior boxes and variances from priorbox layers
                Shape: [1,num_priors,4]
        """
        loc, conf = predictions

        loc_data = loc.data
        conf_data = conf.data
        prior_data = self.priors.data

        num = loc_data.size(0)  # batch size
        num_priors = prior_data.size(0)
        output = torch.zeros(num, self.num_classes, self.top_k, 5)
        conf_preds = conf_data.view(num, num_priors,
                                    self.num_classes).transpose(2, 1)

        # Decode predictions into bboxes.
        for i in range(num):
            decoded_boxes = decode(loc_data[i], prior_data, self.variance)
            # For each class, perform nms
            conf_scores = conf_preds[i].clone()

            for cl in range(1, self.num_classes):
                c_mask = conf_scores[cl].gt(
                    self.conf_thresh).nonzero().view(-1)
                if c_mask.dim() == 0:
                    continue
                scores = conf_scores[cl][c_mask]
                if scores.size(0) == 0 or scores.dim() == 0:
                    continue
                # l_mask = c_mask.unsqueeze(1).expand_as(decoded_boxes)
                # boxes = decoded_boxes[l_mask].view(-1, 4)
                boxes = decoded_boxes[c_mask, :]
                # idx of highest scoring and non-overlapping boxes per class
                ids, count = nms(boxes, scores, self.nms_thresh, self.top_k)
                output[i, cl, :count] = \
                    torch.cat((scores[ids[:count]].unsqueeze(1),
                               boxes[ids[:count]]), 1)
        return output
    def forward(self, predictions):
        """
        Args:
            loc_data: (tensor) Loc preds from loc layers
                Shape: [batch,num_priors*4]
            conf_data: (tensor) Shape: Conf preds from conf layers
                Shape: [batch*num_priors,num_classes]
            prior_data: (tensor) Prior boxes and variances from priorbox layers
                Shape: [1,num_priors,4]
        """
        loc, conf = predictions

        loc_data = loc.data
        conf_data = conf.data
        prior_data = self.priors.data

        num = loc_data.size(0)  # batch size
        num_priors = prior_data.size(0)
        #self.output.zero_()
        if num == 1:
            # size batch x num_classes x num_priors
            conf_preds = conf_data.t().contiguous().unsqueeze(0)
        else:
            conf_preds = conf_data.view(num, num_priors,
                                        self.num_classes).transpose(2, 1)
            #self.output.expand_(num, self.num_classes, self.top_k, 5)
        output = torch.zeros(num, self.num_classes, self.top_k, 5)

        _t = {'decode': Timer(), 'misc': Timer(), 'box_mask':Timer(), 'score_mask':Timer(),'nms':Timer(), 'cpu':Timer(),'sort':Timer()}
        gpunms_time = 0
        scores_time=0
        box_time=0
        cpu_tims=0
        sort_time=0
        decode_time=0
        _t['misc'].tic()
        # Decode predictions into bboxes.
        for i in range(num):
            _t['decode'].tic()
            decoded_boxes = decode(loc_data[i], prior_data, self.variance)
            decode_time += _t['decode'].toc()
            # For each class, perform nms
            conf_scores = conf_preds[i].clone()
            num_det = 0
            for cl in range(1, self.num_classes):
                _t['cpu'].tic()
                c_mask = conf_scores[cl].gt(self.conf_thresh).nonzero().view(-1)
                cpu_tims+=_t['cpu'].toc()
                if c_mask.dim() == 0:
                    continue
                _t['score_mask'].tic()
                scores = conf_scores[cl][c_mask]
                scores_time+=_t['score_mask'].toc()
                if scores.dim() == 0:
                    continue
                _t['box_mask'].tic()
                # l_mask = c_mask.unsqueeze(1).expand_as(decoded_boxes)
                # boxes = decoded_boxes[l_mask].view(-1, 4)
                boxes = decoded_boxes[c_mask, :]
                box_time+=_t['box_mask'].toc()
                # idx of highest scoring and non-overlapping boxes per class
                _t['nms'].tic()
                # cls_dets = torch.cat((boxes, scores), 1)
                # _, order = torch.sort(scores, 0, True)
                # cls_dets = cls_dets[order]
                # keep = nms(cls_dets, self.nms_thresh)
                # cls_dets = cls_dets[keep.view(-1).long()]
                ids, count = nms(boxes, scores, self.nms_thresh, self.top_k)

                gpunms_time += _t['nms'].toc()
                output[i, cl, :count] = \
                    torch.cat((scores[ids[:count]].unsqueeze(1),
                               boxes[ids[:count]]), 1)
        nms_time= _t['misc'].toc()
        # print(nms_time, cpu_tims, scores_time,box_time,gpunms_time)
        # flt = self.output.view(-1, 5)
        # _, idx = flt[:, 0].sort(0)
        # _, rank = idx.sort(0)
        # flt[(rank >= self.top_k).unsqueeze(1).expand_as(flt)].fill_(0)
        return output
Beispiel #3
0
    def forward(self, predictions, return_time=False):
        """
        Args:
            loc_data: (tensor) Loc preds from loc layers
                Shape: [batch,num_priors*4]
            conf_data: (tensor) Shape: Conf preds from conf layers
                Shape: [batch*num_priors,num_classes]
            prior_data: (tensor) Prior boxes and variances from priorbox layers
                Shape: [1,num_priors,4]
        """
        loc, conf = predictions

        loc_data = loc.data
        conf_data = conf.data
        prior_data = self.priors.data

        num = loc_data.size(0)  # batch size
        num_priors = prior_data.size(0)
        #self.output.zero_()
        if num == 1:
            # size batch x num_classes x num_priors
            conf_preds = conf_data.t().contiguous().unsqueeze(0)
        else:
            conf_preds = conf_data.view(num, num_priors,
                                        self.num_classes).transpose(2, 1)
            #self.output.expand_(num, self.num_classes, self.top_k, 5)
        output = torch.zeros(num, self.num_classes, self.top_k, 5)

        _t = {'decode': Timer(), 'misc': Timer(), 'box_mask':Timer(), 'score_mask':Timer(),'nms':Timer(), 'cpu':Timer(),'sort':Timer()}
        from collections import defaultdict
        time_taken = defaultdict(float)
        _t['misc'].tic()
        # Decode predictions into bboxes.
        for i in range(num):
            _t['decode'].tic()
            decoded_boxes = decode(loc_data[i], prior_data, self.variance)
            time_taken['decode'] += _t['decode'].toc()

            # For each class, perform nms
            conf_scores = conf_preds[i].clone()
            num_det = 0
            for cl in range(1, self.num_classes):

                _t['cpu'].tic()
                c_mask = conf_scores[cl].gt(self.conf_thresh).nonzero().view(-1)
                time_taken['cpu'] +=_t['cpu'].toc()

                if c_mask.dim() == 0:
                    continue

                _t['score_mask'].tic()
                scores = conf_scores[cl][c_mask]
                time_taken['score_mask'] +=_t['score_mask'].toc()

                if scores.dim() == 0:
                    continue

                _t['box_mask'].tic()
                # l_mask = c_mask.unsqueeze(1).expand_as(decoded_boxes)
                # boxes = decoded_boxes[l_mask].view(-1, 4)
                boxes = decoded_boxes[c_mask, :]
                time_taken['box_mask'] +=_t['box_mask'].toc()

                # idx of highest scoring and non-overlapping boxes per class
                _t['nms'].tic()
                # cls_dets = torch.cat((boxes, scores), 1)
                # _, order = torch.sort(scores, 0, True)
                # cls_dets = cls_dets[order]
                # keep = nms(cls_dets, self.nms_thresh)
                # cls_dets = cls_dets[keep.view(-1).long()]
                try:
                    ids, count = nms(boxes, scores, self.nms_thresh, self.top_k)
                except Error as e:
                    logger.error(e)
                    logger.debug("BOXES: \n{} \nSCORES: {}\nTHRESH: {}\nTOP_K: {}".format(
                                  boxes, scores, self.nms_thresh, self.top_k))

                time_taken['gpu_nms'] += _t['nms'].toc()

                output[i, cl, :count] = \
                    torch.cat((scores[ids[:count]].unsqueeze(1), boxes[ids[:count]]), 1) \
       
        time_taken['nms_total'] = _t['misc'].toc()
        #keys = time_taken.keys()
        #time_str_format = "{}: {:.4f}s | "

        #str_stats = 'NMS TIME TAKEN - ' + ''.join([
        #    time_str_format.format(k, time_taken[k]) 
        #    for k in ['cpu', 'gpu_nms', 'nms_total']
        #])
        #logger.debug(str_stats)

        # print(nms_time, cpu_tims, scores_time,box_time,gpunms_time)
        # flt = self.output.view(-1, 5)
        # _, idx = flt[:, 0].sort(0)
        # _, rank = idx.sort(0)
        # flt[(rank >= self.top_k).unsqueeze(1).expand_as(flt)].fill_(0)
        if return_time:
            return output, time_taken
        else:
            return output
Beispiel #4
0
    def forward(self, predictions):
        """
        Args:
            loc_data: (tensor) Loc preds from loc layers
                Shape: [batch,num_priors*4]
            conf_data: (tensor) Shape: Conf preds from conf layers
                Shape: [batch*num_priors,num_classes]
            prior_data: (tensor) Prior boxes and variances from priorbox layers
                Shape: [1,num_priors,4]
        """
        loc, conf = predictions

        loc_data = loc.data
        conf_data = conf.data
        prior_data = self.priors.data

        num = loc_data.size(0)  # batch size
        num_priors = prior_data.size(0)
        # self.output.zero_()
        if num == 1:
            # size batch x num_classes x num_priors
            conf_preds = conf_data.t().contiguous().unsqueeze(0)
        else:
            conf_preds = conf_data.view(num, num_priors,
                                        self.num_classes).transpose(2, 1)
            # self.output.expand_(num, self.num_classes, self.top_k, 5)
        output = torch.zeros(num, self.num_classes, self.top_k, 5)

        # Decode predictions into bboxes.
        for i in range(num):
            decoded_boxes = decode(loc_data[i], prior_data, self.variance)

            # print('decoded_boxes: {}'.format(decoded_boxes))

            # For each class, perform nms
            conf_scores = conf_preds[i].clone()
            # print('conf_scores: {}'.format(conf_scores))
            # print(conf_scores.size())

            for cl in range(1, self.num_classes):
                # print(conf_scores[cl])
                # print(conf_scores[cl].size())
                c_mask = conf_scores[cl].gt(
                    self.conf_thresh).nonzero().view(-1)
                # print('cmask: ', c_mask)
                if c_mask.dim() == 0:
                    continue
                scores = conf_scores[cl][c_mask]
                if scores.dim() == 0:
                    continue
                # l_mask = c_mask.unsqueeze(1).expand_as(decoded_boxes)
                # boxes = decoded_boxes[l_mask].view(-1, 4)
                boxes = decoded_boxes[c_mask, :]
                # print(scores, boxes)
                # idx of highest scoring and non-overlapping boxes per class
                # cls_dets = torch.cat((boxes, scores), 1)
                # _, order = torch.sort(scores, 0, True)
                # cls_dets = cls_dets[order]
                # keep = nms(cls_dets, self.nms_thresh)
                # cls_dets = cls_dets[keep.view(-1).long()]

                # print('before nms:')
                # print('boxes: {}'.format(boxes))
                # print('scores: {}'.format(scores))
                # why it is empty?
                ids, count = nms(boxes, scores, self.nms_thresh, self.top_k)
                output[i, cl, :count] = \
                    torch.cat((scores[ids[:count]].unsqueeze(1),
                               boxes[ids[:count]]), 1)
        # print(nms_time, cpu_tims, scores_time,box_time,gpunms_time)
        # flt = self.output.view(-1, 5)
        # _, idx = flt[:, 0].sort(0)
        # _, rank = idx.sort(0)
        # flt[(rank >= self.top_k).unsqueeze(1).expand_as(flt)].fill_(0)
        return output