forked from fanq15/GCoNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·207 lines (171 loc) · 6.75 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
206
207
import torch
import torch.nn as nn
import torch.optim as optim
from util import Logger, AverageMeter, save_checkpoint, save_tensor_img, set_seed
import os
import numpy as np
from matplotlib import pyplot as plt
import time
import argparse
from tqdm import tqdm
from dataset import get_loader
from criterion import Eval
import torchvision.utils as vutils
import torch.nn.functional as F
import pytorch_toolbelt.losses as PTL
# Parameter from command line
parser = argparse.ArgumentParser(description='')
parser.add_argument('--model',
default='CoSalNet',
type=str,
help="Options: '', ''")
parser.add_argument('--loss',
default='DSLoss_IoU',
type=str,
help="Options: '', ''")
parser.add_argument('--bs', '--batch_size', default=16, type=int)
parser.add_argument('--lr',
'--learning_rate',
default=1e-4,
type=float,
help='Initial learning rate')
parser.add_argument('--resume',
default=None,
type=str,
help='path to latest checkpoint')
parser.add_argument('--epochs', default=50, type=int)
parser.add_argument('--start_epoch',
default=0,
type=int,
help='manual epoch number (useful on restarts)')
parser.add_argument('--trainset',
default='Jigsaw2_DUTS',
type=str,
help="Options: 'Jigsaw2_DUTS', 'DUTS_class'")
parser.add_argument('--size',
default=224,
type=int,
help='input size')
parser.add_argument('--tmp', default=None, help='Temporary folder')
args = parser.parse_args()
train_img_path = './data/images/DUTS_class/'
train_gt_path = './data/gts/DUTS_class/'
# make dir for tmp
os.makedirs(args.tmp, exist_ok=True)
# Init log file
logger = Logger(os.path.join(args.tmp, "log.txt"))
set_seed(1996)
# Init model
device = torch.device("cuda")
exec('from models import ' + args.model)
model = eval(args.model+'()')
model = model.to(device)
backbone_params = list(map(id, model.ginet.backbone.parameters()))
base_params = filter(lambda p: id(p) not in backbone_params,
model.ginet.parameters())
all_params = [{'params': base_params}, {'params': model.ginet.backbone.parameters(), 'lr': args.lr * 0.01}]
# Setting optimizer
optimizer = optim.Adam(params=all_params, lr=args.lr, betas=[0.9, 0.99])
scheduler = torch.optim.lr_scheduler.StepLR(optimizer,step_size=50,gamma = 0.1)
for key, value in model.named_parameters():
if 'ginet.backbone' in key and 'ginet.backbone.conv5.conv5_3' not in key:
value.requires_grad = False
for key, value in model.named_parameters():
print(key, value.requires_grad)
# log model and optimizer pars
logger.info("Model details:")
logger.info(model)
logger.info("Optimizer details:")
logger.info(optimizer)
logger.info("Scheduler details:")
logger.info(scheduler)
logger.info("Other hyperparameters:")
logger.info(args)
# Setting Loss
exec('from loss import ' + args.loss)
dsloss = eval(args.loss+'()')
def main():
val_mae_record = []
val_fm_record = []
# Optionally resume from a checkpoint
if args.resume:
if os.path.isfile(args.resume):
logger.info("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
model.ginet.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
scheduler.load_state_dict(checkpoint['scheduler'])
logger.info("=> loaded checkpoint '{}' (epoch {})".format(
args.resume, checkpoint['epoch']))
else:
logger.info("=> no checkpoint found at '{}'".format(args.resume))
print(args.epochs)
for epoch in range(args.start_epoch, args.epochs):
train_loader = get_loader(train_img_path,
train_gt_path,
args.size,
1, #args.bs,
max_num=args.bs, #16, #20,
istrain=True,
shuffle=False,
num_workers=8, #4,
epoch=epoch,
pin=True)
train_loss = train(epoch, train_loader)
# Save checkpoint
save_checkpoint(
{
'epoch': epoch + 1,
'state_dict': model.ginet.state_dict(),
'scheduler': scheduler.state_dict(),
},
path=args.tmp)
ginet_dict = model.ginet.state_dict()
torch.save(ginet_dict, os.path.join(args.tmp, 'final_gconet.pth'))
def train(epoch, train_loader):
loss_log = AverageMeter()
# Switch to train mode
model.train()
model.set_mode('train')
#CE = torch.nn.BCEWithLogitsLoss()
FL = PTL.BinaryFocalLoss()
for batch_idx, batch in enumerate(train_loader):
inputs = batch[0].to(device).squeeze(0)
gts = batch[1].to(device).squeeze(0)
cls_gts = torch.LongTensor(batch[-1]).to(device)
#print(cls_gts[0], cls_gts[-1])
gts_neg = torch.full_like(gts, 0.0)
gts_cat = torch.cat([gts, gts_neg], dim=0)
#print(cls_gts, gts.shape)
scaled_preds, pred_cls, pred_x5 = model(inputs)
loss_sal = dsloss(scaled_preds, gts)
loss_cls = F.cross_entropy(pred_cls, cls_gts) * 3.0
loss_x5 = FL(pred_x5, gts_cat) * 250.0
loss = loss_sal + loss_cls + loss_x5
loss_log.update(loss, inputs.size(0))
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch_idx % 20 == 0:
# NOTE: Top2Down; [0] is the grobal slamap and [5] is the final output
logger.info('Epoch[{0}/{1}] Iter[{2}/{3}] '
'Train Loss: loss_sal: {4:.3f}, loss_cls: {5:.3f}, loss_x5: {6:.3f} '
'Loss_total: {loss.val:.3f} ({loss.avg:.3f}) '.format(
epoch,
args.epochs,
batch_idx,
len(train_loader),
loss_sal,
loss_cls,
loss_x5,
loss=loss_log,
))
scheduler.step()
logger.info('@==Final== Epoch[{0}/{1}] '
'Train Loss: {loss.avg:.3f} '.format(epoch,
args.epochs,
loss=loss_log))
return loss_log.avg
if __name__ == '__main__':
main()