{"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":"code","source":"#NB: Kaggle requires phone verification to use the internet or a GPU. If you haven't done that yet, the cell below will fail\n#    This code is only here to check that your internet is enabled. It doesn't do anything else.\n#    Here's a help thread on getting your phone number verified: https://www.kaggle.com/product-feedback/135367\n\nimport socket,warnings\ntry:\n    socket.setdefaulttimeout(1)\n    socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(('1.1.1.1', 53))\nexcept socket.error as ex: raise Exception(\"STOP: No internet. Click '>|' in top right and set 'Internet' switch to on\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-22T12:51:48.110586Z","iopub.execute_input":"2022-10-22T12:51:48.111021Z","iopub.status.idle":"2022-10-22T12:51:48.127912Z","shell.execute_reply.started":"2022-10-22T12:51:48.110976Z","shell.execute_reply":"2022-10-22T12:51:48.126929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"https://github.com/fastai/course22/blob/master/00-is-it-a-bird-creating-a-model-from-your-own-data.ipynb","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"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","metadata":{"execution":{"iopub.status.busy":"2022-10-22T12:51:48.129696Z","iopub.execute_input":"2022-10-22T12:51:48.130047Z","iopub.status.idle":"2022-10-22T12:51:58.39927Z","shell.execute_reply.started":"2022-10-22T12:51:48.130011Z","shell.execute_reply":"2022-10-22T12:51:58.397947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Skip this cell if you already have duckduckgo_search installed\n!pip install -Uqq duckduckgo_search","metadata":{"execution":{"iopub.status.busy":"2022-10-22T12:51:58.401953Z","iopub.execute_input":"2022-10-22T12:51:58.402469Z","iopub.status.idle":"2022-10-22T12:52:09.100563Z","shell.execute_reply.started":"2022-10-22T12:51:58.402405Z","shell.execute_reply":"2022-10-22T12:52:09.099299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from duckduckgo_search import ddg_images\nfrom fastcore.all import *\n\ndef search_images(term, max_images=200): return L(ddg_images(term, max_results=max_images)).itemgot('image')","metadata":{"execution":{"iopub.status.busy":"2022-10-22T12:55:08.452727Z","iopub.execute_input":"2022-10-22T12:55:08.453164Z","iopub.status.idle":"2022-10-22T12:55:08.510524Z","shell.execute_reply.started":"2022-10-22T12:55:08.453121Z","shell.execute_reply":"2022-10-22T12:55:08.509503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"urls = search_images('bird photos', max_images=1)\nurls[0]","metadata":{"execution":{"iopub.status.busy":"2022-10-22T12:55:24.924947Z","iopub.execute_input":"2022-10-22T12:55:24.925574Z","iopub.status.idle":"2022-10-22T12:55:25.542254Z","shell.execute_reply.started":"2022-10-22T12:55:24.925532Z","shell.execute_reply":"2022-10-22T12:55:25.541257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastdownload import download_url\ndest = 'bird.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":"2022-10-22T12:55:39.872117Z","iopub.execute_input":"2022-10-22T12:55:39.872501Z","iopub.status.idle":"2022-10-22T12:55:44.511411Z","shell.execute_reply.started":"2022-10-22T12:55:39.872461Z","shell.execute_reply":"2022-10-22T12:55:44.510282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"download_url(search_images('forest photos', max_images=1)[0], 'forest.jpg', show_progress=False)\nImage.open('forest.jpg').to_thumb(256,256)","metadata":{"execution":{"iopub.status.busy":"2022-10-22T12:56:02.639139Z","iopub.execute_input":"2022-10-22T12:56:02.639715Z","iopub.status.idle":"2022-10-22T12:56:04.00035Z","shell.execute_reply.started":"2022-10-22T12:56:02.639677Z","shell.execute_reply":"2022-10-22T12:56:03.999424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"searches = 'forest','bird'\npath = Path('bird_or_not')\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} sun photo'))\n    sleep(10)\n    download_images(dest, urls=search_images(f'{o} shade photo'))\n    sleep(10)\n    resize_images(path/o, max_size=400, dest=path/o)","metadata":{"execution":{"iopub.status.busy":"2022-10-22T12:56:47.578483Z","iopub.execute_input":"2022-10-22T12:56:47.579231Z","iopub.status.idle":"2022-10-22T13:01:15.850613Z","shell.execute_reply.started":"2022-10-22T12:56:47.579192Z","shell.execute_reply":"2022-10-22T13:01:15.849153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"failed = verify_images(get_image_files(path))\nfailed.map(Path.unlink)\nlen(failed)","metadata":{"execution":{"iopub.status.busy":"2022-10-22T13:01:15.853314Z","iopub.execute_input":"2022-10-22T13:01:15.854127Z","iopub.status.idle":"2022-10-22T13:01:18.233361Z","shell.execute_reply.started":"2022-10-22T13:01:15.85408Z","shell.execute_reply":"2022-10-22T13:01:18.232326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)\n\ndls.show_batch(max_n=6)","metadata":{"execution":{"iopub.status.busy":"2022-10-22T13:01:18.234924Z","iopub.execute_input":"2022-10-22T13:01:18.235651Z","iopub.status.idle":"2022-10-22T13:01:22.993621Z","shell.execute_reply.started":"2022-10-22T13:01:18.235619Z","shell.execute_reply":"2022-10-22T13:01:22.992729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet18, metrics=error_rate)\nlearn.fine_tune(3)","metadata":{"execution":{"iopub.status.busy":"2022-10-22T13:01:22.996033Z","iopub.execute_input":"2022-10-22T13:01:22.996676Z","iopub.status.idle":"2022-10-22T13:01:58.482763Z","shell.execute_reply.started":"2022-10-22T13:01:22.996634Z","shell.execute_reply":"2022-10-22T13:01:58.481529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"is_bird,_,probs = learn.predict(PILImage.create('bird.jpg'))\nprint(f\"This is a: {is_bird}.\")\nprint(f\"Probability it's a bird: {probs[0]:.4f}\")","metadata":{"execution":{"iopub.status.busy":"2022-10-22T13:01:58.484964Z","iopub.execute_input":"2022-10-22T13:01:58.485421Z","iopub.status.idle":"2022-10-22T13:01:58.624866Z","shell.execute_reply.started":"2022-10-22T13:01:58.485375Z","shell.execute_reply":"2022-10-22T13:01:58.623392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}