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

[Enhance] Merge EditVal/TestLoop and EditPreprocessor to dev-1.x #1656

Merged
merged 9 commits into from
Mar 1, 2023

Conversation

LeoXing1996
Copy link
Collaborator

@LeoXing1996 LeoXing1996 commented Feb 21, 2023

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

Merge refactored loops and preprocessor to dev-1.x.

Modification

  1. EditDataSample is refactored in [Refactor] Refactor PackEditInputs and EditDataSample #1573
  2. EditDatapreprocessor is refactored in [Enhancement] Refactor DataPreprocessor #1537
  3. Metric and Loops are refactored in [Enhancement] Refactor metric and loop #1540
  4. Support stack and split for EditDataSample [Enhancement] Support stack and split for EditDataSampe #1669
  5. Revise the forward logic for GAN models for easy outstanding [Enhancement] Make forward logic more clear for GAN models #1670

Hightlights

  1. GenValLoopandMultiValLoophas been merged toEditValLoop, GenTestLoopandMultiTestLoophas been merged toEditTestLoop`. Use case:
    Case 1: metrics on a single dataset

    >>> # add the following lines in your config
    >>> # 1. use `EditValLoop` instead of `ValLoop` in MMEngine
    >>> val_cfg = dict(type='EditValLoop')
    >>> # 2. specific EditEvaluator instead of Evaluator in MMEngine
    >>> val_evaluator = dict(
    >>>     type='EditEvaluator',
    >>>     metrics=[
    >>>         dict(type='PSNR', crop_border=2, prefix='Set5'),
    >>>         dict(type='SSIM', crop_border=2, prefix='Set5'),
    >>>     ])
    >>> # 3. define dataloader
    >>> val_dataloader = dict(...)

    Case 2: different metrics on different datasets

    >>> # add the following lines in your config
    >>> # 1. use `EditValLoop` instead of `ValLoop` in MMEngine
    >>> val_cfg = dict(type='EditValLoop')
    >>> # 2. specific a list EditEvaluator
    >>> # do not forget to add prefix for each metric group
    >>> div2k_evaluator = dict(
    >>>     type='EditEvaluator',
    >>>     metrics=dict(type='SSIM', crop_border=2, prefix='DIV2K'))
    >>> set5_evaluator = dict(
    >>>     type='EditEvaluator',
    >>>     metrics=[
    >>>         dict(type='PSNR', crop_border=2, prefix='Set5'),
    >>>         dict(type='SSIM', crop_border=2, prefix='Set5'),
    >>>     ])
    >>> # define evaluator config
    >>> val_evaluator = [div2k_evaluator, set5_evaluator]
    >>> # 3. specific a list dataloader for each metric groups
    >>> div2k_dataloader = dict(...)
    >>> set5_dataloader = dict(...)
    >>> # define dataloader config
    >>> val_dataloader = [div2k_dataloader, set5_dataloader]
  1. Support stack and split for EditDataSample, Use case:
# Example for `split`
gen_sample = EditDataSample()
gen_sample.fake_img = outputs  # tensor
gen_sample.noise = noise  # tensor
gen_sample.sample_kwargs = deepcopy(sample_kwargs)  # dict
gen_sample.sample_model = sample_model  # string
# set allow_nonseq_value as True to copy non-sequential data (sample_kwargs and sample_model for this example)
batch_sample_list = gen_sample.split(allow_nonseq_value=True)  

# Example for `stack`
data_sample1 = EditDataSample()
data_sample1.set_gt_label(1)
data_sample1.set_tensor_data({'img': torch.randn(3, 4, 5)})
data_sample1.set_data({'mode': 'a'})
data_sample1.set_metainfo({
    'channel_order': 'rgb',
    'color_flag': 'color'
})
data_sample2 = EditDataSample()
data_sample2.set_gt_label(2)
data_sample2.set_tensor_data({'img': torch.randn(3, 4, 5)})
data_sample2.set_data({'mode': 'b'})
data_sample2.set_metainfo({
    'channel_order': 'rgb',
    'color_flag': 'color'
})
data_sample_merged = EditDataSample.stack([data_sample1, data_sample2])

Who can help? @ them here!

BC-breaking (Optional)

  1. For BaseGAN/CondGAN models, real images are passed from data_samples.gt_img instead of inputs['img']
  2. GenValLoop and MultiValLoop has been merged to EditValLoop, GenTestLoop and MultiTestLoop has been merged to EditTestLoop.
  3. GenDataPreprocessor has been merged into EditDataPreprocessor,
    • No changes are required other than changing the type field in config.
    • Users do not need to define input_view and output_view since we will infer the shape of mean automatically.
    • In evaluation stage, all tensors will be converted to BGR (for three-channel images) and [0, 255].
  4. PixelData has been removed

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.

Checklist

Before PR:

  • I have read and followed the workflow indicated in the CONTRIBUTING.md to create this PR.
  • Pre-commit or linting tools indicated in CONTRIBUTING.md are used to fix the potential lint issues.
  • Bug fixes are covered by unit tests, the case that causes the bug should be added in the unit tests.
  • New functionalities are covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  • The documentation has been modified accordingly, including docstring or example tutorials.

After PR:

  • If the modification has potential influence on downstream or other related projects, this PR should be tested with some of those projects.
  • CLA has been signed and all committers have signed the CLA in this PR.

@LeoXing1996 LeoXing1996 self-assigned this Feb 21, 2023
@LeoXing1996 LeoXing1996 force-pushed the refactor-metric-loop-and-preprocessor branch 2 times, most recently from b93180b to be5f5f7 Compare February 21, 2023 13:23
@codecov
Copy link

codecov bot commented Feb 22, 2023

Codecov Report

Patch coverage: 90.42% and project coverage change: -0.43 ⚠️

Comparison is base (a6d75e9) 88.22% compared to head (2b1bbdc) 87.80%.

❗ Current head 2b1bbdc differs from pull request most recent head c504704. Consider uploading reports for the commit c504704 to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##           dev-1.x    #1656      +/-   ##
===========================================
- Coverage    88.22%   87.80%   -0.43%     
===========================================
  Files          401      399       -2     
  Lines        26383    26368      -15     
  Branches      4083     4114      +31     
===========================================
- Hits         23276    23152     -124     
- Misses        2204     2329     +125     
+ Partials       903      887      -16     
Flag Coverage Δ
unittests 87.80% <90.42%> (-0.43%) ⬇️

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

Impacted Files Coverage Δ
mmedit/datasets/transforms/aug_pixel.py 96.38% <ø> (ø)
mmedit/datasets/transforms/crop.py 85.38% <ø> (ø)
mmedit/engine/hooks/pggan_fetch_data_hook.py 95.74% <ø> (ø)
mmedit/evaluation/metrics/base_gen_metric.py 88.88% <ø> (ø)
mmedit/evaluation/metrics/equivariance.py 80.89% <ø> (ø)
mmedit/models/editors/biggan/biggan_generator.py 83.67% <0.00%> (-1.75%) ⬇️
...edit/models/editors/guided_diffusion/classifier.py 17.06% <ø> (ø)
mmedit/models/editors/stylegan1/stylegan1.py 90.24% <ø> (ø)
...edit/models/editors/stylegan2/stylegan2_modules.py 87.93% <0.00%> (ø)
mmedit/utils/io_utils.py 22.64% <ø> (ø)
... and 98 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@LeoXing1996 LeoXing1996 marked this pull request as ready for review February 23, 2023 11:34
@LeoXing1996 LeoXing1996 force-pushed the refactor-metric-loop-and-preprocessor branch from bf72f22 to f1d8b35 Compare February 23, 2023 11:58
@zengyh1900 zengyh1900 self-requested a review February 24, 2023 01:59
Copy link
Collaborator

@zengyh1900 zengyh1900 left a comment

Choose a reason for hiding this comment

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

This PR involves significant changes.
Please list as detailed as possible use_case, migration notes, etc.

mmedit/evaluation/metrics/ms_ssim.py Outdated Show resolved Hide resolved
mmedit/models/base_models/base_conditional_gan.py Outdated Show resolved Hide resolved
mmedit/models/editors/cyclegan/cyclegan.py Outdated Show resolved Hide resolved
mmedit/structures/edit_data_sample.py Outdated Show resolved Hide resolved
mmedit/models/editors/eg3d/eg3d.py Outdated Show resolved Hide resolved
mmedit/models/editors/ggan/ggan.py Outdated Show resolved Hide resolved
mmedit/models/editors/pggan/pggan.py Outdated Show resolved Hide resolved
mmedit/models/editors/pix2pix/pix2pix.py Outdated Show resolved Hide resolved
mmedit/visualization/gen_visualizer.py Show resolved Hide resolved
@zengyh1900
Copy link
Collaborator

Hi @LeoXing1996 feel free to discuss any comments. If I misunderstand some usage, please correct me.

VongolaWu and others added 8 commits February 28, 2023 21:36
* refactor packeditinput and editdatasample

* move util from formatting to img_utils

* fix bugs in ut

* use permute instead of transpose in all_to_tensor

* remove undesired output results

* fix the path of results in ut

* use np.ascontiguousarray(img) in image_to_tensor

* remove entry function from unit test files

* refine sinGAN's config

Co-authored-by: LeoXing1996 <[email protected]>
* unify GenDataPreprocessor and EditDataPreprocessor

* revise models with EditDataPreprocessor

* adopt configs with revised EditDataPreprocessor

* adopt train_step and unit tests to revised EditDataPreprocessor

* fix unit test of inpainter inferencer

* add more metainfo to EditDataSample

* refactor EditDataPreprocessor to standardize the output channel order and value range of data_samples and inputs

* adopt SR models to refactored EditDataPreprocessor

* revise padding checking in destruct

* revise color order conversion to support get input channel order from metafile and handle single channel images

* remove norm_data_samples_in_testing and use upper case for channel order

* adopt baseGAN and baseCondGAN for new data preprocessor

* convert is_list_of to is_seq_of

* refactor inpaintors to adopt new data preprocessor

* standardize channel order representation and refactor channel order conversion behaviors

* refactor channel conversion in destruct and remove unused codes

* update unit test for edit data preprocessor and fix some bugs

* adopt inpainting models' with data preprocessor

* adopt frame interpolation models' with data preprocessor

* revise train_step and unit tests of global_local and partial_conv

* adopt colorization models' with data preprocessor

* revise train_step and unit tests of aot-gan

* adopt SR and VSR models with data preprocessor

* revise test config of LIIF

* adopt dcgan and SWD, MS-SSIM to new data preprocessor

* adopt wgan, ggan and lsgan to new data preprocessor

* adopt pggan and styleganv1 to new data preprocessor

* adopt pix2pix to new data preprocessor

* adopt cyclegan to new data preprocessor

* adopt conditional-datasets to new data preprocessor

* adopt sngan and sagan to new data preprocessor

* adopt biggan to new data preprocessor

* adopt stylegan2, singan and mspie to new data preprocessor

* adopt stylegan3 to new data preprocessor

* adopt eg3d to new data preprocessor

* revise transform pipelines

* revise unit test of EditDataSample

* adopt baseEditModel, vis_hook and visualizer for new data preprocessor

* some minor fix of edit data preprocessor and add unit test for model utils

* adopt metrics for new data preprocessor

* add env var for deterministic training

* refactor mattor data preprocessor based on edit data preprocessor

* fix lint error

* fix some unit tests

* update unit test of inpainting inferencer, sad and data preprocessor

* optimize the memory cost for AOT-GAN and BasicVSR's unit tests

* optimize the memory cost for IconVSR's unit tests

* skip ttsr unit test on windows CI
* support new loops

* refactor base sample wise metrics, FID and IS

* adopt configs to new loops

* refine EditLoops and add docstring and unit test for EditLoops

* rename GenEvaluator to EditEvaluator

* revise configs for new EditLoops and EditEvaluator

* remove unused imported modules

* revise unit test of BaseSampleWiseMetric

* remove GenDataPreprocessor, GenLoop from configs, docstrings and tutorials

* avoid raise duplicate conversion warning in EditDataprocessor

* revise channel order conversion in FID metric

* rename GenLogProcessor and GenIterTimeHook to EditLogProcessor and EditIterTimeHook

* rename some elements in default_runtime

* support non-scalar in get_log_after_epoch as MMEngine's LogProcessor

* save metainfo in Resize for outkeys

* complete prepare method for Matting metrics

* remove GenLoop, MutliLoop, GenDataPreprocessor

* remove PixelData
add more docstring to pass lint checking
* support stack and split in data sample

* support stack for EditDataPreprocessor

* support length in EditDataSample and revise stack and split operation

* adapt models for the stacked data samples

* support stack for MattorPreprocessor

* remove useless comments

* revise the return type of forward_inference to EditDataSample

* add security checking for EditDataSample.split

* revise inferencer unit tests

* revise inception utils

* revise conditional gan models

* revise unit test for base mattor

* revise unit test for twostage inpaintor and aot-inpaintor

* revise unit test for LIIF

* revise unit test for cyclegan and pix2pix

* revise unit test for pconv

* revise unit test for vis hook + fix bug in split

* revise more unit tests to pass CI and reduce the input resolution of wo-stage UI

* support video process for EditDataPreprocessor.process_image_tensor

* revise unit test of EDVR

* revise comment in EditDataSample.split

* remove more useless comments
* make forward logic more clear for GAN models

* revise eg3d inferencer unit test

* polish length calculate and split operation for EditDataSample

* revise forward pipeline for BaseGAN and BaseCondGAN

* revise forward pipeline for EG3D

* revise forward pipeline for PGGAN

* remove useless comments from SinGAN

* remove is_stacked property from EditDataSample
@LeoXing1996 LeoXing1996 force-pushed the refactor-metric-loop-and-preprocessor branch from e2b0922 to d0bbfe5 Compare February 28, 2023 13:37
Copy link
Collaborator

@zengyh1900 zengyh1900 left a comment

Choose a reason for hiding this comment

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

lgtm

@LeoXing1996 LeoXing1996 merged commit 21ee02b into dev-1.x Mar 1, 2023
@LeoXing1996 LeoXing1996 deleted the refactor-metric-loop-and-preprocessor branch March 1, 2023 03:15
LeoXing1996 added a commit that referenced this pull request Mar 1, 2023
* fix unit test for colorization

* fix GPU unit test for pix2pix and cycleGAN

* adapt EditDataPreprocessor for ADM and fix unit test for ADM

* revise CircleCI config
@Z-Fran Z-Fran mentioned this pull request Mar 2, 2023
7 tasks
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