diff --git a/fluid/PaddleCV/image_classification/README.md b/fluid/PaddleCV/image_classification/README.md index 02500e90e9ff519029952680cb6942ebe38b0a6a..3a20bdf4aa44e2752b8939bfa49886e7c0d5f6f0 100644 --- a/fluid/PaddleCV/image_classification/README.md +++ b/fluid/PaddleCV/image_classification/README.md @@ -81,7 +81,7 @@ python train.py \ * **lr**: initialized learning rate. Default: 0.1. * **pretrained_model**: model path for pretraining. Default: None. * **checkpoint**: the checkpoint path to resume. Default: None. -* **model_category**: the category of models, ("models"|"models_name"). Default: "models". +* **model_category**: the category of models, ("models"|"models_name"). Default: "models_name". Or can start the training step by running the ```run.sh```. @@ -221,6 +221,8 @@ Models are trained by starting with learning rate ```0.1``` and decaying it by ` - Released models: not specify parameter names +**NOTE: These are trained by using model_category=models** + |model | top-1/top-5 accuracy(PIL)| top-1/top-5 accuracy(CV2) | |- |:-: |:-:| |[ResNet152](http://paddle-imagenet-models.bj.bcebos.com/ResNet152_pretrained.zip) | 78.18%/93.93% | 78.11%/94.04% | diff --git a/fluid/PaddleCV/image_classification/README_cn.md b/fluid/PaddleCV/image_classification/README_cn.md index 17e911596f2cb9830a51efaa839d4fa5fff38a74..c9f553e311ff07e76eab2274646270f5de9b8fa2 100644 --- a/fluid/PaddleCV/image_classification/README_cn.md +++ b/fluid/PaddleCV/image_classification/README_cn.md @@ -79,7 +79,7 @@ python train.py \ * **lr**: initialized learning rate. Default: 0.1. * **pretrained_model**: model path for pretraining. Default: None. * **checkpoint**: the checkpoint path to resume. Default: None. -* **model_category**: the category of models, ("models"|"models_name"). Default:"models". +* **model_category**: the category of models, ("models"|"models_name"). Default:"models_name". **数据读取器说明:** 数据读取器定义在```reader.py```和```reader_cv2.py```中, 一般, CV2 reader可以提高数据读取速度, reader(PIL)可以得到相对更高的精度, 在[训练阶段](#training-a-model), 默认采用的增广方式是随机裁剪与水平翻转, 而在[评估](#inference)与[推断](#inference)阶段用的默认方式是中心裁剪。当前支持的数据增广方式有: * 旋转 @@ -213,6 +213,8 @@ Models包括两种模型:带有参数名字的模型,和不带有参数名 - Released models: not specify parameter names +**注意:这是model_category = models 的预训练模型** + |model | top-1/top-5 accuracy(PIL)| top-1/top-5 accuracy(CV2) | |- |:-: |:-:| |[ResNet152](http://paddle-imagenet-models.bj.bcebos.com/ResNet152_pretrained.zip) | 78.18%/93.93% | 78.11%/94.04% | diff --git a/fluid/PaddleCV/image_classification/eval.py b/fluid/PaddleCV/image_classification/eval.py index ddce243fe1fcae81ee6064c7ff185fb8a045a402..0660efe13750467ad6bf964b484c9db0ab44b1ee 100644 --- a/fluid/PaddleCV/image_classification/eval.py +++ b/fluid/PaddleCV/image_classification/eval.py @@ -7,8 +7,6 @@ import time import sys import paddle import paddle.fluid as fluid -#import models -import models_name as models #import reader_cv2 as reader import reader as reader import argparse @@ -26,10 +24,21 @@ add_arg('class_dim', int, 1000, "Class number.") add_arg('image_shape', str, "3,224,224", "Input image size") add_arg('with_mem_opt', bool, True, "Whether to use memory optimization or not.") add_arg('pretrained_model', str, None, "Whether to use pretrained model.") -add_arg('model', str, "SE_ResNeXt50_32x4d", "Set the network to use.") +add_arg('model', str, "SE_ResNeXt50_32x4d", "Set the network to use.") +add_arg('model_category', str, "models_name", "Whether to use models_name or not, valid value:'models','models_name'." ) + # yapf: enable -model_list = [m for m in dir(models) if "__" not in m] + +def set_models(model_category): + global models + assert model_category in ["models", "models_name" + ], "{} is not in lists: {}".format( + model_category, ["models", "models_name"]) + if model_category == "models_name": + import models_name as models + else: + import models as models def eval(args): @@ -40,6 +49,7 @@ def eval(args): with_memory_optimization = args.with_mem_opt image_shape = [int(m) for m in args.image_shape.split(",")] + model_list = [m for m in dir(models) if "__" not in m] assert model_name in model_list, "{} is not in lists: {}".format(args.model, model_list) @@ -63,11 +73,11 @@ def eval(args): acc_top5 = fluid.layers.accuracy(input=out0, label=label, k=5) else: out = model.net(input=image, class_dim=class_dim) - cost = fluid.layers.cross_entropy(input=out, label=label) - + cost, pred = fluid.layers.softmax_with_cross_entropy( + out, label, return_softmax=True) avg_cost = fluid.layers.mean(x=cost) - acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1) - acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5) + acc_top1 = fluid.layers.accuracy(input=pred, label=label, k=1) + acc_top5 = fluid.layers.accuracy(input=pred, label=label, k=5) test_program = fluid.default_main_program().clone(for_test=True) @@ -125,6 +135,7 @@ def eval(args): def main(): args = parser.parse_args() print_arguments(args) + set_models(args.model_category) eval(args) diff --git a/fluid/PaddleCV/image_classification/infer.py b/fluid/PaddleCV/image_classification/infer.py index e89c08d923cdc37596c76dc7146a2666b719844d..88ccf42912b67035895cd81f5f982edca1bd0a3e 100644 --- a/fluid/PaddleCV/image_classification/infer.py +++ b/fluid/PaddleCV/image_classification/infer.py @@ -7,7 +7,6 @@ import time import sys import paddle import paddle.fluid as fluid -import models import reader import argparse import functools @@ -23,9 +22,19 @@ add_arg('image_shape', str, "3,224,224", "Input image size") add_arg('with_mem_opt', bool, True, "Whether to use memory optimization or not.") add_arg('pretrained_model', str, None, "Whether to use pretrained model.") add_arg('model', str, "SE_ResNeXt50_32x4d", "Set the network to use.") +add_arg('model_category', str, "models_name", "Whether to use models_name or not, valid value:'models','models_name'." ) # yapf: enable -model_list = [m for m in dir(models) if "__" not in m] + +def set_models(model_category): + global models + assert model_category in ["models", "models_name" + ], "{} is not in lists: {}".format( + model_category, ["models", "models_name"]) + if model_category == "models_name": + import models_name as models + else: + import models as models def infer(args): @@ -35,7 +44,7 @@ def infer(args): pretrained_model = args.pretrained_model with_memory_optimization = args.with_mem_opt image_shape = [int(m) for m in args.image_shape.split(",")] - + model_list = [m for m in dir(models) if "__" not in m] assert model_name in model_list, "{} is not in lists: {}".format(args.model, model_list) @@ -85,6 +94,7 @@ def infer(args): def main(): args = parser.parse_args() print_arguments(args) + set_models(args.model_category) infer(args) diff --git a/fluid/PaddleCV/image_classification/run.sh b/fluid/PaddleCV/image_classification/run.sh index 41e5e493468a8d8bffbf6c6aabc0d7e7947e989b..b0cc2255b03db82bc88397f625ed68023280d2f0 100644 --- a/fluid/PaddleCV/image_classification/run.sh +++ b/fluid/PaddleCV/image_classification/run.sh @@ -1,16 +1,19 @@ #Hyperparameters config +#Example: SE_ResNext50_32x4d python train.py \ --model=SE_ResNeXt50_32x4d \ - --batch_size=32 \ + --batch_size=400 \ --total_images=1281167 \ --class_dim=1000 \ --image_shape=3,224,224 \ --model_save_dir=output/ \ --with_mem_opt=True \ - --lr_strategy=piecewise_decay \ - --lr=0.1 + --lr_strategy=cosine_decay \ + --lr=0.1 \ + --num_epochs=200 \ + --l2_decay=1.2e-4 \ + --model_category=models_name \ # >log_SE_ResNeXt50_32x4d.txt 2>&1 & - #AlexNet: #python train.py \ # --model=AlexNet \ @@ -20,23 +23,11 @@ python train.py \ # --image_shape=3,224,224 \ # --model_save_dir=output/ \ # --with_mem_opt=True \ +# --model_category=models_name \ # --lr_strategy=piecewise_decay \ # --num_epochs=120 \ -# --lr=0.01 - -#VGG11: -#python train.py \ -# --model=VGG11 \ -# --batch_size=512 \ -# --total_images=1281167 \ -# --class_dim=1000 \ -# --image_shape=3,224,224 \ -# --model_save_dir=output/ \ -# --with_mem_opt=True \ -# --lr_strategy=piecewise_decay \ -# --num_epochs=120 \ -# --lr=0.1 - +# --lr=0.01 \ +# --l2_decay=1e-4 #MobileNet v1: #python train.py \ @@ -47,9 +38,11 @@ python train.py \ # --image_shape=3,224,224 \ # --model_save_dir=output/ \ # --with_mem_opt=True \ +# --model_category=models_name \ # --lr_strategy=piecewise_decay \ # --num_epochs=120 \ -# --lr=0.1 +# --lr=0.1 \ +# --l2_decay=3e-5 #python train.py \ # --model=MobileNetV2 \ @@ -58,10 +51,12 @@ python train.py \ # --class_dim=1000 \ # --image_shape=3,224,224 \ # --model_save_dir=output/ \ +# --model_category=models_name \ # --with_mem_opt=True \ # --lr_strategy=cosine_decay \ -# --num_epochs=200 \ -# --lr=0.1 +# --num_epochs=240 \ +# --lr=0.1 \ +# --l2_decay=4e-5 #ResNet50: #python train.py \ # --model=ResNet50 \ @@ -71,9 +66,11 @@ python train.py \ # --image_shape=3,224,224 \ # --model_save_dir=output/ \ # --with_mem_opt=True \ +# --model_category=models_name \ # --lr_strategy=piecewise_decay \ # --num_epochs=120 \ -# --lr=0.1 +# --lr=0.1 \ +# --l2_decay=1e-4 #ResNet101: #python train.py \ @@ -83,44 +80,58 @@ python train.py \ # --class_dim=1000 \ # --image_shape=3,224,224 \ # --model_save_dir=output/ \ -# --with_mem_opt=False \ +# --model_category=models_name \ +# --with_mem_opt=True \ # --lr_strategy=piecewise_decay \ # --num_epochs=120 \ -# --lr=0.1 +# --lr=0.1 \ +# --l2_decay=1e-4 #ResNet152: #python train.py \ # --model=ResNet152 \ # --batch_size=256 \ # --total_images=1281167 \ +# --class_dim=1000 \ # --image_shape=3,224,224 \ +# --model_save_dir=output/ \ # --lr_strategy=piecewise_decay \ +# --model_category=models_name \ +# --with_mem_opt=True \ # --lr=0.1 \ # --num_epochs=120 \ # --l2_decay=1e-4 -#SE_ResNeXt50: +#SE_ResNeXt50_32x4d: #python train.py \ -# --model=SE_ResNeXt50 \ +# --model=SE_ResNeXt50_32x4d \ # --batch_size=400 \ # --total_images=1281167 \ +# --class_dim=1000 \ # --image_shape=3,224,224 \ # --lr_strategy=cosine_decay \ +# --model_category=models_name \ +# --model_save_dir=output/ \ # --lr=0.1 \ # --num_epochs=200 \ -# --l2_decay=12e-5 +# --with_mem_opt=True \ +# --l2_decay=1.2e-4 -#SE_ResNeXt101: +#SE_ResNeXt101_32x4d: #python train.py \ -# --model=SE_ResNeXt101 \ +# --model=SE_ResNeXt101_32x4d \ # --batch_size=400 \ # --total_images=1281167 \ +# --class_dim=1000 \ # --image_shape=3,224,224 \ # --lr_strategy=cosine_decay \ +# --model_category=models_name \ +# --model_save_dir=output/ \ # --lr=0.1 \ # --num_epochs=200 \ -# --l2_decay=15e-5 +# --with_mem_opt=True \ +# --l2_decay=1.5e-5 #VGG11: #python train.py \ @@ -129,8 +140,12 @@ python train.py \ # --total_images=1281167 \ # --image_shape=3,224,224 \ # --lr_strategy=cosine_decay \ +# --class_dim=1000 \ +# --model_category=models_name \ +# --model_save_dir=output/ \ # --lr=0.1 \ # --num_epochs=90 \ +# --with_mem_opt=True \ # --l2_decay=2e-4 #VGG13: @@ -138,8 +153,42 @@ python train.py \ # --model=VGG13 \ # --batch_size=256 \ # --total_images=1281167 \ +# --class_dim=1000 \ # --image_shape=3,224,224 \ # --lr_strategy=cosine_decay \ # --lr=0.01 \ # --num_epochs=90 \ +# --model_category=models_name \ +# --model_save_dir=output/ \ +# --with_mem_opt=True \ # --l2_decay=3e-4 + +#VGG16: +#python train.py +# --model=VGG16 \ +# --batch_size=256 \ +# --total_images=1281167 \ +# --class_dim=1000 \ +# --lr_strategy=cosine_decay \ +# --image_shape=3,224,224 \ +# --model_category=models_name \ +# --model_save_dir=output/ \ +# --lr=0.01 \ +# --num_epochs=90 \ +# --with_mem_opt=True \ +# --l2_decay=3e-4 + +#VGG19: +#python train.py +# --model=VGG19 \ +# --batch_size=256 \ +# --total_images=1281167 \ +# --class_dim=1000 \ +# --image_shape=3,224,224 \ +# --lr_strategy=cosine_decay \ +# --lr=0.01 \ +# --num_epochs=90 \ +# --with_mem_opt=True \ +# --model_category=models_name \ +# --model_save_dir=output/ \ +# --l2_decay=3e-4 diff --git a/fluid/PaddleCV/image_classification/train.py b/fluid/PaddleCV/image_classification/train.py index 87cef29e0eed4c9ed0944a7f5b3a14b76766d579..145b288620bbbb27693bacbb7145e4df8371a4c2 100644 --- a/fluid/PaddleCV/image_classification/train.py +++ b/fluid/PaddleCV/image_classification/train.py @@ -39,7 +39,7 @@ add_arg('lr_strategy', str, "piecewise_decay", "Set the learning rate add_arg('model', str, "SE_ResNeXt50_32x4d", "Set the network to use.") add_arg('enable_ce', bool, False, "If set True, enable continuous evaluation job.") add_arg('data_dir', str, "./data/ILSVRC2012", "The ImageNet dataset root dir.") -add_arg('model_category', str, "models", "Whether to use models_name or not, valid value:'models','models_name'." ) +add_arg('model_category', str, "models_name", "Whether to use models_name or not, valid value:'models','models_name'." ) add_arg('fp16', bool, False, "Enable half precision training with fp16." ) add_arg('scale_loss', float, 1.0, "Scale loss for fp16." ) add_arg('l2_decay', float, 1e-4, "L2_decay parameter.")