-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathechonet.py
More file actions
791 lines (667 loc) · 28.1 KB
/
echonet.py
File metadata and controls
791 lines (667 loc) · 28.1 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
import collections
import os
from multiprocessing import Pool
from pathlib import Path
from typing import Optional
import cv2
import imageio
import lightning.pytorch as L
import monai.transforms as T
import numpy as np
import pandas
import pandas as pd
import skimage.draw
import torch
import torchvision
from einops import rearrange
from monai.data import DataLoader
from torchvision.transforms.functional import rgb_to_grayscale
from tqdm import tqdm
# TODO: Change this to the correct path
ROOT = "/path/to/echonet-dynamic"
# Used for normalization of the EchoNet dataset
ECHONET_STATS = {"mean": 57.6, "std": 54.1, "median": 40}
MEAN = np.array([32.756416, 32.941185, 33.275223])
STD = np.array([49.92367, 50.0535, 50.396824])
########################
#### Preprocessing #####
########################
def read_video(path):
"""Read video from path and return as numpy array with shape (frames, channels, height, width)."""
vid = imageio.get_reader(path, "ffmpeg")
video = [i for i in vid.iter_data()]
return rearrange(
video, "frames height width channels -> frames channels height width"
)
def _save_transformed_video(path):
video = torch.tensor(read_video(path))
video = rgb_to_grayscale(video)
torch.save(video, path.with_suffix(".pt"))
def transform_all_videos_to_grayscaled_tensors(input_dir, nprocesses=12):
input_dir = Path(input_dir)
videos = list(input_dir.glob("*.avi"))
with Pool(nprocesses) as p:
_ = list(p.imap(_save_transformed_video, tqdm(videos)))
########################
##### Transforms #######
########################
class ImageDropout:
"""Set a random channel and / or pixels to zero. This is for a image generation task."""
def __init__(
self,
keys=None,
channel_wise: bool = True,
pixel_wise: bool = False,
p: float = 0.5,
):
"""
Args:
channel_wise (bool, optional): Whether dropout channel-wise. Defaults to True.
pixel_wise (bool, optional): Whether dropout pixel-wise. Defaults to False.
p (float, optional): Probability of applying dropout. Must be in the range [0, 1]. Defaults to 0.5.
Raises:
AssertionError: If both channel_wise and pixel_wise are False.
AssertionError: If p is not in the range [0, 1].
"""
self.keys = keys
self.channel_wise = channel_wise
self.pixel_wise = pixel_wise
self.p = p
assert (
channel_wise or pixel_wise
), "At least one of channel_wise or pixel_wise must be True."
assert 0 < p < 1, "p must be in the range [0, 1]"
def __call__(self, data: dict) -> torch.Tensor:
"""Set a random channel or values to zero. This is for a image generation task."""
img = data[self.keys]
if self.channel_wise:
# Drop every channel with probability p
mask = torch.rand(img.shape[0]) < self.p
img[mask] = 0.0
data["channel_dropped_idx"] = mask
if self.pixel_wise:
# Drop every foreground pixel with probability p
fg_idx = torch.where(img > 0)
fg_mask = torch.rand(fg_idx[0].shape) < self.p
fg_idx = [i[fg_mask] for i in fg_idx]
img[fg_idx] = 0
data[self.keys] = img
return data
class LoadVideoClip:
"""Read video and select random clip of length 'clip_length'."""
def __init__(
self, keys="image", clip_length: Optional[int] = 16, sampling_rate: int = 4
):
"""
Args:
keys (str): The keys to be loaded from the dataset. Defaults to "image".
clip_length (int, optional): The length of each video clip. Defaults to 16. If None, the whole video is returned.
sampling_rate (int, optional): The sampling rate for the video. Defaults to 1.
"""
self.keys = keys
self.clip_length = clip_length
self.sampling_rate = sampling_rate
def __call__(self, data: dict) -> torch.Tensor:
"""Read video and select random clip of length 'clip_length'."""
path = data[self.keys]
data["path"] = path
video: torch.Tensor = torch.load(path, weights_only=False)
video = video.float()
if self.clip_length is None:
data[self.keys] = video
return data
# Apply sampling rate
if len(video) / self.sampling_rate > self.clip_length:
video = video[:: self.sampling_rate]
start = np.random.randint(0, len(video) - self.clip_length)
data[self.keys] = video[start : start + self.clip_length]
return data
########################
##### Dataclasses ######
########################
class EchoNetDataset:
"""
The dataset is a collection of echocardiograms from the EchoNet challenge.
It can be used to predict the ejection fraction (EF), end-diastolic volume (EDV), or end-systolic volume (ESV).
We also use it for image generation tasks using the dropout transforms.
"""
def __init__(
self,
mode: str = "train",
output_variable: str = "ef",
transforms=None,
) -> None:
"""
Args:
mode: The mode of the dataset. Can be "train", "val", or "test". Defaults to "train".
output_variable: The output variable to predict. Can be "ef", "edv", or "esv". Defaults to "ef".
transforms: A list of transforms to apply to the data. Defaults to None.
"""
self.root = Path("/sc-scratch/sc-scratch-gbm-radiomics/posenc/echonet-dynamic")
self.video_path = self.root / "processed"
self.data = pd.read_csv(self.root / "FileList.csv")
# self.volume_tracing = pd.read_csv(self.root / "VolumeTracings.csv")
self.transforms = transforms
assert output_variable.lower() in ["ef", "edv", "esv"]
self.output_variable = output_variable.upper()
assert mode in ["train", "val", "test"]
self.mode = mode
self.data = self.data[self.data.Split.str.lower() == mode]
# Remove videos with FrameWidth or FrameHeight unequal 112
self.data = self.data[
(self.data.FrameWidth == 112) & (self.data.FrameHeight == 112)
]
self.data.reset_index(inplace=True, drop=True)
def __len__(self) -> int:
return len(self.data)
def __getitem__(self, idx: int) -> dict:
"""Return a sample from the dataset."""
file_name, out_variable = self.data.loc[idx, ["FileName", self.output_variable]]
video_path = str(self.video_path / f"{file_name}.pt")
target = torch.tensor(out_variable, dtype=torch.float32)
sample = {
"image": video_path,
"target": target,
"target_name": self.output_variable,
}
if self.transforms:
sample = self.transforms(sample)
return sample
class EchoNetDataModule(L.LightningDataModule):
"""
DataModule for the EchoNet dataset.
This class is used to load the data, create the dataloaders and handle the specific taks transformations.
Can be regression of output_variable or image generation with channel or pixel wise dropout.
"""
def __init__(
self,
batch_size: int = 32,
num_workers: int = 12,
output_variable: str = "ef",
clip_length: int = 16,
sampling_rate: int = 1,
dropout_channel: bool = False,
dropout_pixel: bool = False,
dropout_p: float = 0.5,
):
"""
Initializes the Echonet dataset.
Args:
batch_size (int): The batch size for data loading. Default is 32.
num_workers (int): The number of worker threads for data loading. Default is 12.
output_variable (str): The output variable to predict. Default is "ef".
clip_length (int): The length of video clips. Default is 16.
dropout_channel (bool): Whether to apply channel dropout. Default is False.
dropout_pixel (bool): Whether to apply pixel dropout. Default is False.
dropout_p (float): The dropout probability. Default is 0.5.
"""
super().__init__()
self.batch_size = batch_size
self.num_workers = num_workers
self.output_variable = output_variable
self.clip_length = clip_length
self.drop_channel = dropout_channel
self.drop_pixel = dropout_pixel
self.dropout_p = dropout_p
self.sampling_rate = sampling_rate
dropout = dropout_channel or dropout_pixel
self.train_transforms = self._get_transforms(augment=True, dropout=dropout)
self.valid_transform = self._get_transforms(augment=False, dropout=dropout)
self.save_hyperparameters()
def _get_transforms(self, augment: bool = False, dropout=False):
transforms = [
LoadVideoClip(
clip_length=self.clip_length, sampling_rate=self.sampling_rate
),
T.NormalizeIntensityd(
keys="image",
subtrahend=ECHONET_STATS["mean"],
divisor=ECHONET_STATS["std"],
nonzero=True,
),
]
if dropout:
transforms += [
T.CopyItemsd(keys="image"),
ImageDropout(
keys="image",
channel_wise=self.drop_channel,
pixel_wise=self.drop_pixel,
p=self.dropout_p,
),
]
if augment:
transforms += [
T.RandGaussianNoised(keys="image", prob=0.33, mean=0, std=0.1),
T.RandShiftIntensityd(keys="image", prob=0.33, offsets=0.1),
T.RandScaleIntensityd(keys="image", prob=0.33, factors=0.1),
]
return T.Compose(transforms)
def setup(self, stage: str = None):
self.train = EchoNetDataset(
mode="train",
output_variable=self.output_variable,
transforms=self.train_transforms,
)
self.val = EchoNetDataset(
mode="val",
output_variable=self.output_variable,
transforms=self.valid_transform,
)
self.test = EchoNetDataset(
mode="test",
output_variable=self.output_variable,
transforms=self.valid_transform,
)
def train_dataloader(self):
return DataLoader(
self.train,
batch_size=self.batch_size,
num_workers=self.num_workers,
shuffle=True,
drop_last=True,
)
def val_dataloader(self):
return DataLoader(
self.val,
batch_size=self.batch_size,
num_workers=self.num_workers,
drop_last=True,
)
def test_dataloader(self):
return DataLoader(
self.test, batch_size=self.batch_size, num_workers=self.num_workers
)
###########################
##### Dataclasses V2 ######
###########################
def loadvideo(filename: str) -> np.ndarray:
"""Loads a video from a file.
Args:
filename (str): filename of video
Returns:
A np.ndarray with dimensions (channels=3, frames, height, width). The
values will be uint8's ranging from 0 to 255.
Raises:
FileNotFoundError: Could not find `filename`
ValueError: An error occurred while reading the video
"""
if not os.path.exists(filename):
raise FileNotFoundError(filename)
capture = cv2.VideoCapture(filename)
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
frame_width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
v = np.zeros((frame_count, frame_height, frame_width, 3), np.uint8)
for count in range(frame_count):
ret, frame = capture.read()
if not ret:
raise ValueError("Failed to load frame #{} of {}.".format(count, filename))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
v[count, :, :] = frame
v = v.transpose((3, 0, 1, 2))
return v
class Echo(torchvision.datasets.VisionDataset):
"""EchoNet-Dynamic Dataset.
Args:
root (string): Root directory of dataset (defaults to `echonet.config.DATA_DIR`)
split (string): One of {``train'', ``val'', ``test'', ``all'', or ``external_test''}
target_type (string or list, optional): Type of target to use,
``Filename'', ``EF'', ``EDV'', ``ESV'', ``LargeIndex'',
``SmallIndex'', ``LargeFrame'', ``SmallFrame'', ``LargeTrace'',
or ``SmallTrace''
Can also be a list to output a tuple with all specified target types.
The targets represent:
``Filename'' (string): filename of video
``EF'' (float): ejection fraction
``EDV'' (float): end-diastolic volume
``ESV'' (float): end-systolic volume
``LargeIndex'' (int): index of large (diastolic) frame in video
``SmallIndex'' (int): index of small (systolic) frame in video
``LargeFrame'' (np.array shape=(3, height, width)): normalized large (diastolic) frame
``SmallFrame'' (np.array shape=(3, height, width)): normalized small (systolic) frame
``LargeTrace'' (np.array shape=(height, width)): left ventricle large (diastolic) segmentation
value of 0 indicates pixel is outside left ventricle
1 indicates pixel is inside left ventricle
``SmallTrace'' (np.array shape=(height, width)): left ventricle small (systolic) segmentation
value of 0 indicates pixel is outside left ventricle
1 indicates pixel is inside left ventricle
Defaults to ``EF''.
mean (int, float, or np.array shape=(3,), optional): means for all (if scalar) or each (if np.array) channel.
Used for normalizing the video. Defaults to 0 (video is not shifted).
std (int, float, or np.array shape=(3,), optional): standard deviation for all (if scalar) or each (if np.array) channel.
Used for normalizing the video. Defaults to 0 (video is not scaled).
length (int or None, optional): Number of frames to clip from video. If ``None'', longest possible clip is returned.
Defaults to 16.
period (int, optional): Sampling period for taking a clip from the video (i.e. every ``period''-th frame is taken)
Defaults to 2.
max_length (int or None, optional): Maximum number of frames to clip from video (main use is for shortening excessively
long videos when ``length'' is set to None). If ``None'', shortening is not applied to any video.
Defaults to 250.
clips (int, optional): Number of clips to sample. Main use is for test-time augmentation with random clips.
Defaults to 1.
pad (int or None, optional): Number of pixels to pad all frames on each side (used as augmentation).
and a window of the original size is taken. If ``None'', no padding occurs.
Defaults to ``None''.
noise (float or None, optional): Fraction of pixels to black out as simulated noise. If ``None'', no simulated noise is added.
Defaults to ``None''.
target_transform (callable, optional): A function/transform that takes in the target and transforms it.
external_test_location (string): Path to videos to use for external testing.
"""
def __init__(
self,
root,
split="train",
target_type="EF",
mean=MEAN,
std=STD,
length=16,
period=2,
max_length=250,
clips=1,
pad=None,
noise=None,
target_transform=None,
external_test_location=None,
):
super().__init__(root, target_transform=target_transform)
self.split = split.upper()
if not isinstance(target_type, list):
target_type = [target_type]
self.target_type = target_type
self.mean = mean
self.std = std
self.length = length
self.max_length = max_length
self.period = period
self.clips = clips
self.pad = pad
self.noise = noise
self.target_transform = target_transform
self.external_test_location = external_test_location
self.fnames, self.outcome = [], []
if self.split == "EXTERNAL_TEST":
self.fnames = sorted(os.listdir(self.external_test_location))
else:
# Load video-level labels
with open(os.path.join(self.root, "FileList.csv")) as f:
data = pandas.read_csv(f)
data["Split"].map(lambda x: x.upper())
if self.split != "ALL":
data = data[data["Split"] == self.split]
self.header = data.columns.tolist()
self.fnames = data["FileName"].tolist()
self.fnames = [
fn + ".avi" for fn in self.fnames if os.path.splitext(fn)[1] == ""
] # Assume avi if no suffix
self.outcome = data.values.tolist()
# Check that files are present
missing = set(self.fnames) - set(
os.listdir(os.path.join(self.root, "Videos"))
)
if len(missing) != 0:
print(
"{} videos could not be found in {}:".format(
len(missing), os.path.join(self.root, "Videos")
)
)
for f in sorted(missing):
print("\t", f)
raise FileNotFoundError(
os.path.join(self.root, "Videos", sorted(missing)[0])
)
# Load traces
self.frames = collections.defaultdict(list)
self.trace = collections.defaultdict(_defaultdict_of_lists)
with open(os.path.join(self.root, "VolumeTracings.csv")) as f:
header = f.readline().strip().split(",")
assert header == ["FileName", "X1", "Y1", "X2", "Y2", "Frame"]
for line in f:
filename, x1, y1, x2, y2, frame = line.strip().split(",")
x1 = float(x1)
y1 = float(y1)
x2 = float(x2)
y2 = float(y2)
frame = int(frame)
if frame not in self.trace[filename]:
self.frames[filename].append(frame)
self.trace[filename][frame].append((x1, y1, x2, y2))
for filename in self.frames:
for frame in self.frames[filename]:
self.trace[filename][frame] = np.array(self.trace[filename][frame])
# A small number of videos are missing traces; remove these videos
keep = [len(self.frames[f]) >= 2 for f in self.fnames]
self.fnames = [f for (f, k) in zip(self.fnames, keep) if k]
self.outcome = [f for (f, k) in zip(self.outcome, keep) if k]
def __getitem__(self, index):
# Find filename of video
if self.split == "EXTERNAL_TEST":
video = os.path.join(self.external_test_location, self.fnames[index])
elif self.split == "CLINICAL_TEST":
video = os.path.join(
self.root, "ProcessedStrainStudyA4c", self.fnames[index]
)
else:
video = os.path.join(self.root, "Videos", self.fnames[index])
# Load video into np.array
video = loadvideo(video).astype(float)
# Add simulated noise (black out random pixels)
# 0 represents black at this point (video has not been normalized yet)
if self.noise is not None:
n = video.shape[1] * video.shape[2] * video.shape[3]
ind = np.random.choice(n, round(self.noise * n), replace=False)
f = ind % video.shape[1]
ind //= video.shape[1]
i = ind % video.shape[2]
ind //= video.shape[2]
j = ind
video[:, f, i, j] = 0
# Apply normalization
if isinstance(self.mean, (float, int)):
video -= self.mean
else:
video -= self.mean.reshape(3, 1, 1, 1)
if isinstance(self.std, (float, int)):
video /= self.std
else:
video /= self.std.reshape(3, 1, 1, 1)
# Set number of frames
c, f, h, w = video.shape
if self.length is None:
# Take as many frames as possible
length = f // self.period
else:
# Take specified number of frames
length = self.length
if self.max_length is not None:
# Shorten videos to max_length
length = min(length, self.max_length)
if f < length * self.period:
# Pad video with frames filled with zeros if too short
# 0 represents the mean color (dark grey), since this is after normalization
video = np.concatenate(
(video, np.zeros((c, length * self.period - f, h, w), video.dtype)),
axis=1,
)
c, f, h, w = video.shape # pylint: disable=E0633
if self.clips == "all":
# Take all possible clips of desired length
start = np.arange(f - (length - 1) * self.period)
else:
# Take random clips from video
start = np.random.choice(f - (length - 1) * self.period, self.clips)
# Gather targets
target = []
for t in self.target_type:
key = self.fnames[index]
if t == "Filename":
target.append(self.fnames[index])
elif t == "LargeIndex":
# Traces are sorted by cross-sectional area
# Largest (diastolic) frame is last
target.append(np.int(self.frames[key][-1]))
elif t == "SmallIndex":
# Largest (diastolic) frame is first
target.append(np.int(self.frames[key][0]))
elif t == "LargeFrame":
target.append(video[:, self.frames[key][-1], :, :])
elif t == "SmallFrame":
target.append(video[:, self.frames[key][0], :, :])
elif t in ["LargeTrace", "SmallTrace"]:
if t == "LargeTrace":
t = self.trace[key][self.frames[key][-1]]
else:
t = self.trace[key][self.frames[key][0]]
x1, y1, x2, y2 = t[:, 0], t[:, 1], t[:, 2], t[:, 3]
x = np.concatenate((x1[1:], np.flip(x2[1:])))
y = np.concatenate((y1[1:], np.flip(y2[1:])))
r, c = skimage.draw.polygon(
np.rint(y).astype(int),
np.rint(x).astype(int),
(video.shape[2], video.shape[3]),
)
mask = np.zeros((video.shape[2], video.shape[3]), np.float32)
mask[r, c] = 1
target.append(mask)
else:
if self.split == "CLINICAL_TEST" or self.split == "EXTERNAL_TEST":
target.append(np.float32(0))
else:
target.append(np.float32(self.outcome[index][self.header.index(t)]))
if target != []:
target = tuple(target) if len(target) > 1 else target[0]
if self.target_transform is not None:
target = self.target_transform(target)
# Select clips from video
video = tuple(
video[:, s + self.period * np.arange(length), :, :] for s in start
)
if self.clips == 1:
video = video[0]
else:
video = np.stack(video)
if self.pad is not None:
# Add padding of zeros (mean color of videos)
# Crop of original size is taken out
# (Used as augmentation)
c, l, h, w = video.shape
temp = np.zeros(
(c, l, h + 2 * self.pad, w + 2 * self.pad), dtype=video.dtype
)
temp[:, :, self.pad : -self.pad, self.pad : -self.pad] = (
video # pylint: disable=E1130
)
i, j = np.random.randint(0, 2 * self.pad, 2)
video = temp[:, :, i : (i + h), j : (j + w)]
video = rearrange(video[0], "F H W -> F () H W")
video = torch.tensor(video, dtype=torch.float32)
return {"image": video, "target": target}
def __len__(self):
return len(self.fnames)
def extra_repr(self) -> str:
"""Additional information to add at end of __repr__."""
lines = ["Target type: {target_type}", "Split: {split}"]
return "\n".join(lines).format(**self.__dict__)
def _defaultdict_of_lists():
"""Returns a defaultdict of lists.
This is used to avoid issues with Windows (if this function is anonymous,
the Echo dataset cannot be used in a dataloader).
"""
return collections.defaultdict(list)
class EchoNetDataModuleV2(L.LightningDataModule):
"""
DataModule for the EchoNet dataset.
This class is used to load the data, create the dataloaders and handle the specific taks transformations.
Can be regression of output_variable or image generation with channel or pixel wise dropout.
"""
def __init__(
self,
batch_size: int = 32,
num_workers: int = 12,
length=16,
period=2,
max_length=250,
clips=1,
pad=None,
noise=None,
target_transform=None,
):
"""
Initializes the Echonet dataset.
Args:
batch_size (int): The batch size for data loading. Default is 32.
num_workers (int): The number of worker threads for data loading. Default is 12.
length (int): The length of video clips. Default is 16.
period (int): The sampling rate for the video. Default is 2.
max_length (int): The maximum length of video clips. Default is 250.
clips (int): The number of clips to sample. Default is 1.
pad (int): The number of pixels to pad all frames on each side. Default is None.
noise (float): The fraction of pixels to black out as simulated noise. Default is None.
target_transform (callable): A function/transform that takes in the target and transforms it. Default is None.
"""
super().__init__()
self.batch_size = batch_size
self.num_workers = num_workers
self.length = length
self.period = period
self.max_length = max_length
self.clips = clips
self.pad = pad
self.noise = noise
self.target_transform = target_transform
self.save_hyperparameters()
def setup(self, stage: str = None):
self.train = Echo(
root=ROOT,
split="train",
length=self.length,
period=self.period,
max_length=self.max_length,
clips=self.clips,
pad=self.pad,
noise=self.noise,
target_transform=self.target_transform,
)
self.val = Echo(
root=ROOT,
split="val",
length=self.length,
period=self.period,
max_length=self.max_length,
clips=self.clips,
pad=self.pad,
noise=self.noise,
target_transform=self.target_transform,
)
self.test = Echo(
root=ROOT,
split="test",
length=self.length,
period=self.period,
max_length=self.max_length,
clips=self.clips,
pad=self.pad,
noise=self.noise,
target_transform=self.target_transform,
)
def train_dataloader(self):
return DataLoader(
self.train,
batch_size=self.batch_size,
num_workers=self.num_workers,
shuffle=True,
drop_last=True,
pin_memory=True,
)
def val_dataloader(self):
return DataLoader(
self.val,
batch_size=self.batch_size,
num_workers=self.num_workers,
drop_last=False,
pin_memory=True,
)
def test_dataloader(self):
return DataLoader(
self.test, batch_size=self.batch_size, num_workers=self.num_workers
)