forked from hehaodele/ProbGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
45 lines (31 loc) · 1.06 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
from torch_head import *
from common_head import *
# ======================================================================================================================
def to_tensor(x):
return torch.tensor(x).to(torch.float).cuda()
def to_np(x):
if isinstance(x, torch.Tensor):
return x.detach().cpu().numpy()
return x
# ======================================================================================================================
class Tee(object):
def __init__(self, name, mode):
self.file = open(name, mode)
self.stdout = sys.stdout
sys.stdout = self
def __del__(self):
sys.stdout = self.stdout
self.file.close()
def write(self, data):
self.file.write(data)
self.stdout.write(data)
def flush(self):
self.file.flush()
def close(self):
self.__del__()
def pickle_save(filename, obj):
import pickle
pickle.dump(obj, open(filename, 'wb'), protocol=4)
def pickle_load(filename):
import pickle
return pickle.load(open(filename, 'rb'))