File size: 9,079 Bytes
3de4238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import torch
import rich
import pickle
import numpy as np


def lengths_to_mask(lengths):
    max_len = max(lengths)
    mask = torch.arange(max_len, device=lengths.device).expand(
        len(lengths), max_len) < lengths.unsqueeze(1)
    return mask


# padding to max length in one batch
def collate_tensors(batch):
    if isinstance(batch[0], np.ndarray):
        batch = [torch.tensor(b).float() for b in batch]

    dims = batch[0].dim()
    max_size = [max([b.size(i) for b in batch]) for i in range(dims)]
    size = (len(batch), ) + tuple(max_size)
    canvas = batch[0].new_zeros(size=size)
    for i, b in enumerate(batch):
        sub_tensor = canvas[i]
        for d in range(dims):
            sub_tensor = sub_tensor.narrow(d, 0, b.size(d))
        sub_tensor.add_(b)
    return canvas

def humanml3d_collate(batch):
    notnone_batches = [b for b in batch if b is not None]
    EvalFlag = False if notnone_batches[0][5] is None else True

    # Sort by text length
    if EvalFlag:
        notnone_batches.sort(key=lambda x: x[5], reverse=True)

    # Motion only
    adapted_batch = {
        "motion":
        collate_tensors([torch.tensor(b[1]).float() for b in notnone_batches]),
        "length": [b[2] for b in notnone_batches],
    }

    # Text and motion
    if notnone_batches[0][0] is not None:
        adapted_batch.update({
            "text": [b[0] for b in notnone_batches],
            "all_captions": [b[7] for b in notnone_batches],
        })

    # Evaluation related
    if EvalFlag:
        adapted_batch.update({
            "text": [b[0] for b in notnone_batches],
            "word_embs":
            collate_tensors(
                [torch.tensor(b[3]).float() for b in notnone_batches]),
            "pos_ohot":
            collate_tensors(
                [torch.tensor(b[4]).float() for b in notnone_batches]),
            "text_len":
            collate_tensors([torch.tensor(b[5]) for b in notnone_batches]),
            "tokens": [b[6] for b in notnone_batches],
        })

    # Tasks
    if len(notnone_batches[0]) == 9:
        adapted_batch.update({"tasks": [b[8] for b in notnone_batches]})

    return adapted_batch

def nymeria_collate_3dscene(batch):
    notnone_batches = [b for b in batch if b is not None]
    EvalFlag = False if notnone_batches[0][5] is None else True

    # Sort by text length
    if EvalFlag:
        notnone_batches.sort(key=lambda x: x[5], reverse=True)

    # Motion only
    adapted_batch = {
        "motion":
        collate_tensors([torch.tensor(b[1], dtype=torch.float32) for b in notnone_batches]),
        "length": [b[2] for b in notnone_batches],
    }

    # Text and motion
    if notnone_batches[0][0] is not None:
        adapted_batch.update({
            "text": [b[0] for b in notnone_batches],
            "all_captions": [b[7] for b in notnone_batches],
            "pc": collate_tensors([torch.as_tensor(b[9], dtype=torch.float32) for b in notnone_batches]),
            "pc_embed": collate_tensors([torch.as_tensor(b[10], dtype=torch.float32) for b in notnone_batches]),
            "pc_mask": collate_tensors([torch.as_tensor(b[11]).long() for b in notnone_batches]),
            "pc_length": [b[12] for b in notnone_batches],
            "three_points": collate_tensors([torch.as_tensor(b[13], dtype=torch.float32) for b in notnone_batches]),
            "three_points_length": [b[14] for b in notnone_batches],
            # "video_rgb": collate_tensors([torch.as_tensor(b[15]) for b in notnone_batches]),
            "video_embed": collate_tensors([torch.as_tensor(b[15], dtype=torch.float32) for b in notnone_batches]),
            "video_lengths": [b[16] for b in notnone_batches],
            "future_text": [b[17] for b in notnone_batches],
            "future_motion": collate_tensors([torch.as_tensor(b[18], dtype=torch.float32) for b in notnone_batches]),
            "future_motion_length": [b[19] for b in notnone_batches],
            "global_head_voxel": collate_tensors([torch.as_tensor(b[22], dtype=torch.float32) for b in notnone_batches]),
            "global_head_rot": collate_tensors([torch.as_tensor(b[23], dtype=torch.float32) for b in notnone_batches]),
            "min_coord": collate_tensors([torch.as_tensor(b[24], dtype=torch.float32) for b in notnone_batches]),
            "obstacle_cot_answer": [b[25] for b in notnone_batches],
            # "past_pose": collate_tensors([torch.as_tensor(b[20], dtype=torch.float32) for b in notnone_batches]),
            # "fut_pose": collate_tensors([torch.as_tensor(b[21], dtype=torch.float32) for b in notnone_batches]),
        })
        # if len(notnone_batches[0]) > 25 :
        #     adapted_batch.update({
        #     "scene_question": [b[25] for b in notnone_batches],
        #     "scene_answer": [b[26] for b in notnone_batches],
        #     })
    
    if not EvalFlag : 
        adapted_batch.update({
            "past_pose": collate_tensors([torch.as_tensor(b[20], dtype=torch.float32) for b in notnone_batches]),
            "fut_pose": collate_tensors([torch.as_tensor(b[21], dtype=torch.float32) for b in notnone_batches]),
        })
    # Evaluation related
    if EvalFlag:
        adapted_batch.update({
            "text": [b[0] for b in notnone_batches],
            "word_embs":
            collate_tensors(
                [torch.tensor(b[3]).float() for b in notnone_batches]),
            "pos_ohot":
            collate_tensors(
                [torch.tensor(b[4]).float() for b in notnone_batches]),
            "text_len":
            collate_tensors([torch.tensor(b[5]) for b in notnone_batches]),
            "tokens": [b[6] for b in notnone_batches],
            "scene_name": [b[26] for b in notnone_batches],
            "data_idx": [b[27] for b in notnone_batches],
        })

    # Tasks
    if notnone_batches[0][8] is not None:
    # if len(notnone_batches[0]) == 9+2:
        adapted_batch.update({"tasks": [b[8] for b in notnone_batches]})

    return adapted_batch


def nymeria_collate_3dscene_pretrain(batch):
    notnone_batches = [b for b in batch if b is not None]
    EvalFlag = False if notnone_batches[0][5] is None else True

    # Sort by text length
    if EvalFlag:
        notnone_batches.sort(key=lambda x: x[5], reverse=True)

    # Motion only
    adapted_batch = {
        "motion":
        collate_tensors([torch.tensor(b[1]).float() for b in notnone_batches]),
        "length": [b[2] for b in notnone_batches],
    }

    # Text and motion
    if notnone_batches[0][0] is not None:
        adapted_batch.update({
            "text": [b[0] for b in notnone_batches],
            "all_captions": [b[7] for b in notnone_batches],
        })
    
    # Text and scene
    if notnone_batches[0][0] is not None:
        adapted_batch.update({
            "pc": collate_tensors([torch.as_tensor(b[9]) for b in notnone_batches]),
            "pc_embed": collate_tensors([torch.as_tensor(b[10]).float() for b in notnone_batches]),
            "pc_mask": collate_tensors([torch.as_tensor(b[11]).long() for b in notnone_batches]),
            "pc_len": [b[12] for b in notnone_batches],
            "video_embed": collate_tensors([torch.as_tensor(b[13]).float() for b in notnone_batches]),
            "video_lengths": [b[14] for b in notnone_batches],
            "scene_question": [b[15] for b in notnone_batches],
            "scene_answer": [b[16] for b in notnone_batches],
            "global_head_voxel": collate_tensors([torch.as_tensor(b[18], dtype=torch.float32) for b in notnone_batches]),
            "global_head_rot": collate_tensors([torch.as_tensor(b[19], dtype=torch.float32) for b in notnone_batches]),
            "min_coord": collate_tensors([torch.as_tensor(b[20], dtype=torch.float32) for b in notnone_batches]),
            "obstacle_answer": [b[21] if len(b) > 21 else '' for b in notnone_batches],
        })
    if not EvalFlag :
        adapted_batch.update({
            "past_pose": collate_tensors([torch.as_tensor(b[17], dtype=torch.float32) for b in notnone_batches]),
        })

    # Evaluation related
    if EvalFlag:
        adapted_batch.update({
            "text": [b[0] for b in notnone_batches],
            "word_embs":
            collate_tensors(
                [torch.tensor(b[3]).float() for b in notnone_batches]),
            "pos_ohot":
            collate_tensors(
                [torch.tensor(b[4]).float() for b in notnone_batches]),
            "text_len":
            collate_tensors([torch.tensor(b[5]) for b in notnone_batches]),
            "tokens": [b[6] for b in notnone_batches],
        })

    # Tasks
    if notnone_batches[0][8] is not None:
    # if len(notnone_batches[0]) == 9+2:
        adapted_batch.update({"tasks": [b[8] for b in notnone_batches]})

    return adapted_batch


def load_pkl(path, description=None, progressBar=False):
    if progressBar:
        with rich.progress.open(path, 'rb', description=description) as file:
            data = pickle.load(file)
    else:
        with open(path, 'rb') as file:
            data = pickle.load(file)
    return data