-
Notifications
You must be signed in to change notification settings - Fork 18
/
train_OFNet_edge.py
168 lines (142 loc) · 6.5 KB
/
train_OFNet_edge.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
import os
from tqdm import tqdm
from utils.lr_scheduler import LR_Scheduler
from dataloaders.datasets.bsds_hd5_dim1 import Mydataset
from torch.utils.data import DataLoader
from my_options.ofnet_options import OFNet_Options
from modeling.ofnet_edge import *
from modeling.sync_batchnorm.replicate import patch_replication_callback
from utils.edge_loss2 import AttentionLossSingleMap
from utils.saver import Saver
from utils.summaries import TensorboardSummary
import scipy.io as sio
import torch
class Trainer(object):
def __init__(self, args):
self.args = args
# Define Saver
self.saver = Saver(args)
self.saver.save_experiment_config()
# Define Tensorboard Summary
self.summary = TensorboardSummary(self.saver.experiment_dir)
self.writer = self.summary.create_summary()
print(self.saver.experiment_dir)
self.output_dir = os.path.join(self.saver.experiment_dir)
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
# Define Dataloader
self.train_dataset = Mydataset(root_path=self.args.data_path, split='trainval', crop_size=self.args.crop_size)
self.test_dataset = Mydataset(root_path=self.args.data_path, split='test', crop_size=self.args.crop_size)
self.train_loader = DataLoader(self.train_dataset, batch_size=self.args.batch_size, shuffle=True,
num_workers=args.workers, pin_memory=True, drop_last=True)
self.test_loader = DataLoader(self.test_dataset, batch_size=1, shuffle=False,
num_workers=args.workers)
# Define network
self.model = OFNet()
if self.args.resnet:
self.model.load_resnet(args.resnet)
# Define Criterion
self.criterion = AttentionLossSingleMap()
# Define Optimizer
self.optimizer = torch.optim.SGD(self.model.parameters(), lr=self.args.lr, momentum=self.args.momentum,
weight_decay=self.args.weight_decay)
# Define lr scheduler
self.scheduler = LR_Scheduler(self.args.lr_scheduler, self.args.lr,
args.epochs, len(self.train_loader))
# Using cuda
if self.args.cuda:
self.model = torch.nn.DataParallel(self.model, device_ids=self.args.gpu_ids)
patch_replication_callback(self.model)
self.model = self.model.cuda()
# Resuming checkpoint
self.best_pred = 0.0
if args.resume is not None:
if not os.path.isfile(args.resume):
raise RuntimeError("=> no checkpoint found at '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
if args.cuda:
self.model.module.load_state_dict(checkpoint['state_dict'])
else:
self.model.load_state_dict(checkpoint['state_dict'])
if not args.ft:
self.optimizer.load_state_dict(checkpoint['optimizer'])
self.best_pred = checkpoint['best_pred']
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.resume, checkpoint['epoch']))
def training(self, epoch):
train_loss = 0.0
self.model.train()
tbar = tqdm(self.train_loader)
num_img_tr = len(self.train_loader)
for i, (image, target) in enumerate(tbar):
if self.args.cuda:
image, target = image.cuda(), target.cuda() #(b,3,w,h) (b,1,w,h)
target = target.unsqueeze(1)
output = self.model(image)
loss = self.criterion(output, target)
self.scheduler(self.optimizer, i, epoch, self.best_pred)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
train_loss += loss.item()
tbar.set_description('Train loss: %.3f' % (train_loss / (i + 1)))
self.writer.add_scalar('train/total_loss_iter', loss.item(), i + num_img_tr * epoch)
self.writer.add_scalar('train/total_loss_epoch', train_loss, epoch)
print('[Epoch: %d, numImages: %5d]' % (epoch, i * self.args.batch_size + image.data.shape[0]))
print('Loss: %.3f' % train_loss)
if self.args.no_val:
# save checkpoint every epoch
if (epoch + 1) % 10 == 0:
is_best = False
self.saver.save_checkpoint({
'epoch': epoch + 1,
'state_dict': self.model.module.state_dict(),
'optimizer': self.optimizer.state_dict(),
'best_pred': self.best_pred,
}, is_best)
def test(self, epoch):
print('Test epoch: %d' % epoch)
self.output_dir = os.path.join(self.saver.experiment_dir, str(epoch+1), 'mat')
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
self.model.eval()
tbar = tqdm(self.test_loader, desc='\r')
for i, image in enumerate(tbar):
name = self.test_loader.dataset.images_name[i]
if self.args.cuda:
image = image.cuda()
with torch.no_grad():
output = self.model(image)
pred = output.data.cpu().numpy()
pred = pred.squeeze()
sio.savemat(os.path.join(self.output_dir, '{}.mat'.format(name)), {'result': pred})
def main():
options = OFNet_Options()
args = options.parse()
args.cuda = not args.no_cuda and torch.cuda.is_available()
if args.cuda:
try:
args.gpu_ids = [int(s) for s in args.gpu_ids.split(',')]
except ValueError:
raise ValueError('Argument --gpu_ids must be a comma-separated list of integers only')
if args.sync_bn is None:
if args.cuda and len(args.gpu_ids) > 1:
args.sync_bn = True
else:
args.sync_bn = False
args.data_path = 'data/BSDS-RIND/BSDS-RIND-Edge/Augmentation/'
print(args)
torch.manual_seed(args.seed)
trainer = Trainer(args)
print('Starting Epoch:', trainer.args.start_epoch)
print('Total Epoches:', trainer.args.epochs)
for epoch in range(trainer.args.start_epoch, trainer.args.epochs):
#trainer.test(epoch)
trainer.training(epoch)
if (epoch+1)%10==0:
trainer.test(epoch)
# if not trainer.args.no_val and epoch % args.eval_interval == (args.eval_interval - 1):
# trainer.validation(epoch)
if __name__ == "__main__":
main()