jjokah commited on
Commit
678d19c
1 Parent(s): 59d17a3

Add notebook that trained the model

Browse files
Files changed (1) hide show
  1. train-model.ipynb +1 -0
train-model.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# What celebration is it?","metadata":{}},{"cell_type":"markdown","source":"Install fastai and duckduckgo","metadata":{}},{"cell_type":"code","source":"# It's a good idea to ensure you're running the latest version of any libraries you need.\n# `!pip install -Uqq <libraries>` upgrades to the latest version of <libraries>\n# NB: You can safely ignore any warnings or errors pip spits out about running as root or incompatibilities\nimport os\niskaggle = os.environ.get('KAGGLE_KERNEL_RUN_TYPE', '')\n\nif iskaggle:\n !pip install -Uqq fastai duckduckgo_search","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:19:18.235583Z","iopub.execute_input":"2023-12-08T18:19:18.236194Z","iopub.status.idle":"2023-12-08T18:19:33.385548Z","shell.execute_reply.started":"2023-12-08T18:19:18.236136Z","shell.execute_reply":"2023-12-08T18:19:33.383784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Prepare code to be exported","metadata":{}},{"cell_type":"code","source":"#|default_exp app","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Download images of christmas, easter, and ramadan celebration","metadata":{}},{"cell_type":"markdown","source":"Create search function","metadata":{}},{"cell_type":"code","source":"from duckduckgo_search import ddg_images\nfrom fastcore.all import *\n\ndef search_images(term, max_images=100):\n print(f\"Searching for '{term}'\")\n return L(ddg_images(term, max_results=max_images)).itemgot('image')","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:19:47.274208Z","iopub.execute_input":"2023-12-08T18:19:47.274678Z","iopub.status.idle":"2023-12-08T18:19:47.842923Z","shell.execute_reply.started":"2023-12-08T18:19:47.274632Z","shell.execute_reply":"2023-12-08T18:19:47.841962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Test the search and view the image result","metadata":{}},{"cell_type":"code","source":"#NB: `search_images` depends on duckduckgo.com, which doesn't always return correct responses.\n# If you get a JSON error, just try running it again (it may take a couple of tries).\nurls = search_images('christmas celebration photos', max_images=1)\nurls[0]","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:31:08.774985Z","iopub.execute_input":"2023-12-08T18:31:08.776528Z","iopub.status.idle":"2023-12-08T18:31:09.353481Z","shell.execute_reply.started":"2023-12-08T18:31:08.776482Z","shell.execute_reply":"2023-12-08T18:31:09.352263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Download **christmas celebration photos** and convert image to thumbnail size","metadata":{}},{"cell_type":"code","source":"from fastdownload import download_url\ndest = 'christmas.jpg'\ndownload_url(urls[0], dest, show_progress=False)\n\nfrom fastai.vision.all import *\nim = Image.open(dest)\nim.to_thumb(256,256)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:31:35.225832Z","iopub.execute_input":"2023-12-08T18:31:35.226300Z","iopub.status.idle":"2023-12-08T18:31:44.091975Z","shell.execute_reply.started":"2023-12-08T18:31:35.226261Z","shell.execute_reply":"2023-12-08T18:31:44.090745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Do the same for **easter celebration photos**","metadata":{}},{"cell_type":"code","source":"download_url(search_images('easter celebration photos', max_images=1)[0], 'easter.jpg', show_progress=False)\nImage.open('easter.jpg').to_thumb(256,256)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:31:56.306456Z","iopub.execute_input":"2023-12-08T18:31:56.307023Z","iopub.status.idle":"2023-12-08T18:31:57.797024Z","shell.execute_reply.started":"2023-12-08T18:31:56.306961Z","shell.execute_reply":"2023-12-08T18:31:57.795707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Also do the same for **ramadan celebration photos**","metadata":{}},{"cell_type":"code","source":"download_url(search_images('ramadan celebration photos', max_images=1)[0], 'ramadan.jpg', show_progress=False)\nImage.open('ramadan.jpg').to_thumb(256,256)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:32:15.128377Z","iopub.execute_input":"2023-12-08T18:32:15.128784Z","iopub.status.idle":"2023-12-08T18:32:16.008975Z","shell.execute_reply.started":"2023-12-08T18:32:15.128745Z","shell.execute_reply":"2023-12-08T18:32:16.007633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Grab few examples of christmas, easter, and ramadan photos from the web and save into a different folder","metadata":{}},{"cell_type":"code","source":"searches = 'christmas celebration','easter celebration', 'ramadan celebration'\npath = Path('celebration')\nfrom time import sleep\n\nfor o in searches:\n dest = (path/o)\n dest.mkdir(exist_ok=True, parents=True)\n download_images(dest, urls=search_images(f'{o} photo'))\n sleep(10) # Pause between searches to avoid over-loading server\n download_images(dest, urls=search_images(f'{o} photo'))\n sleep(10)\n resize_images(path/o, max_size=400, dest=path/o)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:32:24.585419Z","iopub.execute_input":"2023-12-08T18:32:24.586688Z","iopub.status.idle":"2023-12-08T18:34:48.266746Z","shell.execute_reply.started":"2023-12-08T18:32:24.586634Z","shell.execute_reply":"2023-12-08T18:34:48.265373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train the model","metadata":{}},{"cell_type":"markdown","source":"Remove images that failed to download correctly","metadata":{}},{"cell_type":"code","source":"#|export\nfailed = verify_images(get_image_files(path))\nfailed.map(Path.unlink)\nlen(failed)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:41:08.698243Z","iopub.execute_input":"2023-12-08T18:41:08.698875Z","iopub.status.idle":"2023-12-08T18:41:10.012532Z","shell.execute_reply.started":"2023-12-08T18:41:08.698818Z","shell.execute_reply":"2023-12-08T18:41:10.011076Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Prepare to train the model with `DataLoaders`, which contains a training set and a validation set. And then view a sample image from it. ","metadata":{}},{"cell_type":"code","source":"dls = DataBlock(\n blocks=(ImageBlock, CategoryBlock), \n get_items=get_image_files, \n splitter=RandomSplitter(valid_pct=0.2, seed=42),\n get_y=parent_label,\n item_tfms=[Resize(192, method='squish')]\n).dataloaders(path, bs=32)\n\ndls.show_batch(max_n=9)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:41:55.973974Z","iopub.execute_input":"2023-12-08T18:41:55.974573Z","iopub.status.idle":"2023-12-08T18:41:57.372259Z","shell.execute_reply.started":"2023-12-08T18:41:55.974527Z","shell.execute_reply":"2023-12-08T18:41:57.371162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Train the model with the computer vision model, `resnet18`. And `fine_tune()` it.","metadata":{}},{"cell_type":"code","source":"learn = vision_learner(dls, resnet18, metrics=error_rate)\nlearn.fine_tune(3)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:42:32.621144Z","iopub.execute_input":"2023-12-08T18:42:32.621610Z","iopub.status.idle":"2023-12-08T18:46:40.382426Z","shell.execute_reply.started":"2023-12-08T18:42:32.621573Z","shell.execute_reply":"2023-12-08T18:46:40.381060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Use the model","metadata":{}},{"cell_type":"markdown","source":"Test the model with an image to see how well it does with the prediction after training","metadata":{}},{"cell_type":"code","source":"what_celebration,_,probs = learn.predict(PILImage.create('christmas.jpg'))\nprint(f\"This is a: {what_celebration}.\")\nprint(f\"Probability it's christmas: {probs[0]:.4f}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:55:02.981196Z","iopub.execute_input":"2023-12-08T18:55:02.981724Z","iopub.status.idle":"2023-12-08T18:55:03.193242Z","shell.execute_reply.started":"2023-12-08T18:55:02.981676Z","shell.execute_reply":"2023-12-08T18:55:03.192057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save the model","metadata":{}},{"cell_type":"markdown","source":"Export the trained `Learner`. This contains all the information needed to run the model.","metadata":{}},{"cell_type":"code","source":"learn.export('model.pkl')","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:55:17.229199Z","iopub.execute_input":"2023-12-08T18:55:17.229596Z","iopub.status.idle":"2023-12-08T18:55:17.390780Z","shell.execute_reply.started":"2023-12-08T18:55:17.229567Z","shell.execute_reply":"2023-12-08T18:55:17.389660Z"},"trusted":true},"execution_count":null,"outputs":[]}]}