-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdata_parser.py
More file actions
153 lines (129 loc) · 6.21 KB
/
Copy pathdata_parser.py
File metadata and controls
153 lines (129 loc) · 6.21 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
import os
import json
import numpy as np
from collections import namedtuple
from collections import defaultdict
ListData = namedtuple('ListData', ['id', 'label', 'path'])
class DatasetBase(object):
"""
To read json data and construct a list containing video sample `ids`,
`label` and `path`
"""
def __init__(self, args, json_path_input, json_path_labels, data_root,
extension, num_tasks, is_test=False, is_val=False):
self.num_tasks = num_tasks
self.json_path_input = json_path_input
self.json_path_labels = json_path_labels
self.data_root = data_root
self.extension = extension
self.is_test = is_test
self.is_val = is_val
self.just_robot = args.just_robot
self.sim_dir = args.sim_dir
self.num_occur = defaultdict(int)
self.tasks = args.human_tasks
self.add_demos = args.add_demos
if self.add_demos:
self.robot_tasks = args.robot_tasks
# preparing data and class dictionary
self.classes = self.read_json_labels()
self.classes_dict = self.get_two_way_dict(self.classes)
self.json_data = self.read_json_input()
print("Number of human videos:", self.num_occur.values())
def read_json_input(self):
json_data = []
if not self.is_test:
if not self.just_robot: #not self.triplet or not self.add_demos: #self.is_val or
with open(self.json_path_input, 'rb') as jsonfile:
json_reader = json.load(jsonfile)
for elem in json_reader:
label = self.clean_template(elem['template'])
if label not in self.classes_dict.keys(): # or label == 'Pushing something so that it slightly moves':
continue
if label not in self.classes:
raise ValueError("Label mismatch! Please correct")
label_num = self.classes_dict[label]
item = ListData(elem['id'],
label,
os.path.join(self.data_root,
elem['id'] + self.extension)
)
json_data.append(item)
self.num_occur[label] += 1
if self.add_demos:
# Add robot demonstrations or extra robot class to json_data, just use id 300000
robot_tasks = self.robot_tasks
root_in_dir = self.sim_dir
for label_num in robot_tasks:
# add task demos for task label_num
in_dirs = [f'{root_in_dir}/env1/task{label_num}_webm', f'{root_in_dir}/env1_rearranged/task{label_num}_webm']
for in_dir in in_dirs:
label = self.classes_dict[label_num]
num_demos = self.add_demos
self.num_occur[label] += num_demos
if not self.is_val:
for j in range(num_demos):
item = ListData(300000,
label,
os.path.join(in_dir, str(j) + self.extension)
)
json_data.append(item)
else:
for j in range(num_demos, int(1.4*num_demos)):
item = ListData(300000,
label,
os.path.join(in_dir, str(j) + self.extension)
)
json_data.append(item)
else:
with open(self.json_path_input, 'rb') as jsonfile:
json_reader = json.load(jsonfile)
for elem in json_reader:
# add a dummy label for all test samples
item = ListData(elem['id'],
"Holding something",
os.path.join(self.data_root,
elem['id'] + self.extension)
)
json_data.append(item)
return json_data
def read_json_labels(self):
classes = []
with open(self.json_path_labels, 'rb') as jsonfile:
json_reader = json.load(jsonfile)
for elem in json_reader:
classes.append(elem)
return sorted(classes)
def get_two_way_dict(self, classes):
classes_dict = {}
tasks = self.tasks
for i, item in enumerate(classes):
if i not in tasks:
continue
classes_dict[item] = i
classes_dict[i] = item
print("Length of keys", len(classes_dict.keys()), classes_dict.keys())
return classes_dict
def clean_template(self, template):
""" Replaces instances of `[something]` --> `something`"""
template = template.replace("[", "")
template = template.replace("]", "")
return template
class WebmDataset(DatasetBase):
def __init__(self, args, json_path_input, json_path_labels, data_root, num_tasks,
is_test=False, is_val=False):
EXTENSION = ".webm"
super().__init__(args, json_path_input, json_path_labels, data_root,
EXTENSION, num_tasks, is_test, is_val)
class I3DFeatures(DatasetBase):
def __init__(self, json_path_input, json_path_labels, data_root,
is_test=False):
EXTENSION = ".npy"
super().__init__(json_path_input, json_path_labels, data_root,
EXTENSION, is_test)
class ImageNetFeatures(DatasetBase):
def __init__(self, json_path_input, json_path_labels, data_root,
is_test=False):
EXTENSION = ".npy"
super().__init__(json_path_input, json_path_labels, data_root,
EXTENSION, is_test)