-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
37 lines (32 loc) · 977 Bytes
/
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
"""
* FileName: utils.py
* Author: Slatter
* Date: 2023/5/5 17:00
* Description:
"""
import numpy as np
import torch
from torchvision import transforms
def convert_image_to_natural(image):
"""
scale image pixel values to [0, 1]
"""
transform = transforms.Compose([
transforms.Lambda(lambda t: (t + 1) / 2), # scale to [0, 1]
])
image = transform(torch.clamp(image, -1, 1)) # scale to [-1, 1] then scale to [0, 1]
return image
def tensor_image_to_PIL(image):
"""
show tensor images
:param image: (c, h, w)
:return: (h, w, c)
"""
transform = transforms.Compose([
transforms.Lambda(lambda t: (t + 1) / 2),
transforms.Lambda(lambda t: t.permute(1, 2, 0)), # (c, h, w) to (h, w, c)
transforms.Lambda(lambda t: t * 255.),
transforms.Lambda(lambda t: t.numpy().astype(np.uint8)),
transforms.ToPILImage(),
])
return transform(torch.clamp(image, -1, 1))