{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "1378866b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Searching for 'elon musk face'\n", "Searching for 'white person face'\n", "Searching for 'black person face'\n", "Searching for 'asian person face'\n" ] } ], "source": [ "# Gather Data\n", "from duckduckgo_search import ddg_images\n", "from fastcore.all import *\n", "from fastai.vision.all import *\n", "\n", "\n", "def search_images(term, max_images=200):\n", " print(f\"Searching for '{term}'\")\n", " return L(ddg_images(term, max_results=max_images)).itemgot('image')\n", "\n", "import gradio as gr\n", "\n", "types = 'elon musk face','white person face', 'black person face', 'asian person face'\n", "\n", "path = Path('persons')\n", "\n", "\n", "# Only create data once when it's re-run.\n", "if not path.exists():\n", " path.mkdir()\n", " for t in types:\n", " dest = path/t\n", " dest.mkdir(exist_ok=True)\n", " results = search_images(t)\n", " download_images(dest, urls=results)\n", "\n", "\n", " \n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "f76cc0de", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(#629) [Path('persons/elon musk face/c60f56fe-b57c-44b9-a8c6-47f2cbf10771.jpg'),Path('persons/elon musk face/4aac48a6-fda7-451a-ba08-da1b75c1323b.png'),Path('persons/elon musk face/c3740dbb-d839-4066-9513-a388261f7389.jpg'),Path('persons/elon musk face/ea70a9c6-5990-437e-bdfb-cd3013a63d0f.jpg'),Path('persons/elon musk face/a648ebb2-abef-44ac-a650-a99dff4375d0.jpg'),Path('persons/elon musk face/faf90128-0b26-4224-9ed1-9a9e8e32c614.jpg'),Path('persons/elon musk face/6ba4b044-bac8-453d-8a83-01e63e2a668f.jpeg'),Path('persons/elon musk face/61175208-5b37-41eb-aa2b-293207d0d860.jpg'),Path('persons/elon musk face/ace5a005-97c4-4871-98bd-aa210469e477.jpg'),Path('persons/elon musk face/4fc06333-0390-43ed-b45a-2d78c4f95e48.jpg')...]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fns = get_image_files(path)\n", "fns" ] }, { "cell_type": "code", "execution_count": 3, "id": "0b5c9b30", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(#12) [Path('persons/elon musk face/d02548e5-655a-4a2e-af71-d4ab4ea4510b.jpg'),Path('persons/elon musk face/e04381fc-2921-4634-b1f7-a994fd2d7ec5.jpg'),Path('persons/elon musk face/33f813ce-db26-477f-bf69-592f4228271b.jpg'),Path('persons/elon musk face/4c93cfe5-41ce-4b0e-8448-8b8624a635e0.jpg'),Path('persons/black person face/e8e75411-70f5-4bd0-a3d7-9bef36c9f342.jpg'),Path('persons/black person face/1d585214-d24f-4c26-ab0a-ade845b5132a.jpg'),Path('persons/black person face/4982215e-ada5-4a46-b94d-1ff1201914a6.jpg'),Path('persons/black person face/58b0a96c-e994-44d9-8f77-a33420f0cf4f.jpg'),Path('persons/black person face/1b83ed09-4e90-4212-a511-ee3073c8e951.jpg'),Path('persons/white person face/72a3dbcf-baba-483b-8f4c-6ecbc2f7bacf.jpg')...]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "failed = verify_images(fns)\n", "failed" ] }, { "cell_type": "code", "execution_count": 4, "id": "e0561885", "metadata": {}, "outputs": [], "source": [ "failed.map(Path.unlink);" ] }, { "cell_type": "code", "execution_count": 5, "id": "f07d13a5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
ImageDataLoaders(*loaders, path:str|pathlib.Path='.', device=None)
Basic wrapper around several `DataLoader`s with factory methods for computer vision problems
\n", "" ], "text/plain": [ "ImageDataLoaders.from_name_func(path:str|Path, fnames:list, label_func:callable, valid_pct=0.2, seed=None, item_tfms=None, batch_tfms=None, img_cls=, bs:int=64, val_bs:int=None, shuffle:bool=True, device=None)
Create from the name attrs of `fnames` in `path`s with `label_func`
\n", "" ], "text/plain": [ "| epoch | \n", "train_loss | \n", "valid_loss | \n", "error_rate | \n", "time | \n", "
|---|---|---|---|---|
| 0 | \n", "2.229013 | \n", "2.284423 | \n", "0.577236 | \n", "00:19 | \n", "
| epoch | \n", "train_loss | \n", "valid_loss | \n", "error_rate | \n", "time | \n", "
|---|---|---|---|---|
| 0 | \n", "1.476069 | \n", "1.188412 | \n", "0.447154 | \n", "00:26 | \n", "
| 1 | \n", "1.253272 | \n", "1.024922 | \n", "0.349593 | \n", "00:28 | \n", "
| 2 | \n", "1.095812 | \n", "0.967547 | \n", "0.317073 | \n", "00:22 | \n", "
| 3 | \n", "0.954540 | \n", "0.912305 | \n", "0.308943 | \n", "00:29 | \n", "