-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrain.py
206 lines (165 loc) · 7.77 KB
/
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
import os
import shutil
import json
import time
from apex import amp
import apex
import copy
import numpy as np
import torch.distributed as dist
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
from torch.optim.lr_scheduler import LambdaLR, StepLR
from torch.utils.data import DataLoader
from toolbox import MscCrossEntropyLoss
from toolbox import get_dataset
from toolbox import get_logger
from toolbox import get_model
from toolbox import averageMeter, runningScore
from toolbox import ClassWeight, save_ckpt
from toolbox import Ranger
from toolbox import setup_seed
from toolbox import load_ckpt
from toolbox import group_weight_decay
from toolbox import CrossEntropyLoss2d, CrossEntropyLoss2dLabelSmooth, ProbOhemCrossEntropy2d, FocalLoss2d, \
LovaszSoftmax, LDAMLoss
setup_seed(33)
class eeemodelLoss(nn.Module):
def __init__(self, class_weight=None, ignore_index=-100, reduction='mean'):
super(eeemodelLoss, self).__init__()
self.class_weight_semantic = torch.from_numpy(np.array(
[1.5105, 16.6591, 29.4238, 34.6315, 40.0845, 41.4357, 47.9794, 45.3725, 44.9000])).float()
self.class_weight_binary = torch.from_numpy(np.array([1.5121, 10.2388])).float()
self.class_weight_boundary = torch.from_numpy(np.array([1.4459, 23.7228])).float()
self.class_weight = class_weight
self.LovaszSoftmax = LovaszSoftmax()
self.cross_entropy = nn.CrossEntropyLoss()
self.semantic_loss = nn.CrossEntropyLoss(weight=self.class_weight_semantic)
# self.semantic_loss = nn.CrossEntropyLoss()
self.binary_loss = nn.CrossEntropyLoss(weight=self.class_weight_binary)
self.boundary_loss = nn.CrossEntropyLoss(weight=self.class_weight_boundary)
def forward(self, inputs, targets):
# semantic_out, binary_out, boundary_out = inputs
semantic_gt, binary_gt, boundary_gt = targets
semantic_out, binary_out, boundary_out = inputs
loss1 = self.semantic_loss(semantic_out, semantic_gt)
loss2 = self.binary_loss(binary_out, binary_gt)
loss3 = self.boundary_loss(boundary_out, boundary_gt)
loss = loss1 + loss2 + loss3
return loss
def run(args):
torch.cuda.set_device(args.cuda)
with open(args.config, 'r') as fp:
cfg = json.load(fp)
logdir = f'/mnt/Data1/shaohuadong/model/vggmask/{time.strftime("%Y-%m-%d-%H-%M")}({cfg["dataset"]}-{cfg["model_name"]})'
if not os.path.exists(logdir):
os.makedirs(logdir)
shutil.copy(args.config, logdir)
logger = get_logger(logdir)
logger.info(f'Conf | use logdir {logdir}')
# model
model = get_model(cfg)
device = torch.device(f'cuda:{args.cuda}')
model.to(device)
# dataloader
trainset, _, testset = get_dataset(cfg)
train_loader = DataLoader(trainset, batch_size=cfg['ims_per_gpu'], shuffle=True, num_workers=cfg['num_workers'],
pin_memory=True)
# val_loader = DataLoader(valset, batch_size=cfg['ims_per_gpu'], shuffle=False, num_workers=cfg['num_workers'],
# pin_memory=True)
test_loader = DataLoader(testset, batch_size=cfg['ims_per_gpu'], shuffle=False, num_workers=cfg['num_workers'],
pin_memory=True)
params_list = model.parameters()
optimizer = Ranger(params_list, lr=cfg['lr_start'], weight_decay=cfg['weight_decay'])
scheduler = LambdaLR(optimizer, lr_lambda=lambda ep: (1 - ep / cfg['epochs']) ** 0.9)
# criterion = LovaszSoftmax().to(device)
train_criterion = eeemodelLoss().to(device)
criterion = nn.CrossEntropyLoss().to(device)
# 指标 包含unlabel
train_loss_meter = averageMeter()
test_loss_meter = averageMeter()
running_metrics_test = runningScore(cfg['n_classes'], ignore_index=cfg['id_unlabel'])
best_test = 0
amp.register_float_function(torch, 'sigmoid')
model, optimizer = amp.initialize(model, optimizer, opt_level=args.opt_level)
# 每个epoch迭代循环
for ep in range(cfg['epochs']):
# training
model.train()
train_loss_meter.reset()
for i, sample in enumerate(train_loader):
optimizer.zero_grad() # 梯度清零
################### train edit #######################
if cfg['inputs'] == 'rgb':
image = sample['image'].to(device)
label = sample['label'].to(device)
bound = sample['bound'].to(device)
edge = sample['edge'].to(device)
binary_label = sample['binary_label'].to(device)
targets = [label, binary_label, bound]
predict = model(image)
else:
image = sample['image'].to(device)
depth = sample['depth'].to(device)
label = sample['label'].to(device)
bound = sample['bound'].to(device)
edge = sample['edge'].to(device)
binary_label = sample['binary_label'].to(device)
targets = [label, binary_label, bound]
predict = model(image, depth, edge)
# predict = model(image, depth)
loss = train_criterion(predict, targets)
####################################################
with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
optimizer.step()
train_loss_meter.update(loss.item())
scheduler.step(ep)
# test
with torch.no_grad():
model.eval()
running_metrics_test.reset()
test_loss_meter.reset()
for i, sample in enumerate(test_loader):
if cfg['inputs'] == 'rgb':
image = sample['image'].to(device)
label = sample['label'].to(device)
edge = sample['edge'].to(device)
predict = model(image)
else:
image = sample['image'].to(device)
depth = sample['depth'].to(device)
label = sample['label'].to(device)
edge = sample['edge'].to(device)
# predict = model(image, depth)[0]
predict = model(image, depth)
# predict = model(image, depth, edge)[0]
# predict = model(image, depth)
loss = criterion(predict, label)
test_loss_meter.update(loss.item())
predict = predict.max(1)[1].cpu().numpy() # [1, h, w]
label = label.cpu().numpy()
running_metrics_test.update(label, predict)
train_loss = train_loss_meter.avg
test_loss = test_loss_meter.avg
test_macc = running_metrics_test.get_scores()[0]["class_acc: "]
test_miou = running_metrics_test.get_scores()[0]["mIou: "]
test_avg = (test_macc + test_miou) / 2
logger.info(
f'Iter | [{ep + 1:3d}/{cfg["epochs"]}] loss={train_loss:.3f}/{test_loss:.3f}, mPA={test_macc:.3f}, miou={test_miou:.3f}, avg={test_avg:.3f}')
if test_avg > best_test:
best_test = test_avg
save_ckpt(logdir, model)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description="config")
parser.add_argument("--config", type=str, default="configs/EGFNet.json", help="Configuration file to use")
parser.add_argument("--opt_level", type=str, default='O1')
parser.add_argument("--inputs", type=str.lower, default='rgb', choices=['rgb', 'rgbd'])
parser.add_argument("--resume", type=str, default='',
help="use this file to load last checkpoint for continuing training")
parser.add_argument("--cuda", type=int, default=0, help="set cuda device id")
args = parser.parse_args()
run(args)