{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Make sure we've got the latest version of fastai:\n!pip install -Uqq fastai\n!pip install gradio","metadata":{"id":"evvA0fqvSblq","outputId":"ba21b811-767c-459a-ccdf-044758720a55","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-12-03T17:14:24.297943Z","iopub.execute_input":"2022-12-03T17:14:24.298514Z","iopub.status.idle":"2022-12-03T17:15:02.484095Z","shell.execute_reply.started":"2022-12-03T17:14:24.298472Z","shell.execute_reply":"2022-12-03T17:15:02.483065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"First, import all the stuff we need from fastai:","metadata":{}},{"cell_type":"code","source":"from fastai.vision.all import *","metadata":{"id":"44eb0ad3","execution":{"iopub.status.busy":"2022-12-03T17:16:00.100692Z","iopub.execute_input":"2022-12-03T17:16:00.101153Z","iopub.status.idle":"2022-12-03T17:16:03.277961Z","shell.execute_reply.started":"2022-12-03T17:16:00.101109Z","shell.execute_reply":"2022-12-03T17:16:03.277116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/flower-image-dataset/flowers')\nprint(path)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T17:20:25.287829Z","iopub.execute_input":"2022-12-03T17:20:25.288153Z","iopub.status.idle":"2022-12-03T17:20:25.296603Z","shell.execute_reply.started":"2022-12-03T17:20:25.288114Z","shell.execute_reply":"2022-12-03T17:20:25.295802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def is_rose(x): \n    words = x.split(\"_\")\n    last = len(words)-1\n    words.remove(words[last])\n    category = ' '.join(map(str, words))\n    return(category)\n","metadata":{"id":"44eb0ad3","execution":{"iopub.status.busy":"2022-12-03T17:30:18.450651Z","iopub.execute_input":"2022-12-03T17:30:18.450947Z","iopub.status.idle":"2022-12-03T17:30:18.459416Z","shell.execute_reply.started":"2022-12-03T17:30:18.450914Z","shell.execute_reply":"2022-12-03T17:30:18.458429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we can create our `DataLoaders`:","metadata":{}},{"cell_type":"code","source":"dls = ImageDataLoaders.from_name_func('.',\n    get_image_files(path), valid_pct=0.2, seed=42,\n    label_func=is_rose,\n    item_tfms=Resize(192))\n'''\ndls = DataBlock(\n    blocks=(ImageBlock, CategoryBlock), \n    get_items=get_image_files, \n    splitter=RandomSplitter(valid_pct=0.2, seed=42),\n    get_y=parent_label,\n    item_tfms=[Resize(192, method='squish')]\n).dataloaders(path, bs=32)'''\ndls.show_batch(max_n=6)","metadata":{"id":"44eb0ad3","execution":{"iopub.status.busy":"2022-12-03T17:30:19.583751Z","iopub.execute_input":"2022-12-03T17:30:19.584187Z","iopub.status.idle":"2022-12-03T17:30:26.709837Z","shell.execute_reply.started":"2022-12-03T17:30:19.584145Z","shell.execute_reply":"2022-12-03T17:30:26.709057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"... and train our model, a resnet18 (to keep it small and fast):","metadata":{}},{"cell_type":"code","source":"learn = vision_learner(dls, resnet18, metrics=error_rate)\nlearn.fine_tune(3)","metadata":{"id":"c107f724","outputId":"fcc1de68-7c8b-43f5-b9eb-fcdb0773ef07","execution":{"iopub.status.busy":"2022-12-03T17:30:50.953752Z","iopub.execute_input":"2022-12-03T17:30:50.954080Z","iopub.status.idle":"2022-12-03T17:34:35.467637Z","shell.execute_reply.started":"2022-12-03T17:30:50.954043Z","shell.execute_reply":"2022-12-03T17:34:35.466724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we can export our trained `Learner`. This contains all the information needed to run the model:","metadata":{}},{"cell_type":"code","source":"learn.export('model.pkl')","metadata":{"id":"ae2bc6ac","execution":{"iopub.status.busy":"2022-12-03T17:39:14.830015Z","iopub.execute_input":"2022-12-03T17:39:14.830396Z","iopub.status.idle":"2022-12-03T17:39:14.993140Z","shell.execute_reply.started":"2022-12-03T17:39:14.830353Z","shell.execute_reply":"2022-12-03T17:39:14.992347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = load_learner('model.pkl')","metadata":{"execution":{"iopub.status.busy":"2022-12-03T17:39:15.336714Z","iopub.execute_input":"2022-12-03T17:39:15.337366Z","iopub.status.idle":"2022-12-03T17:39:15.383024Z","shell.execute_reply.started":"2022-12-03T17:39:15.337320Z","shell.execute_reply":"2022-12-03T17:39:15.382260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = learn.dls.vocab\nprint(learn.dls.vocab)\nlabelsNew = [\"bougainvillea\",\"daisies\",\"garden roses\",\"gardenias\",\"hibiscus\", 'hydrangeas', 'lilies', 'orchids', 'peonies', 'tulip']\n\ndef predict(img):\n    img = PILImage.create(img)\n    pred,pred_idx,probs = learn.predict(img)\n    return {labelsNew[i]: float(probs[i]) for i in range(len(labelsNew))}","metadata":{"execution":{"iopub.status.busy":"2022-12-03T17:53:29.506156Z","iopub.execute_input":"2022-12-03T17:53:29.506638Z","iopub.status.idle":"2022-12-03T17:53:29.518624Z","shell.execute_reply.started":"2022-12-03T17:53:29.506588Z","shell.execute_reply":"2022-12-03T17:53:29.517758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastdownload import download_url\npredict('../input/testimages/rose.jpeg')","metadata":{"execution":{"iopub.status.busy":"2022-12-03T17:54:45.869978Z","iopub.execute_input":"2022-12-03T17:54:45.870322Z","iopub.status.idle":"2022-12-03T17:54:46.031130Z","shell.execute_reply.started":"2022-12-03T17:54:45.870266Z","shell.execute_reply":"2022-12-03T17:54:46.030438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gradio as gr\ngr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(512, 512)), outputs=gr.outputs.Label(num_top_classes=3)).launch(share=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T17:54:53.238296Z","iopub.execute_input":"2022-12-03T17:54:53.238616Z","iopub.status.idle":"2022-12-03T17:54:59.003857Z","shell.execute_reply.started":"2022-12-03T17:54:53.238580Z","shell.execute_reply":"2022-12-03T17:54:59.003119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Finally, open the Kaggle sidebar on the right if it's not already, and find the section marked \"Output\". Open the `/kaggle/working` folder, and you'll see `model.pkl`. Click on it, then click on the menu on the right that appears, and choose \"Download\". After a few seconds, your model will be downloaded to your computer, where you can then create your app that uses the model.","metadata":{"id":"Q2HTrQKTf3BV"}}]}