-
Notifications
You must be signed in to change notification settings - Fork 107
/
custom_transforms.py
56 lines (34 loc) · 1.21 KB
/
custom_transforms.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
import numpy as np
from torchvision import transforms
from scipy import ndimage
import torch
def to_image_space(x):
return ((np.clip(x, -1, 1) + 1) / 2 * 255).astype(np.uint8)
def to_rgb(x):
return x if x.mode == 'RGB' else x.convert('RGB')
def to_l(x):
return x if x.mode == 'L' else x.convert('L')
def blur_mask(tensor):
np_tensor = tensor.numpy()
smoothed = ndimage.gaussian_filter(np_tensor, sigma=20)
return torch.FloatTensor(smoothed)
def build_transform(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5), mask=False):
#if type(image_size) != tuple:
#image_size = (image_size, image_size)
t = [#transforms.Resize((image_size[0], image_size[1])),
to_rgb,
transforms.ToTensor(),
transforms.Normalize(mean, std)]
if mask:
t.append(blur_mask)
return transforms.Compose(t)
def build_mask_transform(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)):
t = [#transforms.Resize((image_size, image_size)),
to_l,
transforms.ToTensor()]
return transforms.Compose(t)
def to_pil(tensor):
t = transforms.ToPILImage()
return t(tensor)
def tensor_mb(tensor):
return (tensor.element_size() * tensor.nelement()) / 1024 / 1024