Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
liuning-scu-cn committed Aug 3, 2019
1 parent ccd1575 commit 757d528
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 31 deletions.
Empty file modified core/__init__.py
100644 → 100755
Empty file.
Empty file modified core/config.py
100644 → 100755
Empty file.
14 changes: 8 additions & 6 deletions core/loss.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions core/model.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Empty file modified core/re_ranking.py
100644 → 100755
Empty file.
Empty file modified core/schedule.py
100644 → 100755
Empty file.
Empty file modified core/seed.py
100644 → 100755
Empty file.
Empty file modified core/step_lr.py
100644 → 100755
Empty file.
Empty file modified core/utils.py
100644 → 100755
Empty file.
Empty file modified dataload/__init__.py
100644 → 100755
Empty file.
Empty file modified dataload/constant.py
100644 → 100755
Empty file.
Empty file modified dataload/dataloader.py
100644 → 100755
Empty file.
Empty file modified dataload/mydataset.py
100644 → 100755
Empty file.
13 changes: 7 additions & 6 deletions finetune_tiger_cnn5_triplet.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -39,16 +39,17 @@ 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,
probe_paths=probe_paths,
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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -231,4 +233,3 @@ def main():
main()



13 changes: 7 additions & 6 deletions finetune_tiger_cnn8_triplet.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)
Expand All @@ -39,16 +39,17 @@ 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,
probe_paths=probe_paths,
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
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions test.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions train.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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()))
Expand All @@ -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},
Expand Down

0 comments on commit 757d528

Please sign in to comment.