diff --git a/model_zoo/research/cv/Pix2Pix/README.md b/model_zoo/research/cv/Pix2Pix/README.md index 1d22217908..68bad2d896 100644 --- a/model_zoo/research/cv/Pix2Pix/README.md +++ b/model_zoo/research/cv/Pix2Pix/README.md @@ -95,8 +95,11 @@ The entire code structure is as following: ├─ data └─download_Pix2Pix_dataset.sh # download dataset ├── scripts + └─run_infer_310.sh # launch ascend 310 inference └─run_train_ascend.sh # launch ascend training(1 pcs) └─run_eval_ascend.sh # launch ascend eval + └─run_train_gpu.sh # launch gpu training(1 pcs) + └─run_eval_gpu.sh # launch gpu eval ├─ imgs └─Pix2Pix-examples.jpg # Pix2Pix Imgs ├─ src @@ -161,10 +164,31 @@ Major parameters in train.py and config.py as follows: python train.py --device_target [Ascend] --device_id [0] --train_data_dir [./data/facades/train] ``` -## [Evaluation](#contents) +- running on GPU with fixed parameters ```python -python eval.py --device_target [Ascend] --device_id [0] --val_data_dir [./data/facades/test] --ckpt [./results/ckpt/Generator_200.ckpt] +python train.py --device_target [GPU] --device_id [0] --train_data_dir [./data/facades/train] --pad_mode REFLECT +OR +bash scripts/run_train_gpu.sh [DATASET_PATH] [DATASET_NAME] +``` + +## [Evaluation](#contents) + +- running on Ascend + +```python +python eval.py --device_target [Ascend] --device_id [0] --val_data_dir [./data/facades/test] --ckpt [./results/ckpt/Generator_200.ckpt] --pad_mode REFLECT +OR +bash scripts/run_eval.sh +``` + +- running on GPU + +```python +python eval.py --device_target [GPU] --device_id [0] --val_data_dir [./data/facades/test] --ckpt [./train/results/ckpt/Generator_200.ckpt] --predict_dir [./train/results/predict/] \ +--dataset_size 1096 --pad_mode REFLECT +OR +bash scripts/run_eval_gpu.sh [DATASET_PATH] [DATASET_NAME] ``` **Note:**: Before training and evaluating, create folders like "./results/...". Then you will get the results as following in "./results/predict". @@ -183,44 +207,44 @@ bash run_infer_310.sh [The path of the MINDIR for 310 infer] [The path of the da ### Training Performance -| Parameters | single Ascend | -| -------------------------- | ----------------------------------------------------------- | -| Model Version | Pix2Pix | -| Resource | Ascend 910 | -| MindSpore Version | 1.2 | -| Dataset | facades | -| Training Parameters | epoch=200, steps=400, batch_size=1, lr=0.0002 | -| Optimizer | Adam | -| Loss Function | SigmoidCrossEntropyWithLogits Loss & L1 Loss | -| outputs | probability | -| Speed | 1pc(Ascend): 10 ms/step | -| Total time | 1pc(Ascend): 0.3h | -| Checkpoint for Fine tuning | 207M (.ckpt file) | +| Parameters | single Ascend | single GPU | +| -------------------------- | ----------------------------------------------------------- | --------------------------------------------------------------- | +| Model Version | Pix2Pix | Pix2Pix | +| Resource | Ascend 910 | PCIE V100-32G | +| MindSpore Version | 1.2 | 1.3.0 | +| Dataset | facades | facades | +| Training Parameters | epoch=200, steps=400, batch_size=1, lr=0.0002 | epoch=250, steps=400, batch_size=1, lr=0.0002, init_gain=0.0195 | +| Optimizer | Adam | Adam | +| Loss Function | SigmoidCrossEntropyWithLogits Loss & L1 Loss | SigmoidCrossEntropyWithLogits Loss & L1 Loss | +| outputs | probability | probability | +| Speed | 1pc(Ascend): 10 ms/step | 1pc(GPU): 50 ms/step | +| Total time | 1pc(Ascend): 0.3h | 1pc(GPU): 0.9 h | +| Checkpoint for Fine tuning | 207M (.ckpt file) | 207M (.ckpt file) | -| Parameters | single Ascend | -| -------------------------- | ----------------------------------------------------------- | -| Model Version | Pix2Pix | +| Parameters | single Ascend | single GPU | +| -------------------------- | ----------------------------------------------------------- | --------------------------------------------------------------- | +| Model Version | Pix2Pix | Pix2Pix | | Resource | Ascend 910 | -| MindSpore Version | 1.2 | -| Dataset | maps | -| Training Parameters | epoch=200, steps=1096, batch_size=1, lr=0.0002 | -| Optimizer | Adam | -| Loss Function | SigmoidCrossEntropyWithLogits Loss & L1 Loss | -| outputs | probability | -| Speed | 1pc(Ascend): 20 ms/step | -| Total time | 1pc(Ascend): 1.58h | -| Checkpoint for Fine tuning | 207M (.ckpt file) | +| MindSpore Version | 1.2 | 1.3.0 | +| Dataset | maps | maps | +| Training Parameters | epoch=200, steps=1096, batch_size=1, lr=0.0002 | epoch=250, steps=400, batch_size=1, lr=0.0002, init_gain=0.0195 | +| Optimizer | Adam | Adam | +| Loss Function | SigmoidCrossEntropyWithLogits Loss & L1 Loss | SigmoidCrossEntropyWithLogits Loss & L1 Loss | +| outputs | probability | probability | +| Speed | 1pc(Ascend): 20 ms/step | 1pc(GPU): 60 ms/step | +| Total time | 1pc(Ascend): 1.58h | 1pc(GPU): 2.2h | +| Checkpoint for Fine tuning | 207M (.ckpt file) | 207M (.ckpt file) | ### Evaluation Performance -| Parameters | single Ascend | -| ------------------- | --------------------------- | -| Model Version | Pix2Pix | -| Resource | Ascend 910 | -| MindSpore Version | 1.2 | -| Dataset | facades / maps | -| batch_size | 1 | -| outputs | probability | +| Parameters | single Ascend | single GPU | +| ------------------- | --------------------------- | --------------------------- | +| Model Version | Pix2Pix | Pix2Pix | +| Resource | Ascend 910 | PCIE V100-32G | +| MindSpore Version | 1.2 | 1.3.0 | +| Dataset | facades / maps | facades / maps | +| batch_size | 1 | 1 | +| outputs | probability | probability | # [ModelZoo Homepage](#contents) diff --git a/model_zoo/research/cv/Pix2Pix/scripts/run_eval_gpu.sh b/model_zoo/research/cv/Pix2Pix/scripts/run_eval_gpu.sh new file mode 100644 index 0000000000..c3744780b0 --- /dev/null +++ b/model_zoo/research/cv/Pix2Pix/scripts/run_eval_gpu.sh @@ -0,0 +1,49 @@ +#!/bin/bash +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +echo "=======================================================================================================================================" +echo "Please run the eval as: " +echo "python eval.py device_target device_id val_data_dir ckpt" +echo "for example: python eval.py --device_target GPU --device_id 0 --val_data_dir ./facades/test --ckpt ./results/ckpt/Generator_200.ckpt" +echo "=======================================================================================================================================" + +if [ $# != 2 ] +then + echo "Usage: bash run_eval_gpu.sh [DATASET_PATH] [DATASET_NAME]" + exit 1 +fi + +get_real_path(){ + if [ "${1:0:1}" == "/" ]; then + echo "$1" + else + echo "$(realpath -m $PWD/$1)" + fi +} + +PATH1=$(get_real_path $1) + +if [ ! -d $PATH1 ] +then + echo "error: DATASET_PATH=$PATH1 is not a directory" + exit 1 +fi + +if [ $2 == 'facades' ]; then + python eval.py --device_target GPU --device_id 0 --val_data_dir $PATH1 --ckpt ./train/results/ckpt/Generator_200.ckpt --predict_dir ./train/results/predict/ --pad_mod REFLECT +elif [ $2 == 'maps' ]; then + python eval.py --device_target GPU --device_id 0 --val_data_dir $PATH1 --ckpt ./train/results/ckpt/Generator_200.ckpt --predict_dir ./train/results/predict/ --dataset_size 1096 \ + --pad_mode REFLECT +fi diff --git a/model_zoo/research/cv/Pix2Pix/scripts/run_train_gpu.sh b/model_zoo/research/cv/Pix2Pix/scripts/run_train_gpu.sh new file mode 100644 index 0000000000..3e141cf269 --- /dev/null +++ b/model_zoo/research/cv/Pix2Pix/scripts/run_train_gpu.sh @@ -0,0 +1,63 @@ +#!/bin/bash +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +echo "=====================================================================================================================" +echo "Please run the train as: " +echo "python train.py device_target device_id dataset_size train_data_dir" +echo "for example: python train.py --device_target GPU --device_id 0 --dataset_size 400 --train_data_dir ./facades/train" +echo "=====================================================================================================================" + +if [ $# != 2 ] +then + echo "Usage: bash run_train_gpu.sh [DATASET_PATH] [DATASET_NAME]" + exit 1 +fi + +get_real_path(){ + if [ "${1:0:1}" == "/" ]; then + echo "$1" + else + echo "$(realpath -m $PWD/$1)" + fi +} + +PATH1=$(get_real_path $1) + +if [ ! -d $PATH1 ] +then + echo "error: DATASET_PATH=$PATH1 is not a directory" + exit 1 +fi + + +rm -rf ./train +mkdir ./train +mkdir ./train/results +mkdir ./train/results/fake_img +mkdir ./train/results/loss_show +mkdir ./train/results/ckpt +mkdir ./train/results/predict +cp ./*.py ./train +cp ./scripts/*.sh ./train +cp -r ./src ./train +cd ./train || exit + +if [ $2 == 'facades' ]; then + mpirun --allow-run-as-root -n 1 --output-filename log_output --merge-stderr-to-stdout \ + python train.py --device_target GPU --device_id 0 --dataset_size 400 --train_data_dir $PATH1 --pad_mode REFLECT &> log & +elif [ $2 == 'maps' ]; then + mpirun --allow-run-as-root -n 1 --output-filename log_output --merge-stderr-to-stdout \ + python train.py --device_target GPU --device_id 0 --dataset_size 1096 --train_data_dir $PATH1 --pad_mode REFLECT &> log & +fi diff --git a/model_zoo/research/cv/Pix2Pix/src/models/discriminator_model.py b/model_zoo/research/cv/Pix2Pix/src/models/discriminator_model.py index b7dc62ba81..fc6adccc7e 100644 --- a/model_zoo/research/cv/Pix2Pix/src/models/discriminator_model.py +++ b/model_zoo/research/cv/Pix2Pix/src/models/discriminator_model.py @@ -19,6 +19,9 @@ import mindspore.nn as nn from mindspore.ops import Concat +from ..utils.config import get_args + +args = get_args() class ConvNormReLU(nn.Cell): """ @@ -57,6 +60,10 @@ class ConvNormReLU(nn.Cell): has_bias = (norm_mode == 'instance') if padding is None: padding = (kernel_size - 1) // 2 + if args.pad_mode == 'REFLECT': + pad_mode = "REFLECT" + elif arg.pad_mode == "SYMMETRIC": + pad_mode = "SYMMETRIC" if pad_mode == 'CONSTANT': conv = nn.Conv2d(in_planes, out_planes, kernel_size, stride, pad_mode='pad', has_bias=has_bias, padding=padding) diff --git a/model_zoo/research/cv/Pix2Pix/src/utils/config.py b/model_zoo/research/cv/Pix2Pix/src/utils/config.py index 411a36c840..e77aaca2a9 100644 --- a/model_zoo/research/cv/Pix2Pix/src/utils/config.py +++ b/model_zoo/research/cv/Pix2Pix/src/utils/config.py @@ -38,6 +38,8 @@ def get_args(): parser.add_argument('--init_type', type=str, default='normal', help='network initialization, default is normal.') parser.add_argument('--init_gain', type=float, default=0.02, help='scaling factor for normal, xavier and orthogonal, default is 0.02.') + parser.add_argument('--pad_mode', type=str, default='CONSTANT', choices=('CONSTANT', 'REFLECT', 'SYMMETRIC'), + help='scale images to this size, default is CONSTANT.') parser.add_argument('--load_size', type=int, default=286, help='scale images to this size, default is 286.') parser.add_argument('--batch_size', type=int, default=1, help='batch_size, default is 1.') parser.add_argument('--LAMBDA_Dis', type=float, default=0.5, help='weight for Discriminator Loss, default is 0.5.') diff --git a/model_zoo/research/cv/Pix2Pix/src/utils/tools.py b/model_zoo/research/cv/Pix2Pix/src/utils/tools.py index 8e60b05dfe..1ff8f3fd27 100644 --- a/model_zoo/research/cv/Pix2Pix/src/utils/tools.py +++ b/model_zoo/research/cv/Pix2Pix/src/utils/tools.py @@ -23,6 +23,7 @@ from PIL import Image from mindspore import Tensor from src.utils.config import get_args +plt.switch_backend('Agg') args = get_args() def save_losses(G_losses, D_losses, idx):