marii/study_group_example
0
1{2 "cells": [3 {4 "cell_type": "code",5 "execution_count": 9,6 "metadata": {},7 "outputs": [],8 "source": [9 "# default_exp core"10 ]11 },12 {13 "cell_type": "markdown",14 "metadata": {},15 "source": [16 "# module name here\n",17 "\n",18 "> API details."19 ]20 },21 {22 "cell_type": "code",23 "execution_count": 2,24 "metadata": {},25 "outputs": [],26 "source": [27 "#hide\n",28 "from nbdev.showdoc import *"29 ]30 },31 {32 "cell_type": "code",33 "execution_count": 3,34 "metadata": {},35 "outputs": [36 {37 "data": {38 "text/html": [39 "<table border=\"1\" class=\"dataframe\">\n",40 " <thead>\n",41 " <tr style=\"text-align: left;\">\n",42 " <th>epoch</th>\n",43 " <th>train_loss</th>\n",44 " <th>valid_loss</th>\n",45 " <th>accuracy</th>\n",46 " <th>time</th>\n",47 " </tr>\n",48 " </thead>\n",49 " <tbody>\n",50 " <tr>\n",51 " <td>0</td>\n",52 " <td>0.966888</td>\n",53 " <td>0.311951</td>\n",54 " <td>0.899188</td>\n",55 " <td>00:28</td>\n",56 " </tr>\n",57 " </tbody>\n",58 "</table>"59 ],60 "text/plain": [61 "<IPython.core.display.HTML object>"62 ]63 },64 "metadata": {},65 "output_type": "display_data"66 },67 {68 "data": {69 "text/html": [70 "<table border=\"1\" class=\"dataframe\">\n",71 " <thead>\n",72 " <tr style=\"text-align: left;\">\n",73 " <th>epoch</th>\n",74 " <th>train_loss</th>\n",75 " <th>valid_loss</th>\n",76 " <th>accuracy</th>\n",77 " <th>time</th>\n",78 " </tr>\n",79 " </thead>\n",80 " <tbody>\n",81 " <tr>\n",82 " <td>0</td>\n",83 " <td>0.458197</td>\n",84 " <td>0.272663</td>\n",85 " <td>0.908660</td>\n",86 " <td>00:31</td>\n",87 " </tr>\n",88 " </tbody>\n",89 "</table>"90 ],91 "text/plain": [92 "<IPython.core.display.HTML object>"93 ]94 },95 "metadata": {},96 "output_type": "display_data"97 }98 ],99 "source": [100 "from fastai.vision.all import *\n",101 "path = untar_data(URLs.PETS)\n",102 "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))\n",103 "learn = vision_learner(dls, models.resnet50, metrics=accuracy)\n",104 "learn.fine_tune(1)\n",105 "learn.path = Path('.')\n",106 "learn.export()"107 ]108 },109 {110 "cell_type": "code",111 "execution_count": 4,112 "metadata": {},113 "outputs": [],114 "source": [115 "#export\n",116 "learn = load_learner('export.pkl')"117 ]118 },119 {120 "cell_type": "code",121 "execution_count": 5,122 "metadata": {},123 "outputs": [],124 "source": [125 "#export\n",126 "labels = learn.dls.vocab\n",127 "def predict(img):\n",128 " img = PILImage.create(img)\n",129 " pred,pred_idx,probs = learn.predict(img)\n",130 "\n",131 " return {labels[i]: float(probs[i]) for i in range(len(labels))}"132 ]133 },134 {135 "cell_type": "code",136 "execution_count": 6,137 "metadata": {},138 "outputs": [139 {140 "name": "stdout",141 "output_type": "stream",142 "text": [143 "Running on local URL: http://127.0.0.1:7860/\n",144 "Running on public URL: https://31643.gradio.app\n",145 "\n",146 "This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n"147 ]148 },149 {150 "data": {151 "text/html": [152 "\n",153 " <iframe\n",154 " width=\"900\"\n",155 " height=\"500\"\n",156 " src=\"https://31643.gradio.app\"\n",157 " frameborder=\"0\"\n",158 " allowfullscreen\n",159 " \n",160 " ></iframe>\n",161 " "162 ],163 "text/plain": [164 "<IPython.lib.display.IFrame at 0x7f5124f51430>"165 ]166 },167 "metadata": {},168 "output_type": "display_data"169 },170 {171 "data": {172 "text/plain": [173 "(<fastapi.applications.FastAPI at 0x7f51600bf880>,\n",174 " 'http://127.0.0.1:7860/',\n",175 " 'https://31643.gradio.app')"176 ]177 },178 "execution_count": 6,179 "metadata": {},180 "output_type": "execute_result"181 },182 {183 "data": {184 "text/html": [],185 "text/plain": [186 "<IPython.core.display.HTML object>"187 ]188 },189 "metadata": {},190 "output_type": "display_data"191 }192 ],193 "source": [194 "#export\n",195 "import gradio as gr\n",196 "gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(512, 512)), outputs=gr.outputs.Label(num_top_classes=3)).launch(share=True)"197 ]198 },199 {200 "cell_type": "code",201 "execution_count": 10,202 "metadata": {},203 "outputs": [204 {205 "name": "stdout",206 "output_type": "stream",207 "text": [208 "Converted 00_app.ipynb.\r\n",209 "Converted index.ipynb.\r\n"210 ]211 }212 ],213 "source": [214 "! nbdev_build_lib"215 ]216 },217 {218 "cell_type": "code",219 "execution_count": null,220 "metadata": {},221 "outputs": [],222 "source": []223 }224 ],225 "metadata": {226 "kernelspec": {227 "display_name": "Python 3 (ipykernel)",228 "language": "python",229 "name": "python3"230 },231 "language_info": {232 "codemirror_mode": {233 "name": "ipython",234 "version": 3235 },236 "file_extension": ".py",237 "mimetype": "text/x-python",238 "name": "python",239 "nbconvert_exporter": "python",240 "pygments_lexer": "ipython3",241 "version": "3.9.7"242 }243 },244 "nbformat": 4,245 "nbformat_minor": 2246}247 