{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "18acb717",
"metadata": {},
"outputs": [],
"source": [
"#|default_exp app"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "44eb0ad3",
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"from fastai.vision.all import *\n",
"import gradio as gr\n",
"\n",
"def is_classical(x): return x[0].isupper()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d838c0b3",
"metadata": {},
"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",
" label_func=is_classical,\n",
" item_tfms=Resize(192, method='squish'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c107f724",
"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.209574 | \n",
" 0.081121 | \n",
" 0.022327 | \n",
" 00:24 | \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.090262 | \n",
" 0.056602 | \n",
" 0.017591 | \n",
" 00:23 | \n",
"
\n",
" \n",
" 1 | \n",
" 0.035389 | \n",
" 0.037754 | \n",
" 0.014208 | \n",
" 00:22 | \n",
"
\n",
" \n",
" 2 | \n",
" 0.013607 | \n",
" 0.038817 | \n",
" 0.012179 | \n",
" 00:22 | \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": null,
"id": "5171c7fc",
"metadata": {},
"outputs": [],
"source": [
"learn.export('interior.pkl')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ae2bc6ac",
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"learn = load_learner('interior.pkl')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e0bf9da",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"('False', TensorBase(0), TensorBase([9.9999e-01, 8.4523e-06]))"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"learn.predict(im)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0419ed3a",
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"categories = ('classical','japandi','minimal','poho','earthy')\n",
"\n",
"def classify_image(img):\n",
" pred,idx,probs = learn.predict(img)\n",
" return dict(zip(categories, map(float,probs)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "762dec00",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"{'Dog': 0.9999915361404419, 'Cat': 8.452258043689653e-06}"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"classify_image(im)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0518a30a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7860/\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/plain": [
"(,\n",
" 'http://127.0.0.1:7860/',\n",
" None)"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#|export\n",
"image = gr.inputs.Image(shape=(192, 192))\n",
"label = gr.outputs.Label()\n",
"examples = ['classical.jpg','japandi.jpg','minimal.jpg','poho.jpg','earthy.jpg']\n",
"\n",
"intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n",
"intf.launch(inline=False)"
]
},
{
"cell_type": "markdown",
"id": "0d1e90ce",
"metadata": {},
"source": [
"## end -"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "82774c08",
"metadata": {},
"outputs": [],
"source": [
"from nbdev.export import notebook2script"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a880da1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Converted app.ipynb.\n"
]
}
],
"source": [
"notebook2script('app.ipynb')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1a349335",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}