{
"cells": [
{
"cell_type": "markdown",
"id": "7dc067e7",
"metadata": {},
"source": [
"## Gradio Pets"
]
},
{
"cell_type": "markdown",
"id": "05972144",
"metadata": {},
"source": [
"Since the `learn.fine_tune(3)` took very long time in local machine, running it in Kaggle.\n",
"\n",
"- Hugging Face - Org Source: https://huggingface.co/spaces/prasanthntu/pets/blob/main/train.ipynb\n",
"- Kaggle link: https://www.kaggle.com/prasanth07/fastai-prd-apps-pets-training\n",
"\n",
"Part of `fastai-course22` > Lesson 3: https://github.com/prasanth-ntu/fastai-course22/blob/master/kaggle_version/03-which-image-models-are-best.ipynb"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "11b5a5c3",
"metadata": {
"ExecuteTime": {
"end_time": "2023-12-09T14:15:12.722396Z",
"start_time": "2023-12-09T14:15:10.027227Z"
},
"execution": {
"iopub.execute_input": "2023-06-10T08:54:28.429796Z",
"iopub.status.busy": "2023-06-10T08:54:28.428930Z",
"iopub.status.idle": "2023-06-10T08:54:35.857170Z",
"shell.execute_reply": "2023-06-10T08:54:35.856275Z",
"shell.execute_reply.started": "2023-06-10T08:54:28.429766Z"
}
},
"outputs": [],
"source": [
"from fastai.vision.all import *\n",
"import timm\n",
"import os, sys\n",
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "28b2fa9d",
"metadata": {
"ExecuteTime": {
"end_time": "2023-12-09T14:15:13.046568Z",
"start_time": "2023-12-09T14:15:12.724748Z"
},
"execution": {
"iopub.execute_input": "2023-06-10T08:54:35.865899Z",
"iopub.status.busy": "2023-06-10T08:54:35.862479Z",
"iopub.status.idle": "2023-06-10T08:55:08.285776Z",
"shell.execute_reply": "2023-06-10T08:55:08.284743Z",
"shell.execute_reply.started": "2023-06-10T08:54:35.865862Z"
}
},
"outputs": [],
"source": [
"path = untar_data(URLs.PETS)/'images'\n",
"\n",
"dls = ImageDataLoaders.from_name_func('.',\n",
" get_image_files(path), valid_pct=0.2, seed=42,\n",
" # Regex captures the part of the filename before the underscore followed by decimal values, \n",
" # which typically represents the class of the image (e.g., basset_hound_112.jpg > basset_bound)\n",
" label_func=RegexLabeller(pat = r'^([^/]+)_\\d+'), \n",
" item_tfms=Resize(224))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "472c5e8d",
"metadata": {
"ExecuteTime": {
"end_time": "2023-12-09T14:19:19.309791Z",
"start_time": "2023-12-09T14:19:19.295796Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"path: /Users/prasanth.thangavel/.fastai/data/oxford-iiit-pet/images\n",
"sample files: ['Egyptian_Mau_167.jpg', 'pug_52.jpg', 'basset_hound_112.jpg', 'Siamese_193.jpg', 'shiba_inu_122.jpg']\n",
"No. of unique pet types: 35\n",
"Sample pet types: ['Abyssinian', 'Bengal', 'Birman', 'Bombay', 'British']\n"
]
}
],
"source": [
"print(f\"path: {path}\")\n",
"\n",
"# List the contents of the directory\n",
"files = os.listdir(path)\n",
"print (f\"sample files: {files[:5]}\")\n",
"labels = [file.split('_')[0] for file in files]\n",
"labels_unique = np.unique(labels)\n",
"print (f\"No. of unique pet types: {len(labels_unique)}\")\n",
"print (f\"Sample pet types: {list(labels_unique[:5])}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fefc6186",
"metadata": {
"ExecuteTime": {
"start_time": "2023-12-09T14:19:23.273Z"
},
"execution": {
"iopub.execute_input": "2023-06-10T08:55:08.293077Z",
"iopub.status.busy": "2023-06-10T08:55:08.290699Z",
"iopub.status.idle": "2023-06-10T08:55:10.941056Z",
"shell.execute_reply": "2023-06-10T08:55:10.940257Z",
"shell.execute_reply.started": "2023-06-10T08:55:08.293042Z"
}
},
"outputs": [],
"source": [
"dls.show_batch(max_n=4)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "72c0dfe0",
"metadata": {
"ExecuteTime": {
"end_time": "2023-06-10T07:08:36.740818Z",
"start_time": "2023-06-10T07:02:49.332510Z"
},
"execution": {
"iopub.execute_input": "2023-06-10T08:55:10.943412Z",
"iopub.status.busy": "2023-06-10T08:55:10.942959Z",
"iopub.status.idle": "2023-06-10T08:59:37.724361Z",
"shell.execute_reply": "2023-06-10T08:59:37.723260Z",
"shell.execute_reply.started": "2023-06-10T08:55:10.943379Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/conda/lib/python3.10/site-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.\n",
" warnings.warn(\n",
"/opt/conda/lib/python3.10/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet34_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet34_Weights.DEFAULT` to get the most up-to-date weights.\n",
" warnings.warn(msg)\n",
"Downloading: \"https://download.pytorch.org/models/resnet34-b627a593.pth\" to /root/.cache/torch/hub/checkpoints/resnet34-b627a593.pth\n",
"100%|██████████| 83.3M/83.3M [00:00<00:00, 102MB/s] \n"
]
},
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" epoch | \n",
" train_loss | \n",
" valid_loss | \n",
" error_rate | \n",
" time | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1.472346 | \n",
" 0.341487 | \n",
" 0.108931 | \n",
" 01:08 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" epoch | \n",
" train_loss | \n",
" valid_loss | \n",
" error_rate | \n",
" time | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0.443030 | \n",
" 0.287417 | \n",
" 0.092016 | \n",
" 01:05 | \n",
"
\n",
" \n",
" 1 | \n",
" 0.268207 | \n",
" 0.247478 | \n",
" 0.079838 | \n",
" 01:05 | \n",
"
\n",
" \n",
" 2 | \n",
" 0.127706 | \n",
" 0.217290 | \n",
" 0.066306 | \n",
" 01:05 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"learn = vision_learner(dls, resnet34, metrics=error_rate)\n",
"learn.fine_tune(3)"
]
},
{
"cell_type": "markdown",
"id": "91095764",
"metadata": {},
"source": [
"We could try a better model, based on [this analysis](https://www.kaggle.com/code/jhoward/which-image-models-are-best/). The convnext models work great!"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d20f8a44",
"metadata": {
"ExecuteTime": {
"end_time": "2023-06-10T07:08:36.748372Z",
"start_time": "2023-06-10T07:08:36.743317Z"
},
"execution": {
"iopub.execute_input": "2023-06-10T08:59:37.731453Z",
"iopub.status.busy": "2023-06-10T08:59:37.729116Z",
"iopub.status.idle": "2023-06-10T08:59:37.744568Z",
"shell.execute_reply": "2023-06-10T08:59:37.743668Z",
"shell.execute_reply.started": "2023-06-10T08:59:37.731411Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"['convnext_atto',\n",
" 'convnext_atto_ols',\n",
" 'convnext_base',\n",
" 'convnext_femto',\n",
" 'convnext_femto_ols',\n",
" 'convnext_large',\n",
" 'convnext_large_mlp',\n",
" 'convnext_nano',\n",
" 'convnext_nano_ols',\n",
" 'convnext_pico',\n",
" 'convnext_pico_ols',\n",
" 'convnext_small',\n",
" 'convnext_tiny',\n",
" 'convnext_tiny_hnf',\n",
" 'convnext_xlarge',\n",
" 'convnext_xxlarge',\n",
" 'convnextv2_atto',\n",
" 'convnextv2_base',\n",
" 'convnextv2_femto',\n",
" 'convnextv2_huge',\n",
" 'convnextv2_large',\n",
" 'convnextv2_nano',\n",
" 'convnextv2_pico',\n",
" 'convnextv2_small',\n",
" 'convnextv2_tiny']"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"timm.list_models('convnext*')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "03ba90b1",
"metadata": {
"ExecuteTime": {
"end_time": "2023-06-10T08:41:49.062348Z",
"start_time": "2023-06-10T07:08:36.749543Z"
},
"execution": {
"iopub.execute_input": "2023-06-10T08:59:37.747750Z",
"iopub.status.busy": "2023-06-10T08:59:37.747399Z",
"iopub.status.idle": "2023-06-10T09:04:07.470617Z",
"shell.execute_reply": "2023-06-10T09:04:07.469396Z",
"shell.execute_reply.started": "2023-06-10T08:59:37.747715Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/conda/lib/python3.10/site-packages/timm/models/_factory.py:114: UserWarning: Mapping deprecated model name convnext_tiny_in22k to current convnext_tiny.fb_in22k.\n",
" model = create_fn(\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5ed0d349cdbf43a2b7bfd90b5d8d4a61",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Downloading model.safetensors: 0%| | 0.00/178M [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" epoch | \n",
" train_loss | \n",
" valid_loss | \n",
" error_rate | \n",
" time | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1.130062 | \n",
" 0.274265 | \n",
" 0.083221 | \n",
" 01:04 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" epoch | \n",
" train_loss | \n",
" valid_loss | \n",
" error_rate | \n",
" time | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0.284376 | \n",
" 0.228118 | \n",
" 0.069012 | \n",
" 01:08 | \n",
"
\n",
" \n",
" 1 | \n",
" 0.202384 | \n",
" 0.177204 | \n",
" 0.052774 | \n",
" 01:07 | \n",
"
\n",
" \n",
" 2 | \n",
" 0.132728 | \n",
" 0.159799 | \n",
" 0.050068 | \n",
" 01:07 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"learn = vision_learner(dls, 'convnext_tiny_in22k', metrics=error_rate).to_fp16()\n",
"learn.fine_tune(3)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "8f4285ca",
"metadata": {
"ExecuteTime": {
"end_time": "2023-06-10T08:41:49.084075Z",
"start_time": "2023-06-10T08:41:49.084068Z"
},
"execution": {
"iopub.execute_input": "2023-06-10T09:04:07.477615Z",
"iopub.status.busy": "2023-06-10T09:04:07.475375Z",
"iopub.status.idle": "2023-06-10T09:04:07.993243Z",
"shell.execute_reply": "2023-06-10T09:04:07.992276Z",
"shell.execute_reply.started": "2023-06-10T09:04:07.477573Z"
}
},
"outputs": [],
"source": [
"learn.export('model.pkl')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "7dcf5a26",
"metadata": {
"execution": {
"iopub.execute_input": "2023-06-10T09:09:46.142957Z",
"iopub.status.busy": "2023-06-10T09:09:46.142431Z",
"iopub.status.idle": "2023-06-10T09:09:47.355923Z",
"shell.execute_reply": "2023-06-10T09:09:47.354680Z",
"shell.execute_reply.started": "2023-06-10T09:09:46.142898Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/kaggle/working\n"
]
}
],
"source": [
"!pwd"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d02b1a04",
"metadata": {
"execution": {
"iopub.execute_input": "2023-06-10T09:09:47.622030Z",
"iopub.status.busy": "2023-06-10T09:09:47.621603Z",
"iopub.status.idle": "2023-06-10T09:09:48.832629Z",
"shell.execute_reply": "2023-06-10T09:09:48.831425Z",
"shell.execute_reply.started": "2023-06-10T09:09:47.621991Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"__notebook_source__.ipynb model.pkl\n"
]
}
],
"source": [
"!ls"
]
},
{
"cell_type": "markdown",
"id": "63f2fb53",
"metadata": {
"execution": {
"iopub.execute_input": "2023-06-10T09:13:00.036924Z",
"iopub.status.busy": "2023-06-10T09:13:00.036464Z",
"iopub.status.idle": "2023-06-10T09:13:00.045917Z",
"shell.execute_reply": "2023-06-10T09:13:00.044440Z",
"shell.execute_reply.started": "2023-06-10T09:13:00.036887Z"
}
},
"source": [
" Download model.pkl File \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68065602",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "fastai_related",
"language": "python",
"name": "fastai_related"
},
"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.5"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {
"height": "calc(100% - 180px)",
"left": "10px",
"top": "150px",
"width": "384px"
},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 5
}