-
Notifications
You must be signed in to change notification settings - Fork 0
/
gan.py
45 lines (28 loc) · 1008 Bytes
/
gan.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
import matplotlib.pyplot as plt
import numpy as np
import torch
import torchvision.transforms as transforms
from PIL import Image
import os
def transfer(img_path, style, imsize = 256):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = torch.load('./my_models/' + style + '.pth').to(device)
img = image_loader(img_path, imsize, device)
for p in model.parameters():
p.requires_grad = False
return model(img)
def image_loader(image_name, imsize, device):
loader = transforms.Compose([
transforms.Resize(imsize),
transforms.CenterCrop(imsize),
transforms.ToTensor()])
image = Image.open(image_name)
image = loader(image).unsqueeze(0)
return image.to(device, torch.float)
def draw_img(img):
plt.imshow(np.rollaxis(img.add(1).div(2).cpu().detach()[0].numpy(), 0, 3))
plt.show()
if __name__ == '__main__':
# for test
# draw_img(transfer('images/day.jpg', 'vangogh'))
pass