{ "cells": [ { "cell_type": "markdown", "id": "0e7755b5", "metadata": {}, "source": [ "# Alonet : Training your models\n", "\n", "The `alonet` package rely on the `aloscene`& `alodataset` package to provide training, inference and evaluation scripts for promissing computer vision architecture. DETR, Deformable-Detr and RAFT are one of the first include models on alonet. \n", "\n", "Our training scripts are usually split into three parts:\n", "\n", "- The dataset (provided by `alodataset`)\n", "- The data modules\n", "- The training pipeline\n", "\n", "The provided training pipeline use pytorch lightning. Beyond the complementary use of `aloscene` & `alodataset` we provide some helper methods to quickly restore and load previous training.\n", "\n", "## Datasets\n", "\n", "A dataset returns augmented frame tensors with the `aloscene` package. All datasets provided by `alodataset` expose a `train_loader()` method that will be later use within a data modules.\n", "\n", "Here is an exmple of creating a training & validation loader using the Coco dataset." ] }, { "cell_type": "code", "execution_count": 1, "id": "0885b2bf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loading annotations into memory...\n", "Done (t=10.74s)\n", "creating index...\n", "index created!\n", "loading annotations into memory...\n", "Done (t=0.29s)\n", "creating index...\n", "index created!\n" ] } ], "source": [ "import alodataset\n", "\n", "# Using sample\n", "train_loader = alodataset.CocoBaseDataset(sample=True).train_loader(batch_size=2)\n", "\n", "# Using the full dataset\n", "# Training loader\n", "train_loader = alodataset.CocoBaseDataset(\n", " img_folder = \"train2017\",\n", " ann_file = \"annotations/instances_train2017.json\"\n", ").train_loader(batch_size=2)\n", "\n", "# Validation loader\n", "train_loader = alodataset.CocoBaseDataset(\n", " img_folder = \"val2017\",\n", " ann_file = \"annotations/instances_val2017.json\"\n", ").train_loader(batch_size=2)" ] }, { "cell_type": "markdown", "id": "1838740d", "metadata": {}, "source": [ "## Data modules\n", "\n", "Data modules is a concept introduced in pytorch ligntning. In aloception, we use data modules to adapt one dataset to fit a particular training pipeline. This adaptation can include some change on the expected Frame structure or specific augmentation suited for the target training pipeline.\n", "\n", "Here is an example of data modules\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "53fbe2ac", "metadata": {}, "outputs": [], "source": [ "from argparse import ArgumentParser, Namespace\n", "from typing import Optional\n", "from alodataset import transforms as T \n", "import pytorch_lightning as pl\n", "import alodataset\n", "import alonet\n", "\n", "class CocoDetection2Detr(pl.LightningDataModule):\n", " def __init__(self, batch_size=2):\n", " super().__init__()\n", " self.batch_size = batch_size\n", " \n", " def train_transform(self, frame, same_on_sequence: bool = True, same_on_frames: bool = False):\n", " scales = [480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800]\n", " frame = T.Compose([\n", " T.RandomHorizontalFlip(),\n", " T.RandomResizeWithAspectRatio(scales, max_size=1333),\n", " ]\n", " )(frame)\n", " return frame.norm_resnet()\n", "\n", " def val_transform(self, frame):\n", " frame = T.RandomResizeWithAspectRatio(\n", " [800], max_size=1333, same_on_sequence=same_on_sequence, same_on_frames=same_on_frames\n", " )(frame)\n", " return frame.norm_resnet()\n", "\n", " def setup(self, stage: Optional[str] = None) -> None:\n", " if stage == \"fit\" or stage is None:\n", " # Setup train/val loaders\n", " self.coco_train = alodataset.CocoBaseDataset(\n", " img_folder = \"train2017\",\n", " ann_file = \"annotations/instances_val2017.json\"\n", " )\n", " self.coco_val = alodataset.CocoBaseDataset(\n", " img_folder = \"val2017\",\n", " ann_file = \"annotations/instances_val2017.json\"\n", " )\n", "\n", " def train_dataloader(self):\n", " if not hasattr(self, \"coco_train\"): self.setup()\n", " return self.coco_train.train_loader(batch_size=self.batch_size)\n", "\n", " def val_dataloader(self, sampler=None):\n", " if not hasattr(self, \"coco_val\"): self.setup()\n", " return self.coco_val.train_loader(batch_size=self.batch_size)" ] }, { "cell_type": "markdown", "id": "ed13e84b", "metadata": {}, "source": [ "To know more about data modules, please refer to following pytorch ligntning documentation: \n", "https://pytorch-lightning.readthedocs.io/en/latest/extensions/datamodules.html" ] }, { "cell_type": "markdown", "id": "1951481a", "metadata": {}, "source": [ "## Training pipeline\n", "\n", "Our training pipelines are built using Lightning modules. \n", "\n", "Therefore, trainig is all about connecting **data module** with **training pipeline**." ] }, { "cell_type": "code", "execution_count": null, "id": "90caf43f", "metadata": {}, "outputs": [], "source": [ "import alonet\n", "\n", "# Init the training pipeline\n", "detr = alonet.detr.LitDetr()\n", "# With the Data modules\n", "coco_loader = CocoDetection2Detr(batch_size=2)\n", "# Run the training using the two components\n", "detr.run_train(data_loader=coco_loader, project=\"detr\", expe_name=\"test_experiment\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.11" } }, "nbformat": 4, "nbformat_minor": 5 }