-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_AuxAff.py
executable file
·139 lines (96 loc) · 5.41 KB
/
train_AuxAff.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
from torch.backends import cudnn
cudnn.enabled = True
from tool import pyutils, torchutils
import argparse
import importlib
import tool.exutils as exutils
import torch.nn.functional as F
from pathlib import Path
import torch
import os
import numpy as np
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--gpu_ids', type=str, default='0', help='GPU_id')
parser.add_argument("--list_path", default="voc12/train_aug_id.txt", type=str)
parser.add_argument("--img_path", default="", type=str)
parser.add_argument("--save_path", default=None, type=str)
parser.add_argument("--sal_pgt_path", default=None, type=str)
parser.add_argument("--seg_pgt_path", default=None, type=str)
parser.add_argument("--batch_size", default=4, type=int)
parser.add_argument("--num_classes", default=21, type=int)
parser.add_argument("--num_epochs", default=15, type=int)
parser.add_argument("--network", default='AuxSegNet', type=str)
parser.add_argument("--lr", default=0.0007, type=float)
parser.add_argument("--num_workers", default=16, type=int)
parser.add_argument("--wt_dec", default=1e-5, type=float)
parser.add_argument("--init_weights", default='', type=str)
parser.add_argument("--session_name", default="AuxSegNet_", type=str)
parser.add_argument("--crop_size", default=321, type=int)
parser.add_argument('--print_intervals', type=int, default=50)
parser.add_argument('--sal_loss_weight', type=float, default=1.0)
parser.add_argument('--cls_loss_weight', type=float, default=1.0)
parser.add_argument('--seg_loss_weight', type=float, default=1.0)
args = parser.parse_args()
gpu_id = args.gpu_ids
os.environ["CUDA_VISIBLE_DEVICES"] = gpu_id
save_path = args.save_path
Path(args.save_path).mkdir(parents=True, exist_ok=True)
pyutils.Logger(os.path.join(args.save_path, args.session_name + '.log'))
criterion = torch.nn.CrossEntropyLoss(weight=None, ignore_index=255, reduction='elementwise_mean').cuda()
model = getattr(importlib.import_module('network.' + args.network), 'SegNet')(num_classes=args.num_classes)
weights_dict = torch.load(args.init_weights)
model.load_state_dict(weights_dict, strict=False)
img_list = exutils.read_file(args.list_path)
train_size = len(img_list)
max_step = (train_size // args.batch_size) * args.num_epochs
data_list = []
for i in range(200):
np.random.shuffle(img_list)
data_list.extend(img_list)
optimizer = torchutils.PolyOptimizer_cls([
{'params': model.get_1x_lr_params(), 'lr': args.lr},
{'params': model.get_10x_lr_params(), 'lr': 10 * args.lr}
], lr=args.lr, weight_decay=args.wt_dec, max_step=max_step)
model = torch.nn.DataParallel(model).cuda()
model.train()
avg_meter = pyutils.AverageMeter('loss')
timer = pyutils.Timer("Session started: ")
data_gen = exutils.chunker(data_list, args.batch_size)
for iter in range(max_step + 1):
chunk = data_gen.__next__()
img_list = chunk
images, ori_images, sal_images, seg_labels, label, img_names = exutils.get_data_from_chunk(chunk, args)
b, _, w, h = ori_images.shape
label = label.cuda(non_blocking=True)
seg_labels = seg_labels.long().cuda()
sal_images = sal_images.cuda()
images = images.cuda()
x_cls, init_sal, refined_sal, init_seg, refined_seg = model(x=images)
##################################### image classification ###########################################
cls_loss = F.multilabel_soft_margin_loss(x_cls, label)
##################################### saliency detection ###########################################
init_sal = F.interpolate(init_sal, size=(w, h), mode='bilinear', align_corners=False)
sal_labels = sal_images.unsqueeze(1)
init_sal_loss = F.binary_cross_entropy_with_logits(init_sal, sal_labels)
refined_sal = F.interpolate(refined_sal, size=(w, h), mode='bilinear', align_corners=False)
refined_sal_loss = F.binary_cross_entropy_with_logits(refined_sal, sal_labels)
##################################### segmentation ###########################################
init_seg = F.interpolate(init_seg, size=(w, h), mode='bilinear', align_corners=False)
init_seg_loss = criterion(init_seg, seg_labels)
refined_seg = F.interpolate(refined_seg, size=(w, h), mode='bilinear', align_corners=False)
refined_seg_loss = criterion(refined_seg, seg_labels)
loss = args.cls_loss_weight * cls_loss + args.sal_loss_weight * init_sal_loss + \
args.sal_loss_weight * refined_sal_loss + args.seg_loss_weight * init_seg_loss + args.seg_loss_weight * refined_seg_loss
avg_meter.add({'loss': loss.item()})
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (optimizer.global_step - 1) % args.print_intervals == 0:
timer.update_progress(optimizer.global_step / max_step)
print('Iter:%5d/%5d' % (optimizer.global_step - 1, max_step),
'Loss:%.4f' % (avg_meter.pop('loss')),
'imps:%.1f' % ((iter + 1) * args.batch_size / timer.get_stage_elapsed()),
'Fin:%s' % (timer.str_est_finish()),
'lr: %.5f' % (optimizer.param_groups[0]['lr']), flush=True)
torch.save(model.module.state_dict(), os.path.join(save_path, args.session_name + 'final.pth'))