def generate_samples(self, curr_bbox, positive, init=False):
        if init:
            if positive:
                n = self.opts['nPos_init']
                Thre = self.opts['posThre_init']
            else:
                n = self.opts['nNeg_init']
                Thre = self.opts['negThre_init']
        else:
            if positive:
                n = self.opts['nPos_online']
                Thre = self.opts['posThre_online']
            else:
                n = self.opts['nNeg_online']
                Thre = self.opts['negThre_online']

        assert n > 0, "if n = 0, don't initialize this class"

        if positive:
            examples = gen_samples('gaussian', curr_bbox, n * 2, self.opts,
                                   self.opts['finetune_trans'],
                                   self.opts['finetune_scale_factor'])
            r = overlap_ratio(examples,
                              np.matlib.repmat(curr_bbox, len(examples), 1))
            examples = examples[np.array(r) > Thre]
            examples = examples[np.random.randint(
                low=0, high=len(examples), size=min(len(examples), n)), :]

            action_labels = gen_action_labels(self.opts['num_actions'],
                                              self.opts, np.array(examples),
                                              curr_bbox)
            # score labels: 1 is positive. 0 is negative
            score_labels = list(np.ones(len(examples), dtype=int))

        else:
            examples = gen_samples('uniform', curr_bbox, n * 2, self.opts, 2,
                                   5)
            r = overlap_ratio(examples,
                              np.matlib.repmat(curr_bbox, len(examples), 1))
            examples = examples[np.array(r) < Thre]
            examples = examples[np.random.randint(
                low=0, high=len(examples), size=min(len(examples), n)), :]

            action_labels = np.full((self.opts['num_actions'], len(examples)),
                                    fill_value=-1)
            # score labels: 1 is positive. 0 is negative
            score_labels = list(np.zeros(len(examples), dtype=int))

        action_labels = np.transpose(action_labels).tolist()
        bboxes = examples
        labels = action_labels

        return bboxes, labels, score_labels
def process_data_vot(train_sequences, vid_info, opt, train_db_pos,
                     train_db_neg, lock):
    opts = opt.copy()
    train_db_pos_gpu = []
    train_db_neg_gpu = []

    for train_i in range(len(train_sequences)):
        train_db_pos_ = {
            'img_path': [],
            'bboxes': [],
            'labels': [],
            'score_labels': []
        }
        train_db_neg_ = {
            'img_path': [],
            'bboxes': [],
            'labels': [],
            'score_labels': []
        }

        img_idx = train_sequences[train_i]
        gt_bbox = vid_info['gt'][img_idx]

        if len(gt_bbox) == 0:
            continue

        pos_examples = []
        while len(pos_examples) < opts['nPos_train']:
            pos = gen_samples('gaussian', gt_bbox, opts['nPos_train'] * 5,
                              opts, 0.1, 5)
            r = overlap_ratio(pos, np.matlib.repmat(gt_bbox, len(pos), 1))
            pos = pos[np.array(r) > opts['posThre_train']]
            if len(pos) == 0:
                continue
            pos = pos[np.random.
                      randint(low=0,
                              high=len(pos),
                              size=min(len(pos), opts['nPos_train'] -
                                       len(pos_examples))), :]
            pos_examples.extend(pos)

        neg_examples = []
        while len(neg_examples) < opts['nNeg_train']:
            # in original code, this 1 line below use opts['nPos_train'] instead of opts['nNeg_train']
            neg = gen_samples('gaussian', gt_bbox, opts['nNeg_train'] * 5,
                              opts, 2, 10)
            r = overlap_ratio(neg, np.matlib.repmat(gt_bbox, len(neg), 1))
            neg = neg[np.array(r) < opts['negThre_train']]
            if len(neg) == 0:
                continue
            neg = neg[np.random.
                      randint(low=0,
                              high=len(neg),
                              size=min(len(neg), opts['nNeg_train'] -
                                       len(neg_examples))), :]
            neg_examples.extend(neg)

        # examples = pos_examples + neg_examples
        action_labels_pos = gen_action_labels(opts['num_actions'], opts,
                                              np.array(pos_examples), gt_bbox)
        action_labels_neg = np.full((opts['num_actions'], len(neg_examples)),
                                    fill_value=-1)

        action_labels_pos = np.transpose(action_labels_pos).tolist()
        action_labels_neg = np.transpose(action_labels_neg).tolist()

        # action_labels = action_labels_pos + action_labels_neg

        train_db_pos_['img_path'] = np.full(len(pos_examples),
                                            vid_info['img_files'][img_idx])
        train_db_pos_['bboxes'] = pos_examples
        train_db_pos_['labels'] = action_labels_pos
        # score labels: 1 is positive. 0 is negative
        train_db_pos_['score_labels'] = list(
            np.ones(len(pos_examples), dtype=int))

        train_db_neg_['img_path'] = np.full(len(neg_examples),
                                            vid_info['img_files'][img_idx])
        train_db_neg_['bboxes'] = neg_examples
        train_db_neg_['labels'] = action_labels_neg
        # score labels: 1 is positive. 0 is negative
        train_db_neg_['score_labels'] = list(
            np.zeros(len(neg_examples), dtype=int))

        train_db_pos_gpu.append(train_db_pos_)
        train_db_neg_gpu.append(train_db_neg_)

    try:
        lock.acquire()
        #print("len(train_db_pos_gpu): %d"%len(train_db_pos_gpu))
        train_db_pos.extend(train_db_pos_gpu)
        #print("len(train_db_pos): %d" % len(train_db_pos))
        #print("len(train_db_neg_gpu): %d" % len(train_db_neg_gpu))
        train_db_neg.extend(train_db_neg_gpu)
        #print("len(train_db_neg): %d" % len(train_db_neg))
    except Exception as err:
        raise err
    finally:
        lock.release()
def process_data_ILSVR(img_paths, opt, train_db_pos_neg_all, lock):
    opts = opt.copy()
    train_db_pos_neg_gpu = []
    # train_db_neg_gpu = []
    for train_i in img_paths:
        train_db_pos_ = {
            'img_path': '',
            'bboxes': [],
            'labels': [],
            'score_labels': []
        }
        train_db_neg_ = {
            'img_path': '',
            'bboxes': [],
            'labels': [],
            'score_labels': []
        }

        #img_idx = train_sequences[train_i]
        #gt_bbox = vid_info['gt'][img_idx]

        #if len(gt_bbox) == 0:
        #    continue
        gt_file_path = '../datasets/data/ILSVRC/Annotations/VID/train/' + train_i + '.xml'
        #gt_bbox=get_xml_box_label(gt_file_path)
        #opts['imgSize'] = get_xml_img_size(gt_file_path)
        imginfo = get_xml_img_info(gt_file_path)
        gt_bboxs = imginfo['gts']
        opts['imgSize'] = imginfo['imgsize']
        img_path = '../datasets/data/ILSVRC/Data/VID/train/' + train_i + '.JPEG'
        for gt_bbox in gt_bboxs:
            train_db_pos_neg = {
                'img_path': '',
                'bboxes': [],
                'labels': [],
                'score_labels': []
            }
            pos_examples = []
            while len(pos_examples) < opts['nPos_train']:
                pos = gen_samples('gaussian', gt_bbox, opts['nPos_train'] * 5,
                                  opts, 0.1, 5)
                r = overlap_ratio(pos, np.matlib.repmat(gt_bbox, len(pos), 1))
                pos = pos[np.array(r) > opts['posThre_train']]
                if len(pos) == 0:
                    #continue
                    break
                pos = pos[np.random.randint(low=0,
                                            high=len(pos),
                                            size=min(
                                                len(pos), opts['nPos_train'] -
                                                len(pos_examples))), :]
                pos_examples.extend(pos)

            neg_examples = []
            while len(neg_examples) < opts['nNeg_train']:
                # in original code, this 1 line below use opts['nPos_train'] instead of opts['nNeg_train']
                neg = gen_samples('gaussian', gt_bbox, opts['nNeg_train'] * 5,
                                  opts, 2, 10)
                r = overlap_ratio(neg, np.matlib.repmat(gt_bbox, len(neg), 1))
                neg = neg[np.array(r) < opts['negThre_train']]
                if len(neg) == 0:
                    #continue
                    break
                neg = neg[np.random.randint(low=0,
                                            high=len(neg),
                                            size=min(
                                                len(neg), opts['nNeg_train'] -
                                                len(neg_examples))), :]
                neg_examples.extend(neg)

            # examples = pos_examples + neg_examples
            action_labels_pos = gen_action_labels(opts['num_actions'], opts,
                                                  np.array(pos_examples),
                                                  gt_bbox)
            action_labels_neg = np.full(
                (opts['num_actions'], len(neg_examples)), fill_value=-1)

            action_labels_pos = np.transpose(action_labels_pos).tolist()
            action_labels_neg = np.transpose(action_labels_neg).tolist()

            # action_labels = action_labels_pos + action_labels_neg

            # train_db_pos_['bboxes'].extend(pos_examples)
            # train_db_pos_['labels'].extend(action_labels_pos)
            # # score labels: 1 is positive. 0 is negative
            # train_db_pos_['score_labels'].extend(list(np.ones(len(pos_examples), dtype=int)))
            #
            #
            # train_db_neg_['bboxes'].extend(neg_examples)
            # train_db_neg_['labels'].extend(action_labels_neg)
            # # score labels: 1 is positive. 0 is negative
            # train_db_neg_['score_labels'].extend(list(np.zeros(len(neg_examples), dtype=int)))

            train_db_pos_neg['bboxes'].extend(pos_examples)
            train_db_pos_neg['labels'].extend(action_labels_pos)
            # score labels: 1 is positive. 0 is negative
            train_db_pos_neg['score_labels'].extend(
                list(np.ones(len(pos_examples), dtype=int)))

            train_db_pos_neg['bboxes'].extend(neg_examples)
            train_db_pos_neg['labels'].extend(action_labels_neg)
            # score labels: 1 is positive. 0 is negative
            train_db_pos_neg['score_labels'].extend(
                list(np.zeros(len(neg_examples), dtype=int)))

            train_db_pos_neg['img_path'] = img_path
            # train_db_pos_['img_path'] = img_path
            # train_db_neg_['img_path'] = img_path

            # if len(train_db_pos_['bboxes']) != 0 and len(train_db_neg_['bboxes']) != 0:
            #     train_db_pos_gpu.append(train_db_pos_)
            #     train_db_neg_gpu.append(train_db_neg_)
            if len(train_db_pos_neg['bboxes']) == (opts['nPos_train'] +
                                                   opts['nNeg_train']):
                train_db_pos_neg_gpu.append(train_db_pos_neg)
                # train_db_neg_gpu.append(train_db_neg_)
            # box_ii += 1

        # img_ii += 1

        # if img_ii==3471:
        #     print("when gt_skip set to 200, and the img_ii=3472, the gen_samples function can't produce examples that iou>thred")
        #     #'ILSVRC2015_VID_train_0002/ILSVRC2015_train_00633000/000025'
        #reason:the img is so small and unclear
        # if img_ii%1000==0 and img_ii!=0:
        #     t9=time.time()
        #     real_time=t9-t2
        #     all_time=t9-t0
        #     all_h=all_time//3600
        #     all_m=all_time%3600//60
        #     all_s=all_time%60
        #     speed_img=1000/real_time
        #     speed_box=(box_ii-box_ii_start)/real_time
        #     all_speed_img=img_ii/all_time
        #     all_speed_box = box_ii/all_time
        #     print('\ndone imgs: %d , done boxes: %d , all imgs: %d. '%(img_ii,box_ii,all_img_num))
        #     print('real_time speed: %d imgs/s, %d boxes/s'%(speed_img,speed_box))
        #     print('avg_time speed: %d imgs/s, %d boxes/s' % (all_speed_img, all_speed_box))
        #     print('spend time: %d h  %d m  %d s (%d s)'%(all_h,all_m,all_s,all_time))
        #     box_ii_start=box_ii
        #     t2=time.time()
    try:
        lock.acquire()
        # print("len(train_db_pos_gpu): %d" % len(train_db_pos_gpu))
        train_db_pos_neg_all.extend(train_db_pos_neg_gpu)
        # print("len(train_db_pos): %d" % len(train_db_pos))
        # print("len(train_db_neg_gpu): %d" % len(train_db_neg_gpu))
        # train_db_neg.extend(train_db_neg_gpu)
        # print("len(train_db_neg): %d" % len(train_db_neg))
    except Exception as err:
        raise err
    finally:
        lock.release()
Exemplo n.º 4
0
def get_train_dbs(vid_info, opts):
    img = cv2.imread(vid_info['img_files'][0])

    opts['scale_factor'] = 1.05
    opts['imgSize'] = list(img.shape)
    gt_skip = opts['train']['gt_skip']

    if vid_info['db_name'] == 'alov300':
        train_sequences = vid_info['gt_use'] == 1
    else:
        train_sequences = list(range(0, vid_info['nframes'], gt_skip))

    train_db_pos = []
    train_db_neg = []

    for train_i in range(len(train_sequences)):
        train_db_pos_ = {
            'img_path': [],
            'bboxes': [],
            'labels': [],
            'score_labels': []
        }
        train_db_neg_ = {
            'img_path': [],
            'bboxes': [],
            'labels': [],
            'score_labels': []
        }

        img_idx = train_sequences[train_i]
        gt_bbox = vid_info['gt'][img_idx]

        if len(gt_bbox) == 0:
            continue

        pos_examples = []
        while len(pos_examples) < opts['nPos_train']:
            pos = gen_samples('gaussian', gt_bbox, opts['nPos_train'] * 5,
                              opts, 0.1, 5)
            r = overlap_ratio(pos, np.matlib.repmat(gt_bbox, len(pos), 1))
            pos = pos[np.array(r) > opts['posThre_train']]
            if len(pos) == 0:
                continue
            pos = pos[np.random.
                      randint(low=0,
                              high=len(pos),
                              size=min(len(pos), opts['nPos_train'] -
                                       len(pos_examples))), :]
            pos_examples.extend(pos)

        neg_examples = []
        while len(neg_examples) < opts['nNeg_train']:
            # in original code, this 1 line below use opts['nPos_train'] instead of opts['nNeg_train']
            neg = gen_samples('gaussian', gt_bbox, opts['nNeg_train'] * 5,
                              opts, 2, 10)
            r = overlap_ratio(neg, np.matlib.repmat(gt_bbox, len(neg), 1))
            neg = neg[np.array(r) < opts['negThre_train']]
            if len(neg) == 0:
                continue
            neg = neg[np.random.
                      randint(low=0,
                              high=len(neg),
                              size=min(len(neg), opts['nNeg_train'] -
                                       len(neg_examples))), :]
            neg_examples.extend(neg)

        show_examples_test(pos_examples, neg_examples,
                           vid_info['img_files'][img_idx])
        # examples = pos_examples + neg_examples
        action_labels_pos = gen_action_labels(opts['num_actions'], opts,
                                              np.array(pos_examples), gt_bbox)
        action_labels_neg = np.full((opts['num_actions'], len(neg_examples)),
                                    fill_value=-1)

        action_labels_pos = np.transpose(action_labels_pos).tolist()
        action_labels_neg = np.transpose(action_labels_neg).tolist()

        # action_labels = action_labels_pos + action_labels_neg

        train_db_pos_['img_path'] = np.full(len(pos_examples),
                                            vid_info['img_files'][img_idx])
        train_db_pos_['bboxes'] = pos_examples
        train_db_pos_['labels'] = action_labels_pos
        # score labels: 1 is positive. 0 is negative
        train_db_pos_['score_labels'] = list(
            np.ones(len(pos_examples), dtype=int))

        train_db_neg_['img_path'] = np.full(len(neg_examples),
                                            vid_info['img_files'][img_idx])
        train_db_neg_['bboxes'] = neg_examples
        train_db_neg_['labels'] = action_labels_neg
        # score labels: 1 is positive. 0 is negative
        train_db_neg_['score_labels'] = list(
            np.zeros(len(neg_examples), dtype=int))

        train_db_pos.append(train_db_pos_)
        train_db_neg.append(train_db_neg_)

    return train_db_pos, train_db_neg