| 21 | ) |
| 22 | |
| 23 | class EvalDataset(object): |
| 24 | |
| 25 | # subset of dataset to eval |
| 26 | DATASET_DIR = { |
| 27 | 'general': '{}/general', |
| 28 | 'google_apps': '{}/google_apps', |
| 29 | 'install': '{}/install', |
| 30 | 'single': '{}/single', |
| 31 | 'web_shopping': '{}/web_shopping', |
| 32 | 'domestic': '{}/domestic', |
| 33 | 'odyssey': '{}/odyssey', |
| 34 | 'android_control': '{}/android_control', |
| 35 | } |
| 36 | |
| 37 | def __init__(self, data_dir, split="test", ratio=1.0) -> None: |
| 38 | self.ratio = ratio |
| 39 | self.data_dir = os.path.join(data_dir, split) |
| 40 | self.episode_data = self._load_data_() |
| 41 | self.data = self._split_to_steps_(self.episode_data) |
| 42 | |
| 43 | def _load_data_(self): |
| 44 | valid_paths = defaultdict(list) |
| 45 | for subset in self.DATASET_DIR: |
| 46 | subdata_dir = self.DATASET_DIR[subset].format(self.data_dir) |
| 47 | if os.path.exists(subdata_dir): |
| 48 | sequence_names = os.listdir(subdata_dir) |
| 49 | for seq_name in sequence_names: |
| 50 | seq_dir = os.path.join(subdata_dir, seq_name) |
| 51 | if not os.path.isdir(seq_dir): continue |
| 52 | episode_path = os.path.join(seq_dir, f"{seq_name}.json") |
| 53 | valid_paths[subset].append(episode_path) |
| 54 | |
| 55 | sampled_paths = [] |
| 56 | for subset, v_paths in valid_paths.items(): |
| 57 | N = len(v_paths) |
| 58 | k = int(self.ratio * N) |
| 59 | sampled_paths += random.sample(v_paths, k) if self.ratio < 1.0 else v_paths |
| 60 | |
| 61 | ep_data = [] |
| 62 | for episode_path in sampled_paths: |
| 63 | try: |
| 64 | with open(episode_path, "r") as f: |
| 65 | episode_data = json.load(f) |
| 66 | ep_data.append(episode_data) |
| 67 | except json.JSONDecodeError as e: |
| 68 | logging.error(f"JSON decoding failed, file: {episode_path}, error: {e}") |
| 69 | except Exception as e: |
| 70 | logging.error(f"Error occurred when loading file {episode_path}: {e}") |
| 71 | return ep_data |
| 72 | |
| 73 | def _split_to_steps_(self, episode_data): |
| 74 | data = [] |
| 75 | for edx, episode in enumerate(episode_data): |
| 76 | for idx, step in enumerate(episode): |
| 77 | try: |
| 78 | if step.get('subset') is None: |
| 79 | step['subset'] = step['image_path'].split('/')[0] |
| 80 | step['image_full_path'] = os.path.join(self.data_dir, step['image_path']) |