Download dataset.py from vyeevani/robot-test-dataset: direct link, hf CLI and curl.
- Browser
- Download file 8.4 kB
-
https://huggingface.co/datasets/vyeevani/robot-test-dataset/resolve/main/dataset.py
- Command line
-
hf download hf://datasets/vyeevani/robot-test-dataset/dataset.py
-
curl -L -o dataset.py https://huggingface.co/datasets/vyeevani/robot-test-dataset/resolve/main/dataset.py
8.4 kB
| import os | |
| import glob | |
| import re | |
| import tensorflow as tf | |
| import json | |
| import numpy as np | |
| from PIL import Image | |
| def make_json_parser(json_file, image_dir, image_size, greyscale_images=False): | |
| def json_parser(): | |
| with open(json_file, 'r') as file: | |
| data = json.load(file) | |
| for timestep in data['timesteps']: | |
| image_path = os.path.join(image_dir, timestep['image_name']) | |
| image = Image.open(image_path) | |
| if greyscale_images: | |
| image = image.convert('L') | |
| image = np.array(image.resize(image_size)) | |
| if greyscale_images: | |
| image = np.expand_dims(image, axis=0) | |
| image = image.reshape((*image_size, 1)) | |
| current_pose = np.array(timestep['current_pose']['data']) | |
| desired_pose = np.array(timestep['desired_pose']['data']) | |
| yield (image, current_pose, desired_pose) | |
| return json_parser | |
| def make_timestep_dataset(make_json_parser): | |
| return tf.data.Dataset.from_generator( | |
| make_json_parser, | |
| output_signature=(tf.TensorSpec(shape=(None, None, None), dtype=tf.uint8), | |
| tf.TensorSpec(shape=(4, 4), dtype=tf.float32), | |
| tf.TensorSpec(shape=(4, 4), dtype=tf.float32)) | |
| ) | |
| def make_trajectory_dataset(timestep_datasets): | |
| return tf.data.Dataset.from_tensor_slices(timestep_datasets) | |
| def flatten_nested_dataset(dataset): | |
| return dataset.flat_map(lambda x: x) | |
| def get_files(directory): | |
| if not os.path.isdir(directory): | |
| raise ValueError(f"{directory} is not a valid directory.") | |
| search_path = os.path.join(directory, "*.json") | |
| files = glob.glob(search_path) | |
| return files | |
| def make_future_timesteps_dataset(timestep_dataset, future_steps, lookahead_fields): | |
| def lookahead(*args): | |
| return [field[0] if i not in lookahead_fields else field for (i, field) in enumerate(args)] | |
| return timestep_dataset \ | |
| .window(future_steps, shift=1, stride=1, drop_remainder=True) \ | |
| .flat_map(lambda *args: tf.data.Dataset.zip(args)) \ | |
| .batch(future_steps) \ | |
| .map(lookahead) | |
| import unittest | |
| class TestDataset(unittest.TestCase): | |
| def create_synthetic_timestep_dataset(size, image_size): | |
| values = [] | |
| for i in range(size): | |
| image = np.random.rand(*image_size, 3) | |
| current_pose = np.random.rand(4, 4) | |
| desired_pose = np.random.rand(4, 4) | |
| values.append((image, current_pose, desired_pose)) | |
| def generator(): | |
| for value in values: | |
| yield value | |
| return tf.data.Dataset.from_generator( | |
| generator, | |
| output_signature=( | |
| tf.TensorSpec(shape=(None, None, 3), dtype=tf.uint8), | |
| tf.TensorSpec(shape=(4, 4), dtype=tf.float32), | |
| tf.TensorSpec(shape=(4, 4), dtype=tf.float32) | |
| ) | |
| ) | |
| def get_test_dir(cls): | |
| current_file_path = os.path.realpath(__file__) | |
| base_dir = os.path.dirname(current_file_path) | |
| return os.path.join(base_dir, "test_data") | |
| def get_trajectory_files(cls, data_dir): | |
| data_dir = os.path.join(data_dir, "trajectories") | |
| json_files = get_files(data_dir) | |
| return json_files | |
| def get_test_trajectory_files(cls): | |
| return cls.get_trajectory_files(cls.get_test_dir()) | |
| def get_image_dir(cls, data_dir): | |
| return os.path.join(data_dir, "images") | |
| def get_test_image_dir(cls): | |
| return cls.get_image_dir(cls.get_test_dir()) | |
| def test_greyscale_image_generation(self): | |
| json_files = self.get_test_trajectory_files() | |
| self.assertTrue(len(json_files) > 0, "There should be at least one trajectory file") | |
| trajectory_dataset = flatten_nested_dataset( | |
| make_trajectory_dataset([ | |
| make_timestep_dataset(make_json_parser(json_file, self.get_test_image_dir(), (100, 100), greyscale_images=True)) | |
| for json_file in json_files | |
| ]) | |
| ) | |
| for image, _, _ in trajectory_dataset: | |
| self.assertEqual(image.shape[-1], 1, f"Image should be greyscale. found shape {image.shape}") | |
| def check_timesteps_shape(self, timestep_dataset): | |
| count = 0 | |
| for image, current_pose, desired_pose in timestep_dataset: | |
| count += 1 | |
| self.assertEqual(image.shape, (100, 100, 3), "Image should have shape (100, 100, 3)") | |
| self.assertEqual(current_pose.shape, (4, 4), "Current pose should have shape (4, 4)") | |
| self.assertEqual(desired_pose.shape, (4, 4), "Desired pose should have shape (4, 4)") | |
| self.assertGreater(count, 0) | |
| def check_lookahead_timestep_shape(self, lookahead_count, timestep_dataset): | |
| count = 0 | |
| for image, current_pose, desired_pose in timestep_dataset: | |
| count += 1 | |
| self.assertEqual(image.shape, (100, 100, 3), "Image should have shape (100, 100, 3)") | |
| self.assertEqual(current_pose.shape, (4, 4), "Current pose should have shape (4, 4)") | |
| self.assertEqual(desired_pose.shape, (lookahead_count, 4, 4), "Desired pose should have shape (4, 4)") | |
| self.assertGreater(count, 0) | |
| def test_json_dataset(self): | |
| json_files = self.get_test_trajectory_files() | |
| self.assertTrue(len(json_files) > 0, "There should be at least one trajectory file") | |
| trajectory_dataset = flatten_nested_dataset(make_trajectory_dataset([make_timestep_dataset(make_json_parser(json_file, self.get_test_image_dir(), (100, 100))) for json_file in json_files])) | |
| self.check_timesteps_shape(trajectory_dataset) | |
| def test_synthetic_timestep_dataset(self): | |
| synthetic_dataset = self.create_synthetic_timestep_dataset(5, (100, 100)) | |
| self.check_timesteps_shape(synthetic_dataset) | |
| def test_make_future_trajectory_dataset(self): | |
| single_timestep_dataset = self.create_synthetic_timestep_dataset(5, (100, 100)) | |
| single_timestep_data = [] | |
| for data in single_timestep_dataset: | |
| single_timestep_data.append(data) | |
| future_steps = 2 | |
| future_dataset = make_future_timesteps_dataset(single_timestep_dataset, future_steps=future_steps, lookahead_fields=[2]) | |
| loop_count = 0 | |
| self.check_lookahead_timestep_shape(future_steps, future_dataset) | |
| for i, (future_image, future_current_pose, future_desired_poses) in enumerate(future_dataset): | |
| image = single_timestep_data[i][0] | |
| current_pose = single_timestep_data[i][1] | |
| desired_poses = np.array([single_timestep_data[i + j][2] for j in range(future_steps)]) | |
| self.assertTrue(np.array_equal(future_image, image)) | |
| self.assertTrue(np.array_equal(future_current_pose, current_pose)) | |
| self.assertTrue(np.array_equal(future_desired_poses, desired_poses)) | |
| loop_count += 1 | |
| self.assertEqual(loop_count, len(single_timestep_data) - future_steps + 1) | |
| def test_generate_trajectory_unflattened_dataset(self): | |
| single_timestep_datasets = [self.create_synthetic_timestep_dataset(5, (100, 100)) for _ in range(5)] | |
| future_steps = 2 | |
| future_datasets = [make_future_timesteps_dataset(single_timestep_dataset, future_steps=future_steps, lookahead_fields=[2]) for single_timestep_dataset in single_timestep_datasets] | |
| trajectory_dataset = make_trajectory_dataset(future_datasets) | |
| for timesteps in trajectory_dataset: | |
| self.check_lookahead_timestep_shape(future_steps, timesteps) | |
| def test_generate_trajectory_flattened_dataset(self): | |
| single_timestep_datasets = [self.create_synthetic_timestep_dataset(5, (100, 100)) for _ in range(5)] | |
| future_steps = 2 | |
| future_datasets = [make_future_timesteps_dataset(single_timestep_dataset, future_steps=future_steps, lookahead_fields=[2]) for single_timestep_dataset in single_timestep_datasets] | |
| trajectory_dataset = make_trajectory_dataset(future_datasets) | |
| flattened_dataset = flatten_nested_dataset(trajectory_dataset) | |
| self.check_lookahead_timestep_shape(future_steps, flattened_dataset) | |
| if __name__ == '__main__': | |
| unittest.main() |