{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"id": "568693f3",
"metadata": {},
"outputs": [],
"source": [
"!pip install -Uqq fastai\n",
"from fastai.vision.all import *"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "f4dc2701",
"metadata": {},
"outputs": [],
"source": [
"path = untar_data(URLs.PETS)/'images'"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "4a0c1feb",
"metadata": {},
"outputs": [],
"source": [
"def is_cat(x): return x[0].isupper() "
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "88a468a4",
"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": 16,
"id": "2473d452",
"metadata": {},
"outputs": [
{
"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.192978 | \n",
" 0.050510 | \n",
" 0.016238 | \n",
" 00:56 | \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.076380 | \n",
" 0.046858 | \n",
" 0.014208 | \n",
" 01:09 | \n",
"
\n",
" \n",
" 1 | \n",
" 0.042434 | \n",
" 0.023065 | \n",
" 0.008796 | \n",
" 01:09 | \n",
"
\n",
" \n",
" 2 | \n",
" 0.020455 | \n",
" 0.021109 | \n",
" 0.008119 | \n",
" 01:09 | \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": 17,
"id": "5bc7c88a",
"metadata": {},
"outputs": [],
"source": [
"learn.export('model.pkl')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8ccd1cf",
"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.2"
},
"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
}