-
Notifications
You must be signed in to change notification settings - Fork 838
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In `any_softmax`, all operations are in-place, so pass into the `logits.clone()` to prevent outside logits changed.
- Loading branch information
1 parent
c3ac4f5
commit 6300bd7
Showing
3 changed files
with
19 additions
and
9 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
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 |
---|---|---|
|
@@ -4,16 +4,14 @@ | |
@contact: [email protected] | ||
""" | ||
|
||
import math | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
from torch import nn | ||
|
||
from fastreid.config import configurable | ||
from fastreid.layers import * | ||
from fastreid.layers import pooling, any_softmax | ||
from fastreid.utils.weight_init import weights_init_kaiming | ||
from fastreid.layers.weight_init import weights_init_kaiming | ||
from .build import REID_HEADS_REGISTRY | ||
|
||
|
||
|
@@ -78,14 +76,19 @@ def __init__( | |
neck.append(get_norm(norm_type, feat_dim, bias_freeze=True)) | ||
|
||
self.bottleneck = nn.Sequential(*neck) | ||
self.bottleneck.apply(weights_init_kaiming) | ||
|
||
# Linear layer | ||
# Classification head | ||
assert hasattr(any_softmax, cls_type), "Expected cls types are {}, " \ | ||
"but got {}".format(any_softmax.__all__, cls_type) | ||
self.weight = nn.Parameter(torch.normal(0, 0.01, (num_classes, feat_dim))) | ||
self.weight = nn.Parameter(torch.Tensor(num_classes, feat_dim)) | ||
self.cls_layer = getattr(any_softmax, cls_type)(num_classes, scale, margin) | ||
|
||
self.reset_parameters() | ||
|
||
def reset_parameters(self) -> None: | ||
self.bottleneck.apply(weights_init_kaiming) | ||
nn.init.normal_(self.weight, std=0.01) | ||
|
||
@classmethod | ||
def from_config(cls, cfg): | ||
# fmt: off | ||
|
@@ -132,7 +135,8 @@ def forward(self, features, targets=None): | |
else: | ||
logits = F.linear(F.normalize(neck_feat), F.normalize(self.weight)) | ||
|
||
cls_outputs = self.cls_layer(logits, targets) | ||
# Pass logits.clone() into cls_layer, because there is in-place operations | ||
cls_outputs = self.cls_layer(logits.clone(), targets) | ||
|
||
# fmt: off | ||
if self.neck_feat == 'before': feat = pool_feat[..., 0, 0] | ||
|