From 244d73a7541654b1946be72af724dfb1425d0067 Mon Sep 17 00:00:00 2001 From: dengkaipeng Date: Sat, 18 Apr 2020 04:35:40 +0000 Subject: [PATCH] clean vision --- examples/bmn/eval.py | 3 +- .../bmn_model.py => examples/bmn/modeling.py | 4 +- examples/bmn/predict.py | 3 +- examples/bmn/train.py | 3 +- examples/tsm/infer.py | 2 +- examples/tsm/main.py | 2 +- .../models/tsm.py => examples/tsm/modeling.py | 2 +- examples/yolov3/README.md | 10 +--- {hapi/datasets => examples/yolov3}/coco.py | 0 examples/yolov3/infer.py | 5 +- examples/yolov3/main.py | 18 +++---- .../yolov3.py => examples/yolov3/modeling.py | 48 +++++++++++++++++-- .../yolov3/transforms.py | 0 hapi/datasets/__init__.py | 9 +++- hapi/model.py | 2 +- hapi/vision/models/__init__.py | 11 +---- hapi/vision/models/darknet.py | 4 +- hapi/vision/transforms/__init__.py | 7 ++- hapi/vision/transforms/functional.py | 2 + 19 files changed, 87 insertions(+), 48 deletions(-) rename hapi/vision/models/bmn_model.py => examples/bmn/modeling.py (99%) rename hapi/vision/models/tsm.py => examples/tsm/modeling.py (99%) rename {hapi/datasets => examples/yolov3}/coco.py (100%) rename hapi/vision/models/yolov3.py => examples/yolov3/modeling.py (87%) rename hapi/vision/transforms/detection_transforms.py => examples/yolov3/transforms.py (100%) diff --git a/examples/bmn/eval.py b/examples/bmn/eval.py index 071f5d1..2b129d1 100644 --- a/examples/bmn/eval.py +++ b/examples/bmn/eval.py @@ -19,7 +19,8 @@ import logging import paddle.fluid as fluid from hapi.model import set_device, Input -from hapi.vision.models import bmn, BmnLoss + +from modeling import bmn, BmnLoss from bmn_metric import BmnMetric from reader import BmnDataset from config_utils import * diff --git a/hapi/vision/models/bmn_model.py b/examples/bmn/modeling.py similarity index 99% rename from hapi/vision/models/bmn_model.py rename to examples/bmn/modeling.py index e15555d..f0fa26e 100644 --- a/hapi/vision/models/bmn_model.py +++ b/examples/bmn/modeling.py @@ -26,7 +26,7 @@ DATATYPE = 'float32' pretrain_infos = { 'bmn': ('https://paddlemodels.bj.bcebos.com/hapi/bmn.pdparams', - '9286c821acc4cad46d6613b931ba468c') + 'aa84e3386e1fbd117fb96fa572feeb94') } @@ -462,5 +462,5 @@ def bmn(tscale, weight_path = get_weights_path(*(pretrain_infos['bmn'])) assert weight_path.endswith('.pdparams'), \ "suffix of weight must be .pdparams" - model.load(weight_path[:-9]) + model.load(weight_path) return model diff --git a/examples/bmn/predict.py b/examples/bmn/predict.py index fb1b104..6e0ae99 100644 --- a/examples/bmn/predict.py +++ b/examples/bmn/predict.py @@ -19,7 +19,8 @@ import logging import paddle.fluid as fluid from hapi.model import set_device, Input -from hapi.vision.models import bmn, BmnLoss + +from modeling import bmn, BmnLoss from bmn_metric import BmnMetric from reader import BmnDataset from config_utils import * diff --git a/examples/bmn/train.py b/examples/bmn/train.py index 178085a..c6bcdef 100644 --- a/examples/bmn/train.py +++ b/examples/bmn/train.py @@ -19,9 +19,10 @@ import sys import os from hapi.model import set_device, Input -from hapi.vision.models import bmn, BmnLoss + from reader import BmnDataset from config_utils import * +from modeling import bmn, BmnLoss DATATYPE = 'float32' diff --git a/examples/tsm/infer.py b/examples/tsm/infer.py index 3de1c84..a41b667 100644 --- a/examples/tsm/infer.py +++ b/examples/tsm/infer.py @@ -20,9 +20,9 @@ import argparse import numpy as np from hapi.model import Input, set_device -from hapi.vision.models import tsm_resnet50 from check import check_gpu, check_version +from modeling import tsm_resnet50 from kinetics_dataset import KineticsDataset from transforms import * diff --git a/examples/tsm/main.py b/examples/tsm/main.py index 24b3793..4a2b889 100644 --- a/examples/tsm/main.py +++ b/examples/tsm/main.py @@ -24,8 +24,8 @@ from paddle.fluid.dygraph.parallel import ParallelEnv from hapi.model import Model, CrossEntropy, Input, set_device from hapi.metrics import Accuracy -from hapi.vision.models import tsm_resnet50 +from modeling import tsm_resnet50 from check import check_gpu, check_version from kinetics_dataset import KineticsDataset from transforms import * diff --git a/hapi/vision/models/tsm.py b/examples/tsm/modeling.py similarity index 99% rename from hapi/vision/models/tsm.py rename to examples/tsm/modeling.py index 8b50f70..c2422ed 100644 --- a/hapi/vision/models/tsm.py +++ b/examples/tsm/modeling.py @@ -196,7 +196,7 @@ def _tsm_resnet(num_layers, seg_num=8, num_classes=400, pretrained=True): weight_path = get_weights_path(*(pretrain_infos[num_layers])) assert weight_path.endswith('.pdparams'), \ "suffix of weight must be .pdparams" - model.load(weight_path[:-9]) + model.load(weight_path) return model diff --git a/examples/yolov3/README.md b/examples/yolov3/README.md index 9a0d2cc..fdbfc56 100644 --- a/examples/yolov3/README.md +++ b/examples/yolov3/README.md @@ -99,18 +99,12 @@ YOLOv3 的网络结构由基础特征提取网络、multi-scale特征融合层 | ... ``` -```bash -sh pretrain_weights/download.sh -``` - ### 模型训练 数据准备完成后,可使用`main.py`脚本启动训练和评估,如下脚本会自动每epoch交替进行训练和模型评估,并将checkpoint默认保存在`yolo_checkpoint`目录下。 YOLOv3模型训练总batch_size为64训练,以下以使用4卡Tesla P40每卡batch_size为16训练介绍训练方式。对于静态图和动态图,多卡训练中`--batch_size`为每卡上的batch_size,即总batch_size为`--batch_size`乘以卡数。 -YOLOv3模型训练须加载骨干网络[DarkNet53]()的预训练权重,可在训练时通过`--pretrain_weights`指定,若指定为URL,将自动下载权重至`~/.cache/paddle/weights`目录并加载。 - `main.py`脚本参数可通过如下命令查询 ```bash @@ -122,7 +116,7 @@ python main.py --help 使用如下方式进行多卡训练: ```bash -CUDA_VISIBLE_DEVICES=0,1,2,3 python -m paddle.distributed.launch main.py --data= --batch_size=16 --pretrain_weights=https://paddlemodels.bj.bcebos.com/hapi/darknet53_pretrained.pdparams +CUDA_VISIBLE_DEVICES=0,1,2,3 python -m paddle.distributed.launch main.py --data= --batch_size=16 ``` #### 动态图训练 @@ -132,7 +126,7 @@ CUDA_VISIBLE_DEVICES=0,1,2,3 python -m paddle.distributed.launch main.py --data= 使用如下方式进行多卡训练: ```bash -CUDA_VISIBLE_DEVICES=0,1,2,3 python main.py -m paddle.distributed.launch --data= --batch_size=16 -d --pretrain_weights=https://paddlemodels.bj.bcebos.com/hapi/darknet53_pretrained.pdparams +CUDA_VISIBLE_DEVICES=0,1,2,3 python main.py -m paddle.distributed.launch --data= --batch_size=16 -d ``` diff --git a/hapi/datasets/coco.py b/examples/yolov3/coco.py similarity index 100% rename from hapi/datasets/coco.py rename to examples/yolov3/coco.py diff --git a/examples/yolov3/infer.py b/examples/yolov3/infer.py index cc7cbdc..8b0e3ab 100644 --- a/examples/yolov3/infer.py +++ b/examples/yolov3/infer.py @@ -25,8 +25,9 @@ from paddle.fluid.optimizer import Momentum from paddle.io import DataLoader from hapi.model import Model, Input, set_device -from hapi.vision.models import yolov3_darknet53, YoloLoss -from hapi.vision.transforms import * + +from modeling import yolov3_darknet53, YoloLoss +from transforms import * from visualizer import draw_bbox diff --git a/examples/yolov3/main.py b/examples/yolov3/main.py index 7203329..dea9eba 100644 --- a/examples/yolov3/main.py +++ b/examples/yolov3/main.py @@ -27,12 +27,12 @@ from paddle.io import DataLoader from hapi.model import Model, Input, set_device from hapi.distributed import DistributedBatchSampler -from hapi.download import is_url, get_weights_path -from hapi.datasets import COCODataset -from hapi.vision.transforms import * -from hapi.vision.models import yolov3_darknet53, YoloLoss +from hapi.vision.transforms import Compose, BatchCompose +from modeling import yolov3_darknet53, YoloLoss +from coco import COCODataset from coco_metric import COCOMetric +from transforms import * NUM_MAX_BOXES = 50 @@ -126,10 +126,7 @@ def main(): pretrained=pretrained) if FLAGS.pretrain_weights and not FLAGS.eval_only: - pretrain_weights = FLAGS.pretrain_weights - if is_url(pretrain_weights): - pretrain_weights = get_weights_path(pretrain_weights) - model.load(pretrain_weights, skip_mismatch=True, reset_optimizer=True) + model.load(FLAGS.pretrain_weights, skip_mismatch=True, reset_optimizer=True) optim = make_optimizer(len(batch_sampler), parameter_list=model.parameters()) @@ -168,7 +165,7 @@ def main(): save_dir="yolo_checkpoint/mixup", save_freq=10) - # do not use image mixup transfrom in laste FLAGS.no_mixup_epoch epoches + # do not use image mixup transfrom in the last FLAGS.no_mixup_epoch epoches dataset.mixup = False model.fit(train_data=loader, epochs=FLAGS.no_mixup_epoch, @@ -200,8 +197,7 @@ if __name__ == '__main__': parser.add_argument( "-j", "--num_workers", default=4, type=int, help="reader worker number") parser.add_argument( - "-p", "--pretrain_weights", - default="./pretrain_weights/darknet53_pretrained", type=str, + "-p", "--pretrain_weights", default=None, type=str, help="path to pretrained weights") parser.add_argument( "-r", "--resume", default=None, type=str, diff --git a/hapi/vision/models/yolov3.py b/examples/yolov3/modeling.py similarity index 87% rename from hapi/vision/models/yolov3.py rename to examples/yolov3/modeling.py index bafe148..be462f5 100644 --- a/hapi/vision/models/yolov3.py +++ b/examples/yolov3/modeling.py @@ -16,13 +16,13 @@ from __future__ import division from __future__ import print_function import paddle.fluid as fluid -from paddle.fluid.dygraph.nn import Conv2D +from paddle.fluid.dygraph.nn import Conv2D, BatchNorm from paddle.fluid.param_attr import ParamAttr from paddle.fluid.regularizer import L2Decay from hapi.model import Model, Loss from hapi.download import get_weights_path -from .darknet import darknet53, ConvBNLayer +from hapi.vision.models import darknet53 __all__ = ['YoloLoss', 'YOLOv3', 'yolov3_darknet53'] @@ -33,6 +33,46 @@ pretrain_infos = { } +class ConvBNLayer(fluid.dygraph.Layer): + def __init__(self, + ch_in, + ch_out, + filter_size=3, + stride=1, + groups=1, + padding=0, + act="leaky"): + super(ConvBNLayer, self).__init__() + + self.conv = Conv2D( + num_channels=ch_in, + num_filters=ch_out, + filter_size=filter_size, + stride=stride, + padding=padding, + groups=groups, + param_attr=ParamAttr( + initializer=fluid.initializer.Normal(0., 0.02)), + bias_attr=False, + act=None) + self.batch_norm = BatchNorm( + num_channels=ch_out, + param_attr=ParamAttr( + initializer=fluid.initializer.Normal(0., 0.02), + regularizer=L2Decay(0.)), + bias_attr=ParamAttr( + initializer=fluid.initializer.Constant(0.0), + regularizer=L2Decay(0.))) + + self.act = act + + def forward(self, inputs): + out = self.conv(inputs) + out = self.batch_norm(out) + if self.act == 'leaky': + out = fluid.layers.leaky_relu(x=out, alpha=0.1) + return out + class YoloDetectionBlock(fluid.dygraph.Layer): def __init__(self, ch_in, channel): super(YoloDetectionBlock, self).__init__() @@ -118,7 +158,7 @@ class YOLOv3(Model): self.nms_posk = 100 self.draw_thresh = 0.5 - self.backbone = darknet53(pretrained=False) + self.backbone = darknet53(pretrained=(model_mode=='train')) self.block_outputs = [] self.yolo_blocks = [] self.route_blocks = [] @@ -254,7 +294,7 @@ def _yolov3_darknet(num_layers=53, num_classes=80, weight_path = get_weights_path(*(pretrain_infos[num_layers])) assert weight_path.endswith('.pdparams'), \ "suffix of weight must be .pdparams" - model.load(weight_path[:-9]) + model.load(weight_path) return model diff --git a/hapi/vision/transforms/detection_transforms.py b/examples/yolov3/transforms.py similarity index 100% rename from hapi/vision/transforms/detection_transforms.py rename to examples/yolov3/transforms.py diff --git a/hapi/datasets/__init__.py b/hapi/datasets/__init__.py index e982a6b..fc5df64 100644 --- a/hapi/datasets/__init__.py +++ b/hapi/datasets/__init__.py @@ -12,7 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. +from . import folder +from . import mnist +from . import flowers + from .folder import * from .mnist import * from .flowers import * -from .coco import * + +__all__ = folder.__all__ \ + + mnist.__all__ \ + + flowers.__all__ diff --git a/hapi/model.py b/hapi/model.py index 4d27355..063e1a6 100644 --- a/hapi/model.py +++ b/hapi/model.py @@ -816,7 +816,7 @@ class Model(fluid.dygraph.Layer): except ValueError as err: if skip_mismatch: warnings.warn( - ("Skip loading for {}. ".format(key) + err.message)) + ("Skip loading for {}. ".format(key) + str(err))) # reset optimizer when mismatch happens reset_optimizer = True else: diff --git a/hapi/vision/models/__init__.py b/hapi/vision/models/__init__.py index 02cf618..d444cd6 100644 --- a/hapi/vision/models/__init__.py +++ b/hapi/vision/models/__init__.py @@ -17,24 +17,15 @@ from . import vgg from . import mobilenetv1 from . import mobilenetv2 from . import darknet -from . import yolov3 -from . import tsm -from . import bmn_model from .resnet import * from .mobilenetv1 import * from .mobilenetv2 import * from .vgg import * from .darknet import * -from .yolov3 import * -from .tsm import * -from .bmn_model import * __all__ = resnet.__all__ \ + vgg.__all__ \ + mobilenetv1.__all__ \ + mobilenetv2.__all__ \ - + darknet.__all__ \ - + yolov3.__all__ \ - + tsm.__all__ \ - + bmn_model.__all__ + + darknet.__all__ diff --git a/hapi/vision/models/darknet.py b/hapi/vision/models/darknet.py index 027cb4f..df05888 100755 --- a/hapi/vision/models/darknet.py +++ b/hapi/vision/models/darknet.py @@ -21,7 +21,7 @@ from paddle.fluid.dygraph.nn import Conv2D, BatchNorm from hapi.model import Model from hapi.download import get_weights_path -__all__ = ['DarkNet', 'ConvBNLayer', 'darknet53'] +__all__ = ['DarkNet', 'darknet53'] # {num_layers: (url, md5)} pretrain_infos = { @@ -136,7 +136,7 @@ class LayerWarp(fluid.dygraph.Layer): DarkNet_cfg = {53: ([1, 2, 8, 8, 4])} -class DarkNet(fluid.dygraph.Layer): +class DarkNet(Model): """DarkNet model from `"YOLOv3: An Incremental Improvement" `_ diff --git a/hapi/vision/transforms/__init__.py b/hapi/vision/transforms/__init__.py index 4367c71..f7c5b63 100644 --- a/hapi/vision/transforms/__init__.py +++ b/hapi/vision/transforms/__init__.py @@ -12,6 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +from . import transforms +from . import functional + from .transforms import * from .functional import * -from .detection_transforms import * + +__all__ = transforms.__all__ \ + + functional.__all__ diff --git a/hapi/vision/transforms/functional.py b/hapi/vision/transforms/functional.py index 8305619..a4ca466 100644 --- a/hapi/vision/transforms/functional.py +++ b/hapi/vision/transforms/functional.py @@ -26,6 +26,8 @@ else: Sequence = collections.abc.Sequence Iterable = collections.abc.Iterable +__all__ = ['flip', 'resize'] + def flip(image, code): """ -- GitLab