-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata.py
More file actions
160 lines (138 loc) · 6.33 KB
/
data.py
File metadata and controls
160 lines (138 loc) · 6.33 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
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause-Clear
from typing import Dict, List, Union
from datasets import load_dataset
from torch.utils.data import Dataset
class QualcommInteractiveCookingDataset(Dataset):
"""
A dataset class for the Qualcomm Interactive Cooking dataset.
Args:
plan_set (str): Type of planning set, either "main" or "advanced"
split (str): Dataset split, one of "train", "validation", or "test"
"""
hf_dataset_name = "qualcomm/qualcomm-interactive-cooking-dataset"
def __init__(self, plan_set: str, split: str) -> None:
assert plan_set in ["main", "advanced_planning"]
assert split in ["train", "validation", "test"]
self.plan_set = plan_set
self.split = split
self.load_data()
def load_data(self) -> None:
"""
Load data from huggingface based on plan_set and split.
"""
if self.plan_set == "main":
if self.split == "train":
self.annotations = load_dataset(
self.hf_dataset_name, "main", split="train"
).to_list()
self.annotations += load_dataset(
self.hf_dataset_name, "main", split="validation"
).to_list()
elif self.split == "validation":
self.annotations = load_dataset(
self.hf_dataset_name, "main", split="validation"
).to_list()
elif self.split == "test":
self.annotations = load_dataset(
self.hf_dataset_name, "main", split="test"
).to_list()
else:
raise ValueError(f"Invalid Split: {self.split}")
elif self.plan_set == "advanced_planning":
if self.split == "train":
self.annotations = load_dataset(
self.hf_dataset_name, "advanced_planning", split="train"
).to_list()
self.annotations += load_dataset(
self.hf_dataset_name, "advanced_planning", split="validation"
).to_list()
elif self.split == "validation":
self.annotations = load_dataset(
self.hf_dataset_name, "advanced_planning", split="validation"
).to_list()
elif self.split == "test":
self.annotations = load_dataset(
self.hf_dataset_name, "advanced_planning", split="test"
).to_list()
else:
raise ValueError(f"Invalid Split: {self.split}")
else:
raise ValueError(f"Invalid Plan Set: {self.plan_set}")
self.preprocess_data()
def preprocess_data(self) -> None:
"""
Preprocess the loaded annotations.
"""
for annotation in self.annotations:
output_texts = annotation["output_texts"]
output_timestamps = annotation["output_timestamps"]
output_types = annotation["output_types"]
# Ignore any dangling instructions
if not ("finish_all" in output_types[-1]):
if output_types[-1] == "instruction":
for k in [
"remaining_plan",
"output_timestamps",
"output_texts",
"output_types",
"output_actions",
]:
annotation[k] = annotation[k][:-1]
(
instruction_segment_texts,
instruction_segment_texts_timestamps,
instruction_segment_texts_types,
) = ([], [], [])
instrcution_segment_has_mistake = []
_segment_texts, _segment_texts_timestamps, _segment_texts_types = [], [], []
has_mistake = False
for _text, _timestamp, _type in zip(output_texts, output_timestamps, output_types):
if _type == "instruction" or "finish_all" in _type:
if len(_segment_texts) > 0:
instruction_segment_texts.append(_segment_texts)
instruction_segment_texts_timestamps.append(_segment_texts_timestamps)
instruction_segment_texts_types.append(_segment_texts_types)
instrcution_segment_has_mistake.append(has_mistake)
_segment_texts = [_text]
_segment_texts_timestamps = [_timestamp]
_segment_texts_types = [_type]
has_mistake = False
else:
_segment_texts.append(_text)
_segment_texts_timestamps.append(_timestamp)
_segment_texts_types.append(_type)
if "mistake" in _type:
has_mistake = True
annotation.update(
{
"instruction_segment_texts": instruction_segment_texts,
"instruction_segment_texts_timestamps": instruction_segment_texts_timestamps,
"instruction_segment_texts_types": instruction_segment_texts_types,
"instrcution_segment_has_mistake": instrcution_segment_has_mistake,
}
)
def __len__(self) -> int:
"""
Return the number of annotations in the dataset.
Returns:
int: Number of annotations
"""
return len(self.annotations)
def __getitem__(self, video_idx: int) -> Dict[str, Union[str, List[str]]]:
"""
Get a specific item from the dataset.
Args:
video_idx (int): Index of the video to retrieve
Returns:
dict: Dictionary containing video ID, texts, timestamps, types, and mistake indicators
"""
video_idx = video_idx % len(self.annotations)
return_dict = {
"video_id": self.annotations[video_idx]["video_id"],
"texts": self.annotations[video_idx]["instruction_segment_texts"],
"texts_timestamps": self.annotations[video_idx]["instruction_segment_texts_timestamps"],
"texts_types": self.annotations[video_idx]["instruction_segment_texts_types"],
"has_mistake": self.annotations[video_idx]["instrcution_segment_has_mistake"],
}
return return_dict