-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[Fix] Add swap_labe_pairs
in RandomFlip
#2332
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3782c39
[Fix] Add `swap_labe_pairs` in `RandomFlip`
MeowZheng 3a64bb5
[Fix] Add `swap_labe_pairs` in `RandomFlip`
MeowZheng b62ba48
add reference info
MeowZheng 91954b6
add swap_label_pairs in results
MeowZheng fac19cf
revise according to comments
MeowZheng 74c9452
revise according to comments
MeowZheng 2796a2c
revise according to comments
MeowZheng ff515bd
docstring
MeowZheng 374576e
docstring
MeowZheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1027,19 +1027,21 @@ class RandomFlip(BaseTransform): | |||||||||||||||||
- flip_direction | ||||||||||||||||||
|
||||||||||||||||||
Args: | ||||||||||||||||||
prob (float | list[float], optional): The flipping probability. | ||||||||||||||||||
Defaults to None. | ||||||||||||||||||
direction(str | list[str]): The flipping direction. Options | ||||||||||||||||||
If input is a list, the length must equal ``prob``. Each | ||||||||||||||||||
element in ``prob`` indicates the flip probability of | ||||||||||||||||||
corresponding direction. Defaults to 'horizontal'. | ||||||||||||||||||
prob (float | list[float], optional): The flipping probability. | ||||||||||||||||||
Defaults to None. | ||||||||||||||||||
direction(str | list[str]): The flipping direction. Options | ||||||||||||||||||
If input is a list, the length must equal ``prob``. Each | ||||||||||||||||||
element in ``prob`` indicates the flip probability of | ||||||||||||||||||
corresponding direction. Defaults to 'horizontal'. | ||||||||||||||||||
swap_label_pairs (list, optional): The label pair need to be swapped | ||||||||||||||||||
for ground truth, like 'left arm' and 'right arm' need to be | ||||||||||||||||||
swapped after horizontal flipping. | ||||||||||||||||||
""" | ||||||||||||||||||
|
||||||||||||||||||
def __init__( | ||||||||||||||||||
self, | ||||||||||||||||||
prob: Optional[Union[float, Iterable[float]]] = None, | ||||||||||||||||||
direction: Union[str, | ||||||||||||||||||
Sequence[Optional[str]]] = 'horizontal') -> None: | ||||||||||||||||||
def __init__(self, | ||||||||||||||||||
prob: Optional[Union[float, Iterable[float]]] = None, | ||||||||||||||||||
direction: Union[str, Sequence[Optional[str]]] = 'horizontal', | ||||||||||||||||||
swap_label_pairs: Optional[Sequence] = None) -> None: | ||||||||||||||||||
if isinstance(prob, list): | ||||||||||||||||||
assert mmengine.is_list_of(prob, float) | ||||||||||||||||||
assert 0 <= sum(prob) <= 1 | ||||||||||||||||||
|
@@ -1049,6 +1051,7 @@ def __init__( | |||||||||||||||||
raise ValueError(f'probs must be float or list of float, but \ | ||||||||||||||||||
got `{type(prob)}`.') | ||||||||||||||||||
self.prob = prob | ||||||||||||||||||
self.swap_label_pairs = swap_label_pairs | ||||||||||||||||||
|
||||||||||||||||||
valid_directions = ['horizontal', 'vertical', 'diagonal'] | ||||||||||||||||||
if isinstance(direction, str): | ||||||||||||||||||
|
@@ -1064,8 +1067,8 @@ def __init__( | |||||||||||||||||
if isinstance(prob, list): | ||||||||||||||||||
assert len(prob) == len(self.direction) | ||||||||||||||||||
|
||||||||||||||||||
def flip_bbox(self, bboxes: np.ndarray, img_shape: Tuple[int, int], | ||||||||||||||||||
direction: str) -> np.ndarray: | ||||||||||||||||||
def _flip_bbox(self, bboxes: np.ndarray, img_shape: Tuple[int, int], | ||||||||||||||||||
direction: str) -> np.ndarray: | ||||||||||||||||||
"""Flip bboxes horizontally. | ||||||||||||||||||
|
||||||||||||||||||
Args: | ||||||||||||||||||
|
@@ -1096,8 +1099,9 @@ def flip_bbox(self, bboxes: np.ndarray, img_shape: Tuple[int, int], | |||||||||||||||||
or 'diagonal', but got '{direction}'") | ||||||||||||||||||
return flipped | ||||||||||||||||||
|
||||||||||||||||||
def flip_keypoints(self, keypoints: np.ndarray, img_shape: Tuple[int, int], | ||||||||||||||||||
direction: str) -> np.ndarray: | ||||||||||||||||||
def _flip_keypoints(self, keypoints: np.ndarray, img_shape: Tuple[int, | ||||||||||||||||||
int], | ||||||||||||||||||
direction: str) -> np.ndarray: | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
"""Flip keypoints horizontally, vertically or diagonally. | ||||||||||||||||||
|
||||||||||||||||||
Args: | ||||||||||||||||||
|
@@ -1127,6 +1131,32 @@ def flip_keypoints(self, keypoints: np.ndarray, img_shape: Tuple[int, int], | |||||||||||||||||
flipped = np.concatenate([keypoints, meta_info], axis=-1) | ||||||||||||||||||
return flipped | ||||||||||||||||||
|
||||||||||||||||||
def _flip_seg_map(self, seg_map: dict, direction: str) -> np.ndarray: | ||||||||||||||||||
"""Flip segmentation map horizontally, vertically or diagonally. | ||||||||||||||||||
|
||||||||||||||||||
Args: | ||||||||||||||||||
seg_map (numpy.ndarray): segmentation map, shape (H, W). | ||||||||||||||||||
direction (str): Flip direction. Options are 'horizontal', | ||||||||||||||||||
'vertical'. | ||||||||||||||||||
Returns: | ||||||||||||||||||
zhouzaida marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
numpy.ndarray: Flipped segmentation map. | ||||||||||||||||||
""" | ||||||||||||||||||
seg_map = mmcv.imflip(seg_map, direction=direction) | ||||||||||||||||||
if self.swap_label_pairs is not None: | ||||||||||||||||||
# to handle datasets with left/right annotations | ||||||||||||||||||
# like 'Left-arm' and 'Right-arm' in LIP dataset | ||||||||||||||||||
# Modified from https://github.com/openseg-group/openseg.pytorch/blob/master/lib/datasets/tools/cv2_aug_transforms.py # noqa:E501 | ||||||||||||||||||
# Licensed under MIT license | ||||||||||||||||||
temp = seg_map.copy() | ||||||||||||||||||
assert isinstance(self.swap_label_pairs, (tuple, list)) | ||||||||||||||||||
for pair in self.swap_label_pairs: | ||||||||||||||||||
assert isinstance(pair, (tuple, list)) and len(pair) == 2, \ | ||||||||||||||||||
'swap_label_pairs must be a sequence with pair, but got ' \ | ||||||||||||||||||
f'{self.swap_label_pairs}.' | ||||||||||||||||||
seg_map[temp == pair[0]] = pair[1] | ||||||||||||||||||
seg_map[temp == pair[1]] = pair[0] | ||||||||||||||||||
zhouzaida marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
return seg_map | ||||||||||||||||||
|
||||||||||||||||||
@cache_randomness | ||||||||||||||||||
def _choose_direction(self) -> str: | ||||||||||||||||||
"""Choose the flip direction according to `prob` and `direction`""" | ||||||||||||||||||
|
@@ -1162,19 +1192,20 @@ def _flip(self, results: dict) -> None: | |||||||||||||||||
|
||||||||||||||||||
# flip bboxes | ||||||||||||||||||
if results.get('gt_bboxes', None) is not None: | ||||||||||||||||||
results['gt_bboxes'] = self.flip_bbox(results['gt_bboxes'], | ||||||||||||||||||
img_shape, | ||||||||||||||||||
results['flip_direction']) | ||||||||||||||||||
results['gt_bboxes'] = self._flip_bbox(results['gt_bboxes'], | ||||||||||||||||||
img_shape, | ||||||||||||||||||
results['flip_direction']) | ||||||||||||||||||
|
||||||||||||||||||
# flip keypoints | ||||||||||||||||||
if results.get('gt_keypoints', None) is not None: | ||||||||||||||||||
results['gt_keypoints'] = self.flip_keypoints( | ||||||||||||||||||
results['gt_keypoints'] = self._flip_keypoints( | ||||||||||||||||||
results['gt_keypoints'], img_shape, results['flip_direction']) | ||||||||||||||||||
|
||||||||||||||||||
# flip segs | ||||||||||||||||||
# flip seg map | ||||||||||||||||||
if results.get('gt_seg_map', None) is not None: | ||||||||||||||||||
results['gt_seg_map'] = mmcv.imflip( | ||||||||||||||||||
results['gt_seg_map'] = self._flip_seg_map( | ||||||||||||||||||
results['gt_seg_map'], direction=results['flip_direction']) | ||||||||||||||||||
results['swap_label_pairs'] = self.swap_label_pairs | ||||||||||||||||||
|
||||||||||||||||||
def _flip_on_direction(self, results: dict) -> None: | ||||||||||||||||||
"""Function to flip images, bounding boxes, semantic segmentation map | ||||||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the
swap_label_paris
only affect seg maps? How about key points?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And better to provide the detailed usage of the argument. How to specify the
left arm
andright arm
pair?