From 473ae5d9642409c4e3ef68dac25e209ab6c1a3a9 Mon Sep 17 00:00:00 2001 From: Jianfeng Wang Date: Mon, 27 Jul 2020 15:57:46 +0800 Subject: [PATCH] fix(detection): fix random features in py3.5 (#52) * feat(detection): add new models and weights * fix(detection): fix random features in py3.5 --- .gitignore | 1 + README.md | 2 + hubconf.py | 2 + official/vision/detection/README.md | 18 +++++---- official/vision/detection/configs/__init__.py | 2 + .../faster_rcnn_resx101_coco_2x_800size.py | 40 +++++++++++++++++++ .../retinanet_resx101_coco_2x_800size.py | 40 +++++++++++++++++++ official/vision/detection/layers/det/fpn.py | 2 +- official/vision/detection/tools/inference.py | 12 ++---- 9 files changed, 101 insertions(+), 18 deletions(-) create mode 100644 official/vision/detection/configs/faster_rcnn_resx101_coco_2x_800size.py create mode 100644 official/vision/detection/configs/retinanet_resx101_coco_2x_800size.py diff --git a/.gitignore b/.gitignore index 78dab75..f1339d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__/ *log*/ *.so +*.pkl diff --git a/README.md b/README.md index 0cd889e..b4cc661 100644 --- a/README.md +++ b/README.md @@ -79,9 +79,11 @@ export PYTHONPATH=/path/to/models:$PYTHONPATH | retinanet-res50-coco-1x-800size | 36.4 | | retinanet-res50-coco-1x-800size-syncbn | 37.1 | | retinanet-res101-coco-2x-800size | 40.8 | +| retinanet-resx101-coco-2x-800size | 41.8 | | faster-rcnn-res50-coco-1x-800size | 38.8 | | faster-rcnn-res50-coco-1x-800size-syncbn | 39.3 | | faster-rcnn-res101-coco-2x-800size | 43.0 | +| faster-rcnn-resx101-coco-2x-800size | 44.7 | ### 图像分割 diff --git a/hubconf.py b/hubconf.py index 588552d..a932bef 100644 --- a/hubconf.py +++ b/hubconf.py @@ -31,9 +31,11 @@ from official.vision.detection.configs import ( faster_rcnn_res50_coco_1x_800size, faster_rcnn_res50_coco_1x_800size_syncbn, faster_rcnn_res101_coco_2x_800size, + faster_rcnn_resx101_coco_2x_800size, retinanet_res50_coco_1x_800size, retinanet_res50_coco_1x_800size_syncbn, retinanet_res101_coco_2x_800size, + retinanet_resx101_coco_2x_800size, ) from official.vision.detection.models import FasterRCNN, RetinaNet from official.vision.detection.tools.utils import DetEvaluator diff --git a/official/vision/detection/README.md b/official/vision/detection/README.md index f9bf699..4fea8c3 100644 --- a/official/vision/detection/README.md +++ b/official/vision/detection/README.md @@ -6,14 +6,16 @@ 网络的性能在COCO2017数据集上的测试结果如下: -| 模型 | mAP
@5-95 | batch
/gpu | gpu | trainging speed
(8gpu) | -| --- | :---: | :---: | :---: | :---: | -| retinanet-res50-coco-1x-800size | 36.4 | 2 | 2080Ti | 3.1(it/s) | -| retinanet-res50-coco-1x-800size-syncbn | 37.1 | 2 | 2080Ti | 1.7(it/s) | -| retinanet-res101-coco-2x-800size | 40.8 | 2 | 2080Ti | 2.1(it/s) | -| faster-rcnn-res50-coco-1x-800size | 38.8 | 2 | 2080Ti | 3.3(it/s) | -| faster-rcnn-res50-coco-1x-800size-syncbn | 39.3 | 2 | 2080Ti | 1.8(it/s) | -| faster-rcnn-res101-coco-2x-800size | 43.0 | 2 | 2080Ti | 2.3(it/s) | +| 模型 | mAP
@5-95 | batch
/gpu | gpu | trainging speed
(8gpu) | +| --- | :---: | :---: | :---: | :---: | +| retinanet-res50-coco-1x-800size | 36.4 | 2 | 2080Ti | 3.1(it/s) | +| retinanet-res50-coco-1x-800size-syncbn | 37.1 | 2 | 2080Ti | 1.7(it/s) | +| retinanet-res101-coco-2x-800size | 40.8 | 2 | 2080Ti | 2.1(it/s) | +| retinanet-resx101-coco-2x-800size | 41.8 | 2 | 2080Ti | 0.8(it/s) (enable sublinear) | +| faster-rcnn-res50-coco-1x-800size | 38.8 | 2 | 2080Ti | 3.3(it/s) | +| faster-rcnn-res50-coco-1x-800size-syncbn | 39.3 | 2 | 2080Ti | 1.8(it/s) | +| faster-rcnn-res101-coco-2x-800size | 43.0 | 2 | 2080Ti | 2.3(it/s) | +| faster-rcnn-resx101-coco-2x-800size | 44.7 | 2 | 2080Ti | 0.9(it/s) (enable sublinear) | * MegEngine v0.5.1 diff --git a/official/vision/detection/configs/__init__.py b/official/vision/detection/configs/__init__.py index 9eadf38..2abd428 100644 --- a/official/vision/detection/configs/__init__.py +++ b/official/vision/detection/configs/__init__.py @@ -1,9 +1,11 @@ from .faster_rcnn_res50_coco_1x_800size import faster_rcnn_res50_coco_1x_800size from .faster_rcnn_res50_coco_1x_800size_syncbn import faster_rcnn_res50_coco_1x_800size_syncbn from .faster_rcnn_res101_coco_2x_800size import faster_rcnn_res101_coco_2x_800size +from .faster_rcnn_resx101_coco_2x_800size import faster_rcnn_resx101_coco_2x_800size from .retinanet_res50_coco_1x_800size import retinanet_res50_coco_1x_800size from .retinanet_res50_coco_1x_800size_syncbn import retinanet_res50_coco_1x_800size_syncbn from .retinanet_res101_coco_2x_800size import retinanet_res101_coco_2x_800size +from .retinanet_resx101_coco_2x_800size import retinanet_resx101_coco_2x_800size _EXCLUDE = {} __all__ = [k for k in globals().keys() if k not in _EXCLUDE and not k.startswith("_")] diff --git a/official/vision/detection/configs/faster_rcnn_resx101_coco_2x_800size.py b/official/vision/detection/configs/faster_rcnn_resx101_coco_2x_800size.py new file mode 100644 index 0000000..974d2f0 --- /dev/null +++ b/official/vision/detection/configs/faster_rcnn_resx101_coco_2x_800size.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +# MegEngine is Licensed under the Apache License, Version 2.0 (the "License") +# +# Copyright (c) 2014-2020 Megvii Inc. All rights reserved. +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +from megengine import hub + +from official.vision.detection import models + + +class CustomFasterRCNNConfig(models.FasterRCNNConfig): + def __init__(self): + super().__init__() + + self.backbone = "resnext101_32x8d" + + # ------------------------ training cfg ---------------------- # + self.max_epoch = 36 + self.lr_decay_stages = [24, 32, 34] + + +@hub.pretrained( + "https://data.megengine.org.cn/models/weights/" + "faster_rcnn_resx101_coco_2x_800size_44dot7_d03b05b2.pkl" +) +def faster_rcnn_resx101_coco_2x_800size(batch_size=1, **kwargs): + r""" + Faster-RCNN FPN trained from COCO dataset. + `"Faster-RCNN" `_ + `"FPN" `_ + `"COCO" `_ + """ + return models.FasterRCNN(CustomFasterRCNNConfig(), batch_size=batch_size, **kwargs) + + +Net = models.FasterRCNN +Cfg = CustomFasterRCNNConfig diff --git a/official/vision/detection/configs/retinanet_resx101_coco_2x_800size.py b/official/vision/detection/configs/retinanet_resx101_coco_2x_800size.py new file mode 100644 index 0000000..38c5316 --- /dev/null +++ b/official/vision/detection/configs/retinanet_resx101_coco_2x_800size.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +# MegEngine is Licensed under the Apache License, Version 2.0 (the "License") +# +# Copyright (c) 2014-2020 Megvii Inc. All rights reserved. +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +from megengine import hub + +from official.vision.detection import models + + +class CustomRetinaNetConfig(models.RetinaNetConfig): + def __init__(self): + super().__init__() + + self.backbone = "resnext101_32x8d" + + # ------------------------ training cfg ---------------------- # + self.max_epoch = 36 + self.lr_decay_stages = [24, 32, 34] + + +@hub.pretrained( + "https://data.megengine.org.cn/models/weights/" + "retinanet_resx101_coco_2x_800size_41dot8_f92909a1.pkl" +) +def retinanet_resx101_coco_2x_800size(batch_size=1, **kwargs): + r""" + RetinaNet trained from COCO dataset. + `"RetinaNet" `_ + `"FPN" `_ + `"COCO" `_ + """ + return models.RetinaNet(CustomRetinaNetConfig(), batch_size=batch_size, **kwargs) + + +Net = models.RetinaNet +Cfg = CustomRetinaNetConfig diff --git a/official/vision/detection/layers/det/fpn.py b/official/vision/detection/layers/det/fpn.py index eaebf0c..5ec310c 100644 --- a/official/vision/detection/layers/det/fpn.py +++ b/official/vision/detection/layers/det/fpn.py @@ -121,7 +121,7 @@ class FPN(M.Module): for s in range(stage, stage + self.top_block.num_levels): self._out_feature_strides["p{}".format(s + 1)] = 2 ** (s + 1) - self._out_features = list(self._out_feature_strides.keys()) + self._out_features = list(sorted(self._out_feature_strides.keys())) self._out_feature_channels = {k: out_channels for k in self._out_features} def forward(self, x): diff --git a/official/vision/detection/tools/inference.py b/official/vision/detection/tools/inference.py index d91cbd5..01298c7 100644 --- a/official/vision/detection/tools/inference.py +++ b/official/vision/detection/tools/inference.py @@ -25,13 +25,9 @@ logger = mge.get_logger(__name__) def make_parser(): parser = argparse.ArgumentParser() - parser.add_argument( - "-f", "--file", default="net.py", type=str, help="net description file" - ) - parser.add_argument( - "-w", "--weight_file", default=None, type=str, help="weights file", - ) - parser.add_argument("-i", "--image", default="example.jpg", type=str) + parser.add_argument("-f", "--file", type=str, help="net description file") + parser.add_argument("-w", "--weight_file", type=str, help="weights file") + parser.add_argument("-i", "--image", type=str) return parser @@ -39,8 +35,6 @@ def main(): parser = make_parser() args = parser.parse_args() - logger.info("Load Model : %s completed", args.weight_file) - @jit.trace(symbolic=True) def val_func(): pred = model(model.inputs) -- GitLab