{ "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
epochtrain_lossvalid_losserror_ratetime
00.2095740.0811210.02232700:24
" ], "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
epochtrain_lossvalid_losserror_ratetime
00.0902620.0566020.01759100:23
10.0353890.0377540.01420800:22
20.0136070.0388170.01217900:22
" ], "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('model.pkl')" ] }, { "cell_type": "code", "execution_count": null, "id": "ae2bc6ac", "metadata": {}, "outputs": [], "source": [ "#|export\n", "learn = load_learner('model.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 }