{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Is it a bird?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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", "\n", "import socket,warnings\n", "try:\n", " socket.setdefaulttimeout(1)\n", " socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(('1.1.1.1', 53))\n", "except socket.error as ex: raise Exception(\"STOP: No internet. Click '>|' in top right and set 'Internet' switch to on\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# It's a good idea to ensure you're running the latest version of any libraries you need.\n", "# `!pip install -Uqq ` upgrades to the latest version of \n", "# NB: You can safely ignore any warnings or errors pip spits out about running as root or incompatibilities\n", "import os\n", "iskaggle = os.environ.get('KAGGLE_KERNEL_RUN_TYPE', '')\n", "\n", "if iskaggle:\n", " !pip install -Uqq fastai" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1: Download images of birds and non-birds" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Skip this cell if you already have duckduckgo_search installed\n", "!pip install -Uqq duckduckgo_search" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from duckduckgo_search import DDGS #DuckDuckGo has changed the api so we need to update \n", "from fastcore.all import *\n", "\n", "def search_images(keywords, max_images=200): return L(DDGS().images(keywords, max_results=max_images)).itemgot('image')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "urls = search_images('bird photos', max_images=1)\n", "urls[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from fastdownload import download_url\n", "dest = 'bird.jpg'\n", "download_url(urls[0], dest, show_progress=False)\n", "\n", "from fastai.vision.all import *\n", "im = Image.open(dest)\n", "im.to_thumb(256,256)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "download_url(search_images('forest photos', max_images=1)[0], 'forest.jpg', show_progress=False)\n", "Image.open('forest.jpg').to_thumb(256,256)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "searches = 'forest','bird'\n", "path = Path('bird_or_not')\n", "from time import sleep\n", "\n", "for 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)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2: Train our model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "failed = verify_images(get_image_files(path))\n", "failed.map(Path.unlink)\n", "len(failed)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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", "\n", "dls.show_batch(max_n=6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn = vision_learner(dls, resnet18, metrics=error_rate)\n", "learn.fine_tune(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3: Use our model (and build your own!)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "is_bird,_,probs = learn.predict(PILImage.create('bird.jpg'))\n", "print(f\"This is a: {is_bird}.\")\n", "print(f\"Probability it's a bird: {probs[0]:.4f}\\nProbability it's a forest image: {probs[1]:.4f}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 4 }