{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "bf596b85",
"metadata": {},
"outputs": [],
"source": [
"!pip install -Uqq fastai"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f6529ac6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/Users/devudilip/projects/AI/fastai_pet_classifier\r\n"
]
}
],
"source": [
"!pwd"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "0473c217",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"fastai: 2.8.0\n",
"fastcore: 1.8.0\n",
"torch: 2.4.1\n"
]
}
],
"source": [
"import fastai\n",
"import fastcore\n",
"import torch\n",
"\n",
"print(f\"fastai: {fastai.__version__}\")\n",
"print(f\"fastcore: {fastcore.__version__}\")\n",
"print(f\"torch: {torch.__version__}\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "15b4cdfd",
"metadata": {},
"outputs": [],
"source": [
"from fastai.vision.all import *\n",
"import dill"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "399533a7",
"metadata": {},
"outputs": [],
"source": [
"path = untar_data(URLs.PETS)/'images'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "51e0bbe2",
"metadata": {},
"outputs": [],
"source": [
"def is_cat(x): return x[0].isupper() "
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "915cbf12",
"metadata": {},
"outputs": [],
"source": [
"dls = ImageDataLoaders.from_name_func('.',\n",
" get_image_files(path), valid_pct=0.2, seed=42,\n",
" label_func=is_cat,\n",
" item_tfms=Resize(192))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "9bae55cc",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Downloading: \"https://download.pytorch.org/models/resnet18-f37072fd.pth\" to /Users/devudilip/.cache/torch/hub/checkpoints/resnet18-f37072fd.pth\n",
"100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 44.7M/44.7M [00:09<00:00, 4.89MB/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",
" 0.197639 | \n",
" 0.045730 | \n",
" 0.015562 | \n",
" 01:37 | \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.078331 | \n",
" 0.054597 | \n",
" 0.018268 | \n",
" 02:22 | \n",
"
\n",
" \n",
" 1 | \n",
" 0.049279 | \n",
" 0.038220 | \n",
" 0.010149 | \n",
" 02:40 | \n",
"
\n",
" \n",
" 2 | \n",
" 0.023927 | \n",
" 0.026168 | \n",
" 0.008796 | \n",
" 02:56 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"learn = vision_learner(dls, resnet18, metrics=error_rate)\n",
"learn.fine_tune(3)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "9119ee21",
"metadata": {},
"outputs": [],
"source": [
"learn.export('model.pkl', pickle_module=dill)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25a3e6c4",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.12.9"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}