-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtool.py
59 lines (47 loc) · 1.44 KB
/
tool.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
# -*- coding: utf-8 -*-
# --------------------------------------------------
#
# tool.py
#
# Written by aliabbasi -*- [email protected]
# Written by cetinsamet -*- [email protected]
# May, 2020
# --------------------------------------------------
from collections import OrderedDict
import numpy as np
import random
import torch
def set_seed(seed):
""" Seed all random number generators """
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # for multiGPUs.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def set_device():
"""set GPU/CPU device"""
if torch.cuda.is_available():
return torch.device('cuda') # CUDA is available!
else:
return torch.device('cpu') # CUDA is NOT available
def denormalize(x):
"""denormalize image
CelebA images are mapped into [-1, 1] interval before training
This function applies denormalization
Arguments:
x -- image
"""
x = x * .5 + .5
return x
def from_parallel(path, device=torch.device('cpu')):
# original saved file with DataParallel
state_dict = torch.load(path, map_location=device)
# create new OrderedDict that does not contain `module.`
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
return new_state_dict