-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_utils.py
82 lines (61 loc) · 1.84 KB
/
data_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
72
73
74
75
76
77
78
79
80
81
82
"""
Code adopted from pix2pixHD:
https://github.com/NVIDIA/pix2pixHD/blob/master/data/image_folder.py
"""
import os
import torch
import numpy as np
import torch.nn.functional as F
from torchvision import transforms
from PIL import Image
IMG_EXTENSIONS = [
'.jpg', '.JPG', '.jpeg', '.JPEG', '.png', '.PNG', '.ppm', '.PPM', '.bmp',
'.BMP', '.tiff'
]
def is_image_file(filename):
return any(filename.endswith(extension) for extension in IMG_EXTENSIONS)
def make_dataset(dir):
images = []
assert os.path.isdir(dir), '%s is not a valid directory' % dir
for root, _, fnames in sorted(os.walk(dir)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
return images
# Log images
def log_input_image(x, opts):
return tensor2im(x)
# Clip image to range(0,1)
def clip_img(x):
img_tmp = x.clone()[0]
img_tmp = (img_tmp + 1) / 2
img_tmp = torch.clamp(img_tmp, 0, 1)
return [img_tmp.detach().cpu()]
def clip(var):
var = ((var + 1) / 2)
var[var < 0] = 0
var[var > 1] = 1
var = var * 255
return var
def transpose_clip_image(var):
var = var.cpu().detach().transpose(0, 2).transpose(0, 1).numpy()
return clip(var)
def tensor2im(var):
# var shape: (3, H, W)
img = transpose_clip_image(var)
return Image.fromarray(img.astype('uint8'))
def img_to_tensor(x):
out = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
])(x)
return out
def downscale(x, scale_times=1):
for i in range(scale_times):
x = F.interpolate(x, scale_factor=0.5, mode='bilinear')
return x
def upscale(x, scale_times=1):
for i in range(scale_times):
x = F.interpolate(x, scale_factor=2, mode='bilinear')
return x