lrs3.py 2.29 KB
Newer Older
Pingchuan Ma's avatar
Pingchuan Ma committed
1
2
3
4
5
6
7
8
9
10
11
import os

import torchaudio
import torchvision
from torch.utils.data import Dataset


def _load_list(args, *filenames):
    output = []
    length = []
    for filename in filenames:
12
        filepath = os.path.join(args.root_dir, "labels", filename)
Pingchuan Ma's avatar
Pingchuan Ma committed
13
        for line in open(filepath).read().splitlines():
14
15
            dataset, rel_path, input_length = line.split(",")[0], line.split(",")[1], line.split(",")[2]
            path = os.path.normpath(os.path.join(args.root_dir, dataset, rel_path[:-4] + ".mp4"))
Pingchuan Ma's avatar
Pingchuan Ma committed
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
            length.append(int(input_length))
            output.append(path)
    return output, length


def load_video(path):
    """
    rtype: torch, T x C x H x W
    """
    vid = torchvision.io.read_video(path, pts_unit="sec", output_format="THWC")[0]
    vid = vid.permute((0, 3, 1, 2))
    return vid


def load_audio(path):
    """
    rtype: torch, T x 1
    """
    waveform, sample_rate = torchaudio.load(path, normalize=True)
    return waveform.transpose(1, 0)


def load_transcript(path):
    transcript_path = path.replace("video_seg", "text_seg")[:-4] + ".txt"
    return open(transcript_path).read().splitlines()[0]


Pingchuan Ma's avatar
Pingchuan Ma committed
43
44
def load_item(path, modality):
    if modality == "video":
Pingchuan Ma's avatar
Pingchuan Ma committed
45
        return (load_video(path), load_transcript(path))
Pingchuan Ma's avatar
Pingchuan Ma committed
46
    if modality == "audio":
Pingchuan Ma's avatar
Pingchuan Ma committed
47
        return (load_audio(path), load_transcript(path))
Pingchuan Ma's avatar
Pingchuan Ma committed
48
    if modality == "audiovisual":
Pingchuan Ma's avatar
Pingchuan Ma committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        return (load_audio(path), load_video(path), load_transcript(path))


class LRS3(Dataset):
    def __init__(
        self,
        args,
        subset: str = "train",
    ) -> None:

        if subset is not None and subset not in ["train", "val", "test"]:
            raise ValueError("When `subset` is not None, it must be one of ['train', 'val', 'test'].")

        self.args = args

        if subset == "train":
Pingchuan Ma's avatar
Pingchuan Ma committed
65
            self.files, self.lengths = _load_list(self.args, "lrs3_train_transcript_lengths_seg16s.csv")
Pingchuan Ma's avatar
Pingchuan Ma committed
66
        if subset == "val":
Pingchuan Ma's avatar
Pingchuan Ma committed
67
            self.files, self.lengths = _load_list(self.args, "lrs3_test_transcript_lengths_seg16s.csv")
Pingchuan Ma's avatar
Pingchuan Ma committed
68
        if subset == "test":
Pingchuan Ma's avatar
Pingchuan Ma committed
69
            self.files, self.lengths = _load_list(self.args, "lrs3_test_transcript_lengths_seg16s.csv")
Pingchuan Ma's avatar
Pingchuan Ma committed
70
71

    def __getitem__(self, n):
Pingchuan Ma's avatar
Pingchuan Ma committed
72
73
        path = self.files[n]
        return load_item(path, self.args.modality)
Pingchuan Ma's avatar
Pingchuan Ma committed
74
75

    def __len__(self) -> int:
Pingchuan Ma's avatar
Pingchuan Ma committed
76
        return len(self.files)