{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Explore Golden Volumes and Select a Recipe\n",
    "\n",
    "In this guide, we will walk through the process of exploring Golden Recipe volumes using the GRDB API and selecting a suitable recipe ([Golden Recipe](https://docs.latentai.io/leip/design/latest/content/explanation/golden_recipes/)) from the Golden DataFrame.\n",
    "\n",
    "## Understanding Golden Recipe Volumes\n",
    "Golden Recipe volumes are collections of recipes that have demonstrated strong performance on specific datasets. Latent AI continuously works to discover better recipes for increasingly diverse datasets, ensuring that you have access to high-performing models.\n",
    "\n",
    "To begin, let's list the available Golden Recipe volumes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Recipe Volumes:\n",
       " - chemistrylab \n",
       " - bdd100k \n",
       " - insects \n",
       " - wheat \n",
       " - carsimple \n",
       " - xval_det "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import leip_recipe_designer as rd\n",
    "volumes = rd.GoldenVolumes()\n",
    "volumes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Note:\n",
    "The code snippet above retrieves Golden Recipe volumes for the **Detector GRDB**. If you're working on **classifiers**, you can adjust the `task` parameter to specify classification tasks. For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Recipe Volumes:\n",
       " - cifar100 \n",
       " - fgvc_aircraft \n",
       " - bdd100k_phase \n",
       " - stanford_cars \n",
       " - eurosat \n",
       " - gtsrb \n",
       " - xval_cls "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Listing Golden Volumes for the classifier GRDB\n",
    "classifier_volumes = rd.GoldenVolumes(task=\"vision.classification.2d\")\n",
    "classifier_volumes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each name in the list corresponds to a Golden Recipe Volume. These collections contain recipes that have excelled on particular datasets. By exploring the datasets used to train each volume, you can identify the one that closely matches your own data.\n",
    "\n",
    "Volumes named **`xval_...`** are cross-validated across diverse datasets and are the recommended starting points.\n",
    "\n",
    "- **`xval_cls`**: A cross-validated volume containing recipes optimized for **image classification** tasks across multiple datasets.\n",
    "- **`xval_det`**: A cross-validated volume containing recipes optimized for **object detection** tasks across multiple datasets.\n",
    "\n",
    "If any of the volumes seem to have data that is very similar to yours, feel free to start with that one. Keep in mind that data similarity is not a well-defined concept, so choose one volume to begin with and iterate as necessary. For more information, consult the guide on [GRDB Model Training and Optimization](https://docs.latentai.io/leip/design/latest/notebooks/HowToGuides/GRDB_Model_Training_and_Optimization/).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Skipped downloading goldenrecipedb with name \"carsimple\" and variant \"Carsimplev0.3\" (0), as it already exists.\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "91ac2babea304201a527e0cb8da88d01",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Tab(children=(HTML(value='<table>\\n<tbody>\\n<tr><td>Description       </td><td>&#x27;Simple and small dataset …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "volumes[\"carsimple\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can click around on the widgets to explore the data, including the training and validation samples, as well as other metadata such as class distribution and resolution details."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exploring the Golden DataFrame\n",
    "\n",
    "Golden Recipe Volumes are structured as [Pandas DataFrames](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html), allowing you to query and sort them based on your specific performance criteria. For example, you can retrieve the DataFrame for a specific volume like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>neptune_id</th>\n",
       "      <th>hardware</th>\n",
       "      <th>quantization</th>\n",
       "      <th>calib_method</th>\n",
       "      <th>per_channel</th>\n",
       "      <th>height</th>\n",
       "      <th>width</th>\n",
       "      <th>model_family</th>\n",
       "      <th>...</th>\n",
       "      <th>od_task_metric</th>\n",
       "      <th>od_inf_rate_ms</th>\n",
       "      <th>od_inf_energy_mJ</th>\n",
       "      <th>od_size_MB</th>\n",
       "      <th>od_gpu_mem_MB</th>\n",
       "      <th>od_cpu_mem_MB</th>\n",
       "      <th>od_unified_mem_MB</th>\n",
       "      <th>od_total_mem_MB</th>\n",
       "      <th>od_avg_cpu_freq_Hz</th>\n",
       "      <th>sppr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>118984</th>\n",
       "      <td>118984</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-102</td>\n",
       "      <td>cuda:A4500</td>\n",
       "      <td>float32</td>\n",
       "      <td>none</td>\n",
       "      <td>none</td>\n",
       "      <td>736.0</td>\n",
       "      <td>416.0</td>\n",
       "      <td>Yolo v5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.428954</td>\n",
       "      <td>1.401320</td>\n",
       "      <td>117.985512</td>\n",
       "      <td>16.544411</td>\n",
       "      <td>243.000000</td>\n",
       "      <td>697.101562</td>\n",
       "      <td>NaN</td>\n",
       "      <td>940.101562</td>\n",
       "      <td>NaN</td>\n",
       "      <td>eJzlvfmX28iRIPy7/wrOjrXq3q/JyhtIvdfjUbf6mm21tS...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>118985</th>\n",
       "      <td>118985</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-102</td>\n",
       "      <td>cuda:A4500</td>\n",
       "      <td>float16</td>\n",
       "      <td>entropy</td>\n",
       "      <td>False</td>\n",
       "      <td>736.0</td>\n",
       "      <td>416.0</td>\n",
       "      <td>Yolo v5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.432355</td>\n",
       "      <td>1.126059</td>\n",
       "      <td>85.763657</td>\n",
       "      <td>13.229023</td>\n",
       "      <td>218.770279</td>\n",
       "      <td>364.319977</td>\n",
       "      <td>NaN</td>\n",
       "      <td>583.090256</td>\n",
       "      <td>NaN</td>\n",
       "      <td>eJzlvfuX3LaRKPx7/oreu9GVfT93D94gdI43K1t+7bUcXU...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>118986</th>\n",
       "      <td>118986</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-102</td>\n",
       "      <td>cuda:A4500</td>\n",
       "      <td>int8</td>\n",
       "      <td>entropy</td>\n",
       "      <td>False</td>\n",
       "      <td>736.0</td>\n",
       "      <td>416.0</td>\n",
       "      <td>Yolo v5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.162237</td>\n",
       "      <td>1.009040</td>\n",
       "      <td>65.303383</td>\n",
       "      <td>11.787714</td>\n",
       "      <td>222.000000</td>\n",
       "      <td>381.723969</td>\n",
       "      <td>NaN</td>\n",
       "      <td>603.723969</td>\n",
       "      <td>NaN</td>\n",
       "      <td>eJzlvfuX3LaRKPx7/oreu9GVfT93D94gdI43K1t+7bUcXU...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>118987</th>\n",
       "      <td>118987</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-102</td>\n",
       "      <td>cuda:AGX-Xavier</td>\n",
       "      <td>float32</td>\n",
       "      <td>none</td>\n",
       "      <td>none</td>\n",
       "      <td>736.0</td>\n",
       "      <td>416.0</td>\n",
       "      <td>Yolo v5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.428954</td>\n",
       "      <td>7.177697</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>21.236946</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1324.970825</td>\n",
       "      <td>1324.970825</td>\n",
       "      <td>NaN</td>\n",
       "      <td>eJzlvfmX28iRIPy7/wrOjrXq3q/JyhtIvdfjUbf6mm21tS...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>118988</th>\n",
       "      <td>118988</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-102</td>\n",
       "      <td>cuda:AGX-Xavier</td>\n",
       "      <td>float16</td>\n",
       "      <td>entropy</td>\n",
       "      <td>False</td>\n",
       "      <td>736.0</td>\n",
       "      <td>416.0</td>\n",
       "      <td>Yolo v5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.432355</td>\n",
       "      <td>4.659135</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>14.740621</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1326.923950</td>\n",
       "      <td>1326.923950</td>\n",
       "      <td>NaN</td>\n",
       "      <td>eJzlvfuX3LaRKPx7/oreu9GVfT93D94gdI43K1t+7bUcXU...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>123024</th>\n",
       "      <td>123024</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-99</td>\n",
       "      <td>rpi4_8gb</td>\n",
       "      <td>int8</td>\n",
       "      <td>percentile</td>\n",
       "      <td>False</td>\n",
       "      <td>512.0</td>\n",
       "      <td>512.0</td>\n",
       "      <td>EfficientDet</td>\n",
       "      <td>...</td>\n",
       "      <td>0.035733</td>\n",
       "      <td>4296.565918</td>\n",
       "      <td>NaN</td>\n",
       "      <td>69.839480</td>\n",
       "      <td>NaN</td>\n",
       "      <td>324.141998</td>\n",
       "      <td>NaN</td>\n",
       "      <td>324.141998</td>\n",
       "      <td>1757.000000</td>\n",
       "      <td>eJztvf2X3LaVIPr7/BWcnXhl70tVgwBBEn1OJitLjuMdy9...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>123025</th>\n",
       "      <td>123025</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-99</td>\n",
       "      <td>i9-10920X</td>\n",
       "      <td>int8</td>\n",
       "      <td>percentile</td>\n",
       "      <td>True</td>\n",
       "      <td>512.0</td>\n",
       "      <td>512.0</td>\n",
       "      <td>EfficientDet</td>\n",
       "      <td>...</td>\n",
       "      <td>0.029460</td>\n",
       "      <td>134.523331</td>\n",
       "      <td>NaN</td>\n",
       "      <td>40.135960</td>\n",
       "      <td>NaN</td>\n",
       "      <td>266.424469</td>\n",
       "      <td>NaN</td>\n",
       "      <td>266.424469</td>\n",
       "      <td>1621.237564</td>\n",
       "      <td>eJztvf2X3LaVIPr7/BWcnXhl70tVgwBBEn1OJitLjuMdy9...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>123026</th>\n",
       "      <td>123026</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-99</td>\n",
       "      <td>rpi4_8gb</td>\n",
       "      <td>int8</td>\n",
       "      <td>percentile</td>\n",
       "      <td>True</td>\n",
       "      <td>512.0</td>\n",
       "      <td>512.0</td>\n",
       "      <td>EfficientDet</td>\n",
       "      <td>...</td>\n",
       "      <td>0.029460</td>\n",
       "      <td>4543.513184</td>\n",
       "      <td>NaN</td>\n",
       "      <td>71.299512</td>\n",
       "      <td>NaN</td>\n",
       "      <td>326.242004</td>\n",
       "      <td>NaN</td>\n",
       "      <td>326.242004</td>\n",
       "      <td>1757.000000</td>\n",
       "      <td>eJztvf2X3LaVIPr7/BWcnXhl70tVgwBBEn1OJitLjuMdy9...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>123027</th>\n",
       "      <td>123027</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-99</td>\n",
       "      <td>i9-10920X</td>\n",
       "      <td>uint</td>\n",
       "      <td>percentile</td>\n",
       "      <td>False</td>\n",
       "      <td>512.0</td>\n",
       "      <td>512.0</td>\n",
       "      <td>EfficientDet</td>\n",
       "      <td>...</td>\n",
       "      <td>0.030770</td>\n",
       "      <td>142.222061</td>\n",
       "      <td>NaN</td>\n",
       "      <td>42.585248</td>\n",
       "      <td>NaN</td>\n",
       "      <td>234.094345</td>\n",
       "      <td>NaN</td>\n",
       "      <td>234.094345</td>\n",
       "      <td>1480.753062</td>\n",
       "      <td>eJztvf2X3LaVIPr7/BWcnXhl70tVgwBBEn1OJitLjuMdy9...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>123028</th>\n",
       "      <td>123028</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-99</td>\n",
       "      <td>rpi4_8gb</td>\n",
       "      <td>uint</td>\n",
       "      <td>percentile</td>\n",
       "      <td>False</td>\n",
       "      <td>512.0</td>\n",
       "      <td>512.0</td>\n",
       "      <td>EfficientDet</td>\n",
       "      <td>...</td>\n",
       "      <td>0.030770</td>\n",
       "      <td>4499.782715</td>\n",
       "      <td>NaN</td>\n",
       "      <td>71.369056</td>\n",
       "      <td>NaN</td>\n",
       "      <td>324.790039</td>\n",
       "      <td>NaN</td>\n",
       "      <td>324.790039</td>\n",
       "      <td>1757.000000</td>\n",
       "      <td>eJztvf2X3LaVIPr7/BWcnXhl70tVgwBBEn1OJitLjuMdy9...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4045 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            id                  timestamp   neptune_id         hardware  \\\n",
       "118984  118984 2024-10-15 09:38:45.048419  GRDBCAR-102       cuda:A4500   \n",
       "118985  118985 2024-10-15 09:38:45.048419  GRDBCAR-102       cuda:A4500   \n",
       "118986  118986 2024-10-15 09:38:45.048419  GRDBCAR-102       cuda:A4500   \n",
       "118987  118987 2024-10-15 09:38:45.048419  GRDBCAR-102  cuda:AGX-Xavier   \n",
       "118988  118988 2024-10-15 09:38:45.048419  GRDBCAR-102  cuda:AGX-Xavier   \n",
       "...        ...                        ...          ...              ...   \n",
       "123024  123024 2024-10-15 09:38:45.048419   GRDBCAR-99         rpi4_8gb   \n",
       "123025  123025 2024-10-15 09:38:45.048419   GRDBCAR-99        i9-10920X   \n",
       "123026  123026 2024-10-15 09:38:45.048419   GRDBCAR-99         rpi4_8gb   \n",
       "123027  123027 2024-10-15 09:38:45.048419   GRDBCAR-99        i9-10920X   \n",
       "123028  123028 2024-10-15 09:38:45.048419   GRDBCAR-99         rpi4_8gb   \n",
       "\n",
       "       quantization calib_method per_channel  height  width  model_family  \\\n",
       "118984      float32         none        none   736.0  416.0       Yolo v5   \n",
       "118985      float16      entropy       False   736.0  416.0       Yolo v5   \n",
       "118986         int8      entropy       False   736.0  416.0       Yolo v5   \n",
       "118987      float32         none        none   736.0  416.0       Yolo v5   \n",
       "118988      float16      entropy       False   736.0  416.0       Yolo v5   \n",
       "...             ...          ...         ...     ...    ...           ...   \n",
       "123024         int8   percentile       False   512.0  512.0  EfficientDet   \n",
       "123025         int8   percentile        True   512.0  512.0  EfficientDet   \n",
       "123026         int8   percentile        True   512.0  512.0  EfficientDet   \n",
       "123027         uint   percentile       False   512.0  512.0  EfficientDet   \n",
       "123028         uint   percentile       False   512.0  512.0  EfficientDet   \n",
       "\n",
       "        ... od_task_metric od_inf_rate_ms od_inf_energy_mJ  od_size_MB  \\\n",
       "118984  ...       0.428954       1.401320       117.985512   16.544411   \n",
       "118985  ...       0.432355       1.126059        85.763657   13.229023   \n",
       "118986  ...       0.162237       1.009040        65.303383   11.787714   \n",
       "118987  ...       0.428954       7.177697         0.000000   21.236946   \n",
       "118988  ...       0.432355       4.659135         0.000000   14.740621   \n",
       "...     ...            ...            ...              ...         ...   \n",
       "123024  ...       0.035733    4296.565918              NaN   69.839480   \n",
       "123025  ...       0.029460     134.523331              NaN   40.135960   \n",
       "123026  ...       0.029460    4543.513184              NaN   71.299512   \n",
       "123027  ...       0.030770     142.222061              NaN   42.585248   \n",
       "123028  ...       0.030770    4499.782715              NaN   71.369056   \n",
       "\n",
       "        od_gpu_mem_MB  od_cpu_mem_MB  od_unified_mem_MB  od_total_mem_MB  \\\n",
       "118984     243.000000     697.101562                NaN       940.101562   \n",
       "118985     218.770279     364.319977                NaN       583.090256   \n",
       "118986     222.000000     381.723969                NaN       603.723969   \n",
       "118987            NaN            NaN        1324.970825      1324.970825   \n",
       "118988            NaN            NaN        1326.923950      1326.923950   \n",
       "...               ...            ...                ...              ...   \n",
       "123024            NaN     324.141998                NaN       324.141998   \n",
       "123025            NaN     266.424469                NaN       266.424469   \n",
       "123026            NaN     326.242004                NaN       326.242004   \n",
       "123027            NaN     234.094345                NaN       234.094345   \n",
       "123028            NaN     324.790039                NaN       324.790039   \n",
       "\n",
       "        od_avg_cpu_freq_Hz                                               sppr  \n",
       "118984                 NaN  eJzlvfmX28iRIPy7/wrOjrXq3q/JyhtIvdfjUbf6mm21tS...  \n",
       "118985                 NaN  eJzlvfuX3LaRKPx7/oreu9GVfT93D94gdI43K1t+7bUcXU...  \n",
       "118986                 NaN  eJzlvfuX3LaRKPx7/oreu9GVfT93D94gdI43K1t+7bUcXU...  \n",
       "118987                 NaN  eJzlvfmX28iRIPy7/wrOjrXq3q/JyhtIvdfjUbf6mm21tS...  \n",
       "118988                 NaN  eJzlvfuX3LaRKPx7/oreu9GVfT93D94gdI43K1t+7bUcXU...  \n",
       "...                    ...                                                ...  \n",
       "123024         1757.000000  eJztvf2X3LaVIPr7/BWcnXhl70tVgwBBEn1OJitLjuMdy9...  \n",
       "123025         1621.237564  eJztvf2X3LaVIPr7/BWcnXhl70tVgwBBEn1OJitLjuMdy9...  \n",
       "123026         1757.000000  eJztvf2X3LaVIPr7/BWcnXhl70tVgwBBEn1OJitLjuMdy9...  \n",
       "123027         1480.753062  eJztvf2X3LaVIPr7/BWcnXhl70tVgwBBEn1OJitLjuMdy9...  \n",
       "123028         1757.000000  eJztvf2X3LaVIPr7/BWcnXhl70tVgwBBEn1OJitLjuMdy9...  \n",
       "\n",
       "[4045 rows x 26 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = volumes[\"carsimple\"].get_golden_df()\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get a detailed overview of the metrics and configuration parameters stored in a Golden Recipe Volume, use the `describe_table()` method. This will display information like model architecture, hardware deployment metrics, memory usage, and inference performance:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n",
      "id                  Index ID for the recipe wrt central db\n",
      "timestamp           Time the recipe was added to the DB.\n",
      "neptune_id          Neptune AI Id of the model trained.\n",
      "hardware            Deployment hardware on which the metrics were calculated.\n",
      "quantization        Quantization method used for On Device metrics.\n",
      "calib_method        Quantization calibration method used for On Device metrics.\n",
      "per_channel         If per-channel quantization was used for On Device metrics.\n",
      "height              Height of the input to the model\n",
      "width               Width of the input to the model\n",
      "model_family        Architecture of the model\n",
      "backbone            Backbone used to extract the features.\n",
      "optimizer           Optimization algorithm used to adjust weights during training\n",
      "scheduler           Learning rate scheduler used to adjust the optimizer learning rate during training\n",
      "if_task_metric      Relative accuracy using the coco average on the validation set, computed Pytorch (not on edge hardware).\n",
      "if_inf_size         Size of the Pytorch model in bytes in (not on edge hardware).\n",
      "if_inf_macs         Number of Multiply-Accumulate Operations during in framework inference (Not on edge hardware)\n",
      "od_task_metric      On Device Mean Average Precision using the coco average of the model on the validation set.\n",
      "od_inf_rate_ms      On Device Average inference rate in ms of the core artifact, excluding pre- and post processing. Note- post-processing for detector models includes nms and can consume substantial time.\n",
      "od_inf_energy_mJ    On Device Energy per inference in mJ measured (Not avaliable for all devices).\n",
      "od_size_MB          On Device size in MB of the compiled model artifact (modelLibrary.so) saved to disk.\n",
      "od_gpu_mem_MB       On Device GPU Memory usage in MB (Not avaliable for all devices).\n",
      "od_cpu_mem_MB       On Device CPU memory usage in MB, i.e. system memory.\n",
      "od_unified_mem_MB   On Device GPU and CPU memory useage in MB for devices with shared CPU/GPU memory (eg. Jetsons). (Not avaliable for all devices)\n",
      "od_total_mem_MB     On Device combined memory usage in MB CPU + GPU. For CPU, only system memory. For GPU, it is GPU memory + System memory. For devices with combined GPU and CPU (eg. Jetsons), it is combined memory.\n",
      "od_avg_cpu_freq_Hz  On Device CPU clock rate in Hz.\n",
      "sppr                Serialized Packaged Portable Recipes (SPPR) ready to train on selected data\n",
      "------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "volumes[\"carsimple\"].describe_table()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Filtering for Suitable Recipes\n",
    "\n",
    "In our scenario, we aim to identify candidate recipes that offer a balance between accuracy and fast inference. To achieve this, we will analyze the multiply-accumulate (MAC) operations during inference."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculate the 75th Percentile of the Task Metric:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "threshold = df[\"if_task_metric\"].quantile(0.75)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Include only those recipes where the task accuracy metric meets or exceeds the calculated threshold and sort the recipes by inference MACs in ascending order:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>neptune_id</th>\n",
       "      <th>hardware</th>\n",
       "      <th>quantization</th>\n",
       "      <th>calib_method</th>\n",
       "      <th>per_channel</th>\n",
       "      <th>height</th>\n",
       "      <th>width</th>\n",
       "      <th>model_family</th>\n",
       "      <th>...</th>\n",
       "      <th>od_task_metric</th>\n",
       "      <th>od_inf_rate_ms</th>\n",
       "      <th>od_inf_energy_mJ</th>\n",
       "      <th>od_size_MB</th>\n",
       "      <th>od_gpu_mem_MB</th>\n",
       "      <th>od_cpu_mem_MB</th>\n",
       "      <th>od_unified_mem_MB</th>\n",
       "      <th>od_total_mem_MB</th>\n",
       "      <th>od_avg_cpu_freq_Hz</th>\n",
       "      <th>sppr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>122834</th>\n",
       "      <td>122834</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-9</td>\n",
       "      <td>cuda:AGX-Orin</td>\n",
       "      <td>int8</td>\n",
       "      <td>entropy</td>\n",
       "      <td>False</td>\n",
       "      <td>320.0</td>\n",
       "      <td>256.0</td>\n",
       "      <td>NanoDet</td>\n",
       "      <td>...</td>\n",
       "      <td>0.647767</td>\n",
       "      <td>2.243725</td>\n",
       "      <td>32.957386</td>\n",
       "      <td>9.641616</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2400.000488</td>\n",
       "      <td>2400.000488</td>\n",
       "      <td>1581.438461</td>\n",
       "      <td>eJzlvWu3G7exIPo9v4JnTjSy55rceDSAhtbyZGTJdnKu5e...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>122826</th>\n",
       "      <td>122826</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-9</td>\n",
       "      <td>cuda:A4500</td>\n",
       "      <td>float32</td>\n",
       "      <td>none</td>\n",
       "      <td>none</td>\n",
       "      <td>320.0</td>\n",
       "      <td>256.0</td>\n",
       "      <td>NanoDet</td>\n",
       "      <td>...</td>\n",
       "      <td>0.641198</td>\n",
       "      <td>1.545569</td>\n",
       "      <td>96.821007</td>\n",
       "      <td>8.764759</td>\n",
       "      <td>373.0</td>\n",
       "      <td>702.741089</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1075.741089</td>\n",
       "      <td>NaN</td>\n",
       "      <td>eJzlvf2X27ixIPr7/St098brmX0jNT4IgPA5s1nPeGaS+8...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>122827</th>\n",
       "      <td>122827</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-9</td>\n",
       "      <td>cuda:A4500</td>\n",
       "      <td>float16</td>\n",
       "      <td>entropy</td>\n",
       "      <td>False</td>\n",
       "      <td>320.0</td>\n",
       "      <td>256.0</td>\n",
       "      <td>NanoDet</td>\n",
       "      <td>...</td>\n",
       "      <td>0.640356</td>\n",
       "      <td>0.760797</td>\n",
       "      <td>64.680267</td>\n",
       "      <td>7.014277</td>\n",
       "      <td>224.0</td>\n",
       "      <td>446.127960</td>\n",
       "      <td>NaN</td>\n",
       "      <td>670.127960</td>\n",
       "      <td>NaN</td>\n",
       "      <td>eJzlvWu3G7exIPo9v4JnTjSy55rceDSAhtbyZGTJdnKu5e...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>122828</th>\n",
       "      <td>122828</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-9</td>\n",
       "      <td>cuda:A4500</td>\n",
       "      <td>int8</td>\n",
       "      <td>entropy</td>\n",
       "      <td>False</td>\n",
       "      <td>320.0</td>\n",
       "      <td>256.0</td>\n",
       "      <td>NanoDet</td>\n",
       "      <td>...</td>\n",
       "      <td>0.647767</td>\n",
       "      <td>0.875939</td>\n",
       "      <td>54.021370</td>\n",
       "      <td>7.835824</td>\n",
       "      <td>213.0</td>\n",
       "      <td>368.084015</td>\n",
       "      <td>NaN</td>\n",
       "      <td>581.084015</td>\n",
       "      <td>NaN</td>\n",
       "      <td>eJzlvWu3G7exIPo9v4JnTjSy55rceDSAhtbyZGTJdnKu5e...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>122829</th>\n",
       "      <td>122829</td>\n",
       "      <td>2024-10-15 09:38:45.048419</td>\n",
       "      <td>GRDBCAR-9</td>\n",
       "      <td>cuda:AGX-Xavier</td>\n",
       "      <td>float32</td>\n",
       "      <td>none</td>\n",
       "      <td>none</td>\n",
       "      <td>320.0</td>\n",
       "      <td>256.0</td>\n",
       "      <td>NanoDet</td>\n",
       "      <td>...</td>\n",
       "      <td>0.641198</td>\n",
       "      <td>5.011565</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>9.152363</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1944.831177</td>\n",
       "      <td>1944.831177</td>\n",
       "      <td>NaN</td>\n",
       "      <td>eJzlvf2X27ixIPr7/St098brmX0jNT4IgPA5s1nPeGaS+8...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            id                  timestamp neptune_id         hardware  \\\n",
       "122834  122834 2024-10-15 09:38:45.048419  GRDBCAR-9    cuda:AGX-Orin   \n",
       "122826  122826 2024-10-15 09:38:45.048419  GRDBCAR-9       cuda:A4500   \n",
       "122827  122827 2024-10-15 09:38:45.048419  GRDBCAR-9       cuda:A4500   \n",
       "122828  122828 2024-10-15 09:38:45.048419  GRDBCAR-9       cuda:A4500   \n",
       "122829  122829 2024-10-15 09:38:45.048419  GRDBCAR-9  cuda:AGX-Xavier   \n",
       "\n",
       "       quantization calib_method per_channel  height  width model_family  ...  \\\n",
       "122834         int8      entropy       False   320.0  256.0      NanoDet  ...   \n",
       "122826      float32         none        none   320.0  256.0      NanoDet  ...   \n",
       "122827      float16      entropy       False   320.0  256.0      NanoDet  ...   \n",
       "122828         int8      entropy       False   320.0  256.0      NanoDet  ...   \n",
       "122829      float32         none        none   320.0  256.0      NanoDet  ...   \n",
       "\n",
       "       od_task_metric od_inf_rate_ms od_inf_energy_mJ  od_size_MB  \\\n",
       "122834       0.647767       2.243725        32.957386    9.641616   \n",
       "122826       0.641198       1.545569        96.821007    8.764759   \n",
       "122827       0.640356       0.760797        64.680267    7.014277   \n",
       "122828       0.647767       0.875939        54.021370    7.835824   \n",
       "122829       0.641198       5.011565         0.000000    9.152363   \n",
       "\n",
       "        od_gpu_mem_MB  od_cpu_mem_MB  od_unified_mem_MB  od_total_mem_MB  \\\n",
       "122834            NaN            NaN        2400.000488      2400.000488   \n",
       "122826          373.0     702.741089                NaN      1075.741089   \n",
       "122827          224.0     446.127960                NaN       670.127960   \n",
       "122828          213.0     368.084015                NaN       581.084015   \n",
       "122829            NaN            NaN        1944.831177      1944.831177   \n",
       "\n",
       "        od_avg_cpu_freq_Hz                                               sppr  \n",
       "122834         1581.438461  eJzlvWu3G7exIPo9v4JnTjSy55rceDSAhtbyZGTJdnKu5e...  \n",
       "122826                 NaN  eJzlvf2X27ixIPr7/St098brmX0jNT4IgPA5s1nPeGaS+8...  \n",
       "122827                 NaN  eJzlvWu3G7exIPo9v4JnTjSy55rceDSAhtbyZGTJdnKu5e...  \n",
       "122828                 NaN  eJzlvWu3G7exIPo9v4JnTjSy55rceDSAhtbyZGTJdnKu5e...  \n",
       "122829                 NaN  eJzlvf2X27ixIPr7/St098brmX0jNT4IgPA5s1nPeGaS+8...  \n",
       "\n",
       "[5 rows x 26 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filtered_df = df[df[\"if_task_metric\"] >= threshold]\n",
    "sorted_df = filtered_df.sort_values(\"if_inf_macs\")\n",
    "sorted_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Selecting a Recipe\n",
    "\n",
    "After filtering and sorting the DataFrame to identify a suitable recipe, the next step is to retrieve the [Serialized Packaged Portable Recipe (SPPR)](https://docs.latentai.io/leip/design/latest/content/reference/recipe_creators/#leip_recipe_designer.create.from_sppr) for your chosen model configuration. For instance, from the table above, let’s select the recipe with ID `122834` tailored for `cuda:AGX-Orin` (Jetson AGX Orin), which employs `int8` quantization and `entropy` calibration."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-10-29 11:53:04,808 | WARNING  | pantry.build-119 | You requested to build a Pantry, but haven't specified the desired execution contexts. Therefore, will use the installed ones ['leip_af', 'leip_forge', 'leip_stub_gen']\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "workspace = Path('./workspace')\n",
    "pantry = rd.Pantry.build(workspace / \"./my_combined_pantry/\", force_rebuild=False)\n",
    "\n",
    "# Select the recipe with the specified ID\n",
    "recipe_id = 122834\n",
    "selected_recipe = sorted_df[sorted_df['id'] == recipe_id].iloc[0]\n",
    "\n",
    "sppr = selected_recipe['sppr']\n",
    "recipe = rd.create.from_sppr(sppr, pantry=pantry, allow_upgrade=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By following these steps, you can effectively explore Golden Volumes, filter for recipes that meet your performance needs, and select a recipe that best fits your dataset. Now, you can replace the data ingredient with your dataset and begin training as shown in the [Getting Started tutorial](https://docs.latentai.io/leip/design/latest/notebooks/GettingStartedwithLEIPDesign/). \n",
    "\n",
    "Happy experimenting with your new model!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "main",
   "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.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
