-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_load_json.py
133 lines (117 loc) · 5.02 KB
/
data_load_json.py
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
import os
import numpy as np
import torch
from torch.utils.data import Dataset
import json
from collections import namedtuple
Batch = namedtuple('Batch', 'Observations json_id json_len')
class PlanningDataset(Dataset):
"""
load video and action features from dataset
"""
def __init__(self,
root,
args=None,
is_val=False,
model=None,
):
self.is_val = is_val
self.data_root = root
self.args = args
self.max_traj_len = args.horizon
self.vid_names = None
self.frame_cnts = None
self.images = None
self.last_vid = ''
if args.dataset == 'crosstask':
cross_task_data_name = args.json_path_val
# "/data1/wanghanlin/diffusion_planning/jsons_crosstask105/sliding_window_cross_task_data_{}_{}_new_task_id_73.json".format(
# is_val, self.max_traj_len)
if os.path.exists(cross_task_data_name):
with open(cross_task_data_name, 'r') as f:
self.json_data = json.load(f)
print('Loaded {}'.format(cross_task_data_name))
else:
assert 0
elif args.dataset == 'coin':
coin_data_name = args.json_path_val
# "/data1/wanghanlin/diffusion_planning/jsons_coin/sliding_window_cross_task_data_{}_{}_new_task_id_73.json".format(
# is_val, self.max_traj_len)
if os.path.exists(coin_data_name):
with open(coin_data_name, 'r') as f:
self.json_data = json.load(f)
print('Loaded {}'.format(coin_data_name))
else:
assert 0
elif args.dataset == 'NIV':
niv_data_name = args.json_path_val
# "/data1/wanghanlin/diffusion_planning/jsons_niv/sliding_window_cross_task_data_{}_{}_new_task_id_73.json".format(
# is_val, self.max_traj_len)
if os.path.exists(niv_data_name):
with open(niv_data_name, 'r') as f:
self.json_data = json.load(f)
print('Loaded {}'.format(niv_data_name))
else:
assert 0
else:
raise NotImplementedError(
'Dataset {} is not implemented'.format(args.dataset))
self.model = model
self.prepare_data()
self.M = 3
def prepare_data(self):
vid_names = []
frame_cnts = []
for listdata in self.json_data:
vid_names.append(listdata['id'])
frame_cnts.append(listdata['instruction_len'])
self.vid_names = vid_names
self.frame_cnts = frame_cnts
def curate_dataset(self, images, legal_range, M=2):
images_list = []
labels_onehot_list = []
idx_list = []
for start_idx, end_idx, action_label in legal_range:
idx = start_idx
idx_list.append(idx)
image_start_idx = max(0, idx)
if image_start_idx + M <= len(images):
image_start = images[image_start_idx: image_start_idx + M]
else:
image_start = images[len(images) - M: len(images)]
image_start_cat = image_start[0]
for w in range(len(image_start) - 1):
image_start_cat = np.concatenate((image_start_cat, image_start[w + 1]), axis=0)
images_list.append(image_start_cat)
labels_onehot_list.append(action_label)
end_idx = max(2, end_idx)
image_end = images[end_idx - 2:end_idx + M - 2]
image_end_cat = image_end[0]
for w in range(len(image_end) - 1):
image_end_cat = np.concatenate((image_end_cat, image_end[w + 1]), axis=0)
images_list.append(image_end_cat)
return images_list, labels_onehot_list, idx_list
def sample_single(self, index):
folder_id = self.vid_names[index]
if self.args.dataset == 'crosstask':
if folder_id['vid'] != self.last_vid:
images_ = np.load(folder_id['feature'], allow_pickle=True)
self.images = images_['frames_features']
self.last_vid = folder_id['vid']
else:
images_ = np.load(folder_id['feature'], allow_pickle=True)
self.images = images_['frames_features']
images, labels_matrix, idx_list = self.curate_dataset(
self.images, folder_id['legal_range'], M=self.M)
frames = torch.tensor(np.array(images))
return frames
def __getitem__(self, index):
frames = self.sample_single(index)
frames_t = torch.zeros(2, self.args.observation_dim)
frames_t[0, :] = frames[0, :]
frames_t[1, :] = frames[-1, :]
frames_t = frames_t.view(1, 2, -1)
batch = Batch(frames_t, self.vid_names[index], self.frame_cnts[index])
return batch
def __len__(self):
return len(self.json_data)