{"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":"markdown","source":"## Saving a Cats v Dogs Model","metadata":{"id":"98d53c05"}},{"cell_type":"markdown","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.","metadata":{}},{"cell_type":"code","source":"# Make sure we've got the latest version of fastai:\n!pip install -Uqq fastai","metadata":{"id":"evvA0fqvSblq","outputId":"ba21b811-767c-459a-ccdf-044758720a55","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-03T05:51:37.948558Z","iopub.execute_input":"2022-05-03T05:51:37.949032Z","iopub.status.idle":"2022-05-03T05:51:59.531217Z","shell.execute_reply.started":"2022-05-03T05:51:37.948947Z","shell.execute_reply":"2022-05-03T05:51:59.530294Z"},"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-05-03T05:51:59.533878Z","iopub.execute_input":"2022-05-03T05:51:59.534478Z","iopub.status.idle":"2022-05-03T05:52:02.177975Z","shell.execute_reply.started":"2022-05-03T05:51:59.534432Z","shell.execute_reply":"2022-05-03T05:52:02.177267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Download and decompress our dataset, which is pictures of dogs and cats:","metadata":{}},{"cell_type":"code","source":"path = untar_data(URLs.PETS)/'images'","metadata":{"execution":{"iopub.status.busy":"2022-05-03T05:52:02.180192Z","iopub.execute_input":"2022-05-03T05:52:02.180691Z","iopub.status.idle":"2022-05-03T05:53:02.465242Z","shell.execute_reply.started":"2022-05-03T05:52:02.180651Z","shell.execute_reply":"2022-05-03T05:53:02.464516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","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:","metadata":{}},{"cell_type":"code","source":"def is_cat(x): return x[0].isupper() ","metadata":{"id":"44eb0ad3","execution":{"iopub.status.busy":"2022-05-03T05:53:02.467289Z","iopub.execute_input":"2022-05-03T05:53:02.467572Z","iopub.status.idle":"2022-05-03T05:53:02.474701Z","shell.execute_reply.started":"2022-05-03T05:53:02.467536Z","shell.execute_reply":"2022-05-03T05:53:02.474109Z"},"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_cat,\n item_tfms=Resize(192))","metadata":{"id":"44eb0ad3","execution":{"iopub.status.busy":"2022-05-03T05:53:02.475754Z","iopub.execute_input":"2022-05-03T05:53:02.476084Z","iopub.status.idle":"2022-05-03T05:53:06.703777Z","shell.execute_reply.started":"2022-05-03T05:53:02.476052Z","shell.execute_reply":"2022-05-03T05:53:06.703023Z"},"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-05-03T05:53:28.092381Z","iopub.execute_input":"2022-05-03T05:53:28.093059Z"},"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","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"}}]}