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

[CodeCamp2023-527] Add pixel contrast cross entropy loss #3264

Open
wants to merge 20 commits into
base: dev-1.x
Choose a base branch
from

Conversation

BLUE-coconut
Copy link
Contributor

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

Please describe the motivation of this PR and the goal you want to achieve through this PR.

Modification

Please briefly describe what modification is made in this PR.

BC-breaking (Optional)

Does the modification introduce changes that break the backward-compatibility of the downstream repos?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

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

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  3. If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDet or MMDet3D.
  4. The documentation has been modified accordingly, like docstring or example tutorials.

@BLUE-coconut BLUE-coconut force-pushed the BLUE-coconut/Add_Pixel_contrast_cross_entropy_loss branch from 0b363ab to 8eb061f Compare August 16, 2023 10:08
Copy link
Collaborator

@xiexinch xiexinch left a comment

Choose a reason for hiding this comment

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

Hi @BLUE-coconut,
Thanks for your contribution! Could you move your code to the project folder, we'll really appreciate it!

Comment on lines 56 to 59
loss_decode=dict(
type='PixelContrastCrossEntropyLoss',
base_temperature=0.07,
temperature=0.1)),
Copy link
Collaborator

Choose a reason for hiding this comment

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

@@ -0,0 +1,8 @@
_base_ = [
'../_base_/models/fcn_hrcontrast18.py', '../_base_/datasets/vaihingen.py',
Copy link
Collaborator

Choose a reason for hiding this comment

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



@MODELS.register_module()
class HRNetContrastHead(BaseDecodeHead):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might rename it to ContrastHead. Since the segmentation head could be from other methods.

Comment on lines 103 to 107
output['seg'] = self.seghead(inputs)

output['proj'] = self.projhead(inputs)

return output
Copy link
Collaborator

Choose a reason for hiding this comment

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

The output should be a list.

Comment on lines 109 to 110
def loss_by_feat(self, seg_logits: dict,
batch_data_samples: SampleList) -> dict:
Copy link
Collaborator

Choose a reason for hiding this comment

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

The seg_logits should be a list.

Comment on lines 136 to 148
for loss_decode in losses_decode:
if loss_decode.loss_name not in loss:
loss[loss_decode.loss_name] = loss_decode(
seg_logits,
seg_label,
weight=seg_weight,
ignore_index=self.ignore_index)
else:
loss[loss_decode.loss_name] += loss_decode(
seg_logits,
seg_label,
weight=seg_weight,
ignore_index=self.ignore_index)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Following the implementation of PaddleSeg, the input of different loss methods should be different.

https://github.com/PaddlePaddle/PaddleSeg/blob/2c8c35a8949fef74599f5ec557d340a14415f20d/paddleseg/core/train.py#L39

from mmseg.registry import MODELS


def hard_anchor_sampling(X, y_hat, y, ignore_index, max_views, max_samples):
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should add type hints.

Comment on lines 212 to 217
loss_name='loss_pixel_contrast_cross_entropy',
temperature=0.1,
base_temperature=0.07,
ignore_index=255,
max_samples=1024,
max_views=100):
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should add type hints.

@@ -0,0 +1,37 @@
# Pixel contrast cross entropy loss

[Exploring Cross-Image Pixel Contrast for Semantic Segmentation](https://arxiv.org/pdf/2101.11939.pdf)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
[Exploring Cross-Image Pixel Contrast for Semantic Segmentation](https://arxiv.org/pdf/2101.11939.pdf)
> [Exploring Cross-Image Pixel Contrast for Semantic Segmentation](https://arxiv.org/pdf/2101.11939.pdf)

"""
seg_logits = self.forward(inputs)

return self.predict_by_feat(seg_logits, batch_img_metas)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might overwrite the predict_by_feat method

Comment on lines 96 to 99
self.seghead = SegmentationHead(
in_channels=self.in_channels,
out_channels=self.num_classes,
drop_prob=drop_p)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might use MODEL.build() and the SegmentationHead head is not necessary.



@MODELS.register_module()
class ContrastHead(BaseDecodeHead):
Copy link
Collaborator

Choose a reason for hiding this comment

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

The conv_seg module in BaseDecodeHead is not used and it might cause a distributed training error. It suggests to inherit from nn.Module.

@@ -11,6 +11,7 @@
from mmcv.cnn import ConvModule
from torch import Tensor

from mmseg.models.builder import HEADS, build_head
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might remove this line.

drop_p=0.1,
dropout_ratio=-1,
num_classes=19,
norm_cfg=norm_cfg,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't find the definition of norm_cfg.

drop_p=0.1,
dropout_ratio=-1,
num_classes=19,
norm_cfg=norm_cfg,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
norm_cfg=norm_cfg,
norm_cfg=dict(type='BN'),

Copy link
Collaborator

Choose a reason for hiding this comment

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

This parameter seems not in use.

Comment on lines 113 to 127
fcn_head=dict(
type='FCNHead',
in_channels=2048,
in_index=3,
channels=512,
num_convs=2,
concat_input=True,
dropout_ratio=0.1,
num_classes=150,
norm_cfg=norm_cfg,
align_corners=False,
loss_decode=dict(
type='CrossEntropyLoss',
use_sigmoid=False,
loss_weight=1.0))**kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

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

The decode head config might move to config file.

Comment on lines +102 to +112
loss_decode=[
dict(
type='PixelContrastCrossEntropyLoss',
base_temperature=0.07,
temperature=0.1,
ignore_index=255,
max_samples=1024,
max_views=100,
loss_weight=0.1),
dict(type='CrossEntropyLoss', loss_weight=1.0)
],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same to the decode head, it might move to the config file.

@xiexinch xiexinch added the WIP Work in process label Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WIP Work in process
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants