From 757d52868f15a8a7c6e339fe5ddc8c73cbddab34 Mon Sep 17 00:00:00 2001 From: liuning-scu-cn <2742229056@qq.com> Date: Sat, 3 Aug 2019 08:39:04 +0800 Subject: [PATCH] update --- core/__init__.py | 0 core/config.py | 0 core/loss.py | 14 ++++++++------ core/model.py | 10 +++++++--- core/re_ranking.py | 0 core/schedule.py | 0 core/seed.py | 0 core/step_lr.py | 0 core/utils.py | 0 dataload/__init__.py | 0 dataload/constant.py | 0 dataload/dataloader.py | 0 dataload/mydataset.py | 0 finetune_tiger_cnn5_triplet.py | 13 +++++++------ finetune_tiger_cnn8_triplet.py | 13 +++++++------ test.py | 4 ++-- train.py | 12 ++++-------- 17 files changed, 35 insertions(+), 31 deletions(-) mode change 100644 => 100755 core/__init__.py mode change 100644 => 100755 core/config.py mode change 100644 => 100755 core/loss.py mode change 100644 => 100755 core/model.py mode change 100644 => 100755 core/re_ranking.py mode change 100644 => 100755 core/schedule.py mode change 100644 => 100755 core/seed.py mode change 100644 => 100755 core/step_lr.py mode change 100644 => 100755 core/utils.py mode change 100644 => 100755 dataload/__init__.py mode change 100644 => 100755 dataload/constant.py mode change 100644 => 100755 dataload/dataloader.py mode change 100644 => 100755 dataload/mydataset.py mode change 100644 => 100755 finetune_tiger_cnn5_triplet.py mode change 100644 => 100755 finetune_tiger_cnn8_triplet.py mode change 100644 => 100755 test.py mode change 100644 => 100755 train.py diff --git a/core/__init__.py b/core/__init__.py old mode 100644 new mode 100755 diff --git a/core/config.py b/core/config.py old mode 100644 new mode 100755 diff --git a/core/loss.py b/core/loss.py old mode 100644 new mode 100755 index 8b18569..9a6ed35 --- a/core/loss.py +++ b/core/loss.py @@ -172,12 +172,14 @@ def hard_example_mining(dist_mat, labels, return_inds=False): # `dist_ap` means distance(anchor, positive) # both `dist_ap` and `relative_p_inds` with shape [N, 1] - dist_ap, relative_p_inds = torch.max( - dist_mat[is_pos].contiguous().view(N, -1), 1, keepdim=True) + dist_ap, relative_p_inds = torch.max((dist_mat * is_pos.float()).contiguous().view(N, -1), 1, keepdim=True) + # `dist_an` means distance(anchor, negative) # both `dist_an` and `relative_n_inds` with shape [N, 1] - dist_an, relative_n_inds = torch.min( - dist_mat[is_neg].contiguous().view(N, -1), 1, keepdim=True) + temp = dist_mat * is_neg.float() + temp[temp == 0] = 10e5 + dist_an, relative_n_inds = torch.min((temp).contiguous().view(N, -1), 1, keepdim=True) + # shape [N] dist_ap = dist_ap.squeeze(1) dist_an = dist_an.squeeze(1) @@ -189,9 +191,9 @@ def hard_example_mining(dist_mat, labels, return_inds=False): .unsqueeze(0).expand(N, N)) # shape [N, 1] p_inds = torch.gather( - ind[is_pos].contiguous().view(N, -1), 1, relative_p_inds.data) + (ind * is_pos.long()).contiguous().view(N, -1), 1, relative_p_inds.data) n_inds = torch.gather( - ind[is_neg].contiguous().view(N, -1), 1, relative_n_inds.data) + (ind * is_pos.long()).contiguous().view(N, -1), 1, relative_n_inds.data) # shape [N] p_inds = p_inds.squeeze(1) n_inds = n_inds.squeeze(1) diff --git a/core/model.py b/core/model.py old mode 100644 new mode 100755 index 878b672..a0558a6 --- a/core/model.py +++ b/core/model.py @@ -561,7 +561,7 @@ def get_loss(self, logits, labels, direction): loss = self.loss(logits[0], labels) + self.loss(logits[1], direction) triplet_loss = global_loss(TripletLoss(margin=0.3), logits[2], labels)[0] - return loss + triplet_loss + return triplet_loss + loss def features(self, x): # backbone @@ -589,9 +589,7 @@ def forward(self, x, label=None, direction=None): x = self.fc7(x) x = l2_norm(x) - # TigerID glogit = self.cls(x) - # Left/Right dlogit = self.cls_direction(x) return [glogit, dlogit, x] @@ -628,6 +626,9 @@ def __init__(self, classes=107): self.cls_direction = nn.Linear(512, 2) self.loss = LabelSmoothingCrossEntropy(smoothing=0.1) + def fix_params(self, is_training=True): + pass + def get_loss(self, logits, labels, direction): loss = self.loss(logits[0], labels) + self.loss(logits[1], direction) return loss @@ -690,6 +691,9 @@ def __init__(self, classes=107): self.cls_direction = nn.Linear(512, 2) self.loss = LabelSmoothingCrossEntropy(smoothing=0.1) + def fix_params(self, is_training=True): + pass + def get_loss(self, logits, labels, direction): loss = self.loss(logits[0], labels) + self.loss(logits[1], direction) return loss diff --git a/core/re_ranking.py b/core/re_ranking.py old mode 100644 new mode 100755 diff --git a/core/schedule.py b/core/schedule.py old mode 100644 new mode 100755 diff --git a/core/seed.py b/core/seed.py old mode 100644 new mode 100755 diff --git a/core/step_lr.py b/core/step_lr.py old mode 100644 new mode 100755 diff --git a/core/utils.py b/core/utils.py old mode 100644 new mode 100755 diff --git a/dataload/__init__.py b/dataload/__init__.py old mode 100644 new mode 100755 diff --git a/dataload/constant.py b/dataload/constant.py old mode 100644 new mode 100755 diff --git a/dataload/dataloader.py b/dataload/dataloader.py old mode 100644 new mode 100755 diff --git a/dataload/mydataset.py b/dataload/mydataset.py old mode 100644 new mode 100755 diff --git a/finetune_tiger_cnn5_triplet.py b/finetune_tiger_cnn5_triplet.py old mode 100644 new mode 100755 index a8b2691..f943718 --- a/finetune_tiger_cnn5_triplet.py +++ b/finetune_tiger_cnn5_triplet.py @@ -29,7 +29,7 @@ def main(): if os.path.exists(save_dir): raise NameError('model dir exists!') os.makedirs(save_dir) - copyfile('./finetune_triplet.py.py', save_dir + '/train.py') + copyfile('./finetune_tiger_cnn5_triplet.py', save_dir + '/train.py') copyfile('./core/model.py', save_dir + '/model.py') copyfile('./core/config.py', save_dir + '/config.py') logging = init_log(save_dir) @@ -39,7 +39,7 @@ def main(): gallery_paths = ['./datalist/gallery.txt', ] probe_paths = ['./datalist/probe.txt', ] - train_iter, gallery_iter, probe_iter = load_direction_gallery_probe( + train_iter, gallery_iter, probe_iter = load_triplet_direction_gallery_probe( root='./database', train_paths=train_paths, gallery_paths=gallery_paths, @@ -47,8 +47,9 @@ def main(): signal=' ', resize_size=RESIZE_SIZE, input_size=INPUT_SIZE, - batch_size=BATCH_SIZE, - num_workers=2 + batch_size=8, + num_workers=2, + collate_fn=train_collate ) feature_size = 1024 @@ -67,7 +68,7 @@ def main(): exp_lr_scheduler = StepLRScheduler(optimizer=optimizer, decay_t=20, decay_rate=0.1, warmup_lr_init=1e-5, warmup_t=3) - net.load_state_dict(torch.load('./model/tiger_cnn1/model.ckpt')) + net.load_state_dict(torch.load('./model/tiger_cnn1/model.ckpt')['net_state_dict']) # net.fix_params(is_training=False) net = net.cuda() if multi_gpus: @@ -108,6 +109,7 @@ def main(): optimizer.zero_grad() logits = net(inputs, labels) + if multi_gpus: loss = net.module.get_loss(logits, labels, direction) else: @@ -231,4 +233,3 @@ def main(): main() - diff --git a/finetune_tiger_cnn8_triplet.py b/finetune_tiger_cnn8_triplet.py old mode 100644 new mode 100755 index 5e831a9..4844eb4 --- a/finetune_tiger_cnn8_triplet.py +++ b/finetune_tiger_cnn8_triplet.py @@ -20,7 +20,7 @@ init_environment() os.environ['CUDA_VISIBLE_DEVICES'] = '0' multi_gpus = False -model_name = 'tiger_cnn5' +model_name = 'tiger_cnn8' def main(): @@ -29,7 +29,7 @@ def main(): if os.path.exists(save_dir): raise NameError('model dir exists!') os.makedirs(save_dir) - copyfile('./finetune_triplet.py.py', save_dir + '/train.py') + copyfile('./finetune_tiger_cnn8_triplet.py', save_dir + '/train.py') copyfile('./core/model.py', save_dir + '/model.py') copyfile('./core/config.py', save_dir + '/config.py') logging = init_log(save_dir) @@ -39,7 +39,7 @@ def main(): gallery_paths = ['./datalist/gallery.txt', ] probe_paths = ['./datalist/probe.txt', ] - train_iter, gallery_iter, probe_iter = load_direction_gallery_probe( + train_iter, gallery_iter, probe_iter = load_triplet_direction_gallery_probe( root='./database', train_paths=train_paths, gallery_paths=gallery_paths, @@ -47,8 +47,9 @@ def main(): signal=' ', resize_size=RESIZE_SIZE, input_size=INPUT_SIZE, - batch_size=BATCH_SIZE, - num_workers=2 + batch_size=8, + num_workers=2, + collate_fn=train_collate ) feature_size = 1024 @@ -66,7 +67,7 @@ def main(): ) exp_lr_scheduler = StepLRScheduler(optimizer=optimizer, decay_t=20, decay_rate=0.1, warmup_lr_init=1e-5, warmup_t=3) - net.load_state_dict(torch.load('./model/tiger_cnn3/model.ckpt')) + net.load_state_dict(torch.load('./model/tiger_cnn3/model.ckpt')['net_state_dict']) net.fix_params(is_training=False) net = net.cuda() if multi_gpus: diff --git a/test.py b/test.py old mode 100644 new mode 100755 index 9202e26..e36d40c --- a/test.py +++ b/test.py @@ -122,8 +122,8 @@ def main(): ) feature_size = 1024 - net = tiger_cnn8(classes=107) - net.load_state_dict(torch.load('/media/liuning/UBUNTU 16_0/ValidateTiger/model/SEResNet50_0.3TripletTigerCNN3_Finetuning_WarmUp_Direction_288_448_Gallery2_CutOut_RandomErase_FlipTest_0.001lr_8batchsize_20190802_200847/iter07_model.ckpt')['net_state_dict']) + net = tiger_cnn1(classes=107) + net.load_state_dict(torch.load('./model/tiger_cnn1/model.ckpt')['net_state_dict']) net = net.cuda() # val diff --git a/train.py b/train.py old mode 100644 new mode 100755 index 3e411d5..0b8cb69 --- a/train.py +++ b/train.py @@ -20,7 +20,7 @@ init_environment() os.environ['CUDA_VISIBLE_DEVICES'] = '0' multi_gpus = False -model_name = 'tiger_cnn1' +model_name = 'tiger_cnn7' def main(): @@ -95,13 +95,6 @@ def main(): ignore_params += list(map(id, net.fuse_fc7.parameters())) base_params = filter(lambda p: id(p) not in ignore_params, net.parameters()) extra_params = filter(lambda p: id(p) in ignore_params, net.parameters()) - elif model_name == 'tiger_cnn5': - net = tiger_cnn5(classes=107) - ignore_params = list(map(id, net.cls.parameters())) - ignore_params += list(map(id, net.cls_direction.parameters())) - ignore_params += list(map(id, net.fc7.parameters())) - base_params = filter(lambda p: id(p) not in ignore_params, net.parameters()) - extra_params = filter(lambda p: id(p) in ignore_params, net.parameters()) elif model_name == 'tiger_cnn6': net = tiger_cnn6(classes=107) ignore_params = list(map(id, net.cls.parameters())) @@ -116,6 +109,9 @@ def main(): ignore_params += list(map(id, net.fc7.parameters())) base_params = filter(lambda p: id(p) not in ignore_params, net.parameters()) extra_params = filter(lambda p: id(p) in ignore_params, net.parameters()) + else: + _print('error!!!!!! please input correct model name.') + return optimizer = optim.SGD( [{'params': base_params, 'lr': 0.001},