forked from sxhxliang/BigGAN-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
executable file
·35 lines (27 loc) · 889 Bytes
/
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
import os
import torch
from torch.autograd import Variable
from torch.nn import init
def make_folder(path, version):
if not os.path.exists(os.path.join(path, version)):
os.makedirs(os.path.join(path, version))
def tensor2var(x, grad=False):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x, requires_grad=grad)
def var2tensor(x):
return x.data.cpu()
def var2numpy(x):
return x.data.cpu().numpy()
def denorm(x):
out = (x + 1) / 2
return out.clamp_(0, 1)
def weights_init(m):
classname = m.__class__.__name__
# print(classname)
if classname.find('Conv2d') != -1:
init.xavier_normal_(m.weight.data)
init.constant_(m.bias.data, 0.0)
elif classname.find('Linear') != -1:
init.xavier_normal_(m.weight.data)
init.constant_(m.bias.data, 0.0)