question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bad results when training hacnn model with triplet loss engine.

See original GitHub issue

I tried to train hacnn model with triplet loss on my custom dataset (VeRi dataset) and got very bad results. Here are the training settings that I used:

from dataset_torchreid import *
import argparse


parser = argparse.ArgumentParser(description='Training torchreid')
parser.add_argument('--engine', default='softmax', type=str, help='which engine to use: softmax or triplet')
opt = parser.parse_args()

engine = opt.engine

torchreid.data.register_image_dataset('veri_dataset', VeRiDataset)

datamanager = torchreid.data.ImageDataManager(
    root='..Dataset/VeRi_with_plate/',
    sources='veri_dataset',
    height=160,
    width=64,
    train_sampler='RandomIdentitySampler' if engine == "triplet" else "RandomSampler"
)


model = torchreid.models.build_model(
    name='hacnn',
    num_classes=datamanager.num_train_pids,
    loss=engine
)

model = model.cuda()

optimizer = torchreid.optim.build_optimizer(
    model, optim='adam', lr=0.0003
)

scheduler = torchreid.optim.build_lr_scheduler(
    optimizer,
    lr_scheduler='single_step',
    stepsize=20
)

if engine == "triplet":
    save_path = './model/veri_hacnn/hacnn_triplet/'
    model_engine = torchreid.engine.ImageTripletEngine(
        datamanager, model, optimizer, margin=0.3,
        weight_t=1, weight_x=0, scheduler=scheduler
    )
else:
    save_path = './model/veri_hacnn/hacnn_softmax/'
    model_engine = torchreid.engine.ImageSoftmaxEngine(
        datamanager, model, optimizer, scheduler=scheduler
    )

model_engine.run(
    max_epoch=100,
    save_dir=save_path,
    print_freq=10
)

Here are the results that I got after training the model for 100 epochs:

Successfully loaded pretrained weights from "/home/rajat/MyPC/DFKI/MasterThesis/Vehicle_Reidentification/model/veri_hacnn/hacnn_triplet/model.pth.tar-100"
##### Evaluating veri_dataset (source) #####
Extracting features from query set ...
/home/rajat/MyPC/DFKI/MasterThesis/Vehicle_Reidentification/veri/lib/python3.6/site-packages/torch/nn/functional.py:2457: UserWarning: nn.functional.upsample is deprecated. Use nn.functional.interpolate instead.
  warnings.warn("nn.functional.upsample is deprecated. Use nn.functional.interpolate instead.")
Done, obtained 1678-by-1024 matrix
Extracting features from gallery set ...
Done, obtained 11579-by-1024 matrix
Speed: 0.0614 sec/batch
Computing distance matrix with metric=euclidean ...
Computing CMC and mAP ...
** Results **
mAP: 9.6%
CMC curve
Rank-1  : 14.7%
Rank-5  : 25.9%
Rank-10 : 34.9%
Rank-20 : 43.4%
Visualizing top-9 ranks
# query: 1678
# gallery 11579
Saving images to "./model/veri_hacnn/hacnn_triplet/visrank-1/veri_dataset"
Done

If I train the same model but with softmax engine I get pretty good results as compared. I get 60% mAP with softmax engine. But as can be seen in the results above, after training for about 9 hours and for 100 epochs(with triplet loss engine) the mAP came out to be just 9.6%. What could have gone wrong in the training process?

Thanks in Advance.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
KaiyangZhoucommented, Jun 19, 2019

triplet loss does not work well alone for training a model from scratch, I would suggest using softmax only which guarantees performance

I would not use strip-based models (e.g. hacnn) for vehicle reid as vehicles can be upside down

how about using resnet50 or densenet121?

I don’t think resizing image is the culprit, the image textures are more important (this paper also discusses this point https://openreview.net/forum?id=Bygh9j09KX)

Read more comments on GitHub >

github_iconTop Results From Across the Web

In training a triplet network, I first have a solid drop in loss, but ...
When this happens, the distances in (∗) go to zero, the loss gets stuck at α and the model is basically done updating....
Read more >
Multi-Modal Long-Term Person Re-Identification Using ... - MDPI
The final decision of the model is formed using score-level fusion and the weighted sum rule is applied to improve the recognition performance....
Read more >
Correcting the Triplet Selection Bias for Triplet Loss
However, in practice, the training is usually very sensitive to the selec- tion of triplets, e.g., it almost does not converge with randomly...
Read more >
TOWARDS DEEP LEARNING ROBUSTNESS FOR ...
models achieve state-of-the-art results on many popular visual benchmarks with ... debiased neural network training significantly boosts the model.
Read more >
Person ReID in Different Environment Settings Using Deep ...
We proposed a Teacher-Student GAN model to solve the cross-modality person. ReID problem. ... Triplet loss treats the ReID as a image retrieval...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found