{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "98d53c05" }, "source": [ "## Saving a Cats v Dogs Model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a minimal example showing how to train a fastai model on Kaggle, and save it so you can use it in your app." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "_kg_hide-input": true, "_kg_hide-output": true, "execution": { "iopub.execute_input": "2022-05-03T05:51:37.949032Z", "iopub.status.busy": "2022-05-03T05:51:37.948558Z", "iopub.status.idle": "2022-05-03T05:51:59.531217Z", "shell.execute_reply": "2022-05-03T05:51:59.530294Z", "shell.execute_reply.started": "2022-05-03T05:51:37.948947Z" }, "id": "evvA0fqvSblq", "outputId": "ba21b811-767c-459a-ccdf-044758720a55" }, "outputs": [], "source": [ "# Make sure we've got the latest version of fastai:\n", "!pip install -Uqq fastai" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, import all the stuff we need from fastai:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2022-05-03T05:51:59.534478Z", "iopub.status.busy": "2022-05-03T05:51:59.533878Z", "iopub.status.idle": "2022-05-03T05:52:02.177975Z", "shell.execute_reply": "2022-05-03T05:52:02.177267Z", "shell.execute_reply.started": "2022-05-03T05:51:59.534432Z" }, "id": "44eb0ad3" }, "outputs": [], "source": [ "from fastai.vision.all import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Download and decompress our dataset, which is pictures of dogs and cats:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2022-05-03T05:52:02.180691Z", "iopub.status.busy": "2022-05-03T05:52:02.180192Z", "iopub.status.idle": "2022-05-03T05:53:02.465242Z", "shell.execute_reply": "2022-05-03T05:53:02.464516Z", "shell.execute_reply.started": "2022-05-03T05:52:02.180651Z" } }, "outputs": [], "source": [ "path = untar_data(URLs.PETS)/'images'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We need a way to label our images as dogs or cats. In this dataset, pictures of cats are given a filename that starts with a capital letter:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2022-05-03T05:53:02.467572Z", "iopub.status.busy": "2022-05-03T05:53:02.467289Z", "iopub.status.idle": "2022-05-03T05:53:02.474701Z", "shell.execute_reply": "2022-05-03T05:53:02.474109Z", "shell.execute_reply.started": "2022-05-03T05:53:02.467536Z" }, "id": "44eb0ad3" }, "outputs": [], "source": [ "def is_cat(x): return x[0].isupper() " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can create our `DataLoaders`:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2022-05-03T05:53:02.476084Z", "iopub.status.busy": "2022-05-03T05:53:02.475754Z", "iopub.status.idle": "2022-05-03T05:53:06.703777Z", "shell.execute_reply": "2022-05-03T05:53:06.703023Z", "shell.execute_reply.started": "2022-05-03T05:53:02.476052Z" }, "id": "44eb0ad3" }, "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": "markdown", "metadata": {}, "source": [ "... and train our model, a resnet18 (to keep it small and fast):" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2022-05-03T05:53:28.093059Z", "iopub.status.busy": "2022-05-03T05:53:28.092381Z" }, "id": "c107f724", "outputId": "fcc1de68-7c8b-43f5-b9eb-fcdb0773ef07" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/jack/anaconda3/lib/python3.8/site-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at /opt/conda/conda-bld/pytorch_1623448278899/work/c10/core/TensorImpl.h:1156.)\n", " return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
epochtrain_lossvalid_losserror_ratetime
00.1888990.0498490.01826800:06
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
epochtrain_lossvalid_losserror_ratetime
00.0783200.0680750.01623800:06
10.0530890.0354470.01082500:06
20.0250570.0226730.00608900:06
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "learn = vision_learner(dls, resnet18, metrics=error_rate)\n", "learn.fine_tune(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can export our trained `Learner`. This contains all the information needed to run the model:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "ae2bc6ac" }, "outputs": [], "source": [ "learn.export('model.pkl')" ] }, { "cell_type": "markdown", "metadata": { "id": "Q2HTrQKTf3BV" }, "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." ] }, { "cell_type": "code", "execution_count": null, "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.13" } }, "nbformat": 4, "nbformat_minor": 4 }