{ "cells": [ { "cell_type": "code", "execution_count": 12, "id": "41971c73-6b80-487e-acdc-836991e62c9d", "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "execution_count": 7, "id": "9a14bf09-3459-4b81-9e2f-f0430591efa2", "metadata": {}, "outputs": [], "source": [ "#|export\n", "from sys import platform\n", "import pathlib\n", "if platform == 'linux':\n", " pathlib.WindowsPath = pathlib.PosixPath" ] }, { "cell_type": "code", "execution_count": 3, "id": "12171d9b-9822-461a-9c16-3925a275e859", "metadata": {}, "outputs": [], "source": [ "#|export\n", "from fastai.vision.all import *\n", "import gradio as gr\n", "\n", "def is_cat(x): return x[0].isupper()" ] }, { "cell_type": "code", "execution_count": 4, "id": "5f678c6f-a7a8-4da2-845c-2c82f056e890", "metadata": {}, "outputs": [], "source": [ "path = untar_data(URLs.PETS)/'images'" ] }, { "cell_type": "code", "execution_count": null, "id": "9a2dd42f-3fae-4bcf-a7a9-932dedb19684", "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": null, "id": "09e8e2ad-30eb-45bc-aa4b-9fe5a256287a", "metadata": {}, "outputs": [], "source": [ "learn = vision_learner(dls, resnet18, metrics=error_rate)\n", "learn.fine_tune(3)" ] }, { "cell_type": "code", "execution_count": null, "id": "b601d058-559a-4246-b4bb-2ad490d826f5", "metadata": {}, "outputs": [], "source": [ "learn.export('model.pkl')" ] }, { "cell_type": "code", "execution_count": 8, "id": "5b01bcd5-5c99-4531-9f93-70dc32238dc4", "metadata": {}, "outputs": [], "source": [ "#|export\n", "learn = load_learner('model.pkl')" ] }, { "cell_type": "code", "execution_count": 5, "id": "77bd55bf-2d8a-4ca0-bf94-ff9078eb124b", "metadata": {}, "outputs": [], "source": [ "#|export\n", "categories = ('Dog', 'Cat')\n", "\n", "def classify(img):\n", " pred,idx,probs = learn.predict(img)\n", " return dict(zip(categories, map(float, probs)))" ] }, { "cell_type": "code", "execution_count": 8, "id": "dc00d8a5-a4fe-42ec-9daa-cce8ab31400b", "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": [ "(, 'http://127.0.0.1:7860/', None)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#|export\n", "image = gr.inputs.Image(shape=(192, 192))\n", "label = gr.outputs.Label()\n", "examples = ['dog.jpg', 'cat.jpg']\n", "\n", "intf = gr.Interface(fn=classify, inputs=image, outputs=label, examples=examples)\n", "intf.launch(inline=False)" ] }, { "cell_type": "code", "execution_count": 9, "id": "7cf96e37-8c63-4494-84c5-d91b9ff96e2b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Export successful\n" ] } ], "source": [ "import nbdev\n", "nbdev.export.nb_export('app.ipynb', 'app')\n", "print('Export successful')" ] }, { "cell_type": "code", "execution_count": null, "id": "1c513736-d2b5-48d4-89da-746a8b0047cf", "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.7.13" } }, "nbformat": 4, "nbformat_minor": 5 }