-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update directory structure + add preprocessing for BAIR dataset
- Loading branch information
1 parent
a693f20
commit 575b75e
Showing
6 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import os | ||
import io | ||
|
||
import numpy as np | ||
from PIL import Image | ||
import tensorflow as tf | ||
|
||
from tensorflow.python.platform import flags | ||
from tensorflow.python.platform import gfile | ||
|
||
import imageio | ||
|
||
import argparse | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--data_dir', default='', help='base directory to save processed data') | ||
opt = parser.parse_args() | ||
|
||
def get_seq(dname): | ||
data_dir = '%s/softmotion30_44k/%s' % (opt.data_dir, dname) | ||
|
||
filenames = gfile.Glob(os.path.join(data_dir, '*')) | ||
if not filenames: | ||
raise RuntimeError('No data files found.') | ||
|
||
for f in filenames: | ||
k=0 | ||
for serialized_example in tf.python_io.tf_record_iterator(f): | ||
example = tf.train.Example() | ||
example.ParseFromString(serialized_example) | ||
image_seq, action_seq = [], [] | ||
for i in range(30): | ||
image_name = str(i) + '/image_aux1/encoded' | ||
byte_str = example.features.feature[image_name].bytes_list.value[0] | ||
img = Image.frombytes('RGB', (64, 64), byte_str) | ||
arr = np.array(img.getdata()).reshape(img.size[1], img.size[0], 3) | ||
image_seq.append(arr.reshape(1, 64, 64, 3)) | ||
|
||
action_name = str(i) + '/action' | ||
action = example.features.feature[action_name].float_list.value | ||
action = np.array(action).astype('float32') | ||
action_seq.append(action) | ||
image_seq = np.concatenate(image_seq, axis=0) | ||
action_seq = np.stack(action_seq, axis=0) | ||
k=k+1 | ||
yield f, k, image_seq, action_seq | ||
|
||
def convert_data(dname): | ||
seq_generator = get_seq(dname) | ||
n = 0 | ||
while True: | ||
n+=1 | ||
try: | ||
f, k, seq, actions = next(seq_generator) | ||
seq = seq.astype('uint8') | ||
except StopIteration: | ||
break | ||
f = f.split('/')[-1] | ||
os.makedirs('%s/processed_data/%s/%s/%d/' % (opt.data_dir, dname, f[:-10], k), exist_ok=True) | ||
for i in range(len(seq)): | ||
imageio.imwrite('%s/processed_data/%s/%s/%d/%d.png' % (opt.data_dir, dname, f[:-10], k, i), seq[i]) | ||
np.save('%s/processed_data/%s/%s/%d/actions.npy' % (opt.data_dir, dname, f[:-10], k), actions) | ||
|
||
print('%s data: %s (%d) (%d)' % (dname, f, k, n)) | ||
|
||
convert_data('test') | ||
convert_data('train') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import glob | ||
import argparse | ||
import h5py | ||
import numpy as np | ||
from PIL import Image | ||
import os | ||
import os.path as osp | ||
from tqdm import tqdm | ||
import sys | ||
|
||
def convert_data(f, split): | ||
root_dir = args.data_dir | ||
path = osp.join(root_dir, 'processed_data', split) | ||
traj_paths = glob.glob(osp.join(path, '*', '*')) | ||
trajs, actions = [], [] | ||
for traj_path in tqdm(traj_paths): | ||
image_paths = glob.glob(osp.join(traj_path, '*.png')) | ||
image_paths.sort(key=lambda x: int(osp.splitext(osp.basename(x))[0])) | ||
traj = [] | ||
for img_path in image_paths: | ||
img = Image.open(img_path) | ||
arr = np.array(img) # HWC | ||
traj.append(arr) | ||
traj = np.stack(traj, axis=0) # THWC | ||
trajs.append(traj) | ||
|
||
actions.append(np.load(osp.join(traj_path, 'actions.npy'))) | ||
|
||
idxs = np.arange(len(trajs)) * 30 | ||
trajs = np.concatenate(trajs, axis=0) # (NT)HWC | ||
actions = np.concatenate(actions, axis=0) # (NT)(act_dim) | ||
|
||
f.create_dataset(f'{split}_data', data=trajs) | ||
f.create_dataset(f'{split}_actions', data=actions) | ||
f.create_dataset(f'{split}_idx', data=idxs) | ||
|
||
print(split) | ||
print(f'\timages: {f[f"{split}_data"].shape}, {f[f"{split}_data"].dtype}') | ||
print(f'\timages: {f[f"{split}_idx"].shape}, {f[f"{split}_idx"].dtype}') | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--data_dir', type=str, required=True) | ||
parser.add_argument('--output_dir', type=str, required=True) | ||
args = parser.parse_args() | ||
|
||
os.makedirs(args.output_dir, exist_ok=True) | ||
f = h5py.File(osp.join(args.output_dir, 'bair.hdf5'), 'a') | ||
convert_data(f, 'train') | ||
convert_data(f, 'test') | ||
f.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#! /bin/sh | ||
|
||
mkdir -p ${1} | ||
mkdir -p ~/.cache/bair | ||
wget -P ~/.cache/bair/ http://rail.eecs.berkeley.edu/datasets/bair_robot_pushing_dataset_v0.tar | ||
tar -xf ~/.cache/bair/bair_robot_pushing_dataset_v0.tar -C ~/.cache/bair/ | ||
|
||
python scripts/preprocess/bair/bair_extract_images.py --data_dir ~/.cache/bair | ||
python scripts/preprocess/bair/bair_image_to_hdf5.py --data_dir ~/.cache/bair --output_dir ${1} | ||
|
||
rm -r ~/.cache/bair |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.