{ "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": null, "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": null, "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": null, "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": null, "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": null, "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": null, "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": [], "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": null, "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." ] } ], "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.16" } }, "nbformat": 4, "nbformat_minor": 4 }