-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Convert classes used for Detection to nn.Modules #4389
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
Closed
Closed
Changes from all commits
Commits
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 hidden or 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 |
|---|---|---|
|
|
@@ -2,13 +2,13 @@ | |
| import torch | ||
|
|
||
| from collections import OrderedDict | ||
| from torch import Tensor | ||
| from torch import Tensor, nn | ||
| from typing import List, Tuple | ||
|
|
||
| from torchvision.ops.misc import FrozenBatchNorm2d | ||
|
|
||
|
|
||
| class BalancedPositiveNegativeSampler(object): | ||
| class BalancedPositiveNegativeSampler(nn.Module): | ||
| """ | ||
| This class samples batches, ensuring that they contain a fixed proportion of positives | ||
| """ | ||
|
|
@@ -20,10 +20,11 @@ def __init__(self, batch_size_per_image, positive_fraction): | |
| batch_size_per_image (int): number of elements to be selected per image | ||
| positive_fraction (float): percentace of positive elements per batch | ||
| """ | ||
| super().__init__() | ||
| self.batch_size_per_image = batch_size_per_image | ||
| self.positive_fraction = positive_fraction | ||
|
|
||
| def __call__(self, matched_idxs): | ||
| def forward(self, matched_idxs): | ||
| # type: (List[Tensor]) -> Tuple[List[Tensor], List[Tensor]] | ||
| """ | ||
| Args: | ||
|
|
@@ -126,7 +127,7 @@ def encode_boxes(reference_boxes, proposals, weights): | |
| return targets | ||
|
|
||
|
|
||
| class BoxCoder(object): | ||
| class BoxCoder(nn.Module): | ||
| """ | ||
| This class encodes and decodes a set of bounding boxes into | ||
| the representation used for training the regressors. | ||
|
|
@@ -139,6 +140,7 @@ def __init__(self, weights, bbox_xform_clip=math.log(1000. / 16)): | |
| weights (4-element tuple) | ||
| bbox_xform_clip (float) | ||
| """ | ||
| super().__init__() | ||
| self.weights = weights | ||
| self.bbox_xform_clip = bbox_xform_clip | ||
|
|
||
|
|
@@ -228,7 +230,7 @@ def decode_single(self, rel_codes, boxes): | |
| return pred_boxes | ||
|
|
||
|
|
||
| class Matcher(object): | ||
| class Matcher(nn.Module): | ||
| """ | ||
| This class assigns to each predicted "element" (e.g., a box) a ground-truth | ||
| element. Each predicted element will have exactly zero or one matches; each | ||
|
|
@@ -266,14 +268,18 @@ def __init__(self, high_threshold, low_threshold, allow_low_quality_matches=Fals | |
| for predictions that have only low-quality match candidates. See | ||
| set_low_quality_matches_ for more details. | ||
| """ | ||
| super().__init__() | ||
| self.BELOW_LOW_THRESHOLD = -1 | ||
| self.BETWEEN_THRESHOLDS = -2 | ||
| assert low_threshold <= high_threshold | ||
| self.high_threshold = high_threshold | ||
| self.low_threshold = low_threshold | ||
| self.allow_low_quality_matches = allow_low_quality_matches | ||
|
|
||
| def __call__(self, match_quality_matrix): | ||
| def forward(self, match_quality_matrix): | ||
| return self._forward_impl(match_quality_matrix) | ||
|
|
||
| def _forward_impl(self, match_quality_matrix): | ||
|
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. Workaround for overwriting the forward on inheriting classes. We do the same on quantization. |
||
| """ | ||
| Args: | ||
| match_quality_matrix (Tensor[float]): an MxN tensor, containing the | ||
|
|
@@ -354,8 +360,8 @@ class SSDMatcher(Matcher): | |
| def __init__(self, threshold): | ||
| super().__init__(threshold, threshold, allow_low_quality_matches=False) | ||
|
|
||
| def __call__(self, match_quality_matrix): | ||
| matches = super().__call__(match_quality_matrix) | ||
| def forward(self, match_quality_matrix): | ||
| matches = self._forward_impl(match_quality_matrix) | ||
|
|
||
| # For each gt, find the prediction with which it has the highest quality | ||
| _, highest_quality_pred_foreach_gt = match_quality_matrix.max(dim=1) | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.
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.
This class with its encode/decode methods does not play nice with the nn.Module's forward() approach. Not sure if we should convert it but it will allow us to drop its declarations to
__annotations__