{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "7cd0d8fa", "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "markdown", "id": "7195af18", "metadata": {}, "source": [ "## Gradio Pets" ] }, { "cell_type": "code", "execution_count": 1, "id": "44eb0ad3", "metadata": {}, "outputs": [], "source": [ "#|export\n", "from fastai.vision.all import *\n", "import gradio as gr\n", "import timm\n", "from timm.layers import convert_splitbn_model, convert_sync_batchnorm, set_fast_norm" ] }, { "cell_type": "code", "execution_count": null, "id": "8b5cce4a-ba43-4b14-9855-90db162a52df", "metadata": {}, "outputs": [], "source": [ "import torch\n", "print(torch.__version__)" ] }, { "cell_type": "code", "execution_count": null, "id": "3295ef11", "metadata": {}, "outputs": [], "source": [ "im = PILImage.create('basset.jpg')\n", "im.thumbnail((224,224))\n", "im" ] }, { "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": [], "source": [ "learn.predict(im)" ] }, { "cell_type": "code", "execution_count": null, "id": "0419ed3a", "metadata": {}, "outputs": [], "source": [ "#export\n", "categories = learn.dls.vocab\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": [], "source": [ "classify_image('basset.jpg')" ] }, { "cell_type": "code", "execution_count": null, "id": "930cf172", "metadata": {}, "outputs": [], "source": [ "#export\n", "image = gr.inputs.Image(shape=(192, 192))\n", "label = gr.outputs.Label()\n", "examples = ['basset.jpg']" ] }, { "cell_type": "code", "execution_count": null, "id": "4f463e23", "metadata": {}, "outputs": [], "source": [ "#export\n", "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n", "intf.launch(inline=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "82774c08", "metadata": { "scrolled": true, "tags": [] }, "outputs": [], "source": [ "m = learn.model\n", "m" ] }, { "cell_type": "code", "execution_count": null, "id": "10d7900d", "metadata": {}, "outputs": [], "source": [ "l = m.get_submodule('0.model.stem.1')\n", "list(l.parameters())" ] }, { "cell_type": "code", "execution_count": null, "id": "008537b0", "metadata": {}, "outputs": [], "source": [ "l = m.get_submodule('0.model.stages.0.blocks.1.mlp.fc1')\n", "list(l.parameters())" ] }, { "cell_type": "code", "execution_count": null, "id": "ff9d5c73", "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.10.8" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "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 }