Reid strong baseline knowledge distillation [with code]

This project is an update on the basis of Reid strong baseline, realizing knowledge distillation. The project has not yet added the target detection part, and it will be updated from time to time later, please continue to pay attention.

The data set used by Reid is Markt1501, which supports the Resnet series as the baseline network for training. The training adopts the method of representation learning + metric learning, and distillation feature distillation [logic distillation has not been updated yet].

project instruction

1. Only support markt1501 dataset

2. Support resnet network series training (not distilled)

3. Support resnet series feature self-distillation training (temporarily update offline distillation and logical distillation)

4. Teacher network and student network can be freely selected (features of this project)

5. Support the testing of evaluation indicators


Table of contents

Reid training

Knowledge Distillation Training

Teacher Network & Student Network Options

test

Reid related information learning link

Post-plan update


Metric learning employs triplet loss function

Dataset: mark1501 (put the dataset mark1501 under the data folder)

baseline network: support Resnet series, such as resnet18, resnet34, rensnet_ibn, etc.

Reid training

python tools/train.py --model_name resnet50_ibn_a --model_path weights/ReID_resnet50_ibn_a.pth --IMS_PER_BATCH 8 --TEST_IMS_PER_BATCH 4 --MAX_EPOCHS 120

model_name: supported baseline network

支持:resnet18,resnet34,resnet50,resnet101,resnet50_ibn_a

Then the following will appear:

=> Market1501 loaded
Dataset statistics:
  ----------------------------------------
  subset   | # ids | # images | # cameras
  ----------------------------------------
  train    |   751 |    12936 |         6
  query    |   750 |     3368 |         6
  gallery  |   751 |    15913 |         6
  ----------------------------------------
  
2023-05-15 14:30:55.603 | INFO     | engine.trainer:log_training_loss:119 - Epoch[1] Iteration[227/1484] Loss: 6.767, Acc: 0.000, Base Lr: 3.82e-05
2023-05-15 14:30:55.774 | INFO     | engine.trainer:log_training_loss:119 - Epoch[1] Iteration[228/1484] Loss: 6.761, Acc: 0.000, Base Lr: 3.82e-05
2023-05-15 14:30:55.946 | INFO     | engine.trainer:log_training_loss:119 - Epoch[1] Iteration[229/1484] Loss: 6.757, Acc: 0.000, Base Lr: 3.82e-05
2023-05-15 14:30:56.134 | INFO     | engine.trainer:log_training_loss:119 - Epoch[1] Iteration[230/1484] Loss: 6.760, Acc: 0.000, Base Lr: 3.82e-05
2023-05-15 14:30:56.305 | INFO     | engine.trainer:log_training_loss:119 - Epoch[1] Iteration[231/1484] Loss: 6.764, Acc: 0.000, Base Lr: 3.82e-05

After each epoch training is completed, mAP will be tested once:

The mAP of my first epoch here reached 75.1%, Rank-1: 91.7%, Rank-5: 97.2%, Rank-10: 98.2%.

After the test is completed, a pth weight will be saved under the log file, named mAPxx.pth, which is also used for testing.

2023-05-15 14:35:59.753 | INFO     | engine.trainer:print_times:128 - Epoch 1 done. Time per batch: 261.820[s] Speed: 45.4[samples/s]
2023-05-15 14:35:59.755 | INFO     | engine.trainer:print_times:129 - ----------
The test feature is normalized
2023-05-15 14:39:51.025 | INFO     | engine.trainer:log_validation_results:137 - Validation Results - Epoch: 1
2023-05-15 14:39:51.048 | INFO     | engine.trainer:log_validation_results:140 - mAP:75.1%
2023-05-15 14:39:51.051 | INFO     | engine.trainer:log_validation_results:142 - CMC curve, Rank-1  :91.7%
2023-05-15 14:39:51.051 | INFO     | engine.trainer:log_validation_results:142 - CMC curve, Rank-5  :97.2%
2023-05-15 14:39:51.052 | INFO     | engine.trainer:log_validation_results:142 - CMC curve, Rank-10 :98.2%

Knowledge Distillation Training

The supported network is the ResNet series.

Parameter description (the basic parameters are the same as the above training, but with more kd):

--model_name: model name, support Resnet, resnet18_kd, resnet34_kd, resnet50_kd, resnet101_kd

--model_path: pre-weight path

--kd: Turn on distillation mode

--feature_loss_coefficient: The weight of feature distillation, the default is 0.03

The distillation used here is an online distillation (self-distillation), and the offline distillation has not been updated yet.

python tools/train.py --model_name [model name] --model_path [your model weight path] --IMS_PER_BATCH 8 --TEST_IMS_PER_BATCH 4 --kd --feature_loss_coefficient 0.03
=> Market1501 loaded
Dataset statistics:
  ----------------------------------------
  subset   | # ids | # images | # cameras
  ----------------------------------------
  train    |   751 |    12936 |         6
  query    |   750 |     3368 |         6
  gallery  |   751 |    15913 |         6
  ----------------------------------------
resnet50_kd loading pretrained model weight...
label smooth on, numclasses: 751
ready kd train!

After training, the weights will be saved under the logs file, and the naming format is mAP_KD_xx.pth. The following is a comparison of the first Epoch evaluation index before and after resnet50 distillation , which is still improved [due to my limited hardware environment, I just realized the function for everyone].

serious50
Validation Results - Epoch: 1
2023-05-17 20:08:53.642 | INFO     | engine.trainer:log_validation_results:156 - mAP:39.2%
2023-05-17 20:08:53.642 | INFO     | engine.trainer:log_validation_results:158 - CMC curve, Rank-1  :65.6%
2023-05-17 20:08:53.642 | INFO     | engine.trainer:log_validation_results:158 - CMC curve, Rank-5  :80.3%
2023-05-17 20:08:53.642 | INFO     | engine.trainer:log_validation_results:158 - CMC curve, Rank-10 :85.0%
resnet50_kd: [layer3 as teacher network]
2023-05-17 20:22:07.030 | INFO     | engine.trainer:log_validation_results:153 - Validation Results - Epoch: 1
2023-05-17 20:22:07.131 | INFO     | engine.trainer:log_validation_results:156 - mAP:47.9%
2023-05-17 20:22:07.131 | INFO     | engine.trainer:log_validation_results:158 - CMC curve, Rank-1  :73.5%
2023-05-17 20:22:07.131 | INFO     | engine.trainer:log_validation_results:158 - CMC curve, Rank-5  :85.7%
2023-05-17 20:22:07.139 | INFO     | engine.trainer:log_validation_results:158 - CMC curve, Rank-10 :88.9%

Teacher Network & Student Network Options

The teacher network uses a deep network, and the shallow network is a student network.

The specific selection of teacher network and student network can be seen in lines 46 to 58 of engine/trainer.py.

Here, by default, layer3 in resnet is the teacher network, and layer1 and layer2 are student networks. The specific effect can be tried according to your actual task. Feature distillation is used, and logical distillation has not been updated yet.

       elif kd:
            score, feat, layer_out_feat = model(img)
            loss = loss_fn(score, feat, target)
            teacher_feature = layer_out_feat[1].detach()  # 取出教师层
            '''
            (rannge(idx,len(layer_out_feat)),中的idx可以决定哪个作为教师)
            idx=1表示layer4为教师网络,layer3,layer2,layer1为student
            idx=2表示layer3为教师网络,layer2,layer1为student
            idx=3表示layer2为教师网络,layer1为student
            '''
            for index in range(2, len(layer_out_feat)):  # layer4, layer3, layer2, layer1
                if index != 2:  # 排除自己
                    loss += torch.dist(layer_out_feat[index], teacher_feature) * feature_loss_coefficient

test

python tools/test.py --TEST_IMS_PER_BATCH 4 --model_name [your model name] --model_path [your weight path]

Can perform mAP, Rank test


Reid related information learning link

Detailed explanation of data set code: Data set processing of Reid training code - Programmer Sought

Explanation of Reid loss function theory: Reid's loss function theory learning explanation_Representation learning and measurement learning_Love to eat meat Peng's Blog-CSDN Blog

Reid metric learning Triplet loss code explanation: Reid metric learning Triplet loss code analysis. _Peng's Blog-CSDN Blog

reid strong baseline code learning:

Detailed explanation of Reid strong baseline code - Programmer Sought 

Preweight link:

Link: Baidu Netdisk Please enter the extraction code Extraction code: yypn

If the project is useful to you, please click Star

Note: This project is free and open source for the time being , and appropriate charges will be considered after it is improved in the later stage [after all, it is also made by myself]

project code

GitHub - YINYIPENG-EN/reid_strong_baseline_KD: reid strong baseline Knowledge distillation(reid knowledge distillation)

Post-plan update

1. Introduce knowledge distillation training (updated on 2023.05.26)

2. Join YOLOX

Guess you like

Origin blog.csdn.net/z240626191s/article/details/130893528