Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enchance] support infererence with padding #1607

Merged
merged 3 commits into from
Jun 15, 2022
Merged

Conversation

FreyWang
Copy link
Contributor

@FreyWang FreyWang commented May 22, 2022

  • Before padding
    飞书20220522-190911
  • After padding
    飞书20220522-190917

Motivation

I use mmseg to train my model and found the mask has a left offset to the image. Finally I found that it is caused by inference input without padding. For example, A input with origin shape (1920, 1080) and img_scale=(2048, 512) will be resized to (910 , 512). When infer it with resnet50-d8,the final feature map is 114 * 64 and then resize it to 910*512. Attention that 114x8=912. So it is same with pad the input to (912, 512) and resize feature to (910, 512). With 2-pixel left offset

BC-breaking (Optional)

Attention that when dict(type='Pad', size_divisor=32), is add, the model performance will intend to be improved, I have test several mIoU rise

  • configs/deeplabv3plus/deeplabv3plus_r50-d8_512x512_80k_ade20k.py
    from 42.72 to 42.78
  • configs/ocrnet/ocrnet_hr18s_512x512_80k_ade20k.py
    from 35.06 to 35.53
  • configs/knet/knet_s3_upernet_swin-l_8x2_512x512_adamw_80k_ade20k.py
    from 52.05 to 52.61

Use cases (Optional)

Add Pad when inference to make sure that input can be divide to model stride.
[Update]
Attention that Pad must behinde RandomFlip when flip is True

test_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(
        type='MultiScaleFlipAug',
        img_scale=(2048, 512),
        # img_ratios=[0.5, 0.75, 1.0, 1.25, 1.5, 1.75],
        flip=False,
        transforms=[
            dict(type='Resize', keep_ratio=True),
            dict(type='RandomFlip'),
            dict(type='Pad', size_divisor=32),
            dict(type='Normalize', **img_norm_cfg),
            dict(type='ImageToTensor', keys=['img']),
            dict(type='Collect', keys=['img']),
        ])
]

@codecov
Copy link

codecov bot commented May 22, 2022

Codecov Report

Merging #1607 (3e7426d) into master (104429b) will decrease coverage by 0.14%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #1607      +/-   ##
==========================================
- Coverage   90.40%   90.26%   -0.15%     
==========================================
  Files         141      142       +1     
  Lines        8443     8485      +42     
  Branches     1423     1431       +8     
==========================================
+ Hits         7633     7659      +26     
- Misses        572      586      +14     
- Partials      238      240       +2     
Flag Coverage Δ
unittests 90.26% <100.00%> (-0.15%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
mmseg/datasets/pipelines/test_time_aug.py 96.55% <100.00%> (+0.25%) ⬆️
mmseg/models/segmentors/encoder_decoder.py 88.66% <100.00%> (+0.31%) ⬆️
mmseg/apis/train.py 19.51% <0.00%> (ø)
mmseg/utils/__init__.py 100.00% <0.00%> (ø)
mmseg/utils/util_distribution.py 48.38% <0.00%> (ø)
mmseg/datasets/samplers/distributed_sampler.py 56.52% <0.00%> (+4.14%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 104429b...3e7426d. Read the comment docs.

@FreyWang
Copy link
Contributor Author

It may effect pytorch2onnx and i will refactor the code later

Copy link
Collaborator

@MeowZheng MeowZheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FreyWang have you compared the model performance before and after this modification? Would you like to share the results with us? Thank you

@FreyWang
Copy link
Contributor Author

@FreyWang have you compared the model performance before and after this modification? Would you like to share the results with us? Thank you
I only test several model such as deeplabv3+,ocrnet, knet and setr, all gain 0~0.5 mIoU improve. 🥲 Test all config is difficult, some automated scripts is needed
Attention that Pad must behinde RandomFlip when flip is True

@FreyWang
Copy link
Contributor Author

a

the performance is same after this commit if pad is not add to config

@MeowZheng
Copy link
Collaborator

@FreyWang have you compared the model performance before and after this modification? Would you like to share the results with us? Thank you
I only test several model such as deeplabv3+,ocrnet, knet and setr, all gain 0~0.5 mIoU improve. smiling_face_with_tear Test all config is difficult, some automated scripts is needed
Attention that Pad must behinde RandomFlip when flip is True

would you like to present the TTA result with pad and without pad of deeplabv3+?

@FreyWang
Copy link
Contributor Author

@FreyWang have you compared the model performance before and after this modification? Would you like to share the results with us? Thank you
I only test several model such as deeplabv3+,ocrnet, knet and setr, all gain 0~0.5 mIoU improve. smiling_face_with_tear Test all config is difficult, some automated scripts is needed
Attention that Pad must behinde RandomFlip when flip is True

would you like to present the TTA result with pad and without pad of deeplabv3+?

configs/deeplabv3plus/deeplabv3plus_r50-d8_512x512_80k_ade20k.py
from 42.72 to 42.78 after pad is added.
emmm, what is the meaning of TTA. I have only ade20k dataset now

@FreyWang
Copy link
Contributor Author

@FreyWang have you compared the model performance before and after this modification? Would you like to share the results with us? Thank you
I only test several model such as deeplabv3+,ocrnet, knet and setr, all gain 0~0.5 mIoU improve. smiling_face_with_tear Test all config is difficult, some automated scripts is needed
Attention that Pad must behinde RandomFlip when flip is True

would you like to present the TTA result with pad and without pad of deeplabv3+?

I got what you said and I will find time to commit the test code😂

@FreyWang
Copy link
Contributor Author

@MeowZheng after pad
configs/deeplabv3plus/deeplabv3plus_r50-d8_512x512_80k_ade20k.py
default: 42.72 -> 42.78
ms+flip: 43.75->43.77
configs/setr/setr_mla_512x512_160k_b8_ade20k.py
default: 47.34->47.58
ms+flip: 49.05->49.06

@MeowZheng MeowZheng merged commit 6f43f4d into open-mmlab:master Jun 15, 2022
ZhimingNJ pushed a commit to AetrexTechnology/mmsegmentation that referenced this pull request Jun 29, 2022
* [Enchance] support infererence with padding

* limite pad after flip when inference

* add test code
@FreyWang FreyWang deleted the pr1 branch February 17, 2023 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants