{ "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 }