예제 #1
0
 def run(self):
     print 'BlobFetcher started'
     while True:
         db_inds = self._get_next_minibatch_inds()
         minibatch_db = [self._roidb[i] for i in db_inds]
         blobs = get_minibatch(minibatch_db, self._num_classes)
         self._queue.put(blobs)
예제 #2
0
 def run(self):
   print 'BlobFetcher started'; sys.stdout.flush()
   while True:
     db_inds = self._get_next_minibatch_inds()
     im, minibatch_db = self._loader.load_im_and_roi(db_inds)
     minibatch_db = self._loader.add_bbox_regression_targets(minibatch_db)
     blobs = get_minibatch(im, minibatch_db, self._num_classes)
     self._queue.put(blobs)
예제 #3
0
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        db_inds = self._get_next_minibatch_inds()
        minibatch_db = [self._roidb[i] for i in db_inds]
        return get_minibatch(minibatch_db, self._num_classes)
예제 #4
0
 def _get_next_minibatch(self):
   """Return the blobs to be used for the next minibatch."""
   if cfg.TRAIN.USE_PREFETCH:
     return self._blob_queue.get()
   else:
     db_inds = self._get_next_minibatch_inds()
     im, minibatch_db = self._loader.load_im_and_roi(db_inds)
     minibatch_db = self._loader.add_bbox_regression_targets(minibatch_db)
     return get_minibatch(im, minibatch_db, self._num_classes)
예제 #5
0
파일: layer.py 프로젝트: hfslyc/fast-rcnn
 def run(self):
     print 'BlobFetcher started'
     while True:
         db_inds = self._get_next_minibatch_inds()
         minibatch_db = [self._roidb[i] for i in db_inds]
         try:
             blobs = get_minibatch(minibatch_db, self._num_classes)
             self._queue.put(blobs)
         except NoLabels:
             pass #Keep looping
예제 #6
0
 def run(self):
     print 'BlobFetcher started'
     while True:
         db_inds = self._get_next_minibatch_inds()
         minibatch_db = [self._roidb[i] for i in db_inds]
         if cfg.TRAIN.USE_OHEM:
             blobs = get_allrois_minibatch(minibatch_db, self._num_classes)
         else:
             blobs = get_minibatch(minibatch_db, self._num_classes)
         self._queue.put(blobs)
예제 #7
0
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        if cfg.TRAIN.USE_PREFETCH:
            return self._blob_queue.get()
        else:
            db_inds = self._get_next_minibatch_inds()
            minibatch_db = [self._roidb[i] for i in db_inds]
            return get_minibatch(minibatch_db, self._num_classes)
예제 #8
0
 def _get_next_minibatch(self, db_inds, sigma):
     """Return the blobs to be used for the next minibatch.
     """
     minibatch_db = [self._roidb[i] for i in db_inds]
     if cfg.TRAIN.USE_RPN_DB:
         minibatch_db = self.imdb.add_rpn_rois(minibatch_db)
     prepare_roidb(minibatch_db)
     add_bbox_regression_targets(minibatch_db, self.bbox_means,
                                 self.bbox_stds)
     blobs = get_minibatch(minibatch_db, self._num_classes, sigma=sigma)
     if blobs is not None:
         blobs['db_inds'] = db_inds
     return blobs
예제 #9
0
파일: layer.py 프로젝트: fjfabz/mpt-demo
 def _get_next_minibatch(self):
     if cfg.TRAIN.USE_PREFETCH:
         while 1:
             info = self._blob_queue.get()
             if info is not None:
                 return info
     else:
         while 1:
             db_inds = self._get_next_minibatch_inds()
             minibatch_db = [self._roidb[i] for i in db_inds]
             info = get_minibatch(minibatch_db)
             if info is not None:
                 return info
    def __getitem__(self, index):
        # get the anchor index for current sample index
        # here we set the anchor index to the last one
        # sample in this group
        minibatch_db = [self._roidb[index]]
        blobs = get_minibatch(minibatch_db, self._num_classes)

        # print(blobs)
        # import IPython; IPython.embed()
        # assert(1 == 0)

        data = torch.from_numpy(blobs['data'])
        im_info = torch.from_numpy(blobs['im_info'])
        # we need to random shuffle the bounding box.
        data_height, data_width = data.size(1), data.size(2)
        if self.training:
            np.random.shuffle(blobs['gt_boxes'])
            try:
                gt_boxes = torch.from_numpy(blobs['gt_boxes'])
            except:
                print('gt_boxes error')
                import IPython
                IPython.embed()

            im_info[0, 0] = data.size(1)
            im_info[0, 1] = data.size(2)

            gt_boxes_padding = torch.FloatTensor(self.max_num_box,
                                                 gt_boxes.size(1)).zero_()
            num_boxes = min(gt_boxes.size(0), self.max_num_box)
            gt_boxes_padding[:num_boxes, :] = gt_boxes[:num_boxes]

            # permute trim_data to adapt to downstream processing
            try:
                data = data.transpose(2, 3).transpose(1, 2).contiguous()
            except:
                import IPython
                IPython.embed()
            im_info = im_info.view(3)

            return data.squeeze(0), im_info, gt_boxes_padding, num_boxes
        else:
            data = data.permute(0, 3, 1,
                                2).contiguous().view(3, data_height,
                                                     data_width)
            im_info = im_info.view(3)

            gt_boxes = torch.FloatTensor([1, 1, 1, 1, 1])
            num_boxes = 0

            return data, im_info, gt_boxes, num_boxes
예제 #11
0
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        db_inds = self._get_next_minibatch_inds()
        minibatch_db = [self._roidb[i] for i in db_inds]
        if cfg.TRAIN.USE_RPN_DB:
            minibatch_db = self._imdb.add_rpn_rois(minibatch_db)
        prepare_roidb(minibatch_db)
        add_bbox_regression_targets(minibatch_db, self.bbox_means,
                                    self.bbox_stds)
        return get_minibatch(minibatch_db, self._num_classes)
예제 #12
0
    def __getitem__(self, index):
        # testing
        index_ratio = index
        # though it is called minibatch, in fact it contains only one img here
        minibatch_db = [self._roidb[index_ratio]]

        # load query
        blobs = get_minibatch(minibatch_db)
        data = torch.from_numpy(blobs['data'])
        im_info = torch.from_numpy(blobs['im_info'])  # (H, W, scale)
        data_height, data_width = data.size(1), data.size(2)
        data = data.permute(0, 3, 1,
                            2).contiguous().view(3, data_height, data_width)
        im_info = im_info.view(3)
        gt_boxes = torch.from_numpy(blobs['gt_boxes'])
        num_boxes = gt_boxes.size(0)

        # get supports
        support_data_all = np.zeros(
            (self.testing_shot, 3, self.support_im_size, self.support_im_size),
            dtype=np.float32)
        current_gt_class_id = int(gt_boxes[0][4])
        selected_supports = self.support_pool[current_gt_class_id]

        for i, _path in enumerate(selected_supports):
            support_im = imread(_path)[:, :, ::-1]  # rgb -> bgr
            target_size = np.min(
                support_im.shape[0:2])  # don't change the size
            support_im, _ = prep_im_for_blob(support_im, cfg.PIXEL_MEANS,
                                             target_size, cfg.TRAIN.MAX_SIZE)
            _h, _w = support_im.shape[0], support_im.shape[1]
            if _h > _w:
                resize_scale = float(self.support_im_size) / float(_h)
                unfit_size = int(_w * resize_scale)
                support_im = cv2.resize(support_im,
                                        (unfit_size, self.support_im_size),
                                        interpolation=cv2.INTER_LINEAR)
            else:
                resize_scale = float(self.support_im_size) / float(_w)
                unfit_size = int(_h * resize_scale)
                support_im = cv2.resize(support_im,
                                        (self.support_im_size, unfit_size),
                                        interpolation=cv2.INTER_LINEAR)
            h, w = support_im.shape[0], support_im.shape[1]
            support_data_all[i, :, :h, :w] = np.transpose(
                support_im, (2, 0, 1))
        supports = torch.from_numpy(support_data_all)

        return data, im_info, gt_boxes, num_boxes, supports
예제 #13
0
    def __getitem__(self, index):

        minibatch_db = [self._roidb[index]]
        blobs = get_minibatch(minibatch_db, self._num_classes)
        data = torch.from_numpy(blobs['data'])
        im_info = torch.from_numpy(blobs['im_info'])
        ss_boxes = torch.from_numpy(blobs['ss_boxes'])
        num_boxes = torch.from_numpy(blobs['num_boxes'])
        im_label = torch.from_numpy(blobs['im_label'])

        data_height, data_width = data.size(1), data.size(2)
        data = data.permute(0, 3, 1,
                            2).contiguous().view(3, data_height, data_width)

        return data, im_info, ss_boxes, num_boxes, im_label
예제 #14
0
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        if cfg.TRAIN.USE_PREFETCH:
            return self._blob_queue.get()
        else:
            db_inds = self._get_next_minibatch_inds()  # 其实就只有1张
            minibatch_db = [self._roidb[i] for i in db_inds
                            ]  # 从 self._roidb 中取出下一个mini_batch所包含图片的 roidb
            return get_minibatch(
                minibatch_db,
                self._num_classes)  # 与数据相联系的关键,该函数来自roi_data_layer.minibatch
예제 #15
0
파일: layer.py 프로젝트: hfslyc/fast-rcnn
 def _get_next_minibatch(self):
     """Return the blobs to be used for the next minibatch.
     If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
     separate process and made available through self._blob_queue.
     """
     if cfg.TRAIN.USE_PREFETCH:
         return self._blob_queue.get()
     else:
         while True:
             db_inds = self._get_next_minibatch_inds()
             minibatch_db = [self._roidb[i] for i in db_inds]
             try:
                 return get_minibatch(minibatch_db, self._num_classes)
             except NoLabels:
                 #print "Nolabel"
                 pass #Have another go                    
예제 #16
0
파일: layer.py 프로젝트: herobd/fast-rcnn
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        if cfg.TRAIN.USE_PREFETCH:
            return self._blob_queue.get()
        else:
            db_inds = self._get_next_minibatch_inds()
            minibatch_db = [self._roidb[i] for i in db_inds]
            #debug Brian
            #print 'DEBUGGING, should have classes'
            #print minibatch_db[0]
            ##########
            return get_minibatch(minibatch_db, self._num_classes)
예제 #17
0
파일: layer.py 프로젝트: uberlabs/fast-rcnn
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        if cfg.TRAIN.USE_PREFETCH:
            return self._blob_queue.get()
        else:
            db_inds = self._get_next_minibatch_inds()
            minibatch_db = []
            for i in db_inds:
                # image_path = self._imdb.image_path_at(i)
                row = self._roidb.get_row_from_db(i)
                minibatch_db.append(row)
            # minibatch_db = [self._roidb[i] for i in db_inds]
            return get_minibatch(minibatch_db, self._num_classes)
예제 #18
0
    def __getitem__(self, index):
        # get the anchor index for current sample index
        # here we set the anchor index to the last one
        # sample in this group
        minibatch_db = [self._roidb[index]]  # [self._roidb[index_ratio]]
        blobs = get_minibatch(minibatch_db, self._num_classes)
        np.random.shuffle(blobs['rois'])
        rois = torch.from_numpy(blobs['rois'][:self.max_rois_size])
        data = torch.from_numpy(blobs['data'])
        labels = torch.from_numpy(blobs['labels'])
        data_height, data_width = data.size(1), data.size(2)

        data = data.permute(0, 3, 1,
                            2).contiguous().view(3, data_height, data_width)

        info = torch.Tensor([rois.size(0), data_height, data_width])

        return data, rois, labels, info
    def _get_next_minibatch(self, augment_en):
        """Return the blobs to be used for the next minibatch.

    If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
    separate process and made available through self._blob_queue.
    """
        minibatch = None
        while (minibatch is None):
            db_inds = self._get_next_minibatch_inds()
            minibatch_db = [self._roidb[i] for i in db_inds]
            #minibatch_db = [self._roidb[0]]
            #print('minibatch')
            #print(minibatch_db)
            minibatch = get_minibatch(minibatch_db, self._num_classes, augment_en, self._cnt)
            #if(minibatch is None):
                #print('skipping image, augmentation resulted in 0 GT boxes')
            self._cnt += 1
        return minibatch
예제 #20
0
파일: layer.py 프로젝트: only4hj/fast-rcnn
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        if cfg.TRAIN.USE_PREFETCH:
            return self._blob_queue.get()
        else:
            # Clear up the previous labels and bbox_targets to save memory
            if cfg.TRAIN.LAZY_PREPARING_ROIDB == True and self._cur_minibatch_db != None:
                clear_minibatch(self._cur_minibatch_db)

            db_inds = self._get_next_minibatch_inds()
            self._cur_minibatch_db = [self._roidb[i] for i in db_inds]
            return get_minibatch(self._cur_minibatch_db, self._num_classes, 
                                                 self._bbox_means, self._bbox_stds,
                                                 self._proposal_file)
예제 #21
0
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        if cfg.TRAIN.USE_PREFETCH:
            return self._blob_queue.get()
        else:
            # Clear up the previous labels and bbox_targets to save memory
            if cfg.TRAIN.LAZY_PREPARING_ROIDB == True and self._cur_minibatch_db != None:
                clear_minibatch(self._cur_minibatch_db)

            db_inds = self._get_next_minibatch_inds()
            self._cur_minibatch_db = [self._roidb[i] for i in db_inds]
            return get_minibatch(self._cur_minibatch_db, self._num_classes,
                                 self._bbox_means, self._bbox_stds,
                                 self._proposal_file)
예제 #22
0
 def __getitem__(self, index):
     # get the anchor index for current sample index
     # here we set the anchor index to the last one
     # sample in this group
     minibatch_db = [self._roidb[index]]
     blobs = get_minibatch(minibatch_db, self._num_classes)
     data = torch.from_numpy(blobs['data'])
     length, height, width = data.size(-3), data.size(-2), data.size(-1)
     data = data.contiguous().view(3, length, height, width)
     if cfg.TRAIN.HAS_RPN or cfg.TEST.HAS_RPN:
         gt_windows = torch.from_numpy(blobs['gt_windows'])
         #num_twin = gt_windows.size()
         #gt_windows.view(3)
         #print("data {}".format(data.shape))
         #print("gt_windows {}".format(gt_windows.shape))
         return data, gt_windows
     else:  # not using RPN
         raise NotImplementedError
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        if cfg.TRAIN.USE_PREFETCH:
            if self._phase == 'TRAIN':
                timer = Timer()
                timer.tic()
                blob = self._blob_queue_train.get()
                self._blob_queue_train.task_done()
                timer.toc()
                #print 'queue get data time used: ', timer.average_time
                return blob
            else:
                blob = self._blob_queue_val.get()
                self._blob_queue_val.task_done()
                return blob
        else:
            db_inds = self._get_next_minibatch_inds()
            processed_ims = []
            labels = []
            coords = []
            for idx in db_inds:
                idx = int(idx)
                try:
                    if self._phase == 'TEST':
                        img, label, coord = minibatch_detector.get_img(
                            idx, self._roidb, self._u_roidb, self._phase)
                    else:
                        img, label, coord = minibatch.get_img(
                            idx, self._roidb, self._u_roidb, self._phase)
                except:
                    print 'wrong idx:%d' % idx
                    import traceback
                    traceback.print_exc()
                    db_inds.append(self._get_next_ind())
                    continue
                processed_ims.append(img)
                labels.append(label)
                coords.append(coord)
            return get_minibatch(processed_ims, labels, coords)
예제 #24
0
    def _get_next_minibatch(self, args, assign, helper, lock):
        """Return the blobs to be used for the next minibatch.

    If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
    separate process and made available through self._blob_queue.
    """
        batch_size = args.batch_size
        # sync index acquiration
        if lock is not None:
            lock.acquire()
        db_inds = self._get_next_minibatch_inds(batch_size)
        if lock is not None:
            lock.release()
        minibatch_db = [self._roidb[i] for i in db_inds]
        return get_minibatch(minibatch_db,
                             args,
                             assign,
                             helper,
                             augmentation_type=self.augmentation)
예제 #25
0
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        db_inds = self._get_next_minibatch_inds()
        minibatch_db = [self._roidb[i] for i in db_inds]
        if cfg.TRACE:
            print '=======get next minibatch========'
            print 'db_inds: %d'%db_inds
            print 'minibatch_db[0][image]: ' + minibatch_db[0]['image']
        if cfg.DEBUG:
            print 'ROIDataLayer: _get_next_minibatch:'
            print 'db_inds:'
            print db_inds
            print 'minibatch_db[0] key:'
            for key in minibatch_db[0]:
                print key
        return get_minibatch(minibatch_db, self._num_classes)
예제 #26
0
        def get_rpn_testbatch(roidbs, cfg):
            data = []
            label = {}
            im_info = []

            blobs = get_minibatch(roidbs, self._num_classes)
            data = torch.from_numpy(blobs['data'])
            im_info = torch.from_numpy(blobs['im_info'])

            data_height, data_width = data.size(1), data.size(2)

            data = data.permute(0, 3, 1,
                                2).contiguous().view(3, data_height,
                                                     data_width)
            im_info = im_info.view(3)

            gt_boxes = torch.FloatTensor([1, 1, 1, 1, 1])
            num_boxes = 0

            return data, label, im_info, gt_boxes, num_boxes
예제 #27
0
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        db_inds = self._get_next_minibatch_inds()
        minibatch_db = [self._roidb[i] for i in db_inds]
        if cfg.TRACE:
            print '=======get next minibatch========'
            print 'db_inds: %d' % db_inds
            print 'minibatch_db[0][image]: ' + minibatch_db[0]['image']
        if cfg.DEBUG:
            print 'ROIDataLayer: _get_next_minibatch:'
            print 'db_inds:'
            print db_inds
            print 'minibatch_db[0] key:'
            for key in minibatch_db[0]:
                print key
        return get_minibatch(minibatch_db, self._num_classes)
예제 #28
0
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        if cfg.TRAIN.USE_PREFETCH:
            return self._blob_queue.get()
        else:
            if cfg.TRAIN.shuffle_heterology:
                db_inds = self._get_next_minibatch_inds_heterology(
                )  # get a index of sample from random list
            else:
                db_inds = self._get_next_minibatch_inds(
                )  # get a index of sample from random list
            minibatch_db = [self._roidb[i] for i in db_inds]

            # print "Image: {}".format(minibatch_db[0]['image'])

            return get_minibatch(minibatch_db, self._num_classes)
예제 #29
0
    def __getitem__(self, index):
        index_ratio = index

        # get the anchor index for current sample index
        # here we set the anchor index to the last one
        # sample in this group
        minibatch_db = [self._roidb[index_ratio]]
        blobs = get_minibatch(minibatch_db)
        data = torch.from_numpy(blobs['data'])
        im_info = torch.from_numpy(blobs['im_info'])  # (H, W, scale)
        # we need to random shuffle the bounding box.
        data_height, data_width = data.size(1), data.size(2)

        data = data.permute(0, 3, 1, 2).contiguous().view(3, data_height, data_width)
        im_info = im_info.view(3)

        # gt_boxes = torch.FloatTensor([1,1,1,1,1])
        gt_boxes = torch.from_numpy(blobs['gt_boxes'])
        num_boxes = 0

        return data, im_info, gt_boxes, num_boxes
예제 #30
0
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        if cfg.TRAIN.USE_PREFETCH:
            return self._blob_queue.get()
        else:
            db_inds = self._get_next_minibatch_inds()
            minibatch_db = [self._roidb[i] for i in db_inds]

            # FIX - carried out fix to lib/datasets/coco.py
            # gt_class_first = minibatch_db[0]['gt_classes'][0]
            #
            # while gt_class_first == 0:
            #     db_inds = self._get_next_minibatch_inds()
            #     minibatch_db = [self._roidb[i] for i in db_inds]
            #     gt_class_first = minibatch_db[0]['gt_classes'][0]

            return get_minibatch(minibatch_db, self._num_classes)
예제 #31
0
    def _get_next_minibatch(self, scale_idx):
        """Return the blobs to be used for the next minibatch.
        """
        def merge_two_dicts(x, y):
            """Given two dicts, merge them into a new dict as a shallow copy."""
            z = x.copy()
            z.update(y)
            return z

        self.db_inds = self._get_next_minibatch_inds()
        minibatch_db = [
            merge_two_dicts(self._roidb[i], {"idx": i}) for i in self.db_inds
        ]
        res, self.resize_ratio = get_minibatch(minibatch_db, scale_idx)
        for idx, scale_i in zip(self.db_inds, self.resize_ratio):
            if 'seen_scale' not in self._roidb[idx]:
                self._roidb[idx]['seen_scale'] = []
            self._roidb[idx]['seen_scale'].append(scale_i)
            logger.debug('Scale {} seeing for {}, rank: {}'.format(
                scale_i, idx, cfg.RANK))
        return res
    def run(self):
        print 'BlobFetcher started'
        while True:
            #print('Current process id: {}'.format(os.getpid()))
            #print('Current affinity: {}'.format(p.cpu_affinity()))

            db_inds = self._get_next_minibatch_inds()
            processed_ims = []
            labels = []
            coords = []
            db_inds = db_inds[0:cfg.TRAIN.IMS_PER_BATCH]
            for idx in db_inds:
                idx = int(idx)
                # print idx
                try:
                    if self._phase == 'TEST':
                        img, label, coord = minibatch_detector.get_img(
                            idx, self._roidb, self._u_roidb, self._phase)
                    else:
                        img, label, coord = minibatch.get_img(
                            idx, self._roidb, self._u_roidb, self._phase)
                except:
                    print 'wrong idx:%d' % idx
                    import traceback
                    traceback.print_exc()
                    db_inds.append(self._get_next_ind())
                    continue
                if type(img) == type(None):
                    db_inds.append(self._get_next_ind())
                    continue

                # img = np.ones(img.shape)
                # label = np.ones(label.shape)
                # coord = np.ones(coord.shape)
                processed_ims.append(img)
                labels.append(label)
                coords.append(coord)

            blobs = get_minibatch(processed_ims, labels, coords)
            self._queue.put(blobs)
예제 #33
0
    def __getitem__(self, index):
        # get the anchor index for current sample index
        item = self._roidb[index]
        blobs = get_minibatch([item], self.phase)
        data = torch.from_numpy(blobs['data'])
        length, height, width = data.shape[-3:]
        data = data.contiguous().view(3, length, height, width)

        gt_windows = torch.from_numpy(blobs['gt_windows'])
        gt_windows_padding = gt_windows.new(self.max_num_box,
                                            gt_windows.size(1)).zero_()
        num_gt = min(gt_windows.size(0), self.max_num_box)
        gt_windows_padding[:num_gt, :] = gt_windows[:num_gt]

        if self.phase == 'test':
            video_info = ''
            for key, value in item.items():
                video_info = video_info + " {}: {}\n".format(key, value)
            # drop the last "\n"
            video_info = video_info[:-1]
            return data, gt_windows_padding, num_gt, video_info
        else:
            return data, gt_windows_padding, num_gt
예제 #34
0
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        if cfg.TRAIN.USE_PREFETCH:
            return self._blob_queue.get()
        else:
            if cfg.TRAIN.LABEL_SHUFFLING:
                cla_inds = self._get_next_classes_inds()
                db_inds = self._get_next_classes_minibatch_inds(cla_inds)
                minibatch_db = [self._classes_roidb[cla_inds][i] for i in db_inds]
            else:
                db_inds = self._get_next_minibatch_inds()
                minibatch_db = [self._roidb[i] for i in db_inds]
            
            if cfg.TRAIN.USE_OHEM:
                blobs = get_allrois_minibatch(minibatch_db, self._num_classes)
            else:
                blobs = get_minibatch(minibatch_db, self._num_classes)

            return blobs
예제 #35
0
    def train_batch(self, index):
        """ Retrieves a train-mode batch.
       Args:
           index: The index of the batch to get.
       Returns:
           index (int): The index of the retrived batch. (Useful when getting
               batches via next())
           image (Torch Tensor): Normalized image data, with dims:
               (batch_index, C, H, W).
           im_rois (Torch Tensor): Regions of interest for this image, rescaled
                to the 'image' size. With dims: (rois_index, x1, y2, x2, y2).
           bbox_targets (Torch Tensor): The targets for each RoI. With dims:
                (roi_index, num_classes * 4).
           labels (Torch Tensor): The GT label for each ROI. With dims:
                (roi_index, 1).
           bbox_inside_weights (Torch Tensor): Which part of 'bbox_targets'
                applies to the roi. Each row is all 0 except the 4 targets that
                apply to the roi. With dims: (roi_index, num_classes * 4).
       """
        batch_index = self._sample_indexes[index]
        sample = self.samples[batch_index]
        batch = get_minibatch([sample], self.num_classes)

        # shuffle rois, targets and labels
        shuffle_indexes = np.random.permutation(batch['rois'].shape[0])
        im_rois = batch['rois'][shuffle_indexes].astype(float)
        bbox_targets = batch['bbox_targets'][shuffle_indexes]
        bbox_inside_weights = batch['bbox_inside_weights'][shuffle_indexes]
        labels = batch['labels'][shuffle_indexes].astype(float)

        # This was for the other vgg
        # image = batch['data']
        image_pil = self.get_image(sample)
        image_arr = np.asarray(image_pil, dtype='uint8')
        image = normalize_images([image_arr])

        return (index, image, im_rois, bbox_targets, labels, bbox_inside_weights)
예제 #36
0
    def __getitem__(self, index):
        if self.training:
            index_ratio = int(self.ratio_index[index])
        else:
            index_ratio = index

        # get the anchor index for current sample index
        # here we set the anchor index to the last one
        # sample in this group
        minibatch_db = [self._roidb[index_ratio]]
        blobs = get_minibatch(minibatch_db, self._num_classes, self.target)
        if not self.target:
            data = torch.from_numpy(blobs['data'])
        else:
            data = torch.from_numpy(blobs['s'])
            t = torch.from_numpy(blobs['t'])

        im_info = torch.from_numpy(blobs['im_info'])
        # we need to random shuffle the bounding box.
        data_height, data_width = data.size(1), data.size(2)
        if self.training:
            np.random.shuffle(blobs['gt_boxes'])
            gt_boxes = torch.from_numpy(blobs['gt_boxes'])

            ########################################################
            # padding the input image to fixed size for each group #
            ########################################################

            # NOTE1: need to cope with the case where a group cover both conditions. (done)
            # NOTE2: need to consider the situation for the tail samples. (no worry)
            # NOTE3: need to implement a parallel data loader. (no worry)
            # get the index range

            # if the image need to crop, crop to the target size.
            ratio = self.ratio_list_batch[index]

            if self._roidb[index_ratio]['need_crop']:
                if ratio < 1:
                    # this means that data_width << data_height, we need to crop the
                    # data_height
                    min_y = int(torch.min(gt_boxes[:, 1]))
                    max_y = int(torch.max(gt_boxes[:, 3]))
                    trim_size = int(np.floor(data_width / ratio))
                    if trim_size > data_height:
                        trim_size = data_height
                    box_region = max_y - min_y + 1
                    if min_y == 0:
                        y_s = 0
                    else:
                        if (box_region - trim_size) < 0:
                            y_s_min = max(max_y - trim_size, 0)
                            y_s_max = min(min_y, data_height - trim_size)
                            if y_s_min == y_s_max:
                                y_s = y_s_min
                            else:
                                y_s = np.random.choice(range(y_s_min, y_s_max))
                        else:
                            y_s_add = int((box_region - trim_size) / 2)
                            if y_s_add == 0:
                                y_s = min_y
                            else:
                                y_s = np.random.choice(range(min_y, min_y + y_s_add))
                    # crop the image
                    data = data[:, y_s:(y_s + trim_size), :, :]
                    if self.target:
                        t = t[:, y_s:(y_s + trim_size), :, :]
                    # shift y coordiante of gt_boxes
                    gt_boxes[:, 1] = gt_boxes[:, 1] - float(y_s)
                    gt_boxes[:, 3] = gt_boxes[:, 3] - float(y_s)

                    # update gt bounding box according the trip
                    gt_boxes[:, 1].clamp_(0, trim_size - 1)
                    gt_boxes[:, 3].clamp_(0, trim_size - 1)

                else:
                    # this means that data_width >> data_height, we need to crop the
                    # data_width
                    min_x = int(torch.min(gt_boxes[:, 0]))
                    max_x = int(torch.max(gt_boxes[:, 2]))
                    trim_size = int(np.ceil(data_height * ratio))
                    if trim_size > data_width:
                        trim_size = data_width
                    box_region = max_x - min_x + 1
                    if min_x == 0:
                        x_s = 0
                    else:
                        if (box_region - trim_size) < 0:
                            x_s_min = max(max_x - trim_size, 0)
                            x_s_max = min(min_x, data_width - trim_size)
                            if x_s_min == x_s_max:
                                x_s = x_s_min
                            else:
                                x_s = np.random.choice(range(x_s_min, x_s_max))
                        else:
                            x_s_add = int((box_region - trim_size) / 2)
                            if x_s_add == 0:
                                x_s = min_x
                            else:
                                x_s = np.random.choice(range(min_x, min_x + x_s_add))
                    # crop the image
                    data = data[:, :, x_s:(x_s + trim_size), :]
                    if self.target:
                        t = t[:, :, x_s:(x_s + trim_size), :]

                    # shift x coordiante of gt_boxes
                    gt_boxes[:, 0] = gt_boxes[:, 0] - float(x_s)
                    gt_boxes[:, 2] = gt_boxes[:, 2] - float(x_s)
                    # update gt bounding box according the trip
                    gt_boxes[:, 0].clamp_(0, trim_size - 1)
                    gt_boxes[:, 2].clamp_(0, trim_size - 1)

            # based on the ratio, padding the image.
            if ratio < 1:
                # this means that data_width < data_height
                trim_size = int(np.floor(data_width / ratio))

                padding_data = torch.FloatTensor(int(np.ceil(data_width / ratio)), \
                                                 data_width, 3).zero_()

                padding_data[:data_height, :, :] = data[0]
                if self.target:
                    padding_t = torch.FloatTensor(int(np.ceil(data_width / ratio)), \
                                                     data_width, 3).zero_()
                    padding_t[:data_height, :, :] = t[0]

                # update im_info
                im_info[0, 0] = padding_data.size(0)
                # print("height %d %d \n" %(index, anchor_idx))
            elif ratio > 1:
                # this means that data_width > data_height
                # if the image need to crop.
                padding_data = torch.FloatTensor(data_height, \
                                                 int(np.ceil(data_height * ratio)), 3).zero_()
                padding_data[:, :data_width, :] = data[0]
                if self.target:
                    padding_t = torch.FloatTensor(data_height, \
                                                     int(np.ceil(data_height * ratio)), 3).zero_()
                    padding_t[:, :data_width, :] = t[0]

                im_info[0, 1] = padding_data.size(1)
            else:
                trim_size = min(data_height, data_width)
                padding_data = torch.FloatTensor(trim_size, trim_size, 3).zero_()
                padding_data = data[0][:trim_size, :trim_size, :]

                if self.target:
                    padding_t = torch.FloatTensor(trim_size, trim_size, 3).zero_()
                    padding_t = t[0][:trim_size, :trim_size, :]

                gt_boxes[:, :4].clamp_(0, trim_size)
                im_info[0, 0] = trim_size
                im_info[0, 1] = trim_size

            # check the bounding box:
            not_keep = (gt_boxes[:, 0] == gt_boxes[:, 2]) | (gt_boxes[:, 1] == gt_boxes[:, 3])
            keep = torch.nonzero(not_keep == 0).view(-1)

            gt_boxes_padding = torch.FloatTensor(self.max_num_box, gt_boxes.size(1)).zero_()
            if keep.numel() != 0:
                gt_boxes = gt_boxes[keep]
                num_boxes = min(gt_boxes.size(0), self.max_num_box)
                gt_boxes_padding[:num_boxes, :] = gt_boxes[:num_boxes]
            else:
                num_boxes = 0

                # permute trim_data to adapt to downstream processing
            padding_data = padding_data.permute(2, 0, 1).contiguous()
            if self.target:
                padding_t = padding_t.permute(2, 0, 1).contiguous()
            im_info = im_info.view(3)

            if self.target:
                return padding_data, padding_t, im_info, gt_boxes_padding, num_boxes
            else:
                return padding_data, im_info, gt_boxes_padding, num_boxes
        else:
            data = data.permute(0, 3, 1, 2).contiguous().view(3, data_height, data_width)
            im_info = im_info.view(3)

            gt_boxes = torch.FloatTensor([1, 1, 1, 1, 1])
            num_boxes = 0

            return data, im_info, gt_boxes, num_boxes
         # 5. class label
         image_label_data = np.zeros(((64 * len(roidb), 1, 1, 1)), dtype=np.float32)
         
         tic = timeit.default_timer()
         while cur < len(index):
             # do the sampling work
             if cur % 100 == 2:
                 print('Processing the {} th image data'.format(cur))
                 toc = timeit.default_timer()
                 
                 print('Time spent on processing 100 image is {}'.format(toc - tic))
                 tic = timeit.default_timer()
                     
             db_inds = index[cur: cur + cfg.TRAIN.IMS_PER_BATCH]            
             cur_db = [roidb[i] for i in db_inds]
             blob = get_minibatch(cur_db, cfg.HDF5_NUM_CLASS + 1, cfg.HDF5_NUM_LABEL)
 
             # 2. multi_label blob               
             if not args.merge:
                 sleeve_data[cur * cfg.TRAIN.BATCH_SIZE / 2: \
                     (cur + 2) * cfg.TRAIN.BATCH_SIZE / 2, :, :, :] = \
                     blob['sleeve'].reshape(cfg.TRAIN.BATCH_SIZE, 1, 1, 1)
                 texture_data[cur * cfg.TRAIN.BATCH_SIZE / 2: \
                     (cur + 2) * cfg.TRAIN.BATCH_SIZE / 2, :, :, :] = \
                     blob['texture'].reshape(cfg.TRAIN.BATCH_SIZE, 1, 1, 1)
                 neckband_data[cur * cfg.TRAIN.BATCH_SIZE / 2: \
                     (cur + 2) * cfg.TRAIN.BATCH_SIZE / 2, :, :, :] = \
                     blob['neckband'].reshape(cfg.TRAIN.BATCH_SIZE, 1, 1, 1)
                     
             # 4. bbox_tartgets, bbox_loss_weight
             bbox_targets_data[cur * cfg.TRAIN.BATCH_SIZE / 2: \
예제 #38
0
            tic = timeit.default_timer()
            while cur < len(index):
                # do the sampling work
                if cur % 100 == 2:
                    print('Processing the {} th image data'.format(cur))
                    toc = timeit.default_timer()

                    print(
                        'Time spent on processing 100 image is {}'.format(toc -
                                                                          tic))
                    tic = timeit.default_timer()

                db_inds = index[cur:cur + cfg.TRAIN.IMS_PER_BATCH]
                cur_db = [roidb[i] for i in db_inds]
                blob = get_minibatch(cur_db, cfg.HDF5_NUM_CLASS + 1,
                                     cfg.HDF5_NUM_LABEL)

                # 2. multi_label blob
                if not args.merge:
                    sleeve_data[cur * cfg.TRAIN.BATCH_SIZE / 2: \
                        (cur + 2) * cfg.TRAIN.BATCH_SIZE / 2, :, :, :] = \
                        blob['sleeve'].reshape(cfg.TRAIN.BATCH_SIZE, 1, 1, 1)
                    texture_data[cur * cfg.TRAIN.BATCH_SIZE / 2: \
                        (cur + 2) * cfg.TRAIN.BATCH_SIZE / 2, :, :, :] = \
                        blob['texture'].reshape(cfg.TRAIN.BATCH_SIZE, 1, 1, 1)
                    neckband_data[cur * cfg.TRAIN.BATCH_SIZE / 2: \
                        (cur + 2) * cfg.TRAIN.BATCH_SIZE / 2, :, :, :] = \
                        blob['neckband'].reshape(cfg.TRAIN.BATCH_SIZE, 1, 1, 1)

                # 4. bbox_tartgets, bbox_loss_weight
                bbox_targets_data[cur * cfg.TRAIN.BATCH_SIZE / 2: \
예제 #39
0
 def _get_next_minibatch(self):
     """Return the blobs to be used for the next minibatch."""
     db_inds = self._get_next_minibatch_inds()
     minibatch_db = [self._roidb[i] for i in db_inds]
     return get_minibatch(minibatch_db, self._num_classes)
예제 #40
0
    def __getitem__(self, index):
        ##############################
        # in training, for example, the index feed to dataloader may be n1
        # but the img ID may be n2, due to the order of imgs is sorted by ratios
        # so we need to get the true index by self.ratio_index[index]
        ##############################
        if self.training:
            index_ratio = int(self.ratio_index[index])
        else:
            index_ratio = index

        # though it is called minibatch, in fact it contains only one img here
        minibatch_db = [self._roidb[index_ratio]]
        blobs = get_minibatch(minibatch_db)  # [n_box, 5]
        data = torch.from_numpy(blobs['data'])
        im_info = torch.from_numpy(blobs['im_info'])  # (H, W, scale)
        data_height, data_width = data.size(1), data.size(2)  # [1, h, w, c]
        gt_boxes = blobs['gt_boxes']

        #################
        # support data
        #################
        target_size = self.support_im_size
        support_data_all = np.zeros((self.support_way * self.support_shot, 3,
                                     target_size, target_size),
                                    dtype=np.float32)
        # positive supports
        cls_in_query = []
        for i in range(gt_boxes.shape[0]):
            _cls = gt_boxes[i, 4]
            cls_in_query.append(_cls)
        cls_in_query = list(set(cls_in_query))
        pos_cls_idx = int(random.sample(cls_in_query, k=1)[0])
        support_dbs = random.sample(self.support_db[pos_cls_idx],
                                    k=self.support_shot)
        for i, sup_db in enumerate(support_dbs):
            support_roidb = self._roidb[sup_db['roidb_idx']]
            support_box = sup_db['box']
            support_blob = get_minibatch([support_roidb])
            support_im = support_blob['data'][0]
            support_im_h, support_im_w = support_im.shape[0], support_im.shape[
                1]
            support_scale = support_blob['im_info'][0][2]
            support_box = (support_box * support_scale).astype(np.int16)
            support_im_final = np.zeros((3, target_size, target_size),
                                        dtype=np.float32)
            x_min, y_min = support_box[0], support_box[1]
            x_max, y_max = support_box[2], support_box[3]
            box_h, box_w = y_max - y_min, x_max - x_min
            support_im_cropped = support_im[y_min:y_max + 1,
                                            x_min:x_max + 1, :]
            if box_h > box_w:
                resize_scale = float(target_size) / float(box_h)
                unfit_size = int(box_w * resize_scale)
                support_im_cropped = cv2.resize(support_im_cropped,
                                                (unfit_size, target_size),
                                                interpolation=cv2.INTER_LINEAR)
            else:
                resize_scale = float(target_size) / float(box_w)
                unfit_size = int(box_h * resize_scale)
                support_im_cropped = cv2.resize(support_im_cropped,
                                                (target_size, unfit_size),
                                                interpolation=cv2.INTER_LINEAR)

            cropped_h = support_im_cropped.shape[0]
            cropped_w = support_im_cropped.shape[1]
            support_im_final[:, :cropped_h, :cropped_w] = np.transpose(
                support_im_cropped, (2, 0, 1))
            support_data_all[i] = support_im_final

        # negative supports
        if self.support_way != 1:
            neg_cls = []
            for cls_id in range(1, self._num_classes):
                if cls_id not in cls_in_query:
                    neg_cls.append(cls_id)
            neg_cls_idx = random.sample(neg_cls, k=1)[0]
            neg_support_dbs = random.sample(self.support_db[neg_cls_idx],
                                            k=self.support_shot)

            for i, sup_db in enumerate(neg_support_dbs):
                support_roidb = self._roidb[sup_db['roidb_idx']]
                support_box = sup_db['box']
                support_blob = get_minibatch([support_roidb])
                support_im = support_blob['data'][0]
                support_im_h, support_im_w = support_im.shape[
                    0], support_im.shape[1]
                support_scale = support_blob['im_info'][0][2]
                support_box = (support_box * support_scale).astype(np.int16)
                support_im_final = np.zeros((3, target_size, target_size),
                                            dtype=np.float32)
                x_min, y_min = support_box[0], support_box[1]
                x_max, y_max = support_box[2], support_box[3]
                box_h, box_w = y_max - y_min, x_max - x_min
                support_im_cropped = support_im[y_min:y_max + 1,
                                                x_min:x_max + 1, :]
                if box_h > box_w:
                    resize_scale = float(target_size) / float(box_h)
                    unfit_size = int(box_w * resize_scale)
                    support_im_cropped = cv2.resize(
                        support_im_cropped, (unfit_size, target_size),
                        interpolation=cv2.INTER_LINEAR)
                else:
                    resize_scale = float(target_size) / float(box_w)
                    unfit_size = int(box_h * resize_scale)
                    support_im_cropped = cv2.resize(
                        support_im_cropped, (target_size, unfit_size),
                        interpolation=cv2.INTER_LINEAR)

                cropped_h = support_im_cropped.shape[0]
                cropped_w = support_im_cropped.shape[1]
                support_im_final[:, :cropped_h, :cropped_w] = np.transpose(
                    support_im_cropped, (2, 0, 1))
                support_data_all[i + self.support_shot] = support_im_final

        support = torch.from_numpy(support_data_all)

        #################
        # query data
        #################
        # padding the input image to fixed size for each group
        # if the image need to crop, crop to the target size.
        np.random.shuffle(blobs['gt_boxes'])
        gt_boxes = torch.from_numpy(blobs['gt_boxes'])
        ratio = self.ratio_list_batch[index]
        if self._roidb[index_ratio]['need_crop']:
            if ratio < 1:
                # this means that data_width << data_height, we need to crop the data_height
                min_y = int(torch.min(gt_boxes[:, 1]))
                max_y = int(torch.max(gt_boxes[:, 3]))
                trim_size = int(np.floor(data_width / ratio))
                if trim_size > data_height:
                    trim_size = data_height
                box_region = max_y - min_y + 1
                if min_y == 0:
                    y_s = 0
                else:
                    if (box_region - trim_size) < 0:
                        y_s_min = max(max_y - trim_size, 0)
                        y_s_max = min(min_y, data_height - trim_size)
                        if y_s_min == y_s_max:
                            y_s = y_s_min
                        else:
                            y_s = np.random.choice(range(y_s_min, y_s_max))
                    else:
                        y_s_add = int((box_region - trim_size) / 2)
                        if y_s_add == 0:
                            y_s = min_y
                        else:
                            y_s = np.random.choice(
                                range(min_y, min_y + y_s_add))
                # crop the image
                data = data[:, y_s:(y_s + trim_size), :, :]

                # shift y coordiante of gt_boxes
                gt_boxes[:, 1] = gt_boxes[:, 1] - float(y_s)
                gt_boxes[:, 3] = gt_boxes[:, 3] - float(y_s)

                # update gt bounding box according the trip
                gt_boxes[:, 1].clamp_(0, trim_size - 1)
                gt_boxes[:, 3].clamp_(0, trim_size - 1)

            else:
                # this means that data_width >> data_height, we need to crop the
                # data_width
                min_x = int(torch.min(gt_boxes[:, 0]))
                max_x = int(torch.max(gt_boxes[:, 2]))
                trim_size = int(np.ceil(data_height * ratio))
                if trim_size > data_width:
                    trim_size = data_width
                box_region = max_x - min_x + 1
                if min_x == 0:
                    x_s = 0
                else:
                    if (box_region - trim_size) < 0:
                        x_s_min = max(max_x - trim_size, 0)
                        x_s_max = min(min_x, data_width - trim_size)
                        if x_s_min == x_s_max:
                            x_s = x_s_min
                        else:
                            x_s = np.random.choice(range(x_s_min, x_s_max))
                    else:
                        x_s_add = int((box_region - trim_size) / 2)
                        if x_s_add == 0:
                            x_s = min_x
                        else:
                            x_s = np.random.choice(
                                range(min_x, min_x + x_s_add))
                # crop the image
                data = data[:, :, x_s:(x_s + trim_size), :]

                # shift x coordiante of gt_boxes
                gt_boxes[:, 0] = gt_boxes[:, 0] - float(x_s)
                gt_boxes[:, 2] = gt_boxes[:, 2] - float(x_s)
                # update gt bounding box according the trip
                gt_boxes[:, 0].clamp_(0, trim_size - 1)
                gt_boxes[:, 2].clamp_(0, trim_size - 1)

        # no need to crop, or after cropping
        # based on the ratio, padding the image.
        if ratio < 1:
            # this means that data_width < data_height
            trim_size = int(np.floor(data_width / ratio))

            padding_data = torch.FloatTensor(int(np.ceil(data_width / ratio)), \
                                                data_width, 3).zero_()
            padding_data[:data_height, :, :] = data[0]
            # update im_info [[H, W, scale]]
            im_info[0, 0] = padding_data.size(0)
            # print("height %d %d \n" %(index, anchor_idx))
        elif ratio > 1:
            # this means that data_width > data_height
            # if the image need to crop.
            padding_data = torch.FloatTensor(data_height, \
                                                int(np.ceil(data_height * ratio)), 3).zero_()
            padding_data[:, :data_width, :] = data[0]
            im_info[0, 1] = padding_data.size(1)
        else:
            trim_size = min(data_height, data_width)
            padding_data = torch.FloatTensor(trim_size, trim_size, 3).zero_()
            padding_data = data[0][:trim_size, :trim_size, :]
            # gt_boxes.clamp_(0, trim_size)
            gt_boxes[:, :4].clamp_(0, trim_size)
            im_info[0, 0] = trim_size
            im_info[0, 1] = trim_size

        # filt boxes
        fs_gt_boxes = []
        for i in range(gt_boxes.shape[0]):
            if gt_boxes[i, 4] == pos_cls_idx:
                fs_gt_boxes += [gt_boxes[i]]
        fs_gt_boxes = torch.stack(fs_gt_boxes, 0)
        fs_gt_boxes[:, 4] = 1.

        # check the bounding box:
        not_keep = (gt_boxes[:, 0] == gt_boxes[:, 2]) | (gt_boxes[:, 1]
                                                         == gt_boxes[:, 3])
        keep = torch.nonzero(not_keep == 0).view(-1)

        gt_boxes_padding = torch.FloatTensor(self.max_num_box,
                                             gt_boxes.size(1)).zero_()
        if keep.numel() != 0:
            gt_boxes = gt_boxes[keep]
            num_boxes = min(gt_boxes.size(0), self.max_num_box)
            gt_boxes_padding[:num_boxes, :] = gt_boxes[:num_boxes]
        else:
            num_boxes = 0

        #
        not_keep = (fs_gt_boxes[:, 0] == fs_gt_boxes[:, 2]) | (
            fs_gt_boxes[:, 1] == fs_gt_boxes[:, 3])
        keep = torch.nonzero(not_keep == 0).view(-1)

        fs_gt_boxes_padding = torch.FloatTensor(self.max_num_box,
                                                fs_gt_boxes.size(1)).zero_()
        if keep.numel() != 0:
            fs_gt_boxes = fs_gt_boxes[keep]
            num_boxes = min(fs_gt_boxes.size(0), self.max_num_box)
            fs_gt_boxes_padding[:num_boxes, :] = fs_gt_boxes[:num_boxes]
        else:
            num_boxes = 0

        # permute trim_data to adapt to downstream processing
        padding_data = padding_data.permute(2, 0, 1).contiguous()
        im_info = im_info.view(3)

        # to sum up, data in diffenent batches may have different size,
        # but will have same size in the same batch

        return padding_data, im_info, fs_gt_boxes_padding, num_boxes, support_data_all, gt_boxes_padding
예제 #41
0
  def __getitem__(self, index):
    if self.training:
        index_ratio = int(self.ratio_index[index])
    else:
        index_ratio = index

    # get the anchor index for current sample index
    # here we set the anchor index to the last one
    # sample in this group
    minibatch_db = [self._roidb[index_ratio]]
    blobs = get_minibatch(minibatch_db, self._num_classes)
    data = torch.from_numpy(blobs['data'])
    im_info = torch.from_numpy(blobs['im_info'])
    # we need to random shuffle the bounding box.
    data_height, data_width = data.size(1), data.size(2)
    if self.training:
        np.random.shuffle(blobs['gt_boxes'])
        gt_boxes = torch.from_numpy(blobs['gt_boxes'])

        ########################################################
        # padding the input image to fixed size for each group #
        ########################################################

        # NOTE1: need to cope with the case where a group cover both conditions. (done)
        # NOTE2: need to consider the situation for the tail samples. (no worry)
        # NOTE3: need to implement a parallel data loader. (no worry)
        # get the index range

        # if the image need to crop, crop to the target size.
        ratio = self.ratio_list_batch[index]

        if self._roidb[index_ratio]['need_crop']:
            if ratio < 1:
                # this means that data_width << data_height, we need to crop the
                # data_height
                min_y = int(torch.min(gt_boxes[:,1]))
                max_y = int(torch.max(gt_boxes[:,3]))
                trim_size = int(np.floor(data_width / ratio))
                if trim_size > data_height:
                    trim_size = data_height                
                box_region = max_y - min_y + 1
                if min_y == 0:
                    y_s = 0
                else:
                    if (box_region-trim_size) < 0:
                        y_s_min = max(max_y-trim_size, 0)
                        y_s_max = min(min_y, data_height-trim_size)
                        if y_s_min == y_s_max:
                            y_s = y_s_min
                        else:
                            y_s = np.random.choice(range(y_s_min, y_s_max))
                    else:
                        y_s_add = int((box_region-trim_size)/2)
                        if y_s_add == 0:
                            y_s = min_y
                        else:
                            y_s = np.random.choice(range(min_y, min_y+y_s_add))
                # crop the image
                data = data[:, y_s:(y_s + trim_size), :, :]

                # shift y coordiante of gt_boxes
                gt_boxes[:, 1] = gt_boxes[:, 1] - float(y_s)
                gt_boxes[:, 3] = gt_boxes[:, 3] - float(y_s)

                # update gt bounding box according the trip
                gt_boxes[:, 1].clamp_(0, trim_size - 1)
                gt_boxes[:, 3].clamp_(0, trim_size - 1)

            else:
                # this means that data_width >> data_height, we need to crop the
                # data_width
                min_x = int(torch.min(gt_boxes[:,0]))
                max_x = int(torch.max(gt_boxes[:,2]))
                trim_size = int(np.ceil(data_height * ratio))
                if trim_size > data_width:
                    trim_size = data_width                
                box_region = max_x - min_x + 1
                if min_x == 0:
                    x_s = 0
                else:
                    if (box_region-trim_size) < 0:
                        x_s_min = max(max_x-trim_size, 0)
                        x_s_max = min(min_x, data_width-trim_size)
                        if x_s_min == x_s_max:
                            x_s = x_s_min
                        else:
                            x_s = np.random.choice(range(x_s_min, x_s_max))
                    else:
                        x_s_add = int((box_region-trim_size)/2)
                        if x_s_add == 0:
                            x_s = min_x
                        else:
                            x_s = np.random.choice(range(min_x, min_x+x_s_add))
                # crop the image
                data = data[:, :, x_s:(x_s + trim_size), :]

                # shift x coordiante of gt_boxes
                gt_boxes[:, 0] = gt_boxes[:, 0] - float(x_s)
                gt_boxes[:, 2] = gt_boxes[:, 2] - float(x_s)
                # update gt bounding box according the trip
                gt_boxes[:, 0].clamp_(0, trim_size - 1)
                gt_boxes[:, 2].clamp_(0, trim_size - 1)

        # based on the ratio, padding the image.
        if ratio < 1:
            # this means that data_width < data_height
            trim_size = int(np.floor(data_width / ratio))

            padding_data = torch.FloatTensor(int(np.ceil(data_width / ratio)), \
                                             data_width, 3).zero_()

            padding_data[:data_height, :, :] = data[0]
            # update im_info
            im_info[0, 0] = padding_data.size(0)
            # print("height %d %d \n" %(index, anchor_idx))
        elif ratio > 1:
            # this means that data_width > data_height
            # if the image need to crop.
            padding_data = torch.FloatTensor(data_height, \
                                             int(np.ceil(data_height * ratio)), 3).zero_()
            padding_data[:, :data_width, :] = data[0]
            im_info[0, 1] = padding_data.size(1)
        else:
            trim_size = min(data_height, data_width)
            padding_data = torch.FloatTensor(trim_size, trim_size, 3).zero_()
            padding_data = data[0][:trim_size, :trim_size, :]
            # gt_boxes.clamp_(0, trim_size)
            gt_boxes[:, :4].clamp_(0, trim_size)
            im_info[0, 0] = trim_size
            im_info[0, 1] = trim_size


        # check the bounding box:
        not_keep = (gt_boxes[:,0] == gt_boxes[:,2]) | (gt_boxes[:,1] == gt_boxes[:,3])
        keep = torch.nonzero(not_keep == 0).view(-1)

        gt_boxes_padding = torch.FloatTensor(self.max_num_box, gt_boxes.size(1)).zero_()
        if keep.numel() != 0:
            gt_boxes = gt_boxes[keep]
            num_boxes = min(gt_boxes.size(0), self.max_num_box)
            gt_boxes_padding[:num_boxes,:] = gt_boxes[:num_boxes]
        else:
            num_boxes = 0

            # permute trim_data to adapt to downstream processing
        padding_data = padding_data.permute(2, 0, 1).contiguous()
        im_info = im_info.view(3)

        return padding_data, im_info, gt_boxes_padding, num_boxes
    else:
        data = data.permute(0, 3, 1, 2).contiguous().view(3, data_height, data_width)
        im_info = im_info.view(3)

        gt_boxes = torch.FloatTensor([1,1,1,1,1])
        num_boxes = 0

        return data, im_info, gt_boxes, num_boxes
예제 #42
0
 def _get_next_minibatch(self):
     db_inds = self._get_next_minibatch_inds()
     minibatch_db = [self._roidb[i] for i in db_inds]
     return get_minibatch(minibatch_db,self._num_classes)
예제 #43
0
    def __getitem__(self, index):
        if self.training:
            index_ratio = int(self.ratio_index[index])
        else:
            index_ratio = index

        # get the anchor index for current sample index
        # here we set the anchor index to the last one
        # sample in this group
        minibatch_db = [self._roidb[index_ratio]]
        blobs = get_minibatch(minibatch_db, self._num_classes)

        data_left = torch.from_numpy(blobs['data_left'])
        data_right = torch.from_numpy(blobs['data_right'])
        im_info = torch.from_numpy(blobs['im_info'])

        if self.training:
            boxes_all = np.concatenate((blobs['gt_boxes_left'], blobs['gt_boxes_right'],blobs['gt_boxes_merge'],\
                                        blobs['gt_dim_orien'], blobs['gt_kpts']), axis=1)

            np.random.shuffle(boxes_all)

            gt_boxes_left = torch.from_numpy(boxes_all[:, 0:5])
            gt_boxes_right = torch.from_numpy(boxes_all[:, 5:10])
            gt_boxes_merge = torch.from_numpy(boxes_all[:, 10:15])
            gt_dim_orien = torch.from_numpy(boxes_all[:, 15:20])
            gt_kpts = torch.from_numpy(boxes_all[:, 20:26])

            num_boxes = min(gt_boxes_left.size(0), self.max_num_box)

            data_left = data_left[0].permute(2, 0, 1).contiguous()
            data_right = data_right[0].permute(2, 0, 1).contiguous()
            im_info = im_info.view(3)

            gt_boxes_left_padding = torch.FloatTensor(
                self.max_num_box, gt_boxes_left.size(1)).zero_()
            gt_boxes_left_padding[:num_boxes, :] = gt_boxes_left[:num_boxes]
            gt_boxes_left_padding = gt_boxes_left_padding.contiguous()

            gt_boxes_right_padding = torch.FloatTensor(
                self.max_num_box, gt_boxes_right.size(1)).zero_()
            gt_boxes_right_padding[:num_boxes, :] = gt_boxes_right[:num_boxes]
            gt_boxes_right_padding = gt_boxes_right_padding.contiguous()

            gt_boxes_merge_padding = torch.FloatTensor(
                self.max_num_box, gt_boxes_merge.size(1)).zero_()
            gt_boxes_merge_padding[:num_boxes, :] = gt_boxes_merge[:num_boxes]
            gt_boxes_merge_padding = gt_boxes_merge_padding.contiguous()

            gt_dim_orien_padding = torch.FloatTensor(
                self.max_num_box, gt_dim_orien.size(1)).zero_()
            gt_dim_orien_padding[:num_boxes, :] = gt_dim_orien[:num_boxes]
            gt_dim_orien_padding = gt_dim_orien_padding.contiguous()

            gt_kpts_padding = torch.FloatTensor(self.max_num_box,
                                                gt_kpts.size(1)).zero_()
            gt_kpts_padding[:num_boxes, :] = gt_kpts[:num_boxes]
            gt_kpts_padding = gt_kpts_padding.contiguous()

            return data_left, data_right, im_info, gt_boxes_left_padding, gt_boxes_right_padding,\
                   gt_boxes_merge_padding, gt_dim_orien_padding, gt_kpts_padding, num_boxes
        else:
            data_left = data_left[0].permute(2, 0, 1).contiguous()
            data_right = data_right[0].permute(2, 0, 1).contiguous()
            im_info = im_info.view(3)

            gt_boxes = torch.FloatTensor([1, 1, 1, 1, 1])
            num_boxes = 0

            return data_left, data_right, im_info, gt_boxes, gt_boxes, gt_boxes, gt_boxes, gt_boxes, num_boxes
 def _get_next_minibatch(self):
     """Return the blobs to be used for the next minibatch."""
     db_inds = self._get_next_minibatch_inds()
     minibatch_db = [self._roidb[i] for i in db_inds]
     return get_minibatch(minibatch_db, self._num_classes)