rohan2810 commited on
Commit
640a7cb
·
1 Parent(s): a9c7deb

added notebook

Browse files
Files changed (1) hide show
  1. saving-a-basic-fastai-model.ipynb +1 -0
saving-a-basic-fastai-model.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":"2024-04-18T03:32:34.119956Z","iopub.execute_input":"2024-04-18T03:32:34.120643Z","iopub.status.idle":"2024-04-18T03:32:47.911380Z","shell.execute_reply.started":"2024-04-18T03:32:34.120614Z","shell.execute_reply":"2024-04-18T03:32:47.910310Z"},"trusted":true},"execution_count":1,"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":"2024-04-18T03:32:47.913341Z","iopub.execute_input":"2024-04-18T03:32:47.913632Z","iopub.status.idle":"2024-04-18T03:32:58.918062Z","shell.execute_reply.started":"2024-04-18T03:32:47.913607Z","shell.execute_reply":"2024-04-18T03:32:58.917200Z"},"trusted":true},"execution_count":2,"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":"2024-04-18T03:32:58.919146Z","iopub.execute_input":"2024-04-18T03:32:58.919424Z","iopub.status.idle":"2024-04-18T03:33:26.768752Z","shell.execute_reply.started":"2024-04-18T03:32:58.919400Z","shell.execute_reply":"2024-04-18T03:33:26.767973Z"},"trusted":true},"execution_count":3,"outputs":[{"output_type":"display_data","data":{"text/plain":"<IPython.core.display.HTML object>","text/html":"\n<style>\n /* Turns off some styling */\n progress {\n /* gets rid of default border in Firefox and Opera. */\n border: none;\n /* Needs to be in here for Safari polyfill so background images work as expected. */\n background-size: auto;\n }\n progress:not([value]), progress:not([value])::-webkit-progress-bar {\n background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n }\n .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n background: #F44336;\n }\n</style>\n"},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"<IPython.core.display.HTML object>","text/html":"\n <div>\n <progress value='811712512' class='' max='811706944' style='width:300px; height:20px; vertical-align: middle;'></progress>\n 100.00% [811712512/811706944 00:17&lt;00:00]\n </div>\n "},"metadata":{}}]},{"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":"path","metadata":{"execution":{"iopub.status.busy":"2024-04-18T03:34:19.549475Z","iopub.execute_input":"2024-04-18T03:34:19.550553Z","iopub.status.idle":"2024-04-18T03:34:19.556323Z","shell.execute_reply.started":"2024-04-18T03:34:19.550515Z","shell.execute_reply":"2024-04-18T03:34:19.555364Z"},"trusted":true},"execution_count":4,"outputs":[{"execution_count":4,"output_type":"execute_result","data":{"text/plain":"Path('/root/.fastai/data/oxford-iiit-pet/images')"},"metadata":{}}]},{"cell_type":"code","source":"def is_cat(x): return x[0].isupper() ","metadata":{"id":"44eb0ad3","execution":{"iopub.status.busy":"2024-04-18T03:34:27.185879Z","iopub.execute_input":"2024-04-18T03:34:27.186864Z","iopub.status.idle":"2024-04-18T03:34:27.191027Z","shell.execute_reply.started":"2024-04-18T03:34:27.186828Z","shell.execute_reply":"2024-04-18T03:34:27.190085Z"},"trusted":true},"execution_count":5,"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":"2024-04-18T03:51:43.611133Z","iopub.execute_input":"2024-04-18T03:51:43.612221Z","iopub.status.idle":"2024-04-18T03:51:44.137954Z","shell.execute_reply.started":"2024-04-18T03:51:43.612180Z","shell.execute_reply":"2024-04-18T03:51:44.136834Z"},"trusted":true},"execution_count":8,"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":"2024-04-18T03:51:45.659100Z","iopub.execute_input":"2024-04-18T03:51:45.659782Z","iopub.status.idle":"2024-04-18T03:53:05.005614Z","shell.execute_reply.started":"2024-04-18T03:51:45.659751Z","shell.execute_reply":"2024-04-18T03:53:05.004441Z"},"trusted":true},"execution_count":9,"outputs":[{"output_type":"display_data","data":{"text/plain":"<IPython.core.display.HTML object>","text/html":"\n<style>\n /* Turns off some styling */\n progress {\n /* gets rid of default border in Firefox and Opera. */\n border: none;\n /* Needs to be in here for Safari polyfill so background images work as expected. */\n background-size: auto;\n }\n progress:not([value]), progress:not([value])::-webkit-progress-bar {\n background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n }\n .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n background: #F44336;\n }\n</style>\n"},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"<IPython.core.display.HTML object>","text/html":"<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: left;\">\n <th>epoch</th>\n <th>train_loss</th>\n <th>valid_loss</th>\n <th>error_rate</th>\n <th>time</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <td>0</td>\n <td>0.197446</td>\n <td>0.062084</td>\n <td>0.021651</td>\n <td>00:19</td>\n </tr>\n </tbody>\n</table>"},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"<IPython.core.display.HTML object>","text/html":"\n<style>\n /* Turns off some styling */\n progress {\n /* gets rid of default border in Firefox and Opera. */\n border: none;\n /* Needs to be in here for Safari polyfill so background images work as expected. */\n background-size: auto;\n }\n progress:not([value]), progress:not([value])::-webkit-progress-bar {\n background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n }\n .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n background: #F44336;\n }\n</style>\n"},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"<IPython.core.display.HTML object>","text/html":"<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: left;\">\n <th>epoch</th>\n <th>train_loss</th>\n <th>valid_loss</th>\n <th>error_rate</th>\n <th>time</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <td>0</td>\n <td>0.070748</td>\n <td>0.047605</td>\n <td>0.010825</td>\n <td>00:19</td>\n </tr>\n <tr>\n <td>1</td>\n <td>0.034505</td>\n <td>0.038190</td>\n <td>0.010825</td>\n <td>00:20</td>\n </tr>\n <tr>\n <td>2</td>\n <td>0.022978</td>\n <td>0.034068</td>\n <td>0.008119</td>\n <td>00:19</td>\n </tr>\n </tbody>\n</table>"},"metadata":{}}]},{"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","execution":{"iopub.status.busy":"2024-04-18T03:53:07.426137Z","iopub.execute_input":"2024-04-18T03:53:07.426505Z","iopub.status.idle":"2024-04-18T03:53:07.585819Z","shell.execute_reply.started":"2024-04-18T03:53:07.426477Z","shell.execute_reply":"2024-04-18T03:53:07.584824Z"},"trusted":true},"execution_count":11,"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"}}]}