forked from aczhang9/long-video-gan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
200 lines (156 loc) · 7.58 KB
/
dataset.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
from __future__ import annotations
import json
from dataclasses import dataclass
from pathlib import Path, PurePosixPath
from typing import Any, Optional
from zipfile import ZipFile
import einops
import numpy as np
import torch
from PIL import Image
from torch.utils.data import Dataset
# =====================================================================================================================
@dataclass
class VideoDataset(Dataset):
dataset_dir: str
seq_length: int
height: int
width: int
min_spacing: int = 1
max_spacing: int = 1
min_video_length: Optional[int] = None
x_flip: bool = False
def __post_init__(self):
assert self.seq_length >= 1
self.dataset_path = Path(self.dataset_dir).joinpath(f"{self.height:04d}x{self.width:04d}")
assert self.dataset_path.is_dir(), self.dataset_path
self.frame_paths = {}
for partition in self.dataset_path.glob("*.zip"):
with ZipFile(partition) as zf:
with zf.open("frame_paths.json", "r") as fp:
self.frame_paths[partition.stem] = json.load(fp)
self.min_video_length = max(self.min_video_length or 1, (self.seq_length - 1) * self.min_spacing + 1)
self.video_paths = [
(partition_name, clip_path, frame_names)
for partition_name, partition_frame_paths in sorted(self.frame_paths.items())
for clip_path, frame_names in sorted(partition_frame_paths.items())
if len(frame_names) >= self.min_video_length
]
self._zipfiles = {}
def sample_frame_names(self, frame_names: list[str]) -> tuple[list[str], int]:
max_spacing = (
1 if self.seq_length == 1 else min(self.max_spacing, (len(frame_names) - 1) // (self.seq_length - 1))
)
spacing = torch.randint(self.min_spacing, max_spacing + 1, size=()).item()
frame_span = (self.seq_length - 1) * spacing + 1
max_start_index = len(frame_names) - frame_span
start_index = torch.randint(max_start_index + 1, size=()).item()
frame_names = frame_names[start_index : start_index + frame_span : spacing]
return frame_names, spacing
def read_frame(self, partition_name: str, frame_path: str) -> torch.Tensor:
if partition_name not in self._zipfiles:
partition_path = self.dataset_path.joinpath(f"{partition_name}.zip")
self._zipfiles[partition_name] = ZipFile(partition_path)
with self._zipfiles[partition_name].open(frame_path, "r") as fp:
frame = np.array(Image.open(fp))
frame = torch.from_numpy(frame)
frame = einops.rearrange(frame, "h w c -> c h w")
frame = 2 * frame.to(torch.float32) / 255 - 1
return frame
def __getitem__(self, index: int) -> dict[str, Any]:
partition_name, clip_path, frame_names = self.video_paths[index]
frame_names, spacing = self.sample_frame_names(frame_names)
frame_paths = [str(PurePosixPath(clip_path).joinpath(frame_name)) for frame_name in frame_names]
frames = [self.read_frame(partition_name, frame_path) for frame_path in frame_paths]
video = torch.stack(frames, dim=1)
if self.x_flip and torch.rand(()).item() < 0.5:
video = video.flip(dims=(-1,))
return dict(video=video, spacing=spacing)
def __len__(self) -> int:
return len(self.video_paths)
def __getstate__(self):
return dict(self.__dict__, _zipfiles={})
# =====================================================================================================================
@dataclass
class VideoDatasetTwoRes(Dataset):
dataset_dir: str
seq_length: int
lr_height: int
lr_width: int
hr_height: int
hr_width: int
min_spacing: int = 1
max_spacing: int = 1
min_video_length: Optional[int] = None
x_flip: bool = False
def __post_init__(self):
self.lr_dataset = VideoDataset(
self.dataset_dir,
self.seq_length,
self.lr_height,
self.lr_width,
self.min_spacing,
self.max_spacing,
self.min_video_length,
x_flip=self.x_flip,
)
self.hr_dataset = VideoDataset(
self.dataset_dir,
self.seq_length,
self.hr_height,
self.hr_width,
self.min_spacing,
self.max_spacing,
self.min_video_length,
x_flip=self.x_flip,
)
assert self.lr_dataset.video_paths == self.hr_dataset.video_paths
def __getitem__(self, index: int) -> dict[str, Any]:
partition_name, clip_path, frame_names = self.lr_dataset.video_paths[index]
frame_names, spacing = self.lr_dataset.sample_frame_names(frame_names)
frame_paths = [str(PurePosixPath(clip_path).joinpath(frame_name)) for frame_name in frame_names]
lr_frames = [self.lr_dataset.read_frame(partition_name, frame_path) for frame_path in frame_paths]
hr_frames = [self.hr_dataset.read_frame(partition_name, frame_path) for frame_path in frame_paths]
lr_video = torch.stack(lr_frames, dim=1)
hr_video = torch.stack(hr_frames, dim=1)
if self.x_flip and torch.rand(()).item() < 0.5:
lr_video = lr_video.flip(dims=(-1,))
hr_video = hr_video.flip(dims=(-1,))
return dict(lr_video=lr_video, hr_video=hr_video, spacing=spacing)
def __len__(self) -> int:
return len(self.lr_dataset)
# =====================================================================================================================
@dataclass
class VideoDatasetPerImage(Dataset):
dataset_dir: str
height: int
width: int
seq_length: int = 1
x_flip: bool = False
def __post_init__(self):
self.dataset = VideoDataset(self.dataset_dir, seq_length=1, height=self.height, width=self.width)
self.video_paths = []
for partition_name, partition_frame_paths in sorted(self.dataset.frame_paths.items()):
for clip_path, frame_names in sorted(partition_frame_paths.items()):
num_samples_from_source = len(frame_names) - self.seq_length + 1
for start_index in range(0, num_samples_from_source):
sample_frame_names = frame_names[start_index : start_index + self.seq_length]
self.video_paths.append((partition_name, clip_path, sample_frame_names, num_samples_from_source))
def __getitem__(self, index: int) -> dict[str, Any]:
partition_name, clip_path, sample_frame_names, num_samples_from_source = self.video_paths[index]
frame_paths = [str(PurePosixPath(clip_path).joinpath(frame_name)) for frame_name in sample_frame_names]
frames = [self.dataset.read_frame(partition_name, frame_path) for frame_path in frame_paths]
video = torch.stack(frames, dim=1)
if self.x_flip and torch.rand(()).item() < 0.5:
video = video.flip(dims=(-1,))
return dict(video=video, num_samples_from_source=num_samples_from_source)
def __len__(self) -> int:
return len(self.video_paths)
# =====================================================================================================================