-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
137 lines (103 loc) · 3.85 KB
/
utils.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
import torch
import numpy as np
from IOUEval import SegmentationMetric
import logging
import logging.config
from tqdm import tqdm
import os
import torch.nn as nn
from const import *
LOGGING_NAME="custom"
def set_logging(name=LOGGING_NAME, verbose=True):
# sets up logging for the given name
rank = int(os.getenv('RANK', -1)) # rank in world for Multi-GPU trainings
level = logging.INFO if verbose and rank in {-1, 0} else logging.ERROR
logging.config.dictConfig({
'version': 1,
'disable_existing_loggers': False,
'formatters': {
name: {
'format': '%(message)s'}},
'handlers': {
name: {
'class': 'logging.StreamHandler',
'formatter': name,
'level': level,}},
'loggers': {
name: {
'level': level,
'handlers': [name],
'propagate': False,}}})
set_logging(LOGGING_NAME) # run before defining LOGGER
LOGGER = logging.getLogger(LOGGING_NAME) # define globally (used in train.py, val.py, detect.py, etc.)
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count if self.count != 0 else 0
def poly_lr_scheduler(args, optimizer, epoch, power=2):
lr = round(args.lr * (1 - epoch / args.max_epochs) ** power, 8)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr
def train(args, train_loader, model, criterion, optimizer, epoch):
model.train()
total_batches = len(train_loader)
pbar = enumerate(train_loader)
LOGGER.info(('\n' + '%13s' * 4) % ('Epoch','TverskyLoss','FocalLoss' ,'TotalLoss'))
pbar = tqdm(pbar, total=total_batches, bar_format='{l_bar}{bar:10}{r_bar}')
for i, (_,input, target) in pbar:
input = input.cuda().float() / 255.0
output = model(input)
optimizer.zero_grad()
focal_loss,tversky_loss,loss = criterion(output,target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
pbar.set_description(('%13s' * 1 + '%13.4g' * 3) %
(f'{epoch}/{args.max_epochs - 1}', tversky_loss, focal_loss, loss.item()))
@torch.no_grad()
def val(val_loader, model):
model.eval()
DA=SegmentationMetric(2)
da_acc_seg = AverageMeter()
da_IoU_seg = AverageMeter()
da_mIoU_seg = AverageMeter()
total_batches = len(val_loader)
total_batches = len(val_loader)
pbar = enumerate(val_loader)
pbar = tqdm(pbar, total=total_batches)
for i, (_,input, target) in pbar:
input = input.cuda().float() / 255.0
# target = target.cuda()
input_var = input
target_var = target
with torch.no_grad():
output = model(input_var)
out_da=output
target_da=target
_,da_predict=torch.max(out_da, 1)
_,da_gt=torch.max(target_da, 1)
DA.reset()
DA.addBatch(da_predict.cpu(), da_gt.cpu())
da_acc = DA.pixelAccuracy()
da_IoU = DA.IntersectionOverUnion()
da_mIoU = DA.meanIntersectionOverUnion()
da_acc_seg.update(da_acc,input.size(0))
da_IoU_seg.update(da_IoU,input.size(0))
da_mIoU_seg.update(da_mIoU,input.size(0))
da_segment_result = (da_acc_seg.avg,da_IoU_seg.avg,da_mIoU_seg.avg)
return da_segment_result
def save_checkpoint(state, filenameCheckpoint='checkpoint.pth.tar'):
torch.save(state, filenameCheckpoint)
def netParams(model):
return np.sum([np.prod(parameter.size()) for parameter in model.parameters()])