-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
90 lines (68 loc) · 2.45 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import cv2
import numpy as np
import torch
from torch.utils.data import DataLoader
from torchvision import utils
def save_frames_as_video(frames, video_path, fps=30):
height, width, layers = frames[0].shape
video = cv2.VideoWriter(video_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
for frame in frames:
video.write(cv2.cvtColor((frame*255).astype(np.uint8), cv2.COLOR_RGB2BGR))
cv2.destroyAllWindows()
video.release()
def save_image(data, saveas, video=False):
utils.save_image(
data,
saveas,
nrow=data.shape[0]//2,
normalize=True,
range=(-1, 1),
)
def process_data(data, device, dataset):
source, target, background, source_images, source_images_original = data
img = torch.cat([source, background], axis=2).squeeze(0).to(device)
source = source.squeeze(0)
ground_truth = source_images.squeeze(0).to(device)
S = source.shape[0]
return img, S, ground_truth, source_images_original.squeeze(0).to(device)
'''
Conv3D based temporal module is added before the quantization step
LPIPS based perceptual loss is added
'''
def get_facetranslation_latent_conv_perceptual(args, device):
from TemporalAlignment.dataset import TemporalAlignmentDataset
from models.vqvae_conv3d_latent import VQVAE
from loss import VQLPIPS
print(f'Inside conv3d applied before quantization along with perceptual loss')
model = VQVAE(in_channel=3*2).to(device)
vqlpips = VQLPIPS().to(device)
train_dataset = TemporalAlignmentDataset(
'train', 30,
color_jitter_type=args.colorjit,
grayscale_required=args.gray)
val_dataset = TemporalAlignmentDataset(
'val', 50,
color_jitter_type=args.colorjit,
cross_identity_required=args.crossid,
grayscale_required=args.gray,
custom_validation_required=args.custom_validation,
validation_datapoints=args.validation_folder)
try:
train_loader = DataLoader(
train_dataset,
batch_size=1,
shuffle=True,
num_workers=2)
except:
train_loader = None
val_loader = DataLoader(
val_dataset,
shuffle=False,
batch_size=1,
num_workers=2)
return train_loader, val_loader, model, vqlpips
'''
get the loaders and the models
'''
def get_loaders_and_models(args, device):
return get_facetranslation_latent_conv_perceptual(args, device)