Cysu / open-reid

Open source person re-identification library in python
https://cysu.github.io/open-reid/
MIT License
1.34k stars 349 forks source link

Use real features produced by feature extractor at evaluation mode #31

Closed KaiyangZhou closed 6 years ago

KaiyangZhou commented 6 years ago

Hi Tong,

Thanks very much for this great project! I believe this will significantly facilitate the research in person reid.

I have a small suggestion regarding the feature extraction stage in the evaluation mode. When the evaluator.evaluate() is executed, the first step is to extract features followed by construction of the distance matrix used to compute mAPs. However, the features extracted in model.eval() remains the same as those from the training phase, i.e. both are pre-logits produced by the x = self.classifier(x). This is not expected as we want the features to come out from the 'real' feature extractor (resulting in 2048-d vectors in the resnet) instead of the 751-d pre-logit vectors from the classifier (in case of market1501). Therefore, I suggest to modify the def forward(self, x) that will look like:

def forward(self, x):
    x = self.feature_extractor(x)
    if not self.training:
        # self.training decides whether the network is in model.train() or model.eval()
        return x
    x = self.classifier(x)
    return x
Cysu commented 6 years ago

@KaiyangZhou Thank you so much for the suggestions! Actually I have compared the 2048d and 751d before and the latter leads to slightly better performance in general. So in the implementation we use 751d directly.

KaiyangZhou commented 6 years ago

@Cysu Thank you!

The register_forward_hook() also works which was implemented already, but I find using if not self.training is easier.