Exemplo n.º 1
0
    def _get_roidb(self, index):
        img_path = op.join(self.data_dir, self.dataset[index])
        targets = self.annotations[index]

        roidb = {}
        gt_boxes = np.zeros((0, 4), dtype=np.float32)
        gt_classes = np.zeros(0, dtype=np.int32)

        image = cv2.imread(img_path)

        h, w = image.shape[0:2]
        image = cv2.resize(image, (self.im_w, self.im_h),
                           interpolation=cv2.INTER_LINEAR)
        nh, nw = image.shape[0:2]

        yscale = 1.0 * nh / h
        xscale = 1.0 * nw / w

        for obj in targets:
            bbox = np.zeros(4, dtype=np.float32)

            bbox_attribs = obj.find('box').attrib
            attribute_attribs = obj.find('attribute').attrib

            left = float(bbox_attribs['left'])
            top = float(bbox_attribs['top'])
            width = float(bbox_attribs['width'])
            height = float(bbox_attribs['height'])

            bbox[0] = left
            bbox[1] = top
            bbox[2] = left + width - 1
            bbox[3] = top + height - 1

            bbox[[0, 2]] *= xscale
            bbox[[1, 3]] *= yscale
            cat = attribute_attribs['vehicle_type']
            if cat == 'others':
                cat = 'truck'
            cat_ind = CAT_IND_MAP[cat]

            gt_boxes = np.append(gt_boxes, bbox.reshape(-1, 4), axis=0)
            gt_classes = np.append(gt_classes, cat_ind)

        flip_prob = np.random.rand()
        if flip_prob > 0.5:
            image, gt_boxes = self.flip(image, gt_boxes)
        roidb['data'] = image[np.newaxis, :, :, :].astype(np.float32)
        roidb['gt_boxes'] = gt_boxes
        roidb['gt_classes'] = gt_classes

        roidb['bound'] = self.bound

        bbox_overlaps = U.bbox_overlaps_per_image(self.anchors, gt_boxes)

        roidb['anchors'] = self.anchors
        roidb['bbox_overlaps'] = bbox_overlaps

        return roidb
Exemplo n.º 2
0
def best_search_box_train(temp_box, det_box, templates, raw_anchors, bound, K,
                          size, fg_thresh):
    tmpl_sz = templates[:, 0]

    x1, y1, x2, y2 = temp_box[:]
    tw, th = x2 - x1 + 1, y2 - y1 + 1
    cx, cy = x1 + 0.5 * tw, y1 + 0.5 * th

    ind1 = np.where(np.bitwise_and(tmpl_sz >= tw, tmpl_sz >= th) == 1)[0]
    _templates = templates[ind1]

    overlaps_list = []
    tmpls = []
    shift_tmpls = []
    anchors_list = []
    for tmpl in _templates:
        sz_half = tmpl[0] * 0.5
        cx = min(max(cx, sz_half), bound[0] - sz_half)
        cy = min(max(cy, sz_half), bound[1] - sz_half)
        shift_tmpl = np.array(
            [cx - sz_half, cy - sz_half, cx + sz_half, cy + sz_half],
            dtype=np.float32)
        _anchors = gen_region_anchors(raw_anchors, shift_tmpl.reshape(1, -1),
                                      bound, K, size)[0]
        overlaps = bbox_overlaps_per_image(_anchors,
                                           det_box.reshape(1, -1)).ravel()
        fg_inds = np.where(overlaps >= fg_thresh)[0]
        overlaps_list.append(fg_inds.size)
        tmpls.append(tmpl)
        shift_tmpls.append(shift_tmpl)
        anchors_list.append(_anchors)

    overlaps_list = np.array(overlaps_list)
    best_ind = np.argmax(overlaps_list)
    max_overlaps_num = overlaps_list[best_ind]

    replicas = np.where(overlaps_list == max_overlaps_num)[0]
    if replicas.size == 1:
        best_tmpl = shift_tmpls[best_ind]
        return best_tmpl, max_overlaps_num, anchors_list[best_ind]
    else:
        if replicas.size == 0:
            print(overlaps_list)
            print(max_overlaps_num)
            print(replicas)
            assert 0, 'Weird error, replicas is zero'
        tmpls = np.array(tmpls)
        raw_tmpl_inds = np.arange(tmpls.shape[0])
        tmpls = tmpls[replicas]
        best_ind, best_tmpl = best_search_box_test(tmpls, temp_box, bound)
        return best_tmpl, max_overlaps_num, anchors_list[
            raw_tmpl_inds[replicas][best_ind]]
Exemplo n.º 3
0
    def _get_roidb(self, index):
        roidb = {}
        sample = self.annotations[index]
        image_id = sample[0]['image_id']
        img_path = op.join(self.image_dir,
                           'COCO_train2014_%012d.jpg' % image_id)
        image = cv2.imread(img_path)
        if image is None:
            print('{} does not exists!'.format(img_path))

        fx = self.im_w * 1.0 / image.shape[1]
        fy = self.im_h * 1.0 / image.shape[0]

        image = cv2.resize(image, (self.im_w, self.im_h),
                           interpolation=cv2.INTER_LINEAR)

        gt_boxes = np.zeros((0, 4), dtype=np.float32)
        gt_classes = np.zeros(0, dtype=np.int32)

        for entry in sample:
            bbox = np.asarray(entry['bbox']).astype(np.float32)
            bbox[2] += bbox[0]
            bbox[3] += bbox[1]

            bbox[[0, 2]] *= fx
            bbox[[1, 3]] *= fy

            cat_id = entry['category_id']
            class_name = coco_id_name_map[cat_id]
            cls_id = self.coco_class_id_map[class_name]
            gt_boxes = np.append(gt_boxes, bbox.reshape(1, 4), 0)
            gt_classes = np.append(gt_classes, cls_id)

        flip_prob = np.random.rand()
        if flip_prob > 0.5:
            image, gt_boxes = self.flip(image, gt_boxes)

        roidb['data'] = image[np.newaxis, :, :, :].astype(np.float32)
        roidb['gt_boxes'] = gt_boxes
        roidb['gt_classes'] = gt_classes

        roidb['bound'] = self.bound
        bbox_overlaps = U.bbox_overlaps_per_image(self.anchors, gt_boxes)

        roidb['anchors'] = self.anchors
        roidb['bbox_overlaps'] = bbox_overlaps

        return roidb
Exemplo n.º 4
0
    def _get_roidb(self, index):
        item=self.dataset[index]
        file_name=item['name']
        im_path=os.path.join(self.image_directory, file_name)

        image=cv2.imread(im_path)
        h,w=image.shape[0:2]
        image=cv2.resize(image, (self.im_w, self.im_h), interpolation=cv2.INTER_LINEAR)
        nh, nw=image.shape[0:2]

        yscale=1.0*nh/h
        xscale=1.0*nw/w

        labels=item['labels']

        gt_boxes=np.zeros((0,4),dtype=np.float32)
        gt_classes = np.zeros(0, dtype=np.int32)
        
        for inst in labels:
            cat=inst['category']  
            if cat!='drivable area' and cat!='lane':
                box=inst['box2d']
                x1, y1, x2, y2=float(box['x1'])*xscale, float(box['y1'])*yscale, float(box['x2'])*xscale, float(box['y2'])*yscale
                gt_box=np.asarray([[x1,y1,x2,y2]])
                gt_ind=CAT_IND_MAP[cat]

                gt_boxes=np.append(gt_boxes, gt_box, 0)
                gt_classes=np.append(gt_classes, gt_ind)
        
        flip_prob=np.random.rand()
        if flip_prob>0.5:
            image, gt_boxes=self.flip(image, gt_boxes)

        roidb={}
        roidb['data']=image[np.newaxis, :,:,:].astype(np.float32)
        roidb['gt_boxes']=gt_boxes
        
        roidb['gt_classes']=gt_classes

        roidb['bound']=self.bound
        bbox_overlaps=U.bbox_overlaps_per_image(self.anchors, gt_boxes)
        
        roidb['anchors']=self.anchors
        roidb['bbox_overlaps']=bbox_overlaps
 
        return roidb
Exemplo n.º 5
0
    def get_minibatch_inter_seq(self):
        if self.iter_stop:
            self.iter_stop = False
            return None

        roidbs = []

        index_res = self.index_per_seq - self.upper_bound_per_seq
        '''permute'''
        index_res = index_res[self.permute_inds]
        valid_seq_inds = np.where(index_res <= 0)[0]
        num_valid_seqs = len(valid_seq_inds)
        subdir_inds = [
            self.permute_inds[valid_seq_inds[i % num_valid_seqs]]
            for i in range(self.index, self.index + self.batch_size)
        ]

        chosen_samples_per_seq = {}
        for i in subdir_inds:
            if i not in chosen_samples_per_seq.keys():
                chosen_samples_per_seq[i] = 0
            chosen_samples_per_seq[i] += 1

        img_dirs = []
        anno_dirs = []
        img_files = []
        anno_files = []
        cur_image_inds = []

        for i in subdir_inds:
            '''already permuted'''
            #            img_dirs.append(op.join(self.vid_dir, self.img_dirs[i]))
            img_dirs.append(op.join(self.vid_dir, self.anno_dirs[i]))
            anno_dirs.append(op.join(self.annot_dir, self.anno_dirs[i]))

            img_files.append(sorted(os.listdir(img_dirs[-1])))
            anno_files.append(sorted(os.listdir(anno_dirs[-1])))

            cur_image_inds.append(self.index_per_seq[i])

        ref_inds = np.asarray(cur_image_inds)
        tmp = copy.deepcopy(chosen_samples_per_seq)
        for i in range(len(ref_inds)):
            seq_ind = subdir_inds[i]
            ref_inds[i] += (tmp[seq_ind] - chosen_samples_per_seq[seq_ind])
            chosen_samples_per_seq[seq_ind] -= 1

        real_num_samples = ref_inds.size
        interval = np.random.randint(1,
                                     self.max_interval + 1,
                                     size=real_num_samples)
        det_inds = ref_inds + interval
        det_inds = np.minimum(det_inds, self.images_per_seq[subdir_inds] - 1)

        non_empty_batch = False
        for batch_index, inds in enumerate(zip(ref_inds, det_inds)):
            if len(img_files[batch_index]) == 0:
                continue

            roidb = {}

            ref_boxes = {}
            det_boxes = {}

            temp_image = None
            det_image = None

            for ind in inds:
                img_file = img_files[batch_index][ind]
                anno_file = anno_files[batch_index][ind]
                img_index = img_file[:img_file.rfind('.')]
                anno_index = anno_file[:anno_file.rfind('.')]
                assert img_index == anno_index, 'Index not uniformed'

                image = cv2.imread(
                    op.join(img_dirs[batch_index],
                            img_files[batch_index][ind]))

                image, (xstart, ystart), scale = util.resize_and_pad_image(
                    image, self.im_w, self.im_h)

                xml_file = op.join(anno_dirs[batch_index],
                                   anno_files[batch_index][ind])
                tree = ET.parse(xml_file)
                root = tree.getroot()

                objects = root.findall('object')

                if len(objects) == 0:
                    #                    print('{} has no targets'.format(op.join(img_dirs[batch_index], img_files[batch_index][ind])))
                    if DEBUG and self.vis_index < self.num_visualize:
                        cv2.imwrite(
                            op.join('mot_no_target',
                                    img_files[batch_index][ind]), image)

                for obj in objects:
                    obj_id = int(obj.find('trackid').text)
                    bndbox = obj.find('bndbox')

                    bbox = np.zeros(4, dtype=np.float32)
                    bbox[0] = float(bndbox.find('xmin').text)
                    bbox[1] = float(bndbox.find('ymin').text)
                    bbox[2] = float(bndbox.find('xmax').text)
                    bbox[3] = float(bndbox.find('ymax').text)

                    bbox *= scale
                    bbox[[0, 2]] += xstart
                    bbox[[1, 3]] += ystart

                    if ind == inds[0]:
                        ref_boxes[obj_id] = bbox
                    else:
                        det_boxes[obj_id] = bbox

                if ind == inds[0] and len(ref_boxes.keys()) > 0:
                    temp_image = image[np.newaxis, :, :, :].astype(np.float32)
                    non_empty_batch = True
                elif ind == inds[1] and len(ref_boxes.keys()) > 0:
                    det_image = image[np.newaxis, :, :, :].astype(np.float32)

            ref_boxes_align, det_boxes_align = self.align_boxes(
                ref_boxes, det_boxes)

            if len(ref_boxes_align) > 0:
                roidb['raw_temp_boxes'] = ref_boxes_align
                temp_boxes = util.compute_template_boxes(
                    ref_boxes_align,
                    (temp_image.shape[2], temp_image.shape[1]),
                    gain=self.margin_gain,
                    shape='same')
                roidb['temp_image'] = temp_image
                roidb['det_image'] = det_image
                roidb['temp_boxes'] = temp_boxes
                roidb['det_boxes'] = det_boxes_align
                '''NHWC'''
                bound = (det_image.shape[2], det_image.shape[1])
                search_boxes = util.calc_search_boxes(temp_boxes, bound)
                roidb['search_boxes'] = search_boxes
                roidb['bound'] = bound
                '''
                Next
                roidb['bbox_overlaps']
                '''
                #                anchors=self.gen_anchors(search_boxes, bound)
                anchors = G.gen_region_anchors(self.raw_anchors,
                                               search_boxes,
                                               bound,
                                               stride=self.stride,
                                               K=self.K,
                                               rpn_conv_size=self.roi_size)
                bbox_overlaps = U.bbox_overlaps_per_image(
                    np.vstack(anchors), det_boxes_align)
                roidb['anchors'] = anchors
                roidb['bbox_overlaps'] = bbox_overlaps
                roidbs.append(roidb)
        '''
        [NHWC,NHWC]
        '''
        #        assert len(ref_images)==len(box_sizes), 'Images and size array must have the same length: {} v.s. {}'.format(len(ref_images), len(box_sizes))

        for ind, v in tmp.items():
            self.index_per_seq[ind] += v

        index_res = self.index_per_seq - self.upper_bound_per_seq
        index_res = index_res[self.permute_inds[[valid_seq_inds]]]
        invalid_seq_inds = np.where(index_res > 0)[0]

        num_sequences = num_valid_seqs - len(invalid_seq_inds)

        if num_sequences == 0:
            self.index_per_seq = np.zeros(self.num_sequences, dtype=np.int32)
            self.round_per_seq = np.zeros(self.num_sequences, dtype=np.int32)
            self.inds = np.random.permutation(np.arange(self.num_sequences))
            self.index = 0
            self.iter_stop = True
        else:
            self.index += self.batch_size
            if self.index >= num_sequences:
                self.index = 0
            else:
                subtract_inds = np.where(invalid_seq_inds <= self.index)[0]
                self.index -= len(subtract_inds)

        return roidbs, non_empty_batch
Exemplo n.º 6
0
    def get_minibatch_inter_img(self):
        if self.iter_stop:
            self.iter_stop = False
            return None

        roidbs = []
        index = self.permute_inds[self.index]
        while self.index_per_seq[index] > self.upper_bound_per_seq[index]:
            self.index += 1
            if self.index == self.num_sequences:
                self.index = 0
            index = self.permute_inds[self.index]

        anno_dir = op.join(self.annot_dir, self.anno_dirs[index])
        img_dir = op.join(self.vid_dir, self.anno_dirs[index])

        img_files = sorted(os.listdir(img_dir))
        anno_files = sorted(os.listdir(anno_dir))

        cur_image_index = self.index_per_seq[index]
        ref_inds = np.arange(cur_image_index,
                             cur_image_index + self.batch_size)

        real_num_samples = ref_inds.size
        interval = np.random.randint(1,
                                     self.max_interval + 1,
                                     size=real_num_samples)
        det_inds = ref_inds + interval
        det_inds = np.minimum(det_inds, self.images_per_seq[index] - 1)

        for _, inds in enumerate(zip(ref_inds, det_inds)):
            roidb = {}

            ref_boxes = {}
            det_boxes = {}

            temp_image = None
            det_image = None
            for ind in inds:
                img_file = img_files[ind]
                anno_file = anno_files[ind]
                img_index = img_file[:img_file.rfind('.')]
                anno_index = anno_file[:anno_file.rfind('.')]
                assert img_index == anno_index, 'Index not uniformed'

                image = cv2.imread(op.join(img_dir, img_file))

                #                image, (xstart, ystart), scale=util.resize_and_pad_image(image, self.im_w, self.im_h)
                h, w = image.shape[0:2]
                image = cv2.resize(image, (self.im_w, self.im_h),
                                   interpolation=cv2.INTER_LINEAR)
                nh, nw = image.shape[0:2]

                yscale = 1.0 * nh / h
                xscale = 1.0 * nw / w

                xml_file = op.join(anno_dir, anno_files[ind])
                tree = ET.parse(xml_file)
                root = tree.getroot()

                objects = root.findall('object')

                if len(objects) == 0:
                    #                    print('{} has no targets'.format(op.join(img_dirs[batch_index], img_files[batch_index][ind])))
                    if DEBUG and self.vis_index < self.num_visualize:
                        cv2.imwrite(op.join('mot_no_target', img_files[ind]),
                                    image)

                for obj in objects:
                    obj_id = int(obj.find('trackid').text)
                    bndbox = obj.find('bndbox')

                    bbox = np.zeros(4, dtype=np.float32)
                    bbox[0] = float(bndbox.find('xmin').text)
                    bbox[1] = float(bndbox.find('ymin').text)
                    bbox[2] = float(bndbox.find('xmax').text)
                    bbox[3] = float(bndbox.find('ymax').text)
                    '''
                    bbox*=scale
                    bbox[[0,2]]+=xstart
                    bbox[[1,3]]+=ystart
                    '''
                    bbox[[0, 2]] *= xscale
                    bbox[[1, 3]] *= yscale

                    if ind == inds[0]:
                        ref_boxes[obj_id] = bbox
                    else:
                        det_boxes[obj_id] = bbox

                if ind == inds[0] and len(ref_boxes.keys()) > 0:
                    temp_image = image[np.newaxis, :, :, :].astype(np.float32)
                elif ind == inds[1] and len(ref_boxes.keys()) > 0:
                    det_image = image[np.newaxis, :, :, :].astype(np.float32)

            ref_boxes_align, det_boxes_align = self.align_boxes(
                ref_boxes, det_boxes)

            if len(ref_boxes_align) > 0:
                roidb['raw_temp_boxes'] = ref_boxes_align
                #                temp_boxes=util.compute_template_boxes(ref_boxes_align, (temp_image.shape[2], temp_image.shape[1]), gain=self.margin_gain, shape='same')
                roidb['temp_image'] = temp_image
                roidb['det_image'] = det_image
                #                roidb['temp_boxes']=temp_boxes
                roidb['temp_boxes'] = ref_boxes_align
                roidb['det_boxes'] = det_boxes_align
                '''NHWC'''
                bound = (det_image.shape[2], det_image.shape[1])
                search_boxes = util.calc_search_boxes(ref_boxes_align, bound)
                roidb['search_boxes'] = search_boxes
                roidb['bound'] = bound
                '''
                Next
                roidb['bbox_overlaps']
                '''
                #                anchors=self.gen_anchors(search_boxes, bound)
                anchors = G.gen_region_anchors(self.raw_anchors,
                                               search_boxes,
                                               bound,
                                               stride=self.stride,
                                               K=self.K,
                                               rpn_conv_size=self.roi_size)
                bbox_overlaps = U.bbox_overlaps_per_image(
                    np.vstack(anchors), det_boxes_align)
                roidb['anchors'] = anchors
                roidb['bbox_overlaps'] = bbox_overlaps
                roidbs.append(roidb)
        '''
        [NHWC,NHWC]
        '''
        self.index_per_seq[index] += self.batch_size
        index_res = self.index_per_seq - self.upper_bound_per_seq
        index_res = index_res[self.permute_inds]
        valid_seq_inds = np.where(index_res <= 0)[0]
        if valid_seq_inds.size == 0:
            self.index_per_seq = np.zeros(self.num_sequences, dtype=np.int32)
            self.round_per_seq = np.zeros(self.num_sequences, dtype=np.int32)
            self.permute_inds = np.random.permutation(
                np.arange(self.num_sequences))
            self.index = 0
            self.iter_stop = True
        else:
            self.index += 1
            if self.index == self.num_sequences:
                self.index = 0

        return roidbs
Exemplo n.º 7
0
def compute_detection_rpn_targets(anchors,
                                  gt_boxes,
                                  out_size,
                                  bbox_overlaps=None,
                                  K=9,
                                  batch_size=2):
    anchor_cls_targets = np.zeros((0, K * out_size[1], out_size[0]),
                                  dtype=np.int32)
    anchor_bbox_targets = np.zeros((0, 4 * K, out_size[1], out_size[0]),
                                   dtype=np.float32)
    fg_anchor_inds = []
    bg_anchor_inds = []

    #    if bbox_overlaps is None:
    #        bbox_overlaps=bbox_overlaps_batch(anchors, gt_boxes, batch_size, branch='frcnn')
    if bbox_overlaps is None:
        bbox_overlaps = []
        for i in range(batch_size):
            bbox_overlaps.append(
                bbox_overlaps_per_image(anchors, gt_boxes[i], branch='frcnn'))

    bbox_weights = np.zeros((0, 4 * K, out_size[1], out_size[0]),
                            dtype=np.float32)

    for i in range(batch_size):
        bbox_overlap = np.asarray(bbox_overlaps[i])
        max_overlap = np.max(bbox_overlap, axis=1)
        '''cls start'''
        anchor_cls_target = np.zeros(
            anchors.shape[0], dtype=np.int32) - 100  #box_size*A*K-->N*A*K
        fg_anchor_ind = np.where(
            max_overlap >= cfg[cfg.PHASE].RPN_POSITIVE_THRESH)[0]
        bg_anchor_ind = np.where(
            max_overlap < cfg[cfg.PHASE].RPN_NEGATIVE_THRESH)[0]

        fg_anchor_inds.append(fg_anchor_ind)
        bg_anchor_inds.append(bg_anchor_ind)

        anchor_cls_target[fg_anchor_ind] = 1
        anchor_cls_target[bg_anchor_ind] = 0

        anchor_cls_target=anchor_cls_target.reshape(1, out_size[1], out_size[0], K).\
            transpose(0,3,2,1).reshape(1, K*out_size[1], out_size[0])
        anchor_cls_targets = np.append(anchor_cls_targets, anchor_cls_target,
                                       0)
        '''cls end'''
        '''bbox start'''
        bbox_loss_inds = fg_anchor_ind
        mask_inds = np.zeros((anchors.shape[0], 4), dtype=np.float32)
        mask_inds[bbox_loss_inds, :] = 1

        gt_boxes_this_image = gt_boxes[i]

        gt_rois = gt_boxes_this_image[np.argmax(bbox_overlap, axis=1)]
        bbox_deltas = bbox_transform(anchors, gt_rois)

        if cfg.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:
            bbox_deltas = bbox_deltas / cfg.RPN_BBOX_STD_DEV


#        bbox_deltas*=mask_inds
        bbox_deltas = bbox_deltas.reshape(1, out_size[1], out_size[0],
                                          4 * K).transpose((0, 3, 1, 2))
        anchor_bbox_targets = np.append(anchor_bbox_targets, bbox_deltas, 0)
        '''bbox end'''
        '''bbox weights'''
        mask_inds = mask_inds.reshape(1, out_size[1], out_size[0],
                                      4 * K).transpose(0, 3, 1, 2)
        bbox_weights = np.append(bbox_weights, mask_inds, 0)

    return anchor_cls_targets, anchor_bbox_targets, bbox_weights, fg_anchor_inds
Exemplo n.º 8
0
def get_proposal_target(proposal_batch, gt_boxes, gt_classes, batch_size):
    rois_per_image = cfg[cfg.PHASE].BATCH_SIZE
    fg_rois_per_image = int(rois_per_image * cfg[cfg.PHASE].FG_FRACTION)

    num_fg_rois = 0
    num_bg_rois = 0

    proposal_cls_targets = np.zeros(0, dtype=np.int32)
    proposal_bbox_targets = np.zeros((0, 4 * cfg.NUM_CLASSES),
                                     dtype=np.float32)
    all_bbox_weights = np.zeros((0, 4 * cfg.NUM_CLASSES), dtype=np.float32)
    all_proposals = np.zeros((0, 5), dtype=np.float32)

    #    labels=np.zeros(0, dtype=np.int32)
    labels = []

    for i in range(batch_size):
        proposals_this_image = proposal_batch[i]
        #        proposals_this_image=np.vstack(proposal_batch[i])
        gt_boxes_this_image = gt_boxes[i]
        gt_classes_this_image = gt_classes[i]

        bbox_overlaps = bbox_overlaps_per_image(proposals_this_image,
                                                gt_boxes_this_image)
        argmax_overlaps = np.argmax(bbox_overlaps, axis=1)

        max_overlaps = np.max(bbox_overlaps, axis=1)

        fg_inds = np.where(max_overlaps >= cfg[cfg.PHASE].FG_THRESH)[0]
        if cfg.CHOOSE_PROPOSAL:
            fg_rois_this_image = min(fg_rois_per_image, fg_inds.size)
        else:
            fg_rois_this_image = fg_inds.size

        num_fg_rois += fg_rois_this_image

        if cfg.CHOOSE_PROPOSAL and fg_inds.size > 0:
            fg_inds = np.random.choice(fg_inds,
                                       size=fg_rois_this_image,
                                       replace=False)

        bg_inds = np.where(
            np.bitwise_and(bbox_overlaps < cfg[cfg.PHASE].BG_THRESH_HI,
                           bbox_overlaps >= cfg[cfg.PHASE].BG_THRESH_LO) ==
            1)[0]
        if cfg.CHOOSE_PROPOSAL:
            bg_rois_per_image = rois_per_image - fg_rois_this_image
            bg_rois_this_image = min(bg_rois_per_image, bg_inds.size)
        else:
            bg_rois_this_image = bg_inds.size

        num_bg_rois += bg_rois_this_image

        if cfg.CHOOSE_PROPOSAL and bg_inds.size > 0:
            bg_inds = np.random.choice(bg_inds,
                                       size=bg_rois_this_image,
                                       replace=False)
        '''discard rois with too small overlaps (less than BG_THRESH_LO)'''
        keep_inds = np.append(fg_inds, bg_inds)

        assert fg_inds.size == fg_rois_this_image
        assert bg_inds.size == bg_rois_this_image

        proposals_keep = proposals_this_image[keep_inds]
        all_proposals = np.append(all_proposals, proposals_keep, 0)

        max_overlap_boxes = gt_boxes_this_image[argmax_overlaps[keep_inds]]
        gt_cls_inds = gt_classes_this_image[argmax_overlaps[keep_inds]]
        #        print(gt_inds)
        targets = bbox_transform(proposals_keep, max_overlap_boxes)
        if cfg.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:
            # Optionally normalize targets by a precomputed mean and stdev
            targets = targets / cfg.BBOX_STD_DEV

        proposal_cls_target = np.zeros(len(proposals_keep), dtype=np.int32)
        bbox_deltas_classes = np.zeros(
            (len(proposals_keep), 4 * cfg.NUM_CLASSES), dtype=np.float32)

        #        print(gt_cls_inds)
        for j in range(len(keep_inds)):
            proposal_cls_target[j] = gt_cls_inds[j]
            bbox_deltas_classes[j, gt_cls_inds[j] * 4:gt_cls_inds[j] * 4 +
                                4] = targets[j][...]

        bbox_weights = np.ones((len(proposals_keep), 4 * cfg.NUM_CLASSES),
                               dtype=np.float32)
        proposal_cls_target[fg_rois_this_image:] = 0
        bbox_deltas_classes[fg_rois_this_image:, :] = 0.0
        bbox_weights[fg_rois_this_image:, :] = 0.0

        labels_this_image = np.zeros(len(keep_inds), dtype=np.int32)
        labels_this_image[:
                          fg_rois_this_image] = gt_cls_inds[:
                                                            fg_rois_this_image]
        labels_this_image[fg_rois_this_image:] = 0
        labels.append(labels_this_image)

        proposal_cls_targets = np.append(proposal_cls_targets,
                                         proposal_cls_target, 0)
        proposal_bbox_targets = np.append(proposal_bbox_targets,
                                          bbox_deltas_classes, 0)
        all_bbox_weights = np.append(all_bbox_weights, bbox_weights, 0)

#    print(labels)
    return all_proposals, proposal_cls_targets, proposal_bbox_targets, all_bbox_weights, labels