Upload transfer.py
Browse files- transfer.py +85 -0
transfer.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import io
|
3 |
+
import av
|
4 |
+
import json
|
5 |
+
from pickle import dumps, loads
|
6 |
+
import numpy as np
|
7 |
+
import torch
|
8 |
+
from torchvision.transforms.functional import resize
|
9 |
+
import tensorflow as tf
|
10 |
+
import tensorflow_datasets as tfds
|
11 |
+
from einops import rearrange
|
12 |
+
|
13 |
+
def decode_inst(insts):
|
14 |
+
# Utility to decode encoded language instructions
|
15 |
+
decoded_insts = []
|
16 |
+
for inst in insts:
|
17 |
+
decoded_insts.append(bytes(inst[np.where(inst != 0)].tolist()).decode("utf-8"))
|
18 |
+
return decoded_insts
|
19 |
+
|
20 |
+
def save_video(file, video):
|
21 |
+
container = av.open(file, 'w', 'mp4')
|
22 |
+
stream = container.add_stream('libx264', rate=30)
|
23 |
+
stream.height = video[0].shape[0]
|
24 |
+
stream.width = video[0].shape[1]
|
25 |
+
stream.bit_rate = 2000000 # 2Mbps
|
26 |
+
stream.pix_fmt = 'yuv420p'
|
27 |
+
for i in range(len(video)):
|
28 |
+
frame = av.VideoFrame.from_ndarray(video[i], format='rgb24')
|
29 |
+
frame = frame.reformat(format=stream.pix_fmt)
|
30 |
+
for packet in stream.encode(frame):
|
31 |
+
container.mux(packet)
|
32 |
+
# Flush stream
|
33 |
+
for packet in stream.encode():
|
34 |
+
container.mux(packet)
|
35 |
+
container.close()
|
36 |
+
|
37 |
+
if __name__ == '__main__':
|
38 |
+
tf_builder = tfds.builder_from_directory('./droid/1.0.0/')
|
39 |
+
tf_dataset = tf_builder.as_dataset(split="train")
|
40 |
+
skip_episode = 78663
|
41 |
+
js_path = 'index.json'
|
42 |
+
if os.path.exists(js_path):
|
43 |
+
js_data = json.load(open(js_path, 'r'))
|
44 |
+
else:
|
45 |
+
js_data = []
|
46 |
+
for episode_id, episode in enumerate(tf_dataset):
|
47 |
+
file_path = episode['episode_metadata']['file_path'].numpy().decode('utf-8')
|
48 |
+
recording_folderpath = episode['episode_metadata']['recording_folderpath'].numpy().decode('utf-8')
|
49 |
+
if episode_id <= skip_episode or 'success' not in file_path:
|
50 |
+
print(f'skipping {episode_id}/{len(tf_dataset)}')
|
51 |
+
continue
|
52 |
+
left_camera = []
|
53 |
+
arm_camera = []
|
54 |
+
right_camera = []
|
55 |
+
inst = []
|
56 |
+
skip_episode = False
|
57 |
+
for step_id, single_step in enumerate(episode['steps']):
|
58 |
+
if single_step['language_instruction'].numpy().decode('utf-8') not in inst:
|
59 |
+
inst.append(single_step['language_instruction'].numpy().decode('utf-8'))
|
60 |
+
if single_step['language_instruction_2'].numpy().decode('utf-8') not in inst:
|
61 |
+
inst.append(single_step['language_instruction_2'].numpy().decode('utf-8'))
|
62 |
+
if single_step['language_instruction_3'].numpy().decode('utf-8') not in inst:
|
63 |
+
inst.append(single_step['language_instruction_3'].numpy().decode('utf-8'))
|
64 |
+
if len(inst) == 1 and inst[0] == '':
|
65 |
+
skip_episode = True
|
66 |
+
break
|
67 |
+
left_camera.append(single_step['observation']['exterior_image_1_left'].numpy())
|
68 |
+
right_camera.append(single_step['observation']['exterior_image_2_left'].numpy())
|
69 |
+
arm_camera.append(single_step['observation']['wrist_image_left'].numpy())
|
70 |
+
if skip_episode:
|
71 |
+
print(f'skipping {episode_id}/{len(tf_dataset)}')
|
72 |
+
continue
|
73 |
+
print(f'saving {episode_id}/{len(tf_dataset)}')
|
74 |
+
save_video(f'droid_videos/episode_{episode_id}_left_camera.mp4', left_camera)
|
75 |
+
save_video(f'droid_videos/episode_{episode_id}_right_camera.mp4', right_camera)
|
76 |
+
save_video(f'droid_videos/episode_{episode_id}_arm_camera.mp4', arm_camera)
|
77 |
+
for i in range(len(inst)):
|
78 |
+
if inst[i] == '':
|
79 |
+
continue
|
80 |
+
js_data.append({"path": f'droid_videos/episode_{episode_id}_left_camera.mp4', "recording_folder": recording_folderpath, "cap": [inst[i]]})
|
81 |
+
js_data.append({"path": f'droid_videos/episode_{episode_id}_right_camera.mp4', "recording_folder": recording_folderpath, "cap": [inst[i]]})
|
82 |
+
js_data.append({"path": f'droid_videos/episode_{episode_id}_arm_camera.mp4', "recording_folder": recording_folderpath, "cap": [inst[i]]})
|
83 |
+
if episode_id % 1000 < 10:
|
84 |
+
json.dump(js_data, open(js_path, 'w'), indent=4)
|
85 |
+
json.dump(js_data, open(js_path, 'w'), indent=4)
|