-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.py
156 lines (116 loc) · 4.13 KB
/
util.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
import json
import os
import shutil
import sys
def mkdir_if_not_exist(dirname):
if not os.path.exists(dirname):
os.makedirs(dirname)
def yes_no_input():
while True:
choice = raw_input("Please respond with 'yes' or 'no' [y/N]: ").lower()
if choice in ['y', 'ye', 'yes']:
return True
elif choice in ['n', 'no']:
return False
def check_if_done(filename):
if os.path.exists(filename):
print ("%s already exists. Is it O.K. to overwrite it and start this program?" % filename)
if not yes_no_input():
raise Exception("Please restart training after you set args.savename differently!")
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
import torch
torch.save(state, filename)
if is_best:
shutil.copyfile(filename, 'model_best.pth.tar')
def exec_eval(tgt_dataset, label_outdir):
print ("-" * 10 + " Evaluation using this command " + "-" * 10)
eval_str = "python eval.py %s %s" % (tgt_dataset, label_outdir)
print (eval_str)
import subprocess
subprocess.call(eval_str, shell=True)
def calc_entropy(output):
import torch
import torch.nn.functional as F
output = F.softmax(output)
return -torch.mean(output * torch.log(output + 1e-6))
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
def save_dic_to_json(dic, fn, verbose=True):
dic = {str(k): v for k, v in dic.items()}
with open(fn, "w") as f:
json_str = json.dumps(dic, sort_keys=True, indent=4)
if verbose:
print (json_str)
f.write(json_str)
print ("param file '%s' was saved!" % fn)
def emphasize_str(string):
print ('#' * 100)
print (string)
print ('#' * 100)
def adjust_learning_rate(optimizer, lr_init, decay_rate, epoch, num_epochs):
"""Decay Learning rate at 1/2 and 3/4 of the num_epochs"""
lr = lr_init
if epoch >= num_epochs * 0.75:
lr *= decay_rate ** 2
elif epoch >= num_epochs * 0.5:
lr *= decay_rate
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr
def get_class_weight_from_file(n_class, weight_filename=None, add_bg_loss=False):
import torch
weight = torch.ones(n_class)
if weight_filename:
import pandas as pd
loss_df = pd.read_csv(weight_filename)
loss_df.sort_values("class_id", inplace=True)
weight *= torch.FloatTensor(loss_df.weight.values)
if not add_bg_loss:
weight[n_class - 1] = 0 # Ignore background loss
return weight
def save_colorized_lbl(idxed_lbl, outfn, dataset="city"):
"""
Colorize and Save label data (1ch img contains 0~N_class+ 255(Void))
:param idxed_lbl:
:param outfn:
:param dataset:
:return:
"""
import numpy as np
# Save visualized predicted pixel labels(pngs)
if dataset in ["city16", "synthia"]:
info_json_fn = "./dataset/synthia2cityscapes_info.json"
elif dataset == "2d3d":
info_json_fn = "./dataset/2d3d_info.json"
elif dataset in ["nyu", "suncg"]:
info_json_fn = "./dataset/nyu_info.json"
else:
info_json_fn = "./dataset/city_info.json"
# Save visualized predicted pixel labels(pngs)
with open(info_json_fn) as f:
city_info_dic = json.load(f)
palette = np.array(city_info_dic['palette'], dtype=np.uint8)
idxed_lbl.putpalette(palette.flatten())
idxed_lbl.save(outfn)
def set_debugger_org():
if not sys.excepthook == sys.__excepthook__:
from IPython.core import ultratb
sys.excepthook = ultratb.FormattedTB(call_pdb=True)
def set_debugger_org_frc():
from IPython.core import ultratb
sys.excepthook = ultratb.FormattedTB(call_pdb=True)
def set_trace():
from IPython.core.debugger import Pdb
Pdb(color_scheme='Linux').set_trace(sys._getframe().f_back)