-
Notifications
You must be signed in to change notification settings - Fork 86
/
utils.py
61 lines (48 loc) · 1.71 KB
/
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
from torchvision import transforms
import torch
import numpy as np
import av
# Mean and standard deviation used for pre-trained PyTorch models
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
def extract_frames(video_path):
""" Extracts frames from video """
frames = []
video = av.open(video_path)
for frame in video.decode(0):
yield frame.to_image()
def gram_matrix(y):
""" Returns the gram matrix of y (used to compute style loss) """
(b, c, h, w) = y.size()
features = y.view(b, c, w * h)
features_t = features.transpose(1, 2)
gram = features.bmm(features_t) / (c * h * w)
return gram
def train_transform(image_size):
""" Transforms for training images """
transform = transforms.Compose(
[
transforms.Resize(int(image_size * 1.15)),
transforms.RandomCrop(image_size),
transforms.ToTensor(),
transforms.Normalize(mean, std),
]
)
return transform
def style_transform(image_size=None):
""" Transforms for style image """
resize = [transforms.Resize(image_size)] if image_size else []
transform = transforms.Compose(resize + [transforms.ToTensor(), transforms.Normalize(mean, std)])
return transform
def denormalize(tensors):
""" Denormalizes image tensors using mean and std """
for c in range(3):
tensors[:, c].mul_(std[c]).add_(mean[c])
return tensors
def deprocess(image_tensor):
""" Denormalizes and rescales image tensor """
image_tensor = denormalize(image_tensor)[0]
image_tensor *= 255
image_np = torch.clamp(image_tensor, 0, 255).cpu().numpy().astype(np.uint8)
image_np = image_np.transpose(1, 2, 0)
return image_np