File size: 3,074 Bytes
0b32ad6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tempfile
from pathlib import Path

import pytest
import yaml
from dotenv import dotenv_values
from torch.utils.data import Subset
from tqdm import tqdm

from s3prl.downstream.emotion.expert import DownstreamExpert
from s3prl.problem import SuperbER


@pytest.mark.corpus
@pytest.mark.parametrize("fold_id", [0, 1, 2, 3, 4])
def test_er_dataset(fold_id):
    v3_er_folder = Path(__file__).parent.parent / "s3prl" / "downstream" / "emotion"

    IEMOCAP = dotenv_values()["IEMOCAP"]
    with (v3_er_folder / "config.yaml").open() as file:
        config = yaml.load(file, Loader=yaml.FullLoader)["downstream_expert"]
        config["datarc"]["root"] = IEMOCAP
        config["datarc"]["meta_data"] = v3_er_folder / "meta_data"
        config["datarc"]["test_fold"] = f"fold{fold_id + 1}"

    with tempfile.TemporaryDirectory() as tempdir:
        expert = DownstreamExpert(320, config, tempdir)

        train_dataset_v3 = expert.get_dataloader("train").dataset
        valid_dataset_v3 = expert.get_dataloader("dev").dataset
        test_dataset_v3 = expert.get_dataloader("test").dataset

    with tempfile.TemporaryDirectory() as tempdir:
        default_config = SuperbER().default_config()
        train_csv, valid_csv, test_csvs = SuperbER().prepare_data(
            {"iemocap": IEMOCAP, "test_fold": fold_id}, tempdir, tempdir
        )
        encoder_path = SuperbER().build_encoder(
            default_config["build_encoder"],
            tempdir,
            tempdir,
            train_csv,
            valid_csv,
            test_csvs,
        )
        train_dataset_v4 = SuperbER().build_dataset(
            default_config["build_dataset"],
            tempdir,
            tempdir,
            "train",
            train_csv,
            encoder_path,
            None,
        )
        valid_dataset_v4 = SuperbER().build_dataset(
            default_config["build_dataset"],
            tempdir,
            tempdir,
            "valid",
            valid_csv,
            encoder_path,
            None,
        )
        test_dataset_v4 = SuperbER().build_dataset(
            default_config["build_dataset"],
            tempdir,
            tempdir,
            "test",
            test_csvs[0],
            encoder_path,
            None,
        )

    def compare_dataset(v3, v4):
        data_v3 = {}
        for wav, label, name in tqdm(v3, desc="v3"):
            if isinstance(v3, Subset):
                v3 = v3.dataset
            label_name = [k for k, v in v3.class_dict.items() if v == label][0]
            data_v3[name] = label_name

        data_v4 = {}
        for batch in tqdm(v4, desc="v4"):
            data_v4[batch["unique_name"]] = batch["label"]

        assert sorted(data_v3.keys()) == sorted(data_v4.keys())
        for key in data_v3:
            value_v3 = data_v3[key]
            value_v4 = data_v4[key]
            assert value_v3 == value_v4

    compare_dataset(train_dataset_v3, train_dataset_v4)
    compare_dataset(valid_dataset_v3, valid_dataset_v4)
    compare_dataset(test_dataset_v3, test_dataset_v4)