{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "311970df-d109-452d-a843-c31048daf6e3", "metadata": {}, "outputs": [], "source": [ "from fastai.vision.all import *\n", "path = untar_data(URLs.PETS)\n", "dls = ImageDataLoaders.from_name_re(path, get_image_files(path/'images'), pat='(.+)_\\d+.jpg', item_tfms=Resize(460), batch_tfms=aug_transforms(size=224, min_scale=0.75), bs=128)\n", "learn = cnn_learner(dls, models.resnet50, metrics=accuracy)\n", "learn.fine_tune(5)\n", "learn.path = Path('.')\n", "learn.export()" ] }, { "cell_type": "code", "execution_count": 4, "id": "ef4ffc95-6051-4354-af16-25477b279657", "metadata": {}, "outputs": [], "source": [ "from fastai.vision.all import *\n", "learn = load_learner('export.pkl')" ] }, { "cell_type": "code", "execution_count": 5, "id": "49289d4b-7e8c-4264-bb03-8a0d851caf1c", "metadata": {}, "outputs": [], "source": [ "labels = learn.dls.vocab\n", "def predict(img):\n", " img = PILImage.create(img)\n", " pred,pred_idx,probs = learn.predict(img)\n", " return {labels[i]: float(probs[i]) for i in range(len(labels))}" ] }, { "cell_type": "code", "execution_count": 18, "id": "a6b53fe8-ded5-4048-afd7-e488dc884aec", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Bombay_19.jpg\n", "american_pit_bull_terrier_52.jpg\n", "staffordshire_bull_terrier_129.jpg\n", "wheaten_terrier_138.jpg\n", "Egyptian_Mau_57.jpg\n", "chihuahua_93.jpg\n", "yorkshire_terrier_189.jpg\n", "Abyssinian_67.jpg\n", "Egyptian_Mau_63.jpg\n", "basset_hound_12.jpg\n", "american_bulldog_24.jpg\n", "Bengal_109.jpg\n", "British_Shorthair_57.jpg\n", "beagle_120.jpg\n", "staffordshire_bull_terrier_173.jpg\n", "beagle_125.jpg\n", "Birman_113.jpg\n", "Bengal_21.jpg\n", "British_Shorthair_61.jpg\n", "Bombay_25.jpg\n", "basset_hound_17.jpg\n", "Abyssinian_29.jpg\n", "Abyssinian_7.jpg\n", "Bengal_108.jpg\n", "Abyssinian_27.jpg\n", "american_bulldog_83.jpg\n", "Birman_103.jpg\n", "chihuahua_94.jpg\n", "Bengal_19.jpg\n", "american_pit_bull_terrier_76.jpg\n", "yorkshire_terrier_196.jpg\n", "wheaten_terrier_137.jpg\n", "Birman_120.jpg\n" ] } ], "source": [ "import os\n", "for root, dirs, files in os.walk(r'sample_images/'):\n", " for filename in files:\n", " print(filename)" ] }, { "cell_type": "code", "execution_count": 16, "id": "62fe5dc0-5fd1-4cc7-af8d-a325e3915173", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7864/\n", "Running on public URL: https://45906.gradio.app\n", "\n", "This share link will expire in 72 hours. To get longer links, send an email to: support@gradio.app\n" ] }, { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "(,\n", " 'http://127.0.0.1:7864/',\n", " 'https://45906.gradio.app')" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" }, { "name": "stderr", "output_type": "stream", "text": [ "[2021-11-19 20:18:31,342] ERROR in app: Exception on /file/sample_images/staffordshire_bull_terrier_172.jpg [GET]\n", "Traceback (most recent call last):\n", " File \"/home/dnth/anaconda3/envs/gradio/lib/python3.8/site-packages/flask/app.py\", line 2073, in wsgi_app\n", " response = self.full_dispatch_request()\n", " File \"/home/dnth/anaconda3/envs/gradio/lib/python3.8/site-packages/flask/app.py\", line 1518, in full_dispatch_request\n", " rv = self.handle_user_exception(e)\n", " File \"/home/dnth/anaconda3/envs/gradio/lib/python3.8/site-packages/flask_cors/extension.py\", line 165, in wrapped_function\n", " return cors_after_request(app.make_response(f(*args, **kwargs)))\n", " File \"/home/dnth/anaconda3/envs/gradio/lib/python3.8/site-packages/flask/app.py\", line 1516, in full_dispatch_request\n", " rv = self.dispatch_request()\n", " File \"/home/dnth/anaconda3/envs/gradio/lib/python3.8/site-packages/flask/app.py\", line 1502, in dispatch_request\n", " return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)\n", " File \"/home/dnth/anaconda3/envs/gradio/lib/python3.8/site-packages/gradio/networking.py\", line 93, in wrapper\n", " return func(*args, **kwargs)\n", " File \"/home/dnth/anaconda3/envs/gradio/lib/python3.8/site-packages/gradio/networking.py\", line 386, in file\n", " return send_file(os.path.join(app.cwd, path))\n", " File \"/home/dnth/anaconda3/envs/gradio/lib/python3.8/site-packages/flask/helpers.py\", line 612, in send_file\n", " return werkzeug.utils.send_file(\n", " File \"/home/dnth/anaconda3/envs/gradio/lib/python3.8/site-packages/werkzeug/utils.py\", line 701, in send_file\n", " stat = os.stat(path)\n", "FileNotFoundError: [Errno 2] No such file or directory: '/home/dnth/Desktop/webdemos/webdemo-pets-classifier/sample_images/staffordshire_bull_terrier_172.jpg'\n" ] } ], "source": [ "import gradio as gr\n", "\n", "title = \"Pet Breed Classifier\"\n", "description = \"A pet breed classifier trained on the Oxford Pets dataset\"\n", "interpretation='default'\n", "# examples = ['siamese.jpg', 'kitten.jpg']\n", "examples = [\"sample_images/\"+file for file in files] \n", "article=\"

Blog post

\"\n", "enable_queue=True\n", "\n", "gr.Interface(fn=predict,inputs=gr.inputs.Image(shape=(512, 512)),outputs=gr.outputs.Label(num_top_classes=3),title=title,description=description,article=article,examples=examples,interpretation=interpretation,enable_queue=enable_queue).launch(share=True)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "65162304-6635-4cfb-95e1-cf12ceba09f4", "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.8.12" } }, "nbformat": 4, "nbformat_minor": 5 }