22
22
import yaml
23
23
from shutil import copyfile
24
24
from circle_loss import CircleLoss , convert_label_to_similarity
25
+ from instance_loss import InstanceLoss
25
26
26
27
version = torch .__version__
27
28
#fp16
61
62
parser .add_argument ('--circle' , action = 'store_true' , help = 'use Circle loss' )
62
63
parser .add_argument ('--cosface' , action = 'store_true' , help = 'use CosFace loss' )
63
64
parser .add_argument ('--contrast' , action = 'store_true' , help = 'use contrast loss' )
65
+ parser .add_argument ('--instance' , action = 'store_true' , help = 'use instance loss' )
64
66
parser .add_argument ('--triplet' , action = 'store_true' , help = 'use triplet loss' )
65
67
parser .add_argument ('--lifted' , action = 'store_true' , help = 'use lifted loss' )
66
68
parser .add_argument ('--sphere' , action = 'store_true' , help = 'use sphere loss' )
@@ -213,6 +215,8 @@ def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
213
215
criterion_lifted = losses .GeneralizedLiftedStructureLoss (neg_margin = 1 , pos_margin = 0 )
214
216
if opt .contrast :
215
217
criterion_contrast = losses .ContrastiveLoss (pos_margin = 0 , neg_margin = 1 )
218
+ if opt .instance :
219
+ criterion_instance = InstanceLoss ()
216
220
if opt .sphere :
217
221
criterion_sphere = losses .SphereFaceLoss (num_classes = opt .nclasses , embedding_size = 512 , margin = 4 )
218
222
for epoch in range (num_epochs ):
@@ -258,7 +262,7 @@ def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
258
262
259
263
sm = nn .Softmax (dim = 1 )
260
264
log_sm = nn .LogSoftmax (dim = 1 )
261
- return_feature = opt .arcface or opt .cosface or opt .circle or opt .triplet or opt .contrast or opt .lifted or opt .sphere
265
+ return_feature = opt .arcface or opt .cosface or opt .circle or opt .triplet or opt .contrast or opt .instance or opt . lifted or opt .sphere
262
266
if return_feature :
263
267
logits , ff = outputs
264
268
fnorm = torch .norm (ff , p = 2 , dim = 1 , keepdim = True )
@@ -278,6 +282,8 @@ def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
278
282
loss += criterion_lifted (ff , labels ) #/now_batch_size
279
283
if opt .contrast :
280
284
loss += criterion_contrast (ff , labels ) #/now_batch_size
285
+ if opt .instance :
286
+ loss += criterion_instance (ff , labels )
281
287
if opt .sphere :
282
288
loss += criterion_sphere (ff , labels )/ now_batch_size
283
289
elif opt .PCB : # PCB
@@ -421,7 +427,7 @@ def save_network(network, epoch_label):
421
427
# Load a pretrainied model and reset final fully connected layer.
422
428
#
423
429
424
- return_feature = opt .arcface or opt .cosface or opt .circle or opt .triplet or opt .contrast or opt .lifted or opt .sphere
430
+ return_feature = opt .arcface or opt .cosface or opt .circle or opt .triplet or opt .contrast or opt .instance or opt . lifted or opt .sphere
425
431
426
432
if opt .use_dense :
427
433
model = ft_net_dense (len (class_names ), opt .droprate , circle = return_feature )
0 commit comments