Spaces:
Sleeping
Sleeping
File size: 5,672 Bytes
dcc9312 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
{
"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
}
|