Spaces:
Sleeping
Sleeping
Delete 02-saving-a-basic-fastai-model.ipynb
Browse files
02-saving-a-basic-fastai-model.ipynb
DELETED
@@ -1,216 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"cells": [
|
3 |
-
{
|
4 |
-
"cell_type": "markdown",
|
5 |
-
"metadata": {
|
6 |
-
"id": "98d53c05"
|
7 |
-
},
|
8 |
-
"source": [
|
9 |
-
"## Saving a Cats v Dogs Model"
|
10 |
-
]
|
11 |
-
},
|
12 |
-
{
|
13 |
-
"cell_type": "markdown",
|
14 |
-
"metadata": {},
|
15 |
-
"source": [
|
16 |
-
"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."
|
17 |
-
]
|
18 |
-
},
|
19 |
-
{
|
20 |
-
"cell_type": "code",
|
21 |
-
"execution_count": null,
|
22 |
-
"metadata": {
|
23 |
-
"_kg_hide-input": true,
|
24 |
-
"_kg_hide-output": true,
|
25 |
-
"execution": {
|
26 |
-
"iopub.execute_input": "2022-05-03T05:51:37.949032Z",
|
27 |
-
"iopub.status.busy": "2022-05-03T05:51:37.948558Z",
|
28 |
-
"iopub.status.idle": "2022-05-03T05:51:59.531217Z",
|
29 |
-
"shell.execute_reply": "2022-05-03T05:51:59.530294Z",
|
30 |
-
"shell.execute_reply.started": "2022-05-03T05:51:37.948947Z"
|
31 |
-
},
|
32 |
-
"id": "evvA0fqvSblq",
|
33 |
-
"outputId": "ba21b811-767c-459a-ccdf-044758720a55"
|
34 |
-
},
|
35 |
-
"outputs": [],
|
36 |
-
"source": [
|
37 |
-
"# Make sure we've got the latest version of fastai:\n",
|
38 |
-
"!pip install -Uqq fastai"
|
39 |
-
]
|
40 |
-
},
|
41 |
-
{
|
42 |
-
"cell_type": "markdown",
|
43 |
-
"metadata": {},
|
44 |
-
"source": [
|
45 |
-
"First, import all the stuff we need from fastai:"
|
46 |
-
]
|
47 |
-
},
|
48 |
-
{
|
49 |
-
"cell_type": "code",
|
50 |
-
"execution_count": null,
|
51 |
-
"metadata": {
|
52 |
-
"execution": {
|
53 |
-
"iopub.execute_input": "2022-05-03T05:51:59.534478Z",
|
54 |
-
"iopub.status.busy": "2022-05-03T05:51:59.533878Z",
|
55 |
-
"iopub.status.idle": "2022-05-03T05:52:02.177975Z",
|
56 |
-
"shell.execute_reply": "2022-05-03T05:52:02.177267Z",
|
57 |
-
"shell.execute_reply.started": "2022-05-03T05:51:59.534432Z"
|
58 |
-
},
|
59 |
-
"id": "44eb0ad3"
|
60 |
-
},
|
61 |
-
"outputs": [],
|
62 |
-
"source": [
|
63 |
-
"from fastai.vision.all import *"
|
64 |
-
]
|
65 |
-
},
|
66 |
-
{
|
67 |
-
"cell_type": "markdown",
|
68 |
-
"metadata": {},
|
69 |
-
"source": [
|
70 |
-
"Download and decompress our dataset, which is pictures of dogs and cats:"
|
71 |
-
]
|
72 |
-
},
|
73 |
-
{
|
74 |
-
"cell_type": "code",
|
75 |
-
"execution_count": null,
|
76 |
-
"metadata": {
|
77 |
-
"execution": {
|
78 |
-
"iopub.execute_input": "2022-05-03T05:52:02.180691Z",
|
79 |
-
"iopub.status.busy": "2022-05-03T05:52:02.180192Z",
|
80 |
-
"iopub.status.idle": "2022-05-03T05:53:02.465242Z",
|
81 |
-
"shell.execute_reply": "2022-05-03T05:53:02.464516Z",
|
82 |
-
"shell.execute_reply.started": "2022-05-03T05:52:02.180651Z"
|
83 |
-
}
|
84 |
-
},
|
85 |
-
"outputs": [],
|
86 |
-
"source": [
|
87 |
-
"path = untar_data(URLs.PETS)/'images'"
|
88 |
-
]
|
89 |
-
},
|
90 |
-
{
|
91 |
-
"cell_type": "markdown",
|
92 |
-
"metadata": {},
|
93 |
-
"source": [
|
94 |
-
"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:"
|
95 |
-
]
|
96 |
-
},
|
97 |
-
{
|
98 |
-
"cell_type": "code",
|
99 |
-
"execution_count": null,
|
100 |
-
"metadata": {
|
101 |
-
"execution": {
|
102 |
-
"iopub.execute_input": "2022-05-03T05:53:02.467572Z",
|
103 |
-
"iopub.status.busy": "2022-05-03T05:53:02.467289Z",
|
104 |
-
"iopub.status.idle": "2022-05-03T05:53:02.474701Z",
|
105 |
-
"shell.execute_reply": "2022-05-03T05:53:02.474109Z",
|
106 |
-
"shell.execute_reply.started": "2022-05-03T05:53:02.467536Z"
|
107 |
-
},
|
108 |
-
"id": "44eb0ad3"
|
109 |
-
},
|
110 |
-
"outputs": [],
|
111 |
-
"source": [
|
112 |
-
"def is_cat(x): return x[0].isupper() "
|
113 |
-
]
|
114 |
-
},
|
115 |
-
{
|
116 |
-
"cell_type": "markdown",
|
117 |
-
"metadata": {},
|
118 |
-
"source": [
|
119 |
-
"Now we can create our `DataLoaders`:"
|
120 |
-
]
|
121 |
-
},
|
122 |
-
{
|
123 |
-
"cell_type": "code",
|
124 |
-
"execution_count": null,
|
125 |
-
"metadata": {
|
126 |
-
"execution": {
|
127 |
-
"iopub.execute_input": "2022-05-03T05:53:02.476084Z",
|
128 |
-
"iopub.status.busy": "2022-05-03T05:53:02.475754Z",
|
129 |
-
"iopub.status.idle": "2022-05-03T05:53:06.703777Z",
|
130 |
-
"shell.execute_reply": "2022-05-03T05:53:06.703023Z",
|
131 |
-
"shell.execute_reply.started": "2022-05-03T05:53:02.476052Z"
|
132 |
-
},
|
133 |
-
"id": "44eb0ad3"
|
134 |
-
},
|
135 |
-
"outputs": [],
|
136 |
-
"source": [
|
137 |
-
"dls = ImageDataLoaders.from_name_func('.',\n",
|
138 |
-
" get_image_files(path), valid_pct=0.2, seed=42,\n",
|
139 |
-
" label_func=is_cat,\n",
|
140 |
-
" item_tfms=Resize(192))"
|
141 |
-
]
|
142 |
-
},
|
143 |
-
{
|
144 |
-
"cell_type": "markdown",
|
145 |
-
"metadata": {},
|
146 |
-
"source": [
|
147 |
-
"... and train our model, a resnet18 (to keep it small and fast):"
|
148 |
-
]
|
149 |
-
},
|
150 |
-
{
|
151 |
-
"cell_type": "code",
|
152 |
-
"execution_count": null,
|
153 |
-
"metadata": {
|
154 |
-
"execution": {
|
155 |
-
"iopub.execute_input": "2022-05-03T05:53:28.093059Z",
|
156 |
-
"iopub.status.busy": "2022-05-03T05:53:28.092381Z"
|
157 |
-
},
|
158 |
-
"id": "c107f724",
|
159 |
-
"outputId": "fcc1de68-7c8b-43f5-b9eb-fcdb0773ef07"
|
160 |
-
},
|
161 |
-
"outputs": [],
|
162 |
-
"source": [
|
163 |
-
"learn = vision_learner(dls, resnet18, metrics=error_rate)\n",
|
164 |
-
"learn.fine_tune(3)"
|
165 |
-
]
|
166 |
-
},
|
167 |
-
{
|
168 |
-
"cell_type": "markdown",
|
169 |
-
"metadata": {},
|
170 |
-
"source": [
|
171 |
-
"Now we can export our trained `Learner`. This contains all the information needed to run the model:"
|
172 |
-
]
|
173 |
-
},
|
174 |
-
{
|
175 |
-
"cell_type": "code",
|
176 |
-
"execution_count": null,
|
177 |
-
"metadata": {
|
178 |
-
"id": "ae2bc6ac"
|
179 |
-
},
|
180 |
-
"outputs": [],
|
181 |
-
"source": [
|
182 |
-
"learn.export('model.pkl')"
|
183 |
-
]
|
184 |
-
},
|
185 |
-
{
|
186 |
-
"cell_type": "markdown",
|
187 |
-
"metadata": {
|
188 |
-
"id": "Q2HTrQKTf3BV"
|
189 |
-
},
|
190 |
-
"source": [
|
191 |
-
"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."
|
192 |
-
]
|
193 |
-
}
|
194 |
-
],
|
195 |
-
"metadata": {
|
196 |
-
"kernelspec": {
|
197 |
-
"display_name": "Python 3 (ipykernel)",
|
198 |
-
"language": "python",
|
199 |
-
"name": "python3"
|
200 |
-
},
|
201 |
-
"language_info": {
|
202 |
-
"codemirror_mode": {
|
203 |
-
"name": "ipython",
|
204 |
-
"version": 3
|
205 |
-
},
|
206 |
-
"file_extension": ".py",
|
207 |
-
"mimetype": "text/x-python",
|
208 |
-
"name": "python",
|
209 |
-
"nbconvert_exporter": "python",
|
210 |
-
"pygments_lexer": "ipython3",
|
211 |
-
"version": "3.8.16"
|
212 |
-
}
|
213 |
-
},
|
214 |
-
"nbformat": 4,
|
215 |
-
"nbformat_minor": 4
|
216 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|