def get_cfm_result(self):
        # detection threshold for each class
        # (this is adaptively set based on the max_per_set constraint)
        thresh = -np.inf * np.ones(self.num_classes)
        # top_scores will hold one min heap of scores per class (used to enforce
        # the max_per_set constraint)
        top_scores = [[] for _ in xrange(self.num_classes)]
        # all detections and segmentation are collected into a list:
        # Since the number of dets/segs are of variable size
        all_boxes = [[[] for _ in xrange(self.num_images)]
                     for _ in xrange(self.num_classes)]
        all_masks = [[[] for _ in xrange(self.num_images)]
                     for _ in xrange(self.num_classes)]
        _t = {'im_detect': Timer(), 'misc': Timer()}
        for i in xrange(self.num_images):
            _t['im_detect'].tic()
            masks, boxes, seg_scores = self.cfm_network_forward(i)
            for j in xrange(1, self.num_classes):
                inds = np.where(seg_scores[:, j] > thresh[j])[0]
                cls_scores = seg_scores[inds, j]
                cls_boxes = boxes[inds, :]
                cls_masks = masks[inds, :]
                top_inds = np.argsort(-cls_scores)[:self.max_per_image]
                cls_scores = cls_scores[top_inds]
                cls_boxes = cls_boxes[top_inds, :]
                cls_masks = cls_masks[top_inds, :]
                # push new scores onto the min heap
                for val in cls_scores:
                    heapq.heappush(top_scores[j], val)
                # if we've collected more than the max number of detection,
                # then pop items off the min heap and update the class threshold
                if len(top_scores[j]) > self.max_per_set:
                    while len(top_scores[j]) > self.max_per_set:
                        heapq.heappop(top_scores[j])
                    thresh[j] = top_scores[j][0]
                box_before_nms = np.hstack((cls_boxes, cls_scores[:, np.newaxis]))\
                    .astype(np.float32, copy=False)
                mask_before_nms = cls_masks.astype(np.float32, copy=False)
                all_boxes[j][i], all_masks[j][i] = apply_nms_mask_single(
                    box_before_nms, mask_before_nms, cfg.TEST.NMS)
            _t['im_detect'].toc()
            print 'process image %d/%d, forward average time %f' % (
                i, self.num_images, _t['im_detect'].average_time)
        for j in xrange(1, self.num_classes):
            for i in xrange(self.num_images):
                inds = np.where(all_boxes[j][i][:, -1] > thresh[j])[0]
                all_boxes[j][i] = all_boxes[j][i][inds, :]
                all_masks[j][i] = all_masks[j][i][inds]

        return all_boxes, all_masks
Esempio n. 2
0
    def get_cfm_result(self):
        # detection threshold for each class
        # (this is adaptively set based on the max_per_set constraint)
        thresh = -np.inf * np.ones(self.num_classes)
        # top_scores will hold one min heap of scores per class (used to enforce
        # the max_per_set constraint)
        top_scores = [[] for _ in xrange(self.num_classes)]
        # all detections and segmentation are collected into a list:
        # Since the number of dets/segs are of variable size
        all_boxes = [[[] for _ in xrange(self.num_images)]
                     for _ in xrange(self.num_classes)]
        all_masks = [[[] for _ in xrange(self.num_images)]
                     for _ in xrange(self.num_classes)]
        _t = {'im_detect': Timer(), 'misc': Timer()}
        for i in xrange(self.num_images):
            _t['im_detect'].tic()
            masks, boxes, seg_scores = self.cfm_network_forward(i)
            for j in xrange(1, self.num_classes):
                inds = np.where(seg_scores[:, j] > thresh[j])[0]
                cls_scores = seg_scores[inds, j]
                cls_boxes = boxes[inds, :]
                cls_masks = masks[inds, :]
                top_inds = np.argsort(-cls_scores)[:self.max_per_image]
                cls_scores = cls_scores[top_inds]
                cls_boxes = cls_boxes[top_inds, :]
                cls_masks = cls_masks[top_inds, :]
                # push new scores onto the min heap
                for val in cls_scores:
                    heapq.heappush(top_scores[j], val)
                # if we've collected more than the max number of detection,
                # then pop items off the min heap and update the class threshold
                if len(top_scores[j]) > self.max_per_set:
                    while len(top_scores[j]) > self.max_per_set:
                        heapq.heappop(top_scores[j])
                    thresh[j] = top_scores[j][0]
                box_before_nms = np.hstack((cls_boxes, cls_scores[:, np.newaxis]))\
                    .astype(np.float32, copy=False)
                mask_before_nms = cls_masks.astype(np.float32, copy=False)
                all_boxes[j][i], all_masks[j][i] = apply_nms_mask_single(box_before_nms, mask_before_nms, cfg.TEST.NMS)
            _t['im_detect'].toc()
            print 'process image %d/%d, forward average time %f' % (i, self.num_images,
                                                                    _t['im_detect'].average_time)
        for j in xrange(1, self.num_classes):
            for i in xrange(self.num_images):
                inds = np.where(all_boxes[j][i][:, -1] > thresh[j])[0]
                all_boxes[j][i] = all_boxes[j][i][inds, :]
                all_masks[j][i] = all_masks[j][i][inds]

        return all_boxes, all_masks
    def get_segmentation_result(self):
        # detection threshold for each class
        # (this is adaptively set based on the max_per_set constraint)
        thresh = -np.inf * np.ones(self.num_classes)
        # top_scores will hold one min heap of scores per class (used to enforce
        # the max_per_set constraint)
        top_scores = [[] for _ in xrange(self.num_classes)]
        # all detections and segmentation are collected into a list:
        # Since the number of dets/segs are of variable size
        all_boxes = [[[] for _ in xrange(self.num_images)]
                     for _ in xrange(self.num_classes)]
        all_masks = [[[] for _ in xrange(self.num_images)]
                     for _ in xrange(self.num_classes)]

        _t = {'im_detect': Timer(), 'misc': Timer()}
        for i in xrange(self.num_images):
            im = cv2.imread(self.imdb.image_path_at(i))
            _t['im_detect'].tic()
            masks, boxes, seg_scores = self._segmentation_forward(im)
            _t['im_detect'].toc()
            if not cfg.TEST.USE_MASK_MERGE:
                for j in xrange(1, self.num_classes):
                    inds = np.where(seg_scores[:, j] > thresh[j])[0]
                    cls_scores = seg_scores[inds, j]
                    cls_boxes = boxes[inds, :]
                    cls_masks = masks[inds, :]
                    top_inds = np.argsort(-cls_scores)[:self.max_per_image]
                    cls_scores = cls_scores[top_inds]
                    cls_boxes = cls_boxes[top_inds, :]
                    cls_masks = cls_masks[top_inds, :]
                    # push new scores onto the min heap
                    for val in cls_scores:
                        heapq.heappush(top_scores[j], val)
                    # if we've collected more than the max number of detection,
                    # then pop items off the min heap and update the class threshold
                    if len(top_scores[j]) > self.max_per_set:
                        while len(top_scores[j]) > self.max_per_set:
                            heapq.heappop(top_scores[j])
                        thresh[j] = top_scores[j][0]
                    # Add new boxes into record
                    box_before_nms = np.hstack((cls_boxes, cls_scores[:, np.newaxis]))\
                        .astype(np.float32, copy=False)
                    mask_before_nms = cls_masks.astype(np.float32, copy=False)
                    all_boxes[j][i], all_masks[j][i] = apply_nms_mask_single(
                        box_before_nms, mask_before_nms, cfg.TEST.NMS)
            else:
                if cfg.TEST.USE_GPU_MASK_MERGE:
                    result_mask, result_box = gpu_mask_voting(
                        masks, boxes, seg_scores, self.num_classes,
                        self.max_per_image, im.shape[1], im.shape[0])
                else:
                    result_box, result_mask = cpu_mask_voting(
                        masks, boxes, seg_scores, self.num_classes,
                        self.max_per_image, im.shape[1], im.shape[0])
                # no need to create a min heap since the output will not exceed max number of detection
                for j in xrange(1, self.num_classes):
                    all_boxes[j][i] = result_box[j - 1]
                    all_masks[j][i] = result_mask[j - 1]

            print 'process image %d/%d, forward average time %f' % (
                i, self.num_images, _t['im_detect'].average_time)

        for j in xrange(1, self.num_classes):
            for i in xrange(self.num_images):
                inds = np.where(all_boxes[j][i][:, -1] > thresh[j])[0]
                all_boxes[j][i] = all_boxes[j][i][inds, :]
                all_masks[j][i] = all_masks[j][i][inds]

        return all_boxes, all_masks
Esempio n. 4
0
    def get_segmentation_result(self):
        # detection threshold for each class
        # (this is adaptively set based on the max_per_set constraint)
        thresh = -np.inf * np.ones(self.num_classes)
        # top_scores will hold one min heap of scores per class (used to enforce
        # the max_per_set constraint)
        top_scores = [[] for _ in xrange(self.num_classes)]
        # all detections and segmentation are collected into a list:
        # Since the number of dets/segs are of variable size
        all_boxes = [[[] for _ in xrange(self.num_images)]
                     for _ in xrange(self.num_classes)]
        all_masks = [[[] for _ in xrange(self.num_images)]
                     for _ in xrange(self.num_classes)]

        _t = {'im_detect': Timer(), 'misc': Timer()}
        for i in xrange(self.num_images):
            im = cv2.imread(self.imdb.image_path_at(i))
            _t['im_detect'].tic()
            masks, boxes, seg_scores = self._segmentation_forward(im)
            _t['im_detect'].toc()
            if not cfg.TEST.USE_MASK_MERGE:
                for j in xrange(1, self.num_classes):
                    inds = np.where(seg_scores[:, j] > thresh[j])[0]
                    cls_scores = seg_scores[inds, j]
                    cls_boxes = boxes[inds, :]
                    cls_masks = masks[inds, :]
                    top_inds = np.argsort(-cls_scores)[:self.max_per_image]
                    cls_scores = cls_scores[top_inds]
                    cls_boxes = cls_boxes[top_inds, :]
                    cls_masks = cls_masks[top_inds, :]
                    # push new scores onto the min heap
                    for val in cls_scores:
                        heapq.heappush(top_scores[j], val)
                    # if we've collected more than the max number of detection,
                    # then pop items off the min heap and update the class threshold
                    if len(top_scores[j]) > self.max_per_set:
                        while len(top_scores[j]) > self.max_per_set:
                            heapq.heappop(top_scores[j])
                        thresh[j] = top_scores[j][0]
                    # Add new boxes into record
                    box_before_nms = np.hstack((cls_boxes, cls_scores[:, np.newaxis]))\
                        .astype(np.float32, copy=False)
                    mask_before_nms = cls_masks.astype(np.float32, copy=False)
                    all_boxes[j][i], all_masks[j][i] = apply_nms_mask_single(box_before_nms, mask_before_nms, cfg.TEST.NMS)
            else:
                if cfg.TEST.USE_GPU_MASK_MERGE:
                    result_mask, result_box = gpu_mask_voting(masks, boxes, seg_scores, self.num_classes,
                                                              self.max_per_image, im.shape[1], im.shape[0])
                else:
                    result_box, result_mask = cpu_mask_voting(masks, boxes, seg_scores, self.num_classes,
                                                              self.max_per_image, im.shape[1], im.shape[0])
                # no need to create a min heap since the output will not exceed max number of detection
                for j in xrange(1, self.num_classes):
                    all_boxes[j][i] = result_box[j-1]
                    all_masks[j][i] = result_mask[j-1]

            print 'process image %d/%d, forward average time %f' % (i, self.num_images,
                                                                    _t['im_detect'].average_time)

        for j in xrange(1, self.num_classes):
            for i in xrange(self.num_images):
                inds = np.where(all_boxes[j][i][:, -1] > thresh[j])[0]
                all_boxes[j][i] = all_boxes[j][i][inds, :]
                all_masks[j][i] = all_masks[j][i][inds]

        return all_boxes, all_masks