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

Improve semantic segmentation models #14355

Merged
merged 13 commits into from
Nov 17, 2021

Conversation

NielsRogge
Copy link
Contributor

@NielsRogge NielsRogge commented Nov 10, 2021

What does this PR do?

  • add SegFormer documentation (including a figure)
  • fix padding (fixes SegformerFeatureExtractor trying to access non-existent .ndim attribute #14332) for SegFormer
  • add attribute to the configuration of SegFormer and BEiT called semantic_ignore_index, which defaults to 255. The loss functions of semantic segmentation models typically use 255 instead of -100. The reason for this is that some datasets include a 0 in the annotated segmentation maps to indicate "background", however it can be that background is not included in any of the labels of the dataset. e.g. ADE20k has 150 labels, but "background" is not included. Therefore, one reduces the labels of all segmentation maps by 1 value, and replaces the 0 by 255 as shown here. It's only after that that images get resized using PIL. However, if we replace values by -100, PIL can't read these images, and will thrown an error.
  • add option to pass segmentation_maps to BeitFeatureExtractor, and add corresponding tests.

To do:
SegformerFeatureExtractor currently includes the align, do_random_crop and do_pad arguments at initialization, however I wonder whether it's maybe better to remove those, and only include the bare minimum in the feature extractors (similar to ViTFeatureExtractor) to get started (i.e. resizing, center cropping, normalizing). Things like random cropping and padding is maybe already a bit too much and makes the feature extractors more complex. It's also not easy to determine good default values for this feature extractor; should it randomly crop + pad by default, or not?

  • remove random cropping and padding from SegformerFeatureExtractor, if one agrees on this.
  • make tests of SegformerFeatureExtractor and BeitFeatureExtractor consistent.
  • update the preprocessor_config.json of the semantic segmentation models on the hub.

Copy link
Collaborator

@sgugger sgugger left a comment

Choose a reason for hiding this comment

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

Thanks for adding this, I left w few comments on the names.

@@ -76,6 +77,26 @@ def prepare_feat_extract_dict(self):
}


def prepare_semantic_single_inputs():
ds = load_dataset("hf-internal-testing/fixtures_ade20k", split="test")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's give a better name than just ds, we try to avoid such short names in the codebase.

src/transformers/models/beit/feature_extraction_beit.py Outdated Show resolved Hide resolved
src/transformers/models/beit/configuration_beit.py Outdated Show resolved Hide resolved
Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

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

Cool, thanks! Agree with both of you to keep the data augmentation part out of the feature extractor. LGTM!

docs/source/model_doc/segformer.rst Show resolved Hide resolved
@NielsRogge NielsRogge merged commit a2864a5 into huggingface:master Nov 17, 2021
Albertobegue pushed a commit to Albertobegue/transformers that referenced this pull request Jan 27, 2022
* Improve tests

* Improve documentation

* Add ignore_index attribute

* Add semantic_ignore_index to BEiT model

* Add segmentation maps argument to BEiTFeatureExtractor

* Simplify SegformerFeatureExtractor and corresponding tests

* Improve tests

* Apply suggestions from code review

* Minor docs improvements

* Streamline segmentation map tests of SegFormer and BEiT

* Improve reduce_labels docs and test

* Fix code quality

* Fix code quality again
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.

SegformerFeatureExtractor trying to access non-existent .ndim attribute
3 participants