{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Create Table from Image Folder\n", "\n", "Create a 3LC Table from a folder structure containing images organized by class labels for image classification tasks.\n", "\n", "![img](../images/create-image-classification-table.jpg)\n", "\n", "\n", "\n", "Image folder structures are a common way to organize classification datasets, following the PyTorch ImageFolder convention. This approach is ideal when you have images already sorted into subdirectories by their respective classes.\n", "\n", "We use the `tlc.Table.from_image_folder` method to automatically create a table from a folder structure where each subfolder represents a class. The method reads the folder structure and creates appropriate image and label columns. It assumes a `torchvision.ImageFolder`-like structure:\n", "\n", "```\n", "cats-and-dogs/\n", " ├── cats/\n", " │ ├── cat1.jpg\n", " │ ├── cat2.jpg\n", " │ └── ...\n", " └── dogs/\n", " ├── dog1.jpg\n", " ├── dog2.jpg\n", " └── ...\n", "```\n", "\n", "The resulting table will have columns for images, labels, and an optional hidden weight column that won't be visible in the table rows." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Project setup" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "parameters" ] }, "outputs": [], "source": [ "DATA_PATH = \"../../data\"\n", "PROJECT_NAME = \"3LC Tutorials - Cats & Dogs\"\n", "DATASET_NAME = \"cats-and-dogs\"\n", "TABLE_NAME = \"initial-cls\"\n", "INSTALL_DEPENDENCIES = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install dependencies" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if INSTALL_DEPENDENCIES:\n", " %pip install -q 3lc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "\n", "import tlc" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image_folder = (Path(DATA_PATH) / \"cats-and-dogs\").absolute()\n", "\n", "assert image_folder.exists()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create Table" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "table = tlc.Table.from_image_folder(\n", " image_folder,\n", " table_name=TABLE_NAME,\n", " dataset_name=DATASET_NAME,\n", " project_name=PROJECT_NAME,\n", ")\n", "\n", "print(f\"Created table with {len(table)} rows.\")\n", "\n", "print(\"\\nFirst row (sample view):\")\n", "print(table[0])\n", "\n", "print(\"\\nFirst row (table view):\")\n", "print(table.table_rows[0])" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.3" } }, "nbformat": 4, "nbformat_minor": 2 }