-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
32 lines (28 loc) · 920 Bytes
/
Copy pathutils.py
File metadata and controls
32 lines (28 loc) · 920 Bytes
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
import numpy as np
import torch
import random
from PIL import Image
def enable_deterministic_execution():
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.allow_tf32 = False
if torch.cuda.is_available():
torch.backends.cuda.matmul.allow_tf32 = False
torch.use_deterministic_algorithms(True)
def seed_everything(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def denormalize(image):
image = (image / 2 + 0.5).clamp(0, 1)
image = image.cpu().permute(0, 2, 3, 1).numpy()
return (image * 255).round().astype("uint8")
def save_image(image, filename):
"""
Image should be in range (0, 255) and numpy array
"""
image = Image.fromarray(image)
image.save(filename)