diff --git a/configs/cityscape.yaml b/configs/cityscape.yaml index 0650d58a068e19c6cfe36f2d01c8e37dd5935045..f14234d77970e680cb39be46287f94ee5999c1a7 100644 --- a/configs/cityscape.yaml +++ b/configs/cityscape.yaml @@ -10,18 +10,6 @@ AUG: MIN_SCALE_FACTOR: 0.5 # for stepscaling SCALE_STEP_SIZE: 0.25 # for stepscaling MIRROR: True - RICH_CROP: - ENABLE: False - ASPECT_RATIO: 0.33 - BLUR: True - BLUR_RATIO: 0.1 - FLIP: True - FLIP_RATIO: 0.2 - MAX_ROTATION: 15 - MIN_AREA_RATIO: 0.5 - BRIGHTNESS_JITTER_RATIO: 0.5 - CONTRAST_JITTER_RATIO: 0.5 - SATURATION_JITTER_RATIO: 0.5 BATCH_SIZE: 4 DATASET: DATA_DIR: "./dataset/cityscapes/" @@ -44,8 +32,7 @@ TEST: TEST_MODEL: "snapshots/cityscape_v5/final/" TRAIN: MODEL_SAVE_DIR: "snapshots/cityscape_v7/" - PRETRAINED_MODEL: u"pretrain/deeplabv3plus_gn_init" - RESUME: False + PRETRAINED_MODEL_DIR: "pretrain/deeplabv3plus_gn_init" SNAPSHOT_EPOCH: 10 SOLVER: LR: 0.001 diff --git a/configs/unet_pet.yaml b/configs/unet_pet.yaml index 23bd68d5918fe89c3771cdc2e46871918b54e29c..2f3cc50e7e99ea7b8ff749d57f8319aa6b212a6f 100644 --- a/configs/unet_pet.yaml +++ b/configs/unet_pet.yaml @@ -12,18 +12,6 @@ AUG: MIN_SCALE_FACTOR: 0.75 # for stepscaling SCALE_STEP_SIZE: 0.25 # for stepscaling MIRROR: True - RICH_CROP: - ENABLE: False - ASPECT_RATIO: 0.33 - BLUR: True - BLUR_RATIO: 0.1 - FLIP: True - FLIP_RATIO: 0.2 - MAX_ROTATION: 15 - MIN_AREA_RATIO: 0.5 - BRIGHTNESS_JITTER_RATIO: 0.5 - CONTRAST_JITTER_RATIO: 0.5 - SATURATION_JITTER_RATIO: 0.5 BATCH_SIZE: 4 DATASET: DATA_DIR: "./dataset/mini_pet/" @@ -45,8 +33,7 @@ TEST: TEST_MODEL: "./test/saved_model/unet_pet/final/" TRAIN: MODEL_SAVE_DIR: "./test/saved_models/unet_pet/" - PRETRAINED_MODEL: "./test/models/unet_coco/" - RESUME: False + PRETRAINED_MODEL_DIR: "./test/models/unet_coco/" SNAPSHOT_EPOCH: 10 SOLVER: NUM_EPOCHS: 500 diff --git a/docs/configs/train_group.md b/docs/configs/train_group.md index 3c7c0fbb6efc04d232bbe3ba54275cb91bac699b..6c8a0d79c79af665d8c7bf54a2b7555aa024bb8d 100644 --- a/docs/configs/train_group.md +++ b/docs/configs/train_group.md @@ -11,7 +11,7 @@ TRAIN Group存放所有和训练相关的配置

-## `PRETRAINED_MODEL` +## `PRETRAINED_MODEL_DIR` 预训练模型路径 ## 默认值 @@ -28,19 +28,15 @@ TRAIN Group存放所有和训练相关的配置

-## `RESUME` -是否从预训练模型中恢复参数并继续训练 +## `RESUME_MODEL_DIR` +从指定路径中恢复参数并继续训练 ## 默认值 -False +无 ## 注意事项 -* 当该字段被置为True且`PRETRAINED_MODEL`不存在时,该选项不生效 - -* 当该字段被置为True且`PRETRAINED_MODEL`存在时,PaddleSeg会恢复到上一次训练的最近一个epoch,并且恢复训练过程中的临时变量(如已经衰减过的学习率,Optimizer的动量数据等) - -* 当该字段被置为True且`PRETRAINED_MODEL`存在时,`PRETRAINED_MODEL`路径的最后一个目录必须为int数值或者字符串final,PaddleSeg会将int数值作为当前起始EPOCH继续训练,若目录为final,则不会继续训练。若目录不满足上述条件,PaddleSeg会抛出错误。 +* 当`RESUME_MODEL_DIR`存在时,PaddleSeg会恢复到上一次训练的最近一个epoch,并且恢复训练过程中的临时变量(如已经衰减过的学习率,Optimizer的动量数据等),`PRETRAINED_MODEL`路径的最后一个目录必须为int数值或者字符串final,PaddleSeg会将int数值作为当前起始EPOCH继续训练,若目录为final,则不会继续训练。若目录不满足上述条件,PaddleSeg会抛出错误。

@@ -57,4 +53,4 @@ False * 仅在GPU多卡训练时该开关有效(Windows不支持多卡训练,因此无需打开该开关) -* GPU多卡训练时,建议开启该开关,可以提升模型的训练效果 \ No newline at end of file +* GPU多卡训练时,建议开启该开关,可以提升模型的训练效果 diff --git a/docs/data_aug.md b/docs/data_aug.md index 04a5ef3c6c3b37689fdace629f8973171904c389..92022bf54b3c791dcc9af820c54aad98e77e4428 100644 --- a/docs/data_aug.md +++ b/docs/data_aug.md @@ -35,9 +35,6 @@ rich crop是指对图像进行多种变换,保证在训练过程中数据的 - blur 图像加模糊,使用开关`AUG.RICH_CROP.BLUR`,为False时该项功能关闭。`AUG.RICH_CROP.BLUR_RATIO`控制加入模糊的概率。 -- flip -图像上下翻转,使用开关`AUG.RICH_CROP.FLIP`,为False时该项功能关闭。`AUG.RICH_CROP.FLIP_RATIO`控制上下翻转的概率。 - - rotation 图像旋转,`AUG.RICH_CROP.MAX_ROTATION`控制最大旋转角度。旋转产生的多余的区域的填充值为均值。 diff --git a/docs/usage.md b/docs/usage.md index f9fc13bee08d9441b45664687a980c70b43bc9bd..7b06846c565281ede2dab7bc5d40eaa1eb607941 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -76,7 +76,7 @@ python pdseg/train.py --use_gpu \ --tb_log_dir train_log \ --cfg configs/unet_pet.yaml \ BATCH_SIZE 4 \ - TRAIN.PRETRAINED_MODEL pretrained_model/unet_bn_coco \ + TRAIN.PRETRAINED_MODEL_DIR pretrained_model/unet_bn_coco \ TRAIN.SYNC_BATCH_NORM True \ SOLVER.LR 5e-5 ``` diff --git a/pdseg/reader.py b/pdseg/reader.py index b4ebb4eba753b91aaa619c012611b7cab0614841..c839828cf99b89ceff62837cd6877a5659f80d06 100644 --- a/pdseg/reader.py +++ b/pdseg/reader.py @@ -261,17 +261,18 @@ class SegDataset(object): SATURATION_JITTER_RATIO, contrast_jitter_ratio=cfg.AUG.RICH_CROP. CONTRAST_JITTER_RATIO) - if cfg.AUG.RICH_CROP.FLIP: - if cfg.AUG.RICH_CROP.FLIP_RATIO <= 0: - n = 0 - elif cfg.AUG.RICH_CROP.FLIP_RATIO >= 1: - n = 1 - else: - n = int(1.0 / cfg.AUG.RICH_CROP.FLIP_RATIO) - if n > 0: - if np.random.randint(0, n) == 0: - img = img[::-1, :, :] - grt = grt[::-1, :] + + if cfg.AUG.FLIP: + if cfg.AUG.FLIP_RATIO <= 0: + n = 0 + elif cfg.AUG.FLIP_RATIO >= 1: + n = 1 + else: + n = int(1.0 / cfg.AUG.FLIP_RATIO) + if n > 0: + if np.random.randint(0, n) == 0: + img = img[::-1, :, :] + grt = grt[::-1, :] if cfg.AUG.MIRROR: if np.random.randint(0, 2) == 1: diff --git a/pdseg/train.py b/pdseg/train.py index 3238701d0ec3832a098c26ae14ab3474fb76b873..295468ef14b87d8f4886fe0203d362bef3611a42 100644 --- a/pdseg/train.py +++ b/pdseg/train.py @@ -152,15 +152,15 @@ def load_checkpoint(exe, program): Load checkpoiont from pretrained model directory for resume training """ - print('Resume model training from:', cfg.TRAIN.PRETRAINED_MODEL) - if not os.path.exists(cfg.TRAIN.PRETRAINED_MODEL): + print('Resume model training from:', cfg.TRAIN.RESUME_MODEL_DIR) + if not os.path.exists(cfg.TRAIN.RESUME_MODEL_DIR): raise ValueError("TRAIN.PRETRAIN_MODEL {} not exist!".format( - cfg.TRAIN.PRETRAINED_MODEL)) + cfg.TRAIN.RESUME_MODEL_DIR)) fluid.io.load_persistables( - exe, cfg.TRAIN.PRETRAINED_MODEL, main_program=program) + exe, cfg.TRAIN.RESUME_MODEL_DIR, main_program=program) - model_path = cfg.TRAIN.PRETRAINED_MODEL + model_path = cfg.TRAIN.RESUME_MODEL_DIR # Check is path ended by path spearator if model_path[-1] == os.sep: model_path = model_path[0:-1] @@ -255,11 +255,11 @@ def train(cfg): # Resume training begin_epoch = cfg.SOLVER.BEGIN_EPOCH - if cfg.TRAIN.RESUME: + if cfg.TRAIN.RESUME_MODEL_DIR: begin_epoch = load_checkpoint(exe, train_prog) # Load pretrained model - elif os.path.exists(cfg.TRAIN.PRETRAINED_MODEL): - print('Pretrained model dir:', cfg.TRAIN.PRETRAINED_MODEL) + elif os.path.exists(cfg.TRAIN.PRETRAINED_MODEL_DIR): + print('Pretrained model dir:', cfg.TRAIN.PRETRAINED_MODEL_DIR) load_vars = [] load_fail_vars = [] @@ -268,10 +268,10 @@ def train(cfg): Check whehter persitable variable shape is match with current network """ var_exist = os.path.exists( - os.path.join(cfg.TRAIN.PRETRAINED_MODEL, var.name)) + os.path.join(cfg.TRAIN.PRETRAINED_MODEL_DIR, var.name)) if var_exist: var_shape = parse_shape_from_file( - os.path.join(cfg.TRAIN.PRETRAINED_MODEL, var.name)) + os.path.join(cfg.TRAIN.PRETRAINED_MODEL_DIR, var.name)) return var_shape == shape return False @@ -285,10 +285,10 @@ def train(cfg): load_fail_vars.append(x) if cfg.MODEL.FP16: # If open FP16 training mode, load FP16 var separate - load_fp16_vars(exe, cfg.TRAIN.PRETRAINED_MODEL, train_prog) + load_fp16_vars(exe, cfg.TRAIN.PRETRAINED_MODEL_DIR, train_prog) else: fluid.io.load_vars( - exe, dirname=cfg.TRAIN.PRETRAINED_MODEL, vars=load_vars) + exe, dirname=cfg.TRAIN.PRETRAINED_MODEL_DIR, vars=load_vars) for var in load_vars: print("Parameter[{}] loaded sucessfully!".format(var.name)) for var in load_fail_vars: @@ -299,7 +299,7 @@ def train(cfg): len(load_vars) + len(load_fail_vars))) else: print('Pretrained model dir {} not exists, training from scratch...'. - format(cfg.TRAIN.PRETRAINED_MODEL)) + format(cfg.TRAIN.PRETRAINED_MODEL_DIR)) fetch_list = [avg_loss.name, lr.name] if args.debug: diff --git a/pdseg/utils/config.py b/pdseg/utils/config.py index d386721f46b09f2d306ad753d3d5d4b71639fbc1..1c1c56a6508988cf23fc382771593bd9e8cc52b4 100644 --- a/pdseg/utils/config.py +++ b/pdseg/utils/config.py @@ -69,6 +69,10 @@ cfg.DATASET.IGNORE_INDEX = 255 ########################### 数据增强配置 ###################################### # 图像镜像左右翻转 cfg.AUG.MIRROR = True +# 图像上下翻转开关,True/False +cfg.AUG.FLIP = False +# 图像启动上下翻转的概率,0-1 +cfg.AUG.FLIP_RATIO = 0.5 # 图像resize的固定尺寸(宽,高),非负 cfg.AUG.FIX_RESIZE_SIZE = tuple() # 图像resize的方式有三种: @@ -107,18 +111,14 @@ cfg.AUG.RICH_CROP.CONTRAST_JITTER_RATIO = 0.5 cfg.AUG.RICH_CROP.BLUR = False # 图像启动模糊百分比,0-1 cfg.AUG.RICH_CROP.BLUR_RATIO = 0.1 -# 图像上下翻转开关,True/False -cfg.AUG.RICH_CROP.FLIP = False -# 图像启动上下翻转的概率,0-1 -cfg.AUG.RICH_CROP.FLIP_RATIO = 0.2 ########################### 训练配置 ########################################## # 模型保存路径 cfg.TRAIN.MODEL_SAVE_DIR = '' # 预训练模型路径 -cfg.TRAIN.PRETRAINED_MODEL = '' +cfg.TRAIN.PRETRAINED_MODEL_DIR = '' # 是否resume,继续训练 -cfg.TRAIN.RESUME = False +cfg.TRAIN.RESUME_MODEL_DIR = '' # 是否使用多卡间同步BatchNorm均值和方差 cfg.TRAIN.SYNC_BATCH_NORM = False # 模型参数保存的epoch间隔数,可用来继续训练中断的模型 diff --git a/test/configs/deeplabv3p_xception65_cityscapes.yaml b/test/configs/deeplabv3p_xception65_cityscapes.yaml index 612ac31bb304081a6c8900ff08d3ef61df62fbdf..349646f743f10c7970b248b30c258574c8478c68 100644 --- a/test/configs/deeplabv3p_xception65_cityscapes.yaml +++ b/test/configs/deeplabv3p_xception65_cityscapes.yaml @@ -10,18 +10,6 @@ AUG: MIN_SCALE_FACTOR: 0.5 # for stepscaling SCALE_STEP_SIZE: 0.25 # for stepscaling MIRROR: True - RICH_CROP: - ENABLE: False - ASPECT_RATIO: 0.33 - BLUR: True - BLUR_RATIO: 0.1 - FLIP: True - FLIP_RATIO: 0.2 - MAX_ROTATION: 15 - MIN_AREA_RATIO: 0.5 - BRIGHTNESS_JITTER_RATIO: 0.5 - CONTRAST_JITTER_RATIO: 0.5 - SATURATION_JITTER_RATIO: 0.5 BATCH_SIZE: 4 DATASET: DATA_DIR: "./dataset/cityscapes/" @@ -46,8 +34,7 @@ TEST: TEST_MODEL: "snapshots/cityscape_v5/final/" TRAIN: MODEL_SAVE_DIR: "snapshots/cityscape_v5/" - PRETRAINED_MODEL: "pretrain/deeplabv3plus_gn_init" - RESUME: False + PRETRAINED_MODEL_DIR: "pretrain/deeplabv3plus_gn_init" SNAPSHOT_EPOCH: 10 SOLVER: LR: 0.001 diff --git a/test/configs/unet_pet.yaml b/test/configs/unet_pet.yaml index e561e463bbdaddbe532d47a862c3e29095909f95..3a3cf65a09dfbff51e79ca65bd19c1c11fb75d64 100644 --- a/test/configs/unet_pet.yaml +++ b/test/configs/unet_pet.yaml @@ -12,18 +12,6 @@ AUG: MIN_SCALE_FACTOR: 0.75 # for stepscaling SCALE_STEP_SIZE: 0.25 # for stepscaling MIRROR: True - RICH_CROP: - ENABLE: False - ASPECT_RATIO: 0.33 - BLUR: True - BLUR_RATIO: 0.1 - FLIP: True - FLIP_RATIO: 0.2 - MAX_ROTATION: 15 - MIN_AREA_RATIO: 0.5 - BRIGHTNESS_JITTER_RATIO: 0.5 - CONTRAST_JITTER_RATIO: 0.5 - SATURATION_JITTER_RATIO: 0.5 BATCH_SIZE: 6 DATASET: DATA_DIR: "./dataset/pet/" @@ -45,8 +33,7 @@ TEST: TEST_MODEL: "./test/saved_model/unet_pet/final/" TRAIN: MODEL_SAVE_DIR: "./test/saved_models/unet_pet/" - PRETRAINED_MODEL: "./test/models/unet_coco/" - RESUME: False + PRETRAINED_MODEL_DIR: "./test/models/unet_coco/" SNAPSHOT_EPOCH: 10 SOLVER: NUM_EPOCHS: 500 diff --git a/test/local_test_cityscapes.py b/test/local_test_cityscapes.py index 6618695a60aae5f07230c546337b611d7c1cc78a..051faaa1b4e4c769a996b94b411b65815eb3a9e7 100644 --- a/test/local_test_cityscapes.py +++ b/test/local_test_cityscapes.py @@ -45,7 +45,8 @@ if __name__ == "__main__": saved_model = os.path.join(LOCAL_PATH, "saved_model", model_name) parser = argparse.ArgumentParser(description="PaddleSeg loacl test") - parser.add_argument("--devices", + parser.add_argument( + "--devices", dest="devices", help="GPU id of running. if more than one, use spacing to separate.", nargs="+", @@ -75,7 +76,7 @@ if __name__ == "__main__": train( flags=["--cfg", cfg, "--use_gpu", "--log_steps", "10"], options=[ - "SOLVER.NUM_EPOCHS", "1", "TRAIN.PRETRAINED_MODEL", test_model, + "SOLVER.NUM_EPOCHS", "1", "TRAIN.PRETRAINED_MODEL_DIR", test_model, "TRAIN.MODEL_SAVE_DIR", saved_model ], devices=devices) diff --git a/test/local_test_pet.py b/test/local_test_pet.py index 7d0cf58cd1235575fc960769d5142865993b5763..f043d16a5c7a9d7d45db8ce91864a8c5325876b9 100644 --- a/test/local_test_pet.py +++ b/test/local_test_pet.py @@ -46,7 +46,8 @@ if __name__ == "__main__": saved_model = os.path.join(LOCAL_PATH, "saved_model", model_name) parser = argparse.ArgumentParser(description="PaddleSeg loacl test") - parser.add_argument("--devices", + parser.add_argument( + "--devices", dest="devices", help="GPU id of running. if more than one, use spacing to separate.", nargs="+", @@ -59,7 +60,7 @@ if __name__ == "__main__": train( flags=["--cfg", cfg, "--use_gpu", "--log_steps", "10"], options=[ - "SOLVER.NUM_EPOCHS", "1", "TRAIN.PRETRAINED_MODEL", test_model, + "SOLVER.NUM_EPOCHS", "1", "TRAIN.PRETRAINED_MODEL_DIR", test_model, "TRAIN.MODEL_SAVE_DIR", saved_model, "DATASET.TRAIN_FILE_LIST", os.path.join(DATASET_PATH, "mini_pet", "file_list", "train_list.txt"), "DATASET.VAL_FILE_LIST", diff --git a/turtorial/finetune_deeplabv3plus.md b/turtorial/finetune_deeplabv3plus.md index 082410e59fb11bb344dd381fef26f1e38eb3d0d5..02ce636a2f50f0387dea68a653b5443134a4edbc 100644 --- a/turtorial/finetune_deeplabv3plus.md +++ b/turtorial/finetune_deeplabv3plus.md @@ -67,7 +67,7 @@ MODEL: DEEPLAB: BACKBONE: "xception_65" TRAIN: - PRETRAINED_MODEL: "./pretrained_model/deeplabv3p_xception65_bn_pet/" + PRETRAINED_MODEL_DIR: "./pretrained_model/deeplabv3p_xception65_bn_pet/" # 其他配置 @@ -79,7 +79,6 @@ AUG: BATCH_SIZE: 4 TRAIN: MODEL_SAVE_DIR: "./finetune/deeplabv3p_xception65_bn_pet/" - RESUME: False SNAPSHOT_EPOCH: 10 TEST: TEST_MODEL: "./finetune/deeplabv3p_xception65_bn_pet/final" diff --git a/turtorial/finetune_icnet.md b/turtorial/finetune_icnet.md index 234e9d175769400916a06e314b6fe09471352f8c..3fbebb306860d73e0d04dd8db4020b9103cdd8cc 100644 --- a/turtorial/finetune_icnet.md +++ b/turtorial/finetune_icnet.md @@ -66,7 +66,7 @@ MODEL: DEFAULT_NORM_TYPE: "bn" MULTI_LOSS_WEIGHT: "[1.0, 0.4, 0.16]" TRAIN: - PRETRAINED_MODEL: "./pretrained_model/icnet_bn_cityscapes/" + PRETRAINED_MODEL_DIR: "./pretrained_model/icnet_bn_cityscapes/" # 其他配置 @@ -78,7 +78,6 @@ AUG: BATCH_SIZE: 4 TRAIN: MODEL_SAVE_DIR: "./finetune/icnet_pet/" - RESUME: False SNAPSHOT_EPOCH: 10 TEST: TEST_MODEL: "./finetune/icnet_pet/final" @@ -118,4 +117,4 @@ python pdseg/eval.py --use_gpu --cfg ./configs/test_pet.yaml |预训练模型名称|BackBone|Norm|数据集|配置| |-|-|-|-|-| -|icnet_bn_cityscapes|-|bn|Cityscapes|MODEL.MODEL_NAME: icnet
MODEL.DEFAULT_NORM_TYPE: bn| +|icnet_bn_cityscapes|-|bn|Cityscapes|MODEL.MODEL_NAME: icnet
MODEL.DEFAULT_NORM_TYPE: bn
MULTI_LOSS_WEIGHT: [1.0, 0.4, 0.16]| diff --git a/turtorial/finetune_unet.md b/turtorial/finetune_unet.md index 0ae18101bc63c7cd0253ec5c8cbf7f3b74107d23..77a407bac09a8ad0866c66f585c49390205aa6d8 100644 --- a/turtorial/finetune_unet.md +++ b/turtorial/finetune_unet.md @@ -65,7 +65,7 @@ MODEL: MODEL_NAME: "unet" DEFAULT_NORM_TYPE: "bn" TRAIN: - PRETRAINED_MODEL: "./pretrained_model/unet_bn_coco/" + PRETRAINED_MODEL_DIR: "./pretrained_model/unet_bn_coco/" # 其他配置 @@ -77,7 +77,6 @@ AUG: BATCH_SIZE: 4 TRAIN: MODEL_SAVE_DIR: "./finetune/unet_pet/" - RESUME: False SNAPSHOT_EPOCH: 10 TEST: TEST_MODEL: "./finetune/unet_pet/final"