-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathumt_train.py
766 lines (692 loc) · 29.5 KB
/
umt_train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
# coding:utf-8
# --------------------------------------------------------
# Pytorch multi-GPU Faster R-CNN
# Licensed under The MIT License [see LICENSE for details]
# Written by Jiasen Lu, Jianwei Yang, based on code from Ross Girshick
# --------------------------------------------------------
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import numpy as np
import pprint
import pdb
import time
import _init_paths
import torch
from torch.autograd import Variable
import torch.nn as nn
from model.utils.config import cfg, cfg_from_file, cfg_from_list
from model.utils.net_utils import (
adjust_learning_rate,
save_checkpoint,
get_dataloader,
setup_seed,
)
from model.ema.optim_weight_ema import WeightEMA
from model.utils.parser_func import parse_args, set_dataset_args
from model.rpn.bbox_transform import clip_boxes
from model.nms.nms_wrapper import nms
from model.rpn.bbox_transform import bbox_transform_inv
from prettytimer import PrettyTimer
def get_cfg():
args = parse_args()
print("Called with args:")
print(args)
args = set_dataset_args(args)
if args.cfg_file is not None:
cfg_from_file(args.cfg_file)
if args.set_cfgs is not None:
cfg_from_list(args.set_cfgs)
print("Using config:")
pprint.pprint(cfg)
# np.random.seed(cfg.RNG_SEED)
setup_seed(cfg.RNG_SEED)
return args
if __name__ == "__main__":
args = get_cfg()
output_dir = f"{args.save_dir}/{args.net}/{args.dataset}"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
if args.dataset_t == "water":
args.aug = False
if args.dataset_t == "foggy_cityscape":
# initilize the network here.
from model.umt_faster_rcnn_truncate.umt_vgg16 import vgg16
from model.umt_faster_rcnn_truncate.umt_resnet import resnet
else:
from model.umt_faster_rcnn.umt_vgg16 import vgg16
from model.umt_faster_rcnn.umt_resnet import resnet
student_save_name = os.path.join(
output_dir,
"conf_{}_conf_gamma_{}_source_like_{}_aug_{}_target_like_{}_pe_{}_pl_{}_thresh_{}"
"_lambda_{}_student_target_{}".format(
args.conf,
args.conf_gamma,
args.source_like,
args.aug,
args.target_like,
args.pretrained_epoch,
args.pl,
args.threshold,
args.lam,
args.dataset_t,
),
)
print("Model will be saved to: ")
print(student_save_name)
# torch.backends.cudnn.benchmark = True
if torch.cuda.is_available() and not args.cuda:
print("WARNING: You have a CUDA device, so you should probably run with --cuda")
# train set
# -- Note: Use validation set and disable the flipped to enable faster loading.
cfg.TRAIN.USE_FLIPPED = True
cfg.USE_GPU_NMS = args.cuda
# source train set
s_imdb, s_train_size, s_dataloader = get_dataloader(args.imdb_name, args)
# source-like/fake-source train set data loader
if args.source_like:
s_fake_imdb, s_fake_train_size, s_fake_dataloader = get_dataloader(
args.imdb_name_fake_source, args, sequential=True, augment=args.aug
)
else:
s_fake_imdb, s_fake_train_size, s_fake_dataloader = get_dataloader(
args.imdb_name_target, args, sequential=True, augment=args.aug
)
# target train set
t_imdb, t_train_size, t_dataloader = get_dataloader(
args.imdb_name_target, args, sequential=True, augment=args.aug
)
# target-like/fake-target train set
t_fake_imdb, t_fake_train_size, t_fake_dataloader = get_dataloader(
args.imdb_name_fake_target, args
)
print("{:d} source roidb entries".format(s_train_size))
print("{:d} source like roidb entries".format(s_fake_train_size))
print("{:d} target roidb entries".format(t_train_size))
print("{:d} target like roidb entries".format(t_fake_train_size))
# initilize the tensor holder here.
im_data = torch.FloatTensor(1)
im_info = torch.FloatTensor(1)
num_boxes = torch.LongTensor(1)
gt_boxes = torch.FloatTensor(1)
# ship to cuda
if args.cuda:
im_data = im_data.cuda()
im_info = im_info.cuda()
num_boxes = num_boxes.cuda()
gt_boxes = gt_boxes.cuda()
# make variable
im_data = Variable(im_data)
im_info = Variable(im_info)
num_boxes = Variable(num_boxes)
gt_boxes = Variable(gt_boxes)
if args.cuda:
cfg.CUDA = True
imdb = s_imdb
if args.net == "vgg16":
student_fasterRCNN = vgg16(
imdb.classes,
pretrained=True,
class_agnostic=args.class_agnostic,
conf=args.conf,
)
teacher_fasterRCNN = vgg16(
imdb.classes,
pretrained=True,
class_agnostic=args.class_agnostic,
conf=args.conf,
)
elif args.net == "res101":
student_fasterRCNN = resnet(
imdb.classes,
101,
pretrained=True,
class_agnostic=args.class_agnostic,
conf=args.conf,
)
teacher_fasterRCNN = resnet(
imdb.classes,
101,
pretrained=True,
class_agnostic=args.class_agnostic,
conf=args.conf,
)
elif args.net == "res50":
student_fasterRCNN = resnet(
imdb.classes, 50, pretrained=True, class_agnostic=args.class_agnostic
)
teacher_fasterRCNN = resnet(
imdb.classes, 50, pretrained=True, class_agnostic=args.class_agnostic
)
else:
print("network is not defined")
pdb.set_trace()
student_fasterRCNN.create_architecture()
teacher_fasterRCNN.create_architecture()
lr = cfg.TRAIN.LEARNING_RATE
lr = args.lr
student_detection_params = []
params = []
for key, value in dict(student_fasterRCNN.named_parameters()).items():
if value.requires_grad:
if "bias" in key:
params += [
{
"params": [value],
"lr": lr * (cfg.TRAIN.DOUBLE_BIAS + 1),
"weight_decay": cfg.TRAIN.BIAS_DECAY
and cfg.TRAIN.WEIGHT_DECAY
or 0,
}
]
else:
params += [
{
"params": [value],
"lr": lr,
"weight_decay": cfg.TRAIN.WEIGHT_DECAY,
}
]
student_detection_params += [value]
teacher_detection_params = []
for key, value in dict(teacher_fasterRCNN.named_parameters()).items():
if value.requires_grad:
teacher_detection_params += [value]
value.requires_grad = False
if args.optimizer == "adam":
lr = lr * 0.1
student_optimizer = torch.optim.Adam(params)
elif args.optimizer == "sgd":
student_optimizer = torch.optim.SGD(params, momentum=cfg.TRAIN.MOMENTUM)
teacher_optimizer = WeightEMA(
teacher_detection_params, student_detection_params, alpha=args.teacher_alpha
)
if args.cuda:
student_fasterRCNN.cuda()
teacher_fasterRCNN.cuda()
if args.resume:
student_checkpoint = torch.load(args.student_load_name)
args.session = student_checkpoint["session"]
args.start_epoch = student_checkpoint["epoch"]
student_fasterRCNN.load_state_dict(student_checkpoint["model"])
student_optimizer.load_state_dict(student_checkpoint["optimizer"])
lr = student_optimizer.param_groups[0]["lr"]
if "pooling_mode" in student_checkpoint.keys():
cfg.POOLING_MODE = student_checkpoint["pooling_mode"]
print("loaded checkpoint %s" % (args.student_load_name))
teacher_checkpoint = torch.load(args.teacher_load_name)
teacher_fasterRCNN.load_state_dict(teacher_checkpoint["model"])
if "pooling_mode" in teacher_checkpoint.keys():
cfg.POOLING_MODE = teacher_checkpoint["pooling_mode"]
print("loaded checkpoint %s" % (args.teacher_load_name))
if args.mGPUs:
student_fasterRCNN = nn.DataParallel(student_fasterRCNN)
teacher_fasterRCNN = nn.DataParallel(teacher_fasterRCNN)
iters_per_epoch = int(10000 / args.batch_size)
if args.use_tfboard:
from tensorboardX import SummaryWriter
logger = SummaryWriter("logs")
count_iter = 0
conf_gamma = args.conf_gamma
pretrained_epoch = args.pretrained_epoch
timer = PrettyTimer()
for epoch in range(args.start_epoch, args.max_epochs + 1):
# setting to train mode
student_fasterRCNN.train()
teacher_fasterRCNN.train()
loss_temp = 0
start = time.time()
epoch_start = time.time()
if epoch % (args.lr_decay_step + 1) == 0:
adjust_learning_rate(student_optimizer, args.lr_decay_gamma)
lr *= args.lr_decay_gamma
data_iter_s = iter(s_dataloader)
data_iter_t = iter(t_dataloader)
data_iter_s_fake = iter(s_fake_dataloader)
data_iter_t_fake = iter(t_fake_dataloader)
for step in range(1, iters_per_epoch + 1):
timer.start("iter")
try:
data_s = next(data_iter_s)
except:
data_iter_s = iter(s_dataloader)
data_s = next(data_iter_s)
try:
data_s_fake = next(data_iter_s_fake)
except:
data_iter_s_fake = iter(s_fake_dataloader)
data_s_fake = next(data_iter_s_fake)
try:
data_t = next(data_iter_t)
except:
data_iter_t = iter(t_dataloader)
data_t = next(data_iter_t)
assert (
data_s_fake[0].size() == data_t[0].size()
), "The size should be same between source fake and target"
assert (
data_s_fake[1] == data_t[1]
).all(), "The image info should be same between source fake and target"
try:
data_t_fake = next(data_iter_t_fake)
except:
data_iter_t_fake = iter(t_fake_dataloader)
data_t_fake = next(data_iter_t_fake)
# eta = 1.0
count_iter += 1
# put source data into variable
im_data.data.resize_(data_s[0].size()).copy_(data_s[0])
im_info.data.resize_(data_s[1].size()).copy_(data_s[1])
gt_boxes.data.resize_(data_s[2].size()).copy_(data_s[2])
num_boxes.data.resize_(data_s[3].size()).copy_(data_s[3])
student_fasterRCNN.zero_grad()
(
rois,
cls_prob,
bbox_pred,
rpn_loss_cls,
rpn_loss_box,
RCNN_loss_cls,
RCNN_loss_bbox,
rois_label,
out_d_pixel,
out_d,
confidence_loss,
_,
) = student_fasterRCNN(im_data, im_info, gt_boxes, num_boxes, hints=True)
loss = (
rpn_loss_cls.mean()
+ rpn_loss_box.mean()
+ RCNN_loss_cls.mean()
+ RCNN_loss_bbox.mean()
)
if args.conf:
conf_loss = confidence_loss.mean()
if args.target_like:
# put fake target data into variable
im_data.data.resize_(data_t_fake[0].size()).copy_(data_t_fake[0])
im_info.data.resize_(data_t_fake[1].size()).copy_(data_t_fake[1])
# gt is empty
gt_boxes.data.resize_(data_t_fake[2].size()).copy_(data_t_fake[2])
num_boxes.data.resize_(data_t_fake[3].size()).copy_(data_t_fake[3])
(
rois,
cls_prob,
bbox_pred,
rpn_loss_cls_t_fake,
rpn_loss_box_t_fake,
RCNN_loss_cls_t_fake,
RCNN_loss_bbox_t_fake,
rois_label_t_fake,
out_d_pixel,
out_d,
_,
_,
) = student_fasterRCNN(
im_data, im_info, gt_boxes, num_boxes, hints=False
) # --------------------------------
loss += (
rpn_loss_cls_t_fake.mean()
+ rpn_loss_box_t_fake.mean()
+ RCNN_loss_cls_t_fake.mean()
+ RCNN_loss_bbox_t_fake.mean()
)
if epoch > pretrained_epoch and args.pl:
teacher_fasterRCNN.eval()
im_data.data.resize_(data_s_fake[0].size()).copy_(data_s_fake[0])
im_info.data.resize_(data_s_fake[1].size()).copy_(data_s_fake[1])
# gt is emqpty
gt_boxes.data.resize_(1, 1, 5).zero_()
num_boxes.data.resize_(1).zero_()
(
rois,
cls_prob,
bbox_pred,
rpn_loss_cls_,
rpn_loss_box_,
RCNN_loss_cls_,
RCNN_loss_bbox_,
rois_label_,
d_pred_,
_,
_,
confidence_s_fake,
) = teacher_fasterRCNN(im_data, im_info, gt_boxes, num_boxes, test=True)
scores = cls_prob.data
boxes = rois.data[:, :, 1:5]
if cfg.TEST.BBOX_REG:
# Apply bounding-box regression deltas
box_deltas = bbox_pred.data
if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:
# Optionally normalize targets by a precomputed mean and stdev
if args.class_agnostic:
box_deltas = (
box_deltas.view(-1, 4)
* torch.FloatTensor(
cfg.TRAIN.BBOX_NORMALIZE_STDS
).cuda()
+ torch.FloatTensor(
cfg.TRAIN.BBOX_NORMALIZE_MEANS
).cuda()
)
box_deltas = box_deltas.view(1, -1, 4)
else:
box_deltas = (
box_deltas.view(-1, 4)
* torch.FloatTensor(
cfg.TRAIN.BBOX_NORMALIZE_STDS
).cuda()
+ torch.FloatTensor(
cfg.TRAIN.BBOX_NORMALIZE_MEANS
).cuda()
)
box_deltas = box_deltas.view(1, -1, 4 * len(imdb.classes))
pred_boxes = bbox_transform_inv(boxes, box_deltas, 1)
pred_boxes = clip_boxes(pred_boxes, im_info.data, 1)
else:
# Simply repeat the boxes, once for each class
pred_boxes = np.tile(boxes, (1, scores.shape[1]))
scores = scores.squeeze()
if args.conf:
scores = torch.sqrt(
scores * confidence_s_fake
) # using confidence score to adjust scores
pred_boxes = pred_boxes.squeeze()
gt_boxes_target = []
pre_thresh = 0.0
thresh = args.threshold
empty_array = np.transpose(np.array([[], [], [], [], []]), (1, 0))
for j in range(1, len(imdb.classes)):
inds = torch.nonzero(scores[:, j] > pre_thresh).view(-1)
# if there is det
if inds.numel() > 0:
cls_scores = scores[:, j][inds]
_, order = torch.sort(cls_scores, 0, True)
if args.class_agnostic:
cls_boxes = pred_boxes[inds, :]
else:
cls_boxes = pred_boxes[inds][:, j * 4 : (j + 1) * 4]
cls_dets = torch.cat((cls_boxes, cls_scores.unsqueeze(1)), 1)
# cls_dets = torch.cat((cls_boxes, cls_scores), 1)
cls_dets = cls_dets[order]
keep = nms(cls_dets, cfg.TEST.NMS)
cls_dets = cls_dets[keep.view(-1).long()]
# all_boxes[j][i] = cls_dets.cpu().numpy()
cls_dets_numpy = cls_dets.cpu().numpy()
for i in range(np.minimum(10, cls_dets_numpy.shape[0])):
bbox = tuple(
int(np.round(x)) for x in cls_dets_numpy[i, :4]
)
score = cls_dets_numpy[i, -1]
if score > thresh:
gt_boxes_target.append(list(bbox[0:4]) + [j])
gt_boxes_padding = torch.FloatTensor(cfg.MAX_NUM_GT_BOXES, 5).zero_()
if len(gt_boxes_target) != 0:
gt_boxes_numpy = torch.FloatTensor(gt_boxes_target)
num_boxes_cpu = torch.LongTensor(
[min(gt_boxes_numpy.size(0), cfg.MAX_NUM_GT_BOXES)]
)
gt_boxes_padding[:num_boxes_cpu, :] = gt_boxes_numpy[:num_boxes_cpu]
else:
num_boxes_cpu = torch.LongTensor([0])
# teacher_fasterRCNN.train()
# put source-like data into variable
im_data.data.resize_(data_t[0].size()).copy_(data_t[0])
im_info.data.resize_(data_t[1].size()).copy_(data_t[1])
gt_boxes_padding = torch.unsqueeze(gt_boxes_padding, 0)
gt_boxes.data.resize_(gt_boxes_padding.size()).copy_(gt_boxes_padding)
num_boxes.data.resize_(num_boxes_cpu.size()).copy_(num_boxes_cpu)
(
rois,
cls_prob,
bbox_pred,
rpn_loss_cls_s_fake,
rpn_loss_box_s_fake,
RCNN_loss_cls_s_fake,
RCNN_loss_bbox_s_fake,
rois_label_s_fake,
out_d_pixel,
out_d,
_,
_,
) = student_fasterRCNN(im_data, im_info, gt_boxes, num_boxes)
loss += args.lam * (
rpn_loss_cls_s_fake.mean()
+ rpn_loss_box_s_fake.mean()
+ RCNN_loss_cls_s_fake.mean()
+ RCNN_loss_bbox_s_fake.mean()
)
if args.conf:
loss += conf_gamma * conf_loss
loss_temp += loss.item()
student_optimizer.zero_grad()
loss.backward()
student_optimizer.step()
teacher_fasterRCNN.zero_grad()
teacher_optimizer.step()
timer.end("iter")
estimate_time = timer.eta(
"iter", count_iter, args.max_epochs * iters_per_epoch
)
if step % args.disp_interval == 0:
end = time.time()
if step > 0:
loss_temp /= args.disp_interval
if args.mGPUs:
loss_rpn_cls = rpn_loss_cls.mean().item()
loss_rpn_box = rpn_loss_box.mean().item()
loss_rcnn_cls = RCNN_loss_cls.mean().item()
loss_rcnn_box = RCNN_loss_bbox.mean().item()
fg_cnt = torch.sum(rois_label.data.ne(0))
bg_cnt = rois_label.data.numel() - fg_cnt
if args.pl and epoch > pretrained_epoch:
loss_rpn_cls_s_fake = rpn_loss_cls_s_fake.mean().item()
loss_rpn_box_s_fake = rpn_loss_box_s_fake.mean().item()
loss_rcnn_cls_s_fake = RCNN_loss_cls_s_fake.mean().item()
loss_rcnn_box_s_fake = RCNN_loss_bbox_s_fake.mean().item()
fg_cnt_s_fake = torch.sum(rois_label_s_fake.data.ne(0))
bg_cnt_s_fake = rois_label_s_fake.data.numel() - fg_cnt_s_fake
if args.target_like:
loss_rpn_cls_t_fake = rpn_loss_cls_t_fake.mean().item()
loss_rpn_box_t_fake = rpn_loss_box_t_fake.mean().item()
loss_rcnn_cls_t_fake = RCNN_loss_cls_t_fake.mean().item()
loss_rcnn_box_t_fake = RCNN_loss_bbox_t_fake.mean().item()
fg_cnt_t_fake = torch.sum(rois_label_t_fake.data.ne(0))
bg_cnt_t_fake = rois_label_t_fake.data.numel() - fg_cnt_t_fake
# dloss_s_fake = dloss_s_fake.mean().item()
# dloss_t_fake = dloss_t_fake.mean().item()
# dloss_s_p_fake = dloss_s_p_fake.mean().item()
# dloss_t_p_fake = dloss_t_p_fake.mean().item()
else:
loss_rpn_cls = rpn_loss_cls.item()
loss_rpn_box = rpn_loss_box.item()
loss_rcnn_cls = RCNN_loss_cls.item()
loss_rcnn_box = RCNN_loss_bbox.item()
fg_cnt = torch.sum(rois_label.data.ne(0))
bg_cnt = rois_label.data.numel() - fg_cnt
if args.conf:
loss_conf = conf_loss.item()
if args.pl and epoch > pretrained_epoch:
loss_rpn_cls_s_fake = rpn_loss_cls_s_fake.item()
loss_rpn_box_s_fake = rpn_loss_box_s_fake.item()
loss_rcnn_cls_s_fake = RCNN_loss_cls_s_fake.item()
loss_rcnn_box_s_fake = RCNN_loss_bbox_s_fake.item()
fg_cnt_s_fake = torch.sum(rois_label_s_fake.data.ne(0))
bg_cnt_s_fake = rois_label_s_fake.data.numel() - fg_cnt
if args.target_like:
loss_rpn_cls_t_fake = rpn_loss_cls_t_fake.item()
loss_rpn_box_t_fake = rpn_loss_box_t_fake.item()
loss_rcnn_cls_t_fake = RCNN_loss_cls_t_fake.item()
loss_rcnn_box_t_fake = RCNN_loss_bbox_t_fake.item()
fg_cnt_t_fake = torch.sum(rois_label_t_fake.data.ne(0))
bg_cnt_t_fake = rois_label_t_fake.data.numel() - fg_cnt_t_fake
print(
"[session %d][epoch %2d][iter %4d/%4d] lr: %.2e, loss: %.4f, eta: %s"
% (
args.session,
epoch,
step,
iters_per_epoch,
lr,
loss_temp,
estimate_time,
)
)
print(
"\t\t\tfg/bg=(%d/%d), time cost: %f" % (fg_cnt, bg_cnt, end - start)
)
print(
"\t\t\trpn_cls: %.4f, rpn_box: %.4f, rcnn_cls: %.4f, rcnn_box %.4f"
% (loss_rpn_cls, loss_rpn_box, loss_rcnn_cls, loss_rcnn_box)
)
if args.pl and epoch > pretrained_epoch:
print("\t\t\tfg/bg=(%d/%d)" % (fg_cnt_s_fake, bg_cnt_s_fake))
print(
"\t\t\trpn_cls_s_fake: %.4f, rpn_box_s_fake: %.4f, rcnn_cls_s_fake: %.4f, rcnn_box_s_fake %.4f"
% (
loss_rpn_cls_s_fake,
loss_rpn_box_s_fake,
loss_rcnn_cls_s_fake,
loss_rcnn_box_s_fake,
)
)
if args.target_like:
print("\t\t\tfg/bg=(%d/%d)" % (fg_cnt_t_fake, bg_cnt_t_fake))
print(
"\t\t\trpn_cls_t_fake: %.4f, rpn_box_t_fake: %.4f, rcnn_cls_t_fake: %.4f, rcnn_box_t_fake %.4f"
% (
loss_rpn_cls_t_fake,
loss_rpn_box_t_fake,
loss_rcnn_cls_t_fake,
loss_rcnn_box_t_fake,
)
)
if args.conf is True:
print(f"\t\t\tconf loss: {loss_conf:.4}")
if args.use_tfboard:
info = {
"loss": loss_temp,
"loss_rpn_cls": loss_rpn_cls,
"loss_rpn_box": loss_rpn_box,
"loss_rcnn_cls": loss_rcnn_cls,
"loss_rcnn_box": loss_rcnn_box,
"loss_rpn_cls_s_fake": loss_rpn_cls_s_fake,
"loss_rpn_box_s_fake": loss_rpn_box_s_fake,
"loss_rcnn_cls_s_fake": loss_rcnn_cls_s_fake,
"loss_rcnn_box_s_fake": loss_rcnn_box_s_fake,
"loss_rpn_cls_t_fake": loss_rpn_cls_t_fake
if args.target_like is True
else 0,
"loss_rpn_box_t_fake": loss_rpn_box_t_fake
if args.target_like is True
else 0,
"loss_rcnn_cls_t_fake": loss_rcnn_cls_t_fake
if args.target_like is True
else 0,
"loss_rcnn_box_t_fake": loss_rcnn_box_t_fake
if args.target_like is True
else 0,
"loss_conf": loss_conf if args.conf is True else 0,
"conf_gamma": conf_gamma,
}
logger.add_scalars(
"logs_s_{}/losses".format(args.session),
info,
(epoch - 1) * iters_per_epoch + step,
)
loss_temp = 0
start = time.time()
student_save_name = os.path.join(
output_dir,
"conf_{}_conf_gamma_{}_source_like_{}_aug_{}_target_like_{}_pe_{}_pl_{}_"
"thresh_{}_lambda_{}_lam2_{}_student_target_{}_session_{}_epoch_{}_step_{}.pth".format(
args.conf,
args.conf_gamma,
args.source_like,
args.aug,
args.target_like,
args.pretrained_epoch,
args.pl,
args.threshold,
args.lam,
args.lam2,
args.dataset_t,
args.session,
epoch,
step,
),
)
save_checkpoint(
{
"session": args.session,
"epoch": epoch + 1,
"model": student_fasterRCNN.mumt_train.pyodule.state_dict()
if args.mGPUs
else student_fasterRCNN.state_dict(),
"optimizer": student_optimizer.state_dict(),
"pooling_mode": cfg.POOLING_MODE,
"class_agnostic": args.class_agnostic,
},
student_save_name,
)
print("save student model: {}".format(student_save_name))
teacher_save_name = os.path.join(
output_dir,
"conf_{}_conf_gamma_{}_source_like_{}_aug_{}_target_like_{}_pe_{}_pl_{}_"
"thresh_{}_lambda_{}_lam2_{}_teacher_target_{}_session_{}_epoch_{}_step_{}.pth".format(
args.conf,
args.conf_gamma,
args.source_like,
args.aug,
args.target_like,
args.pretrained_epoch,
args.pl,
args.threshold,
args.lam,
args.lam2,
args.dataset_t,
args.session,
epoch,
step,
),
)
save_checkpoint(
{
"session": args.session,
"epoch": epoch + 1,
"model": teacher_fasterRCNN.mumt_train.pyodule.state_dict()
if args.mGPUs
else teacher_fasterRCNN.state_dict(),
"pooling_mode": cfg.POOLING_MODE,
"class_agnostic": args.class_agnostic,
},
teacher_save_name,
)
print("save teacher model: {}".format(teacher_save_name))
epoch_end = time.time()
print("epoch cost time: {} min".format((epoch_end - epoch_start) / 60.0))
# cmd = (
# f"python test_net_global_local.py --dataset {args.dataset_t} --net {args.net}"
# f" --load_name {student_save_name}"
# )
# print("cmd: ", cmd)
# cmd = [i.strip() for i in cmd.split(" ") if len(i.strip()) > 0]
# try:
# proc = subprocess.Popen(cmd)
# proc.wait()
# except (KeyboardInterrupt, SystemExit):
# pass
# cmd = (
# f"python test_net_global_local.py --dataset {args.dataset_t} --net {args.net}"
# f" --load_name {teacher_save_name}"
# )
# print("cmd: ", cmd)
# cmd = [i.strip() for i in cmd.split(" ") if len(i.strip()) > 0]
# try:
# proc = subprocess.Popen(cmd)
# proc.wait()
# except (KeyboardInterrupt, SystemExit):
# pass
if args.use_tfboard:
logger.close()