-
Notifications
You must be signed in to change notification settings - Fork 1
/
random_sample_from_single.py
35 lines (30 loc) · 1.08 KB
/
random_sample_from_single.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 torch
import torch.nn as nn
import numpy as np
from PIL import Image
from utils import get_training_imgs
def random_sample_from_single(Gs, sigmas, h, w, scale=4/3, device="cuda:0"):
prev = torch.zeros(1, 3, h, w).to(device)
for i in range(len(Gs)):
G = Gs[i]
sigma = sigmas[i]
z = torch.randn(1, 3, h, w).to(device) * sigma
prev = G(z, prev)
if i == len(Gs) - 1:
break
h, w = int(h * scale), int(w * scale)
upsample = nn.Upsample((h, w))
prev = upsample(prev)
return prev
if __name__ == "__main__":
img_path = "./star.jpg"
model_path = "./star.pth"
h, w = 50, 25
imgs = get_training_imgs(img_path)
checkpoint = torch.load(model_path)
device = "cuda:0" if torch.cuda.is_available() else "cpu"
sigmas = checkpoint["sigmas"]
Gs = checkpoint["Gs"]
gen = random_sample_from_single(Gs, sigmas, h, w, device=device).cpu().detach().numpy()[0]
gen = np.transpose(gen, axes=[1, 2, 0])
Image.fromarray(np.uint8((gen+1)*127.5)).show()