Exemplo n.º 1
0
    def by_dataset(self, vid_path='./data/freeman1/'):
        assert os.path.exists(vid_path)

        gt_boxes = BoundingBox.read_vid_gt(vid_path)

        curr_bbox = None
        self.stopwatch.start('total')
        _logger.info('---- start dataset l=%d' % (len(gt_boxes)))
        for idx, gt_box in enumerate(gt_boxes):
            img = commons.imread(
                os.path.join(vid_path, 'img', '%04d.jpg' % (idx + 1)))
            self.imgwh = Coordinate.get_imgwh(img)
            if idx == 0:
                # initialization : initial fine-tuning
                self.initial_finetune(img, gt_box)
                curr_bbox = gt_box

            # tracking
            predicted_box = self.tracking(img, curr_bbox)
            self.show(img, gt_box=gt_box, predicted_box=predicted_box)
            # cv2.imwrite('/Users/ildoonet/Downloads/aaa/%d.jpg' % self.iteration, img)
            curr_bbox = predicted_box
        self.stopwatch.stop('total')

        _logger.info('----')
        _logger.info(self.stopwatch)
        _logger.info('%.3f FPS' %
                     (len(gt_boxes) / self.stopwatch.get_elapsed('total')))
Exemplo n.º 2
0
    def redetection_by_sampling(self, prev_box, img):
        """
        default redetection method
        """
        imgwh = Coordinate.get_imgwh(img)
        translation_f = min(1.5, 0.6 * 1.15**self.failed_cnt)
        candidates = prev_box.gen_noise_samples(imgwh, 'gaussian', ADNetConf.g()['redetection']['samples'],
                                                gaussian_translation_f=translation_f)

        scores = []
        for c_batch in commons.chunker(candidates, ADNetRunner.MAX_BATCHSIZE):
            samples = [commons.extract_region(img, box) for box in c_batch]
            classes = self.persistent_sess.run(
                self.adnet.layer_scores,
                feed_dict={
                    self.adnet.input_tensor: samples,
                    self.adnet.action_history_tensor: [commons.onehot_flatten(self.action_histories_old)]*len(c_batch),
                    self.tensor_is_training: False
                }
            )
            scores.extend([x[1] for x in classes])
        top5_idx = [i[0] for i in sorted(enumerate(scores), reverse=True, key=lambda x: x[1])][:5]
        mean_score = sum([scores[x] for x in top5_idx]) / 5.0
        if mean_score >= self.latest_score:
            mean_box = candidates[0]
            for i in range(1, 5):
                mean_box += candidates[i]
            return mean_box / 5.0, mean_score
        return None, 0.0
Exemplo n.º 3
0
    def run_tracker(self, decoded_img_arr, BBox_assorted_dict,
                    manually_corrected):

        # Convert into Bounding box object
        # Bounding box object will have xmin, ymin, width and height
        # Derive width and height from the 4-coordinates
        # gt_boxes must have list of Bunding box objects
        gt_boxes_dict = self.accumulate_ground_truth_boxes(BBox_assorted_dict)
        # initialization : initial fine-tuning
        # Multiple trackid and BBox association
        multi_track_bbox = {}
        # AdNetTracker.RUN_COUNTER = 0
        for trackid, gt_box in gt_boxes_dict.items():
            st_time = datetime.now()
            print("trackid>>", trackid)
            curr_bbox = gt_box
            self.imgwh = Coordinate.get_imgwh(decoded_img_arr)
            print("self.imgwh >>", self.imgwh)
            if AdNetTracker.RUN_COUNTER == 0 or manually_corrected:
                self.initial_finetune(decoded_img_arr, gt_box)
            AdNetTracker.RUN_COUNTER += 1

            # predicted_box = self.tracking(decoded_img_arr, curr_bbox, trackid)
            # print("predicted_box>>", predicted_box, predicted_box.xy.x, predicted_box.xy.y)
            print("previous box::", curr_bbox)
            # predicted_box_dict contains key as track ID and value is BBox object
            predicted_box_dict = self.tracking(decoded_img_arr, curr_bbox,
                                               trackid)
            print("predicted_box_dict::", predicted_box_dict)
            ed_time = datetime.now()
            duration = ed_time - st_time
            print("tracking done in...", duration)
            multi_track_bbox.update(predicted_box_dict)
        # print("multi_track_bbox>>", multi_track_bbox)
        return multi_track_bbox
Exemplo n.º 4
0
    def by_dataset(self, vid_path='./data/LT52/'):
        """
        './data/BlurCar2/'
        LT52

        """
        assert os.path.exists(vid_path)
        id=0
        gt_boxes = BoundingBox.read_vid_gt(vid_path,id)
        print("gt_boxes>>", gt_boxes)

        curr_bbox = None
        self.stopwatch.start('total')
        _logger.info('---- start dataset l=%d' % (len(gt_boxes)))
        for idx, gt_box in enumerate(gt_boxes):
            print('\nimage number : %04d.jpg' %(idx+1))
            print('vid path is %s' %vid_path)
            if idx!=1:
                img = commons.imread(os.path.join(vid_path, 'img', '%04d.jpeg' % (idx + 1)))
                self.imgwh = Coordinate.get_imgwh(img)
                if idx == 0:
                    # initialization : initial fine-tuning
                    # print("gt_box >", gt_box)
                    self.initial_finetune(img, gt_box)
                    curr_bbox = gt_box

                # tracking
                # print("curr_bbox>>", curr_bbox, idx)
                predicted_box = self.tracking(img, curr_bbox,idx)
                # print("predicted_box>>", predicted_box, predicted_box.xy.x, predicted_box.xy.y)
                self.show(img, idx,gt_box=gt_box, predicted_box=predicted_box)
                # cv2.imwrite('/Users/ildoonet/Downloads/aaa/%d.jpg' % self.iteration, img)
                curr_bbox = predicted_box
        self.stopwatch.stop('total')

        _logger.info('----')
        _logger.info(self.stopwatch)
        _logger.info('%.3f FPS' % (len(gt_boxes) / self.stopwatch.get_elapsed('total')))
Exemplo n.º 5
0
    def tracking(self, img, curr_bbox):
        self.iteration += 1
        is_tracked = True
        boxes = []
        self.latest_score = -1
        self.stopwatch.start('tracking.do_action')
        for track_i in range(ADNetConf.get()['predict']['num_action']):
            patch = commons.extract_region(img, curr_bbox)

            # forward with image & action history
            actions, classes = self.persistent_sess.run(
                [self.adnet.layer_actions, self.adnet.layer_scores],
                feed_dict={
                    self.adnet.input_tensor: [patch],
                    self.adnet.action_history_tensor:
                    [commons.onehot_flatten(self.action_histories)],
                    self.tensor_is_training:
                    False
                })

            latest_score = classes[0][1]
            if latest_score < ADNetConf.g()['predict']['thresh_fail']:
                is_tracked = False
                self.action_histories_old = np.copy(self.action_histories)
                self.action_histories = np.insert(self.action_histories, 0,
                                                  12)[:-1]
                break
            else:
                self.failed_cnt = 0
            self.latest_score = latest_score

            # move box
            action_idx = np.argmax(actions[0])
            self.action_histories = np.insert(self.action_histories, 0,
                                              action_idx)[:-1]
            prev_bbox = curr_bbox
            curr_bbox = curr_bbox.do_action(self.imgwh, action_idx)
            if action_idx != ADNetwork.ACTION_IDX_STOP:
                if prev_bbox == curr_bbox:
                    print('action idx', action_idx)
                    print(prev_bbox)
                    print(curr_bbox)
                    raise Exception('box not moved.')

            # oscillation check
            if action_idx != ADNetwork.ACTION_IDX_STOP and curr_bbox in boxes:
                action_idx = ADNetwork.ACTION_IDX_STOP

            if action_idx == ADNetwork.ACTION_IDX_STOP:
                break

            boxes.append(curr_bbox)
        self.stopwatch.stop('tracking.do_action')

        # redetection when tracking failed
        new_score = 0.0
        if not is_tracked:
            self.failed_cnt += 1
            # run redetection callback function
            new_box, new_score = self.callback_redetection(curr_bbox, img)
            if new_box is not None:
                curr_bbox = new_box
                patch = commons.extract_region(img, curr_bbox)
            _logger.debug('redetection success=%s' %
                          (str(new_box is not None)))

        # save samples
        if is_tracked or new_score > ADNetConf.g(
        )['predict']['thresh_success']:
            self.stopwatch.start('tracking.save_samples.roi')
            imgwh = Coordinate.get_imgwh(img)
            pos_num, neg_num = ADNetConf.g(
            )['finetune']['pos_num'], ADNetConf.g()['finetune']['neg_num']
            pos_boxes, neg_boxes = curr_bbox.get_posneg_samples(
                imgwh,
                pos_num,
                neg_num,
                use_whole=False,
                pos_thresh=ADNetConf.g()['finetune']['pos_thresh'],
                neg_thresh=ADNetConf.g()['finetune']['neg_thresh'],
                uniform_translation_f=2,
                uniform_scale_f=5)
            self.stopwatch.stop('tracking.save_samples.roi')
            self.stopwatch.start('tracking.save_samples.feat')
            feats = self._get_features([
                commons.extract_region(img, box)
                for i, box in enumerate(pos_boxes)
            ])
            for box, feat in zip(pos_boxes, feats):
                box.feat = feat
            feats = self._get_features([
                commons.extract_region(img, box)
                for i, box in enumerate(neg_boxes)
            ])
            for box, feat in zip(neg_boxes, feats):
                box.feat = feat
            pos_lb_action = BoundingBox.get_action_labels(pos_boxes, curr_bbox)
            self.histories.append((pos_boxes, neg_boxes, pos_lb_action,
                                   np.copy(img), self.iteration))

            # clear old ones
            self.histories = self.histories[-ADNetConf.g(
            )['finetune']['long_term']:]
            self.stopwatch.stop('tracking.save_samples.feat')

        # online finetune
        if self.iteration % ADNetConf.g(
        )['finetune']['interval'] == 0 or is_tracked is False:
            img_pos, img_neg = [], []
            pos_boxes, neg_boxes, pos_lb_action = [], [], []
            pos_term = 'long_term' if is_tracked else 'short_term'
            for i in range(ADNetConf.g()['finetune'][pos_term]):
                if i >= len(self.histories):
                    break
                pos_boxes.extend(self.histories[-(i + 1)][0])
                pos_lb_action.extend(self.histories[-(i + 1)][2])
                img_pos.extend([self.histories[-(i + 1)][3]] *
                               len(self.histories[-(i + 1)][0]))
            for i in range(ADNetConf.g()['finetune']['short_term']):
                if i >= len(self.histories):
                    break
                neg_boxes.extend(self.histories[-(i + 1)][1])
                img_neg.extend([self.histories[-(i + 1)][3]] *
                               len(self.histories[-(i + 1)][1]))
            self.stopwatch.start('tracking.online_finetune')
            self._finetune_fc((img_pos, img_neg), pos_boxes, neg_boxes,
                              pos_lb_action,
                              ADNetConf.get()['finetune']['learning_rate'],
                              ADNetConf.get()['finetune']['iter'])
            _logger.debug('finetuned')
            self.stopwatch.stop('tracking.online_finetune')

        visualizer.image('patch', patch)
        # cv2.imshow('patch', patch)
        return curr_bbox