Skip to content

Commit

Permalink
Upgrade supported paddle version
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobholamovic committed Aug 17, 2023
1 parent b5c716c commit b7d4ad7
Show file tree
Hide file tree
Showing 124 changed files with 10,407 additions and 2,316 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 0. set args
ARG PPTAG=2.4.1 # tags refer to https://hub.docker.com/r/paddlepaddle/paddle/tags
ARG PPTAG=2.5.1 # tags refer to https://hub.docker.com/r/paddlepaddle/paddle/tags

# 1. pull base image
FROM paddlepaddle/paddle:${PPTAG}
Expand Down
6 changes: 3 additions & 3 deletions docs/docker_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

PaddleRS提供`Dockerfile`,可构建基础镜像用于开发或部署。在镜像构建过程中,默认将拉取PaddleRS develop分支内容,并存放在`/opt/PaddleRS`。在构建镜像时可以通过`PPTAG`参数指定要使用的PaddlePaddle版本,例如:

- 安装CPU版本的PaddlePaddle-2.4.1,未指定`PPTAG`的情况下将默认安装此版本:
- 安装CPU版本的PaddlePaddle-2.5.1,未指定`PPTAG`的情况下将默认安装此版本:

```shell
docker build -t paddlers:latest -f Dockerfile .
```

- 安装GPU版本PaddlePaddle-2.4.1,使用CUDA 11.7、cuDNN 8.4以及TensorRT 8.4:
- 安装GPU版本PaddlePaddle-2.5.1,使用CUDA 11.7、cuDNN 8.4以及TensorRT 8.4:

```shell
docker build -t paddlers:latest -f Dockerfile . --build-arg PPTAG=2.4.1-gpu-cuda11.7-cudnn8.4-trt8.4
docker build -t paddlers:latest -f Dockerfile . --build-arg PPTAG=2.5.1-gpu-cuda11.7-cudnn8.4-trt8.4
```

其他环境的`PPTAG`可以参考[此处](https://hub.docker.com/r/paddlepaddle/paddle/tags)。请注意,如果需要安装GPU版本的PaddlePaddle,请确保Docker版本>=19。
Expand Down
6 changes: 3 additions & 3 deletions docs/docker_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

PaddleRS provides `Dockerfile` to build a base Docker image for development/deployment. By default the develop branch of PaddleRS is fetched and stored in `/opts/PaddleRS` during image build. The `PPTAG` argument can be specified to the PaddlePaddle version you want to install. For example,

- To install CPU version of PaddlePaddle-2.4.1 (which is installed when `docker build` does not receive a `PPTAG` argument), run:
- To install CPU version of PaddlePaddle-2.5.1 (which is installed when `docker build` does not receive a `PPTAG` argument), run:

```shell
docker build -t paddlers:latest -f Dockerfile .
```

- To install GPU version of PaddlePaddle-2.4.1, with CUDA 11.8, cuDNN 8.4, and TensorRT 8.4, run:
- To install GPU version of PaddlePaddle-2.5.1, with CUDA 11.8, cuDNN 8.4, and TensorRT 8.4, run:

```shell
docker build -t paddlers:latest -f Dockerfile . --build-arg PPTAG=2.4.1-gpu-cuda11.7-cudnn8.4-trt8.4
docker build -t paddlers:latest -f Dockerfile . --build-arg PPTAG=2.5.1-gpu-cuda11.7-cudnn8.4-trt8.4
```

You can find a full list of available PaddlePaddle versions [here](https://hub.docker.com/r/paddlepaddle/paddle/tags). Please note that if a GPU version of PaddlePaddle is to be used, the version of Docker should >=19.
Expand Down
4 changes: 1 addition & 3 deletions docs/quick_start_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
## 环境准备

1. [安装PaddlePaddle](https://www.paddlepaddle.org.cn/install/quick)
- 版本要求:PaddlePaddle>=2.2.0
- 版本要求:PaddlePaddle>=2.5.0

2. 安装PaddleRS

如果希望获取更加稳定的体验,请下载安装[PaddleRS发行版](https://github.com/PaddlePaddle/PaddleRS/releases)

```shell
pip install -r requirements.txt
pip install .
```

Expand All @@ -22,7 +21,6 @@ PaddleRS代码会跟随开发进度不断更新,如果希望使用最新功能
git clone https://github.com/PaddlePaddle/PaddleRS
cd PaddleRS
git checkout develop
pip install -r requirements.txt
pip install .
```

Expand Down
2 changes: 1 addition & 1 deletion docs/quick_start_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Prerequisites

1. [Install PaddlePaddle](https://www.paddlepaddle.org.cn/install/quick)
- Version requirements: PaddlePaddle>=2.2.0
- Version requirements: PaddlePaddle>=2.5.0

2. Install PaddleRS

Expand Down
2 changes: 1 addition & 1 deletion paddlers/datasets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import copy

from paddle.io import Dataset
from paddle.fluid.dataloader.collate import default_collate_fn
from paddle.io.dataloader.collate import default_collate_fn

from paddlers.utils import get_num_workers
import paddlers.utils.logging as logging
Expand Down
4 changes: 2 additions & 2 deletions paddlers/models/paddleseg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from . import models, datasets, transforms
from . import models, datasets, transforms, optimizers

__version__ = '2.7.0'
__version__ = '2.8.0'
18 changes: 10 additions & 8 deletions paddlers/models/paddleseg/core/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ def slide_inference(model, im, crop_size, stride):
w_crop, h_crop = crop_size
w_stride, h_stride = stride
# calculate the crop nums
rows = np.int(np.ceil(1.0 * (h_im - h_crop) / h_stride)) + 1
cols = np.int(np.ceil(1.0 * (w_im - w_crop) / w_stride)) + 1
rows = int(np.ceil(1.0 * (h_im - h_crop) / h_stride)) + 1
cols = int(np.ceil(1.0 * (w_im - w_crop) / w_stride)) + 1
# prevent negative sliding rounds when imgs after scaling << crop_size
rows = 1 if h_im <= h_crop else rows
cols = 1 if w_im <= w_crop else cols
Expand Down Expand Up @@ -208,12 +208,13 @@ def aug_inference(model,
final_logit = 0
h_input, w_input = im.shape[-2], im.shape[-1]
flip_comb = flip_combination(flip_horizontal, flip_vertical)
num_augs = len(scales) * len(flip_comb)
for scale in scales:
h = int(h_input * scale + 0.5)
w = int(w_input * scale + 0.5)
im = F.interpolate(im, [h, w], mode='bilinear')
im_scale = F.interpolate(im, [h, w], mode='bilinear')
for flip in flip_comb:
im_flip = tensor_flip(im, flip)
im_flip = tensor_flip(im_scale, flip)
logit = inference(
model,
im_flip,
Expand All @@ -222,10 +223,11 @@ def aug_inference(model,
stride=stride)
logit = tensor_flip(logit, flip)
logit = F.interpolate(logit, [h_input, w_input], mode='bilinear')

logit = F.softmax(logit, axis=1)
final_logit = final_logit + logit

# Accumulate final logits in place
final_logit += logit
# We average the accumulated logits to make the numeric values of `final_logit`
# comparable to single-scale logits
final_logit /= num_augs
final_logit = reverse_transform(final_logit, trans_info, mode='bilinear')
pred = paddle.argmax(final_logit, axis=1, keepdim=True, dtype='int32')

Expand Down
3 changes: 3 additions & 0 deletions paddlers/models/paddleseg/core/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,6 @@ def predict(model,
pred_mask.save(pred_saved_path)

progbar_pred.update(i + 1)

logger.info("Predicted images are saved in {} and {} .".format(
added_saved_dir, pred_saved_dir))
101 changes: 83 additions & 18 deletions paddlers/models/paddleseg/core/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
import time
from collections import deque
import shutil
from copy import deepcopy

import paddle
import paddle.nn.functional as F

from paddlers.models.paddleseg.utils import (TimeAverager, calculate_eta, resume, logger,
worker_init_fn, train_profiler, op_flops_funs)
worker_init_fn, train_profiler, op_flops_funs,
init_ema_params, update_ema_model)
from paddlers.models.paddleseg.core.val import evaluate


Expand Down Expand Up @@ -68,6 +70,7 @@ def train(model,
log_iters=10,
num_workers=0,
use_vdl=False,
use_ema=False,
losses=None,
keep_checkpoint_max=5,
test_config=None,
Expand Down Expand Up @@ -102,6 +105,13 @@ def train(model,
profiler_options (str, optional): The option of train profiler.
to_static_training (bool, optional): Whether to use @to_static for training.
"""

if use_ema:
ema_model = deepcopy(model)
ema_model.eval()
for param in ema_model.parameters():
param.stop_gradient = True

model.train()
nranks = paddle.distributed.ParallelEnv().nranks
local_rank = paddle.distributed.ParallelEnv().local_rank
Expand Down Expand Up @@ -154,14 +164,16 @@ def train(model,
avg_loss_list = []
iters_per_epoch = len(batch_sampler)
best_mean_iou = -1.0
best_ema_mean_iou = -1.0
best_model_iter = -1
reader_cost_averager = TimeAverager()
batch_cost_averager = TimeAverager()
save_models = deque()
batch_start = time.time()

iter = start_iter
while iter < iters:
if iter == start_iter and use_ema:
init_ema_params(ema_model, model)
for data in loader:
iter += 1
if iter > iters:
Expand All @@ -176,6 +188,7 @@ def train(model,
edges = None
if 'edge' in data.keys():
edges = data['edge'].astype('int64')

if hasattr(model, 'data_format') and model.data_format == 'NHWC':
images = images.transpose((0, 2, 3, 1))

Expand All @@ -189,11 +202,19 @@ def train(model,
custom_black_list={'bilinear_interp_v2'}):
logits_list = ddp_model(images) if nranks > 1 else model(
images)
loss_list = loss_computation(
logits_list=logits_list,
labels=labels,
edges=edges,
losses=losses)
if nranks > 1 and hasattr(ddp_model._layers,
'loss_computation'):
loss_list = ddp_model._layers.loss_computation(
logits_list, losses, data)
elif nranks == 1 and hasattr(model, 'loss_computation'):
loss_list = model.loss_computation(logits_list, losses,
data)
else:
loss_list = loss_computation(
logits_list=logits_list,
labels=labels,
edges=edges,
losses=losses)
loss = sum(loss_list)

scaled = scaler.scale(loss) # scale the loss
Expand All @@ -204,18 +225,23 @@ def train(model,
scaler.minimize(optimizer, scaled) # update parameters
else:
logits_list = ddp_model(images) if nranks > 1 else model(images)
loss_list = loss_computation(
logits_list=logits_list,
labels=labels,
edges=edges,
losses=losses)

if nranks > 1 and hasattr(ddp_model._layers,
'loss_computation'):
loss_list = ddp_model._layers.loss_computation(logits_list,
losses, data)
elif nranks == 1 and hasattr(model, 'loss_computation'):
loss_list = model.loss_computation(logits_list, losses,
data)
else:
loss_list = loss_computation(
logits_list=logits_list,
labels=labels,
edges=edges,
losses=losses)
loss = sum(loss_list)
loss.backward()
# if the optimizer is ReduceOnPlateau, the loss is the one which has been pass into step.
if isinstance(optimizer, paddle.optimizer.lr.ReduceOnPlateau):
optimizer.step(loss)
else:
optimizer.step()
optimizer.step()

lr = optimizer.get_lr()

Expand All @@ -225,7 +251,10 @@ def train(model,
else:
lr_sche = optimizer._learning_rate
if isinstance(lr_sche, paddle.optimizer.lr.LRScheduler):
lr_sche.step()
if isinstance(lr_sche, paddle.optimizer.lr.ReduceOnPlateau):
lr_sche.step(loss)
else:
lr_sche.step()

train_profiler.add_profiler_step(profiler_options)

Expand Down Expand Up @@ -273,6 +302,9 @@ def train(model,
reader_cost_averager.reset()
batch_cost_averager.reset()

if use_ema:
update_ema_model(ema_model, model, step=iter)

if (iter % save_interval == 0 or
iter == iters) and (val_dataset is not None):
num_workers = 1 if num_workers > 0 else 0
Expand All @@ -288,6 +320,15 @@ def train(model,
amp_level=amp_level,
**test_config)

if use_ema:
ema_mean_iou, ema_acc, _, _, _ = evaluate(
ema_model,
val_dataset,
num_workers=num_workers,
precision=precision,
amp_level=amp_level,
**test_config)

model.train()

if (iter % save_interval == 0 or iter == iters) and local_rank == 0:
Expand All @@ -299,6 +340,12 @@ def train(model,
os.path.join(current_save_dir, 'model.pdparams'))
paddle.save(optimizer.state_dict(),
os.path.join(current_save_dir, 'model.pdopt'))

if use_ema:
paddle.save(
ema_model.state_dict(),
os.path.join(current_save_dir, 'ema_model.pdparams'))

save_models.append(current_save_dir)
if len(save_models) > keep_checkpoint_max > 0:
model_to_remove = save_models.popleft()
Expand All @@ -315,10 +362,28 @@ def train(model,
logger.info(
'[EVAL] The model with the best validation mIoU ({:.4f}) was saved at iter {}.'
.format(best_mean_iou, best_model_iter))
if use_ema:
if ema_mean_iou > best_ema_mean_iou:
best_ema_mean_iou = ema_mean_iou
best_ema_model_iter = iter
best_ema_model_dir = os.path.join(save_dir,
"ema_best_model")
paddle.save(ema_model.state_dict(),
os.path.join(best_ema_model_dir,
'ema_model.pdparams'))
logger.info(
'[EVAL] The EMA model with the best validation mIoU ({:.4f}) was saved at iter {}.'
.format(best_ema_mean_iou, best_ema_model_iter))

if use_vdl:
log_writer.add_scalar('Evaluate/mIoU', mean_iou, iter)
log_writer.add_scalar('Evaluate/Acc', acc, iter)

if use_ema:
log_writer.add_scalar('Evaluate/Ema_mIoU',
ema_mean_iou, iter)
log_writer.add_scalar('Evaluate/Ema_Acc', ema_acc,
iter)
batch_start = time.time()

# Calculate flops.
Expand Down
1 change: 1 addition & 0 deletions paddlers/models/paddleseg/cvlibs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
from . import manager
from . import param_init
from .config import Config
from .builder import Builder, SegBuilder
Loading

0 comments on commit b7d4ad7

Please sign in to comment.