diff --git a/examples/yolov3/.gitignore b/examples/yolov3/.gitignore index 01cdc1cbde3507576115b3a9832be30158e56fe5..19ac4b2c67adf15aaa730c8b9823a2b5c2da8780 100644 --- a/examples/yolov3/.gitignore +++ b/examples/yolov3/.gitignore @@ -1 +1,2 @@ dataset/voc* +pretrain_weights/darknet53_pretrained.pdparams diff --git a/examples/yolov3/README.md b/examples/yolov3/README.md index cc6d302a544f9b8d9cd06fc363b90d053919a5e9..a8784ac88a4cb1dd3d72424ff749c1b239faa87f 100644 --- a/examples/yolov3/README.md +++ b/examples/yolov3/README.md @@ -99,9 +99,17 @@ YOLOv3 的网络结构由基础特征提取网络、multi-scale特征融合层 | ... ``` +### 预训练权重下载 + +YOLOv3模型训练需下载骨干网络DarkNet53的预训练权重,可通过如下方式下载。 + +```bash +sh pretrain_weights/download.sh +``` + ### 模型训练 -数据准备完毕后,可使用`main.py`脚本启动训练和评估,如下脚本会自动每epoch交替进行训练和模型评估,并将checkpoint默认保存在`yolo_checkpoint`目录下。 +数据和预训练权重下载完成后,可使用`main.py`脚本启动训练和评估,如下脚本会自动每epoch交替进行训练和模型评估,并将checkpoint默认保存在`yolo_checkpoint`目录下。 YOLOv3模型训练总batch_size为64训练,以下以使用4卡Tesla P40每卡batch_size为16训练介绍训练方式。对于静态图和动态图,多卡训练中`--batch_size`为每卡上的batch_size,即总batch_size为`--batch_size`乘以卡数。 @@ -117,7 +125,7 @@ python main.py --help 使用如下方式进行多卡训练: ```bash -CUDA_VISIBLE_DEVICES=0,1,2,3 python -m paddle.distributed.launch main.py --data= --batch_size=16 +CUDA_VISIBLE_DEVICES=0,1,2,3 python -m paddle.distributed.launch main.py --data= --batch_size=16 --pretrain_weights=./pretrain_weights/darknet53_pretrained ``` #### 动态图训练 @@ -127,7 +135,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 +CUDA_VISIBLE_DEVICES=0,1,2,3 python main.py -m paddle.distributed.launch --data= --batch_size=16 -d --pretrain_weights=./pretrain_weights/darknet53_pretrained ``` diff --git a/examples/yolov3/main.py b/examples/yolov3/main.py index ebe85543712d82267eaded26da4c1db8800b735f..89ba67756844551760ca70c9074eb7c316d8dbc0 100644 --- a/examples/yolov3/main.py +++ b/examples/yolov3/main.py @@ -28,6 +28,7 @@ from paddle.io import DataLoader from hapi.model import Model, Input, set_device from hapi.distributed import DistributedBatchSampler from hapi.datasets import COCODataset +from hapi.download import get_weights_path from hapi.vision.transforms import * from hapi.vision.models import yolov3_darknet53, YoloLoss @@ -124,7 +125,7 @@ def main(): model_mode='eval' if FLAGS.eval_only else 'train', pretrained=pretrained) - if FLAGS.pretrain_weights is not None: + if FLAGS.pretrain_weights and not FLAGS.eval_only: model.load(FLAGS.pretrain_weights, skip_mismatch=True, reset_optimizer=True) optim = make_optimizer(len(batch_sampler), parameter_list=model.parameters()) @@ -196,7 +197,8 @@ if __name__ == '__main__': parser.add_argument( "-j", "--num_workers", default=4, type=int, help="reader worker number") parser.add_argument( - "-p", "--pretrain_weights", default=None, type=str, + "-p", "--pretrain_weights", + default="./pretrain_weights/darknet53_pretrained", type=str, help="path to pretrained weights") parser.add_argument( "-r", "--resume", default=None, type=str, diff --git a/examples/yolov3/pretrain_weights/download.sh b/examples/yolov3/pretrain_weights/download.sh new file mode 100644 index 0000000000000000000000000000000000000000..6982f441969069ea2b53de5242fa455161119573 --- /dev/null +++ b/examples/yolov3/pretrain_weights/download.sh @@ -0,0 +1,5 @@ +DIR="$( cd "$(dirname "$0")" ; pwd -P )" +cd "$DIR" + +echo "Downloading https://paddlemodels.bj.bcebos.com/hapi/darknet53_pretrained.pdparams" +wget https://paddlemodels.bj.bcebos.com/hapi/darknet53_pretrained.pdparams diff --git a/hapi/vision/models/darknet.py b/hapi/vision/models/darknet.py index 85e25f4e1205ea62ec878409d640ba42e7335ee2..027cb4f2094e37ede347c090c5849cfcbf00611e 100755 --- a/hapi/vision/models/darknet.py +++ b/hapi/vision/models/darknet.py @@ -136,7 +136,7 @@ class LayerWarp(fluid.dygraph.Layer): DarkNet_cfg = {53: ([1, 2, 8, 8, 4])} -class DarkNet(Model): +class DarkNet(fluid.dygraph.Layer): """DarkNet model from `"YOLOv3: An Incremental Improvement" `_ diff --git a/hapi/vision/models/yolov3.py b/hapi/vision/models/yolov3.py index 840a402e3e1b473cc3c3deffc1b2d0fe6e7a2307..bafe148b14b7e1baf8124c61969238e049e2d6f0 100644 --- a/hapi/vision/models/yolov3.py +++ b/hapi/vision/models/yolov3.py @@ -118,7 +118,7 @@ class YOLOv3(Model): self.nms_posk = 100 self.draw_thresh = 0.5 - self.backbone = darknet53(pretrained=(model_mode=='train')) + self.backbone = darknet53(pretrained=False) self.block_outputs = [] self.yolo_blocks = [] self.route_blocks = []