-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
71 lines (59 loc) · 1.77 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
import os
import argparse
import torch
import torchvision
import torchvision.transforms as T
from PIL import Image
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader
def update_ema_params(
target: torch.nn.Module, source: torch.nn.Module, decay_rate=0.995
):
targParams = target.parameters()
srcParams = source.parameters()
for targParam, srcParam in zip(targParams, srcParams):
targParam.data.mul_(decay_rate).add_(
srcParam.data, alpha=1 - decay_rate
)
def plot_images(images: torch.Tensor):
plt.figure(figsize=(32, 32))
plt.imshow(
torch.cat(
[
torch.cat([i for i in images.cpu()], dim=-1),
],
dim=-2,
)
.permute(1, 2, 0)
.cpu()
)
plt.show()
def save_images(images: torch.Tensor, path: str, **kwargs):
grid = torchvision.utils.make_grid(images, **kwargs)
ndarr = grid.permute(1, 2, 0).cpu().numpy()
im = Image.fromarray(ndarr)
im.save(path)
def get_data(args: argparse.Namespace):
NUM_WORKER = 4
transforms = T.Compose(
[
T.Resize(int(args.size * 1.25)),
T.RandomResizedCrop(args.size, scale=(0.8, 1.0)),
T.ToTensor(),
T.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]
)
dataset = torchvision.datasets.ImageFolder(
args.root_dir, transform=transforms
)
return DataLoader(
dataset,
batch_size=args.batch_size,
shuffle=True,
num_workers=NUM_WORKER,
)
def setup_logging(run: str = "run"):
os.makedirs("models", exist_ok=True)
os.makedirs("results", exist_ok=True)
os.makedirs(os.path.join("models", run), exist_ok=True)
os.makedirs(os.path.join("results", run), exist_ok=True)