-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove __target__ for Detection CollateFN (#1470)
* remove __target__ * undo * fix * add __init__ __all__ * fix __call__ in CrowdDetectionCollateFN + use main import --------- Co-authored-by: Eugene Khvedchenya <ekhvedchenya@gmail.com>
- Loading branch information
1 parent
98f3226
commit 94c0f9e
Showing
20 changed files
with
289 additions
and
188 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from .dataset_exceptions import ( | ||
EmptyDatasetException, | ||
DatasetItemsException, | ||
DatasetValidationException, | ||
IllegalDatasetParameterException, | ||
ParameterMismatchException, | ||
UnsupportedBatchItemsFormat, | ||
) | ||
from .loss_exceptions import RequiredLossComponentReductionException, IllegalRangeForLossAttributeException | ||
from .factory_exceptions import UnknownTypeException | ||
from .kd_trainer_exceptions import ( | ||
KDModelException, | ||
UnsupportedKDModelArgException, | ||
UnsupportedKDArchitectureException, | ||
ArchitectureKwargsException, | ||
InconsistentParamsException, | ||
TeacherKnowledgeException, | ||
UndefinedNumClassesException, | ||
) | ||
from .sg_trainer_exceptions import IllegalDataloaderInitialization, UnsupportedOptimizerFormat, UnsupportedTrainingParameterFormat, GPUModeNotSetupError | ||
|
||
__all__ = [ | ||
"EmptyDatasetException", | ||
"DatasetItemsException", | ||
"DatasetValidationException", | ||
"IllegalDatasetParameterException", | ||
"ParameterMismatchException", | ||
"UnsupportedBatchItemsFormat", | ||
"RequiredLossComponentReductionException", | ||
"IllegalRangeForLossAttributeException", | ||
"UnknownTypeException", | ||
"KDModelException", | ||
"UnsupportedKDModelArgException", | ||
"UnsupportedKDArchitectureException", | ||
"ArchitectureKwargsException", | ||
"InconsistentParamsException", | ||
"TeacherKnowledgeException", | ||
"UndefinedNumClassesException", | ||
"IllegalDataloaderInitialization", | ||
"UnsupportedOptimizerFormat", | ||
"UnsupportedTrainingParameterFormat", | ||
"GPUModeNotSetupError", | ||
] |
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
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from .detection_collate_fn import DetectionCollateFN | ||
from .ppyoloe_collate_fn import PPYoloECollateFN | ||
from .crowd_detection_collate_fn import CrowdDetectionCollateFN | ||
from .crowd_detection_ppyoloe_collate_fn import CrowdDetectionPPYoloECollateFN | ||
|
||
__all__ = ["DetectionCollateFN", "PPYoloECollateFN", "CrowdDetectionCollateFN", "CrowdDetectionPPYoloECollateFN"] |
26 changes: 26 additions & 0 deletions
26
src/super_gradients/training/utils/collate_fn/crowd_detection_collate_fn.py
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Tuple, Dict | ||
|
||
import torch | ||
|
||
from super_gradients.common.registry import register_collate_function | ||
from super_gradients.common.exceptions.dataset_exceptions import DatasetItemsException | ||
from super_gradients.training.utils.collate_fn.detection_collate_fn import DetectionCollateFN | ||
|
||
|
||
@register_collate_function() | ||
class CrowdDetectionCollateFN(DetectionCollateFN): | ||
""" | ||
Collate function for Yolox training with additional_batch_items that includes crowd targets | ||
""" | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.expected_item_names = ("image", "targets", "crowd_targets") | ||
|
||
def __call__(self, data) -> Tuple[torch.Tensor, torch.Tensor, Dict[str, torch.Tensor]]: | ||
try: | ||
images_batch, labels_batch, crowd_labels_batch = list(zip(*data)) | ||
except (ValueError, TypeError): | ||
raise DatasetItemsException(data_sample=data[0], collate_type=type(self), expected_item_names=self.expected_item_names) | ||
|
||
return self._format_images(images_batch), self._format_targets(labels_batch), {"crowd_targets": self._format_targets(crowd_labels_batch)} |
30 changes: 30 additions & 0 deletions
30
src/super_gradients/training/utils/collate_fn/crowd_detection_ppyoloe_collate_fn.py
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import Union, List, Tuple, Dict | ||
|
||
import torch | ||
|
||
from super_gradients.common.registry import register_collate_function | ||
from super_gradients.common.exceptions.dataset_exceptions import DatasetItemsException | ||
from super_gradients.training.utils.collate_fn.ppyoloe_collate_fn import PPYoloECollateFN | ||
|
||
|
||
@register_collate_function() | ||
class CrowdDetectionPPYoloECollateFN(PPYoloECollateFN): | ||
""" | ||
Collate function for Yolox training with additional_batch_items that includes crowd targets | ||
""" | ||
|
||
def __init__(self, random_resize_sizes: Union[List[int], None] = None, random_resize_modes: Union[List[int], None] = None): | ||
super().__init__(random_resize_sizes, random_resize_modes) | ||
self.expected_item_names = ("image", "targets", "crowd_targets") | ||
|
||
def __call__(self, data) -> Tuple[torch.Tensor, torch.Tensor, Dict[str, torch.Tensor]]: | ||
|
||
if self.random_resize_sizes is not None: | ||
data = self.random_resize(data) | ||
|
||
try: | ||
images_batch, labels_batch, crowd_labels_batch = list(zip(*data)) | ||
except (ValueError, TypeError): | ||
raise DatasetItemsException(data_sample=data[0], collate_type=type(self), expected_item_names=self.expected_item_names) | ||
|
||
return self._format_images(images_batch), self._format_targets(labels_batch), {"crowd_targets": self._format_targets(crowd_labels_batch)} |
47 changes: 47 additions & 0 deletions
47
src/super_gradients/training/utils/collate_fn/detection_collate_fn.py
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from typing import Tuple, List, Union | ||
|
||
import numpy as np | ||
import torch | ||
|
||
from super_gradients.common.registry import register_collate_function | ||
from super_gradients.common.exceptions.dataset_exceptions import DatasetItemsException | ||
|
||
|
||
@register_collate_function() | ||
class DetectionCollateFN: | ||
""" | ||
Collate function for Yolox training | ||
""" | ||
|
||
def __init__(self): | ||
self.expected_item_names = ("image", "targets") | ||
|
||
def __call__(self, data) -> Tuple[torch.Tensor, torch.Tensor]: | ||
try: | ||
images_batch, labels_batch = list(zip(*data)) | ||
except (ValueError, TypeError): | ||
raise DatasetItemsException(data_sample=data[0], collate_type=type(self), expected_item_names=self.expected_item_names) | ||
|
||
return self._format_images(images_batch), self._format_targets(labels_batch) | ||
|
||
def _format_images(self, images_batch: List[Union[torch.Tensor, np.array]]) -> torch.Tensor: | ||
images_batch = [torch.tensor(img) for img in images_batch] | ||
images_batch_stack = torch.stack(images_batch, 0) | ||
if images_batch_stack.shape[3] == 3: | ||
images_batch_stack = torch.moveaxis(images_batch_stack, -1, 1).float() | ||
return images_batch_stack | ||
|
||
def _format_targets(self, labels_batch: List[Union[torch.Tensor, np.array]]) -> torch.Tensor: | ||
""" | ||
Stack a batch id column to targets and concatenate | ||
:param labels_batch: a list of targets per image (each of arbitrary length) | ||
:return: one tensor of targets of all imahes of shape [N, 6], where N is the total number of targets in a batch | ||
and the 1st column is batch item index | ||
""" | ||
labels_batch = [torch.tensor(labels) for labels in labels_batch] | ||
labels_batch_indexed = [] | ||
for i, labels in enumerate(labels_batch): | ||
batch_column = labels.new_ones((labels.shape[0], 1)) * i | ||
labels = torch.cat((batch_column, labels), dim=-1) | ||
labels_batch_indexed.append(labels) | ||
return torch.cat(labels_batch_indexed, 0) |
Oops, something went wrong.