File size: 6,848 Bytes
011fbb6 ab6871c 011fbb6 ab6871c | 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 | {
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Ego-1K Quickstart\n",
"\n",
"This notebook demonstrates how to load and explore the [Ego-1K](https://huggingface.co/datasets/facebook/ego-1k) dataset — a large-scale, time-synchronized collection of egocentric multiview videos with 12 cameras, 956 recordings, and ~491K frames.\n",
"\n",
"**What you'll learn:**\n",
"1. Load frame-level metadata (poses, calibration, scene info) from Parquet\n",
"2. Parse rig calibration and device pose\n",
"3. Stream images from WebDataset tar shards\n",
"4. Visualize a multiview frame (12 cameras)\n",
"\n",
"**Prerequisites:**\n",
"```bash\n",
"pip install datasets webdataset torch numpy Pillow huggingface_hub matplotlib\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import io\n",
"import json\n",
"import os\n",
"import logging\n",
"\n",
"import matplotlib.pyplot as plt\n",
"\n",
"import numpy as np\n",
"import torch\n",
"import webdataset as wds\n",
"from PIL import Image\n",
"\n",
"from datasets import load_dataset\n",
"from huggingface_hub import HfApi\n",
"\n",
"HF_REPO_ID = \"facebook/ego-1k\"\n",
"CAMERAS = [f\"200-{i}\" for i in range(1, 13)] # 12 cameras"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"HF_TOKEN = os.environ.get(\"HF_TOKEN\")\n",
"api = HfApi(token=HF_TOKEN)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load frame-level metadata from Parquet (no images downloaded)\n",
"ds = load_dataset(HF_REPO_ID, split=\"test\", token=HF_TOKEN)\n",
"print(f\"{len(ds)} frames, columns: {ds.column_names}\")\n",
"\n",
"example = ds[0]\n",
"print(f\"Example: scene={example['scene_id']}, frame={example['frame_id']}, \"\n",
" f\"source={example['source']}, lux={example['lux_bins']}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Parse rig calibration and device pose from one example\n",
"rig = json.loads(example[\"rig_calibration\"])\n",
"pose = json.loads(example[\"pose\"]) if example[\"pose\"] else None\n",
"\n",
"K = np.array(rig[\"200-1\"][\"K\"])\n",
"E = np.array(rig[\"200-1\"][\"E\"])\n",
"print(f\"Cameras: {sorted(rig.keys())}\")\n",
"print(f\"Intrinsics K: {K.shape}, Extrinsics E: {E.shape}\")\n",
"if pose is not None:\n",
" print(f\"Device pose: {np.array(pose).shape}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def decode_wds_sample(sample, cameras):\n",
" \"\"\"Decode a WebDataset sample into images, intrinsics, extrinsics, and pose.\"\"\"\n",
" meta = json.loads(sample[\"metadata.json\"])\n",
" rig = meta[\"rig_calibration\"]\n",
"\n",
" images, intrinsics, extrinsics = [], [], []\n",
" for cam in cameras:\n",
" img = Image.open(io.BytesIO(sample[f\"{cam}.png\"])).convert(\"RGB\")\n",
" img_tensor = torch.from_numpy(np.array(img)).permute(2, 0, 1).float() / 255.0\n",
" images.append(img_tensor)\n",
" intrinsics.append(torch.tensor(rig[cam][\"K\"], dtype=torch.float32))\n",
" extrinsics.append(torch.tensor(rig[cam][\"E\"], dtype=torch.float32))\n",
"\n",
" pose_raw = meta.get(\"pose\")\n",
" pose = (\n",
" torch.tensor(pose_raw, dtype=torch.float32)\n",
" if pose_raw is not None\n",
" else torch.zeros(4, 4)\n",
" )\n",
"\n",
" return {\n",
" \"images\": torch.stack(images),\n",
" \"intrinsics\": torch.stack(intrinsics),\n",
" \"extrinsics\": torch.stack(extrinsics),\n",
" \"pose\": pose,\n",
" \"scene_id\": meta[\"scene_id\"],\n",
" \"frame_id\": meta[\"frame_id\"],\n",
" }\n",
"\n",
"\n",
"# Discover tar shards for the test split\n",
"all_files = api.list_repo_files(repo_id=HF_REPO_ID, repo_type=\"dataset\")\n",
"shard_files = sorted(\n",
" f for f in all_files if f.startswith(\"shards/test/\") and f.endswith(\".tar\")\n",
")\n",
"\n",
"# Stream one frame from the first shard (each shard is ~1.5 GB)\n",
"shard = shard_files[0]\n",
"url = f\"https://huggingface.co/datasets/{HF_REPO_ID}/resolve/main/{shard}\"\n",
"url = f\"pipe:curl -s -L {url} -H 'Authorization:Bearer {HF_TOKEN}'\"\n",
"\n",
"pipeline = wds.WebDataset(\n",
" url, nodesplitter=wds.split_by_node, shardshuffle=False\n",
").map(lambda s: decode_wds_sample(s, CAMERAS))\n",
"\n",
"sample = next(iter(pipeline))\n",
"print(f\"scene={sample['scene_id']}, frame={sample['frame_id']}, \"\n",
" f\"images={sample['images'].shape}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Visualize all 12 camera views from the streamed frame\n",
"fig, axes = plt.subplots(2, 6, figsize=(24, 8))\n",
"fig.suptitle(\n",
" f\"Ego-1K — Scene {sample['scene_id']}, Frame {sample['frame_id']}\",\n",
" fontsize=14,\n",
")\n",
"\n",
"for idx, ax in enumerate(axes.flat):\n",
" # Convert from (C, H, W) float tensor to (H, W, C) numpy for display\n",
" img = sample[\"images\"][idx].permute(1, 2, 0).numpy()\n",
" ax.imshow(img)\n",
" ax.set_title(CAMERAS[idx], fontsize=10)\n",
" ax.axis(\"off\")\n",
"\n",
"plt.tight_layout()\n",
"plt.show()"
]
}
],
"metadata": {
"fileHeader": "",
"fileUid": "8662119b-f87a-4fee-a46f-a39f70aa16b7",
"isAdHoc": false,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|