03. 使用 GluonCV Auto 任务通过 HPO 训练分类器或检测器

上一个图像分类示例展示了使用 gluoncv.auto.estimators 提供的估计器进行训练/评估/预测的基本用法。类似地,您可以使用 SSDEstimatorYOLOv3EstimatorCenterNetEstimatorFasterRCNNEstimator 训练目标检测器。

在本教程中,我们将稍微深入一步,进入超参数调优领域!我们将向您展示如何将实验卸载到后端 HPO 搜索器,以便在计算资源充足的情况下获得更好的结果。

使用 GluonCV auto 任务进行 HPO

from gluoncv.auto.tasks.image_classification import ImageClassification
from gluoncv.auto.tasks.object_detection import ObjectDetection
import autogluon.core as ag

在本教程中,我们使用一个用于目标检测的小型样本数据集。对于图像分类,请参考 02. 使用 Auto Estimator 训练图像分类

train = ObjectDetection.Dataset.from_voc(
    'https://autogluon.s3.amazonaws.com/datasets/tiny_motorbike.zip')
train, val, test = train.random_split(val_size=0.1, test_size=0.1)

输出

tiny_motorbike/
├── Annotations/
├── ImageSets/
└── JPEGImages/

定义搜索空间

我们展示了一个运行目标检测 HPO 的最小示例。对于图像分类,代码更改很小,只需将数据集和 ObjectDetection 替换为 ImageClassification 即可。

我们使用非常保守的搜索空间来减少 CI 运行时长,并且我们将试验次数限制为 1 次以减少构建时间。您可以随意调整 num_trialstime_limitsepochs 以及使用 ag.Categoricalag.Intag.Real 等调优其他搜索空间,以便获得更好的结果。

time_limits = 60 * 60  # 1hr
search_args = {'lr': ag.Categorical(1e-3, 1e-2),
               'num_trials': 1,
               'epochs': 2,
               'num_workers': 16,
               'batch_size': ag.Categorical(4, 8),
               'ngpus_per_trial': 1,
               'search_strategy': 'random',
               'time_limits': time_limits}

根据配置构建一个目标检测任务。

task = ObjectDetection(search_args)

自动拟合模型。

detector = task.fit(train, val)

输出

/usr/local/lib/python3.6/dist-packages/mxnet/gluon/block.py:1512: UserWarning: Cannot decide type for the following arguments. Consider providing them as input:
        data: None
  input_sym_arg_type = in_param.infer_type()[0]
Downloading /root/.mxnet/models/ssd_512_resnet50_v1_coco-c4835162.zip from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/ssd_512_resnet50_v1_coco-c4835162.zip...

  0%|          | 0/181188 [00:00<?, ?KB/s]
  0%|          | 102/181188 [00:00<03:45, 804.38KB/s]
  0%|          | 510/181188 [00:00<01:21, 2222.93KB/s]
  1%|1         | 2182/181188 [00:00<00:24, 7211.57KB/s]
  4%|4         | 7549/181188 [00:00<00:07, 22966.10KB/s]
  8%|7         | 13735/181188 [00:00<00:04, 35658.31KB/s]
 12%|#2        | 22266/181188 [00:00<00:03, 51445.39KB/s]
 16%|#6        | 29643/181188 [00:00<00:02, 58417.34KB/s]
 21%|##1       | 38238/181188 [00:00<00:02, 66907.39KB/s]
 25%|##5       | 45503/181188 [00:00<00:01, 68662.98KB/s]
 30%|##9       | 53467/181188 [00:01<00:01, 71512.42KB/s]
 34%|###4      | 61721/181188 [00:01<00:01, 74835.29KB/s]
 38%|###8      | 69274/181188 [00:01<00:01, 74705.17KB/s]
 43%|####2     | 77166/181188 [00:01<00:01, 75966.98KB/s]
 47%|####6     | 84798/181188 [00:01<00:01, 74113.26KB/s]
 51%|#####1    | 93002/181188 [00:01<00:01, 76167.78KB/s]
 56%|#####5    | 100646/181188 [00:01<00:01, 75728.33KB/s]
 60%|#####9    | 108620/181188 [00:01<00:00, 76911.50KB/s]
 64%|######4   | 116631/181188 [00:01<00:00, 77858.78KB/s]
 69%|######8   | 124429/181188 [00:02<00:00, 76685.95KB/s]
 73%|#######3  | 132829/181188 [00:02<00:00, 78842.09KB/s]
 78%|#######7  | 140725/181188 [00:02<00:00, 66991.48KB/s]
 82%|########1 | 147734/181188 [00:02<00:00, 65527.36KB/s]
 85%|########5 | 154661/181188 [00:02<00:00, 66526.31KB/s]
 90%|########9 | 162901/181188 [00:02<00:00, 70911.20KB/s]
 94%|#########3| 170142/181188 [00:02<00:00, 69750.56KB/s]
 98%|#########8| 177589/181188 [00:02<00:00, 69904.37KB/s]
181189KB [00:02, 64041.11KB/s]
/usr/local/lib/python3.6/dist-packages/mxnet/gluon/block.py:1512: UserWarning: Cannot decide type for the following arguments. Consider providing them as input:
        data: None
  input_sym_arg_type = in_param.infer_type()[0]
/usr/local/lib/python3.6/dist-packages/mxnet/gluon/block.py:1512: UserWarning: Cannot decide type for the following arguments. Consider providing them as input:
        data: None
  input_sym_arg_type = in_param.infer_type()[0]

在测试集上评估最终模型。

test_map = detector.evaluate(test)
print("mAP on test dataset:")
for category, score in zip(*test_map):
    print(category, score)

输出

mAP on test dataset:
motorbike 0.557581493355552
bus nan
boat nan
cow 0.0
person 0.4424933160156914
bicycle nan
car 1.0000000000000002
dog nan
chair nan
pottedplant nan
mAP 0.5000187023428109

保存最终模型。

detector.save('detector.pkl')

加载模型并运行一些预测。

detector2 = ObjectDetection.load('detector.pkl')
pred = detector2.predict(test.iloc[0]['image'])
print(pred)

输出

/usr/local/lib/python3.6/dist-packages/mxnet/gluon/block.py:1512: UserWarning: Cannot decide type for the following arguments. Consider providing them as input:
        data: None
  input_sym_arg_type = in_param.infer_type()[0]
   predict_class  ...                                       predict_rois
0            car  ...  {'xmin': 0.2950457036495209, 'ymin': 0.1961218...
1            car  ...  {'xmin': 0.1063406690955162, 'ymin': 0.1650179...
2         person  ...  {'xmin': 0.9072394967079163, 'ymin': 0.0386775...
3            car  ...  {'xmin': 0.497192919254303, 'ymin': 0.32654511...
4         person  ...  {'xmin': 0.1039300486445427, 'ymin': 0.0214233...
..           ...  ...                                                ...
76        person  ...  {'xmin': 0.9762840270996094, 'ymin': 0.0143931...
77     motorbike  ...  {'xmin': 0.7977412939071655, 'ymin': 0.1901351...
78        person  ...  {'xmin': 0.14319948852062225, 'ymin': 0.497429...
79        person  ...  {'xmin': 0.7110783457756042, 'ymin': 0.1970322...
80        person  ...  {'xmin': 0.1752810776233673, 'ymin': 0.1064255...

[81 rows x 3 columns]

固定目标检测算法

默认情况下,调度器从 SSDEstimatorYOLOv3EstimatorCenterNetEstimatorFasterRCNNEstimator 中选择算法。如果在 HPO 期间想训练特定算法,可以固定该估计器。

例如,这告诉搜索器只使用 YOLOv3 模型。

search_args = {'lr': ag.Categorical(1e-3, 1e-2),
               'num_trials': 1,
               'estimator': 'yolo3',
               'epochs': 2,
               'num_workers': 16,
               'batch_size': ag.Categorical(4, 8),
               'ngpus_per_trial': 1,
               'search_strategy': 'random',
               'time_limits': time_limits}

指定用于迁移学习的模型

允许从之前训练好的模型进行迁移学习是利用有限训练数据实现快速收敛和高性能模型的关键。默认情况下,我们启用从在 COCO 数据集上训练的目标检测网络进行迁移学习,这些模型会自动从 gluoncv 模型动物园下载。

默认选择的预训练模型是 ‘ssd_512_resnet50_v1_coco’、‘yolo3_darknet53_coco’、‘faster_rcnn_resnet50_v1b_coco’ 和 ‘center_net_resnet50_v1b_coco’。

您可以继续使用它们,或者替换为您喜欢的模型,例如:

search_args = {'lr': ag.Categorical(1e-3, 1e-2),
               'num_trials': 1,
               'transfer': ag.Categorical('ssd_512_mobilenet1.0_coco',
                                          'yolo3_mobilenet1.0_coco'),
               'epochs': 10,
               'num_workers': 16,
               'batch_size': ag.Categorical(4, 8),
               'ngpus_per_trial': 1,
               'search_strategy': 'random',
               'time_limits': time_limits}

现在您已经准备好使用 gluoncv.auto 中的 HPO 功能了!请持续关注,我们将推出更多功能和教程,以提供更快、更便捷的训练/测试/推断体验。

脚本总运行时间: ( 1 分钟 0.965 秒)

由 Sphinx-Gallery 生成的图库