-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathutils.py
141 lines (105 loc) · 3.1 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
138
139
140
141
import os
import pandas as pd
import pickle
import json
import random
import numpy as np
import torch
import torch.nn as nn
try:
import moxing as mox
open = mox.file.File
except Exception:
pass
def setup_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def read_file(path):
da_df = pd.read_csv(
path, index_col=False, header=None
)
return da_df
def save_data(da_df, path):
da_df.to_csv(path)
print("File saved in {}.".format(path))
def load_pickle(fpath):
with open(fpath, "rb") as fr:
data = pickle.load(fr)
return data
def save_pickle(data, fpath):
with open(fpath, "wb") as fw:
pickle.dump(data, fw)
return data
def load_json(fpath):
with open(fpath, "r") as fr:
data = json.load(fr)
return data
def save_json(data, fpath):
with open(fpath, "w") as fr:
data = json.dump(data, fr)
return data
def append_to_logs(fpath, logs):
with open(fpath, "a", encoding="utf-8") as fa:
for log in logs:
fa.write("{}\n".format(log))
fa.write("\n")
def format_logs(logs):
def formal_str(x):
if isinstance(x, int):
return str(x)
elif isinstance(x, float):
return "{:.5f}".format(x)
else:
return str(x)
logs_str = []
for key, elems in logs.items():
log_str = "[{}]: ".format(key)
log_str += " ".join([formal_str(e) for e in elems])
logs_str.append(log_str)
return logs_str
def listfiles(fdir):
for root, dirs, files in os.walk(fdir):
print(root, dirs, files)
def set_gpu(x):
os.environ['CUDA_VISIBLE_DEVICES'] = x
print('using gpu:', x)
class Averager():
def __init__(self):
self.n = 0
self.v = 0
def add(self, x):
self.v = (self.v * self.n + x) / (self.n + 1)
self.n += 1
def item(self):
return self.v
def count_acc(logits, label):
pred = torch.argmax(logits, dim=1)
if torch.cuda.is_available():
return (pred == label).type(torch.cuda.FloatTensor).mean().item()
else:
return (pred == label).type(torch.FloatTensor).mean().item()
def prediction_mask(logits, label):
pred = torch.argmax(logits, dim=1)
mask = (pred == label).float()
return mask
def weights_init(m):
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(
m.weight, mode='fan_out', nonlinearity='relu'
)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
try:
nn.init.constant_(m.bias, 0)
except Exception:
pass