Beispiel #1
0
def test_center_net_estimator():
    from gluoncv.auto.tasks import ObjectDetection
    task = ObjectDetection({'num_trials': 1})
    detector = task.fit(OBJECT_DETCTION_DATASET)
    assert task.fit_summary.get('valid_map', 0) > 0
    _, _, test_data = OBJECT_DETCTION_DATASET.random_split()
    test_result = detector.predict(test_data)
Beispiel #2
0
 def _validate_data(self, data):
     """Check whether data is valid, try to convert with best effort if not"""
     if len(data) < 1:
         raise ValueError('Empty dataset.')
     if not (hasattr(data, 'classes') and hasattr(data, 'to_mxnet')):
         if isinstance(data, pd.DataFrame):
             # raw dataframe, try to add metadata automatically
             infer_classes = []
             if 'image' in data.columns:
                 # check image relative/abs path is valid
                 sample = data.iloc[0]['image']
                 if not os.path.isfile(sample):
                     raise OSError(
                         f'Detected invalid image path `{sample}`, please ensure all image paths are absolute or you are using the right working directory.'
                     )
             if 'rois' in data.columns and 'image' in data.columns:
                 sample = data.iloc[0]['rois']
                 for sample_key in ('class', 'xmin', 'ymin', 'xmax',
                                    'ymax'):
                     assert sample_key in sample, f'key `{sample_key}` required in `rois`'
                 class_column = data.rois.apply(
                     lambda x: x.get('class', 'unknown'))
                 infer_classes = class_column.unique().tolist()
                 data['rois'] = data['rois'].apply(lambda x: x.update(
                     {'difficult': x.get('difficult', 0)} or x))
                 data = _ObjectDetection.Dataset(
                     data.sort_values('image').reset_index(drop=True),
                     classes=infer_classes)
             elif 'image' in data and 'class' in data and 'xmin' in data and 'ymin' in data and 'xmax' in data and 'ymax' in data:
                 infer_classes = data['class'].unique().tolist()
                 if 'difficult' not in data.columns:
                     data['difficult'] = 0
                 data = _ObjectDetection.Dataset(
                     data.sort_values('image').reset_index(drop=True),
                     classes=infer_classes)
                 data = data.pack()
                 data.classes = infer_classes
             else:
                 err_msg = 'Unable to convert raw DataFrame to ObjectDetector Dataset, ' + \
                           '`image` and `rois` columns are required.' + \
                           'You may visit `https://auto.gluon.ai/stable/tutorials/object_detection/dataset.html` ' + \
                           'for details.'
                 raise AttributeError(err_msg)
             logger.log(
                 20,
                 'Converting raw DataFrame to ObjectDetector.Dataset...')
             logger.log(
                 20,
                 f'Detected {len(infer_classes)} unique classes: {infer_classes}'
             )
             instruction = 'train_data = ObjectDetector.Dataset(train_data, classes=["foo", "bar"])'
             logger.log(
                 20,
                 f'If you feel the `classes` is inaccurate, please construct the dataset explicitly, e.g. {instruction}'
             )
     return data
Beispiel #3
0
def test_time_out_detection():
    time_limit = 30
    from gluoncv.auto.tasks import ObjectDetection
    task = ObjectDetection({
        'num_trials': 1,
        'epochs': 50,
        'time_limits': time_limit
    })
    tic = time.time()
    detector = task.fit(OBJECT_DETECTION_TRAIN)
    # check time_limit with a little bit overhead
    assert (time.time() - tic) < time_limit + 180
Beispiel #4
0
def test_object_detection_estimator_transfer():
    from gluoncv.auto.tasks import ObjectDetection
    task = ObjectDetection({
        'num_trials':
        1,
        'transfer':
        ag.Categorical('yolo3_darknet53_coco', 'ssd_512_resnet50_v1_voc'),
        'estimator':
        'ssd'
    })
    detector = task.fit(OBJECT_DETCTION_DATASET)
    assert task.fit_summary().get('valid_map', 0) > 0
    _, _, test_data = OBJECT_DETCTION_DATASET.random_split()
    test_result = detector.predict(test_data)
Beispiel #5
0
def test_object_detection_estimator_transfer():
    from gluoncv.auto.tasks import ObjectDetection
    task = ObjectDetection({
        'num_trials':
        1,
        'epochs':
        1,
        'transfer':
        ag.Categorical('yolo3_darknet53_coco', 'ssd_512_resnet50_v1_voc'),
        'estimator':
        'ssd',
        'batch_size':
        4
    })
    detector = task.fit(OBJECT_DETECTION_TRAIN)
    assert task.fit_summary().get('valid_map', 0) > 0
    test_result = detector.predict(OBJECT_DETECTION_TEST)
Beispiel #6
0
        'batch_size':
        ag.Int(3, 6),  # [8, 16, 32, 64]
        'momentum':
        ag.Real(0.85, 0.95),
        'wd':
        ag.Real(1e-6, 1e-2, log=True),
        'epochs':
        20,
        'num_trials':
        args.num_trials,
        'search_strategy':
        'bayesopt'
    }

    # specify learning task
    task = ObjectDetection(config)

    # specify dataset
    dataset = Dataset.get(args.dataset)
    train_data, valid_data = dataset.split(0.8)

    # fit auto estimator
    detector = task.fit(train_data, valid_data)

    # evaluate auto estimator
    eval_map = detector.evaluate(valid_data)
    logging.info('evaluation: mAP={}'.format(eval_map[-1][-1]))

    # save and load auto estimator
    detector.save('ssd_detector.pkl')
    detector = ObjectDetection.load('ssd_detector.pkl')
Beispiel #7
0
def test_object_detection_estimator():
    from gluoncv.auto.tasks import ObjectDetection
    task = ObjectDetection({'num_trials': 1, 'epochs': 1})
    detector = task.fit(OBJECT_DETECTION_TRAIN)
    assert task.fit_summary().get('valid_map', 0) > 0
    test_result = detector.predict(OBJECT_DETECTION_TEST)
Beispiel #8
0
def test_center_net_estimator():
    from gluoncv.auto.tasks import ObjectDetection
    task = ObjectDetection({'num_trials': 1})
    detector = task.fit(OBJECT_DETCTION_DATASET)
    assert task.fit_summary.get('valid_map', 0) > 0
        'dataset': 'voc_tiny',
        'meta_arch': 'ssd',
        'lr': ag.Categorical(1e-3, 5e-4),
        'epochs': 5,
        'num_trials': 2
    }

    if 'meta_arch' in args:
        if args['meta_arch'] == 'ssd':
            estimator = SSDEstimator
        elif args['meta_arch'] == 'faster_rcnn':
            estimator = FasterRCNNEstimator
        elif args['meta_arch'] == 'yolo3':
            estimator = YOLOv3Estimator
        elif args['meta_arch'] == 'center_net':
            estimator = CenterNetEstimator
        else:
            raise NotImplementedError('%s is not implemented.',
                                      args['meta_arch'])
    else:
        estimator = None

    task = ObjectDetection(args, estimator)

    detector = task.fit()
    test_map = detector.evaluate()
    # print("mAP on test dataset: {}".format(test_map[-1][-1]))
    # print(test_map)
    detector.save('detector.pkl')
    detector = ObjectDetection.load('detector.pkl')