forked from basiclab/GNGAN-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
53 lines (40 loc) · 1.42 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
import os
import random
from contextlib import contextmanager
import torch
import numpy as np
from torchvision.utils import save_image
from tqdm import tqdm
device = torch.device('cuda:0')
def save_images(images, output_dir, verbose=False):
os.makedirs(output_dir, exist_ok=True)
for i, image in enumerate(tqdm(images, dynamic_ncols=True, leave=False,
disable=(not verbose), desc="save_images")):
save_image(image, os.path.join(output_dir, '%d.png' % i))
def infiniteloop(dataloader):
while True:
for x, y in iter(dataloader):
yield x, y
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
# torch.backends.cudnn.deterministic = True
# torch.backends.cudnn.benchmark = False
def ema(source, target, decay):
source_dict = source.state_dict()
target_dict = target.state_dict()
for key in source_dict.keys():
target_dict[key].data.copy_(
target_dict[key].data * decay +
source_dict[key].data * (1 - decay))
@contextmanager
def module_no_grad(m: torch.nn.Module):
requires_grad_dict = dict()
for name, param in m.named_parameters():
requires_grad_dict[name] = param.requires_grad
param.requires_grad_(False)
yield m
for name, param in m.named_parameters():
param.requires_grad_(requires_grad_dict[name])