Example #1
0
    def setUp(self):
        # sample data generator
        batch_size = 64
        self.dataset = DetectionDataset(data_type='train')
        self.imgs, self.labs_info = self.dataset[:batch_size]
        print('Number of Classes : {}'.format(self.dataset.num_classes))
        print('Image shape : {} || Label shape : {}'.format(self.imgs.shape, self.labs_info.shape))
        self.strides = [4, 8, 16]
        self.scales = [10, 25, 40]
        self.ratios = [(1, 1),
                  (1.5, 0.5),
                  (1.2, 0.8),
                  (0.8, 1.2),
                  (1.4, 1.4)]

        self.prior = PriorBoxes(self.strides, self.scales, self.ratios)
        self.prior_boxes = self.prior.generate((128, 128))  # prior boxes shape : (6720, 4)

        # 0번째 이미지를 쌤플 이미지로 사용함.
        self.group_labs = self.labs_info.groupby('image_index')
        for ind, labs in self.group_labs:
            self.labs = labs
            break
        self.gt_boxes = self.labs[['cx', 'cy', 'w', 'h']].values
        self.gt_labels = self.labs['label'].values
        self.iou = calculate_iou(self.prior_boxes, self.gt_boxes)
        print('Ground Truths Shape : {}'.format(self.gt_boxes.shape))
        print('IOU Shape : {}'.format(self.iou.shape))
        print(list(self.labs.groupby('image_index')))
Example #2
0
def label_generator(train_labels_bucket, prior_boxes, n_classes):
    train_labels = []
    for index, train_label_df in train_labels_bucket:
        gt_boxes = train_label_df[['cx', 'cy', 'w', 'h']].values  # shape => [N_prior , 4]
        gt_labels = train_label_df['label'].values  # shape => [N_prior, N_classes]

        # Calculate IOU
        iou = calculate_iou(prior_boxes, gt_boxes)

        # Generate Detection Labels
        matched_labels = matching_labels_coords(prior_boxes, iou, n_classes, gt_boxes, gt_labels)
        train_labels.append(matched_labels)
    return np.asarray(train_labels)
    for (x, y, w, h) in rects:
        proposedRects.append((x, y, x + w, y + h))

    # to count number of positive and negative region of interests saved
    positiveROIs = 0
    negativeROIs = 0

    # loop over max number of region proposals
    for proposedRect in proposedRects[:config.MAX_PROPOSALS_NO]:
        # unpack the proposed rectangle bounding box
        (proposedStartX, proposedStartY, proposedEndX,
         proposedEndY) = proposedRect

        # loop over the ground-truth bounding boxes
        for boundBox in boundBoxes:
            iou = iou_fn.calculate_iou(boundBox, proposedRect)
            (boundStartX, boundStartY, boundEndX, boundEndY) = boundBox

            roi = None
            outputPath = None

            if iou > 0.7 and positiveROIs <= config.MAX_POSITIVE_NO:
                # extract ROI and derive positive output path
                roi = image[proposedStartY:proposedEndY,
                            proposedStartX:proposedEndX]
                filename = "{}.png".format(totalPositive)
                outputPath = os.path.sep.join([config.POSITVE_PATH, filename])

                # increment positive counters
                positiveROIs += 1
                totalPositive += 1