Image classifier model
Image classifier using fastai
Let's build an image classifier model to distinguish between biryani and pulao using minimal code using fastai
Download and Install the dependency
!pip install -Uqq fastai
!pip install -Uqq duckduckgo_search
!pip install -Uqq duckduckgo_search
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m94.9/94.9 kB[0m [31m6.8 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m3.3/3.3 MB[0m [31m78.7 MB/s[0m eta [36m0:00:00[0m
[?25h
Downloading Images with the DuckDuckGo Search API.
from duckduckgo_search import DDGS #
from fastcore.all import *
def search_images(keywords, max_images=200): return L(DDGS().images(keywords, max_results=max_images)).itemgot('image')
urls = search_images('biryani photos', max_images=1)
urls[0]
'https://norecipes.com/wp-content/uploads/2017/05/chicken-biryani-12-1200x1799.jpg'
Download and display a sample biryani image
from fastdownload import download_url
dest = 'biryani.jpg'
download_url(urls[0], dest, show_progress=False)
from fastai.vision.all import *
im = Image.open(dest)
im.to_thumb(256,256)
Download and display the sample pulao image
download_url(search_images('pulao photos', max_images=1)[0], 'pulao.jpg', show_progress=False)
Image.open('pulao.jpg').to_thumb(256,256)
Download a few sample images and store them in a directory
searches = 'biryani','pulao'
path = Path('biryani_or_pulao')
from time import sleep
for o in searches:
dest = (path/o)
dest.mkdir(exist_ok=True, parents=True)
download_images(dest, urls=search_images(f'{o} photo'))
sleep(10)
resize_images(path/o, max_size=400, dest=path/o)
/usr/local/lib/python3.11/dist-packages/PIL/Image.py:1045: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images
warnings.warn(
Path('biryani_or_pulao').ls()
(#2) [Path('biryani_or_pulao/biryani'),Path('biryani_or_pulao/pulao')]
Path('biryani_or_pulao/biryani').ls()
(#174) [Path('biryani_or_pulao/biryani/17e337aa-0f1f-4308-870d-af00065fb1d7.jpg'),Path('biryani_or_pulao/biryani/f1f7c76e-641e-4068-ab5a-a6a654a2df74.jpg'),Path('biryani_or_pulao/biryani/e1b5b551-4686-440e-b43b-47e84c8999bb.jpg'),Path('biryani_or_pulao/biryani/5076de53-8079-438d-b89c-fdd411a1e653.jpg'),Path('biryani_or_pulao/biryani/aeffe34a-c897-47f1-b69d-12d74b1fa59a.jpg'),Path('biryani_or_pulao/biryani/f747a9f3-c1eb-4397-a904-1f8899bd19d1.jpg'),Path('biryani_or_pulao/biryani/cbd00d4a-426e-4625-845d-9fc1a539c9d1.png'),Path('biryani_or_pulao/biryani/71fc60b9-5508-43bb-9cf6-b6c89e2f635b.jpg'),Path('biryani_or_pulao/biryani/4e3b1f22-27aa-45ef-a87b-c947c652cc3d.jpg'),Path('biryani_or_pulao/biryani/eeb50ba0-3179-4b45-a7d2-fb170edb41f4.jpg'),Path('biryani_or_pulao/biryani/5536f520-25c6-4f74-8d5a-f8b2748f4152.png'),Path('biryani_or_pulao/biryani/4964f454-4820-4d81-bd85-20be06293a5c.jpg'),Path('biryani_or_pulao/biryani/debfdb4d-2464-4537-8d2a-4b159047062a.jpg'),Path('biryani_or_pulao/biryani/c16a9633-fac3-4972-90f9-eea623094afd.jpg'),Path('biryani_or_pulao/biryani/59b02b49-7198-4250-ac3e-93b9fc4d5887.jpg'),Path('biryani_or_pulao/biryani/51eed15e-3515-4803-817c-298296f734e2.jpg'),Path('biryani_or_pulao/biryani/e020b91d-a9dd-4da1-9633-0b2d2135894c.jpg'),Path('biryani_or_pulao/biryani/a8bb03e7-b043-4fa7-9ada-9016380b1b86.jpg'),Path('biryani_or_pulao/biryani/d05eedea-e8e0-4809-866b-d512726347a8.jpg'),Path('biryani_or_pulao/biryani/1d9c0d3b-dbb4-425b-92ed-d3bda522333a.jpg')...]
Path('biryani_or_pulao/pulao').ls()
(#191) [Path('biryani_or_pulao/pulao/30f11181-d643-4f19-bb70-3900cff8b90b.JPG'),Path('biryani_or_pulao/pulao/8bb4a1a0-d7e6-4054-8ed0-16d9fa68ab91.webp'),Path('biryani_or_pulao/pulao/aaac006b-7285-4833-85f3-a21eb43f14d6.jpg'),Path('biryani_or_pulao/pulao/7dd32886-28e4-4da3-a5fa-98e5adc198a1.jpg'),Path('biryani_or_pulao/pulao/0f83b123-0e3a-461e-9677-67cc01561da1.jpg'),Path('biryani_or_pulao/pulao/f0e1241c-22cb-466e-975f-71bd83d37f19.jpg'),Path('biryani_or_pulao/pulao/2dac1277-77ea-47bf-bf2b-511d261d858a.jpg'),Path('biryani_or_pulao/pulao/e383c40e-749e-47a5-b57e-ce20cd99c50d.jpg'),Path('biryani_or_pulao/pulao/dd11740c-dbec-40ef-844f-be91efd01178.jpg'),Path('biryani_or_pulao/pulao/f6a40dea-5993-4259-a248-090dbbb0cd4e.jpg'),Path('biryani_or_pulao/pulao/9fdf60c1-2a0b-49e7-92e9-e34751e21be4.jpg'),Path('biryani_or_pulao/pulao/491129c8-b027-4cf0-b5fc-3e9439167189.jpg'),Path('biryani_or_pulao/pulao/66ed3e44-7e7f-4d8a-9914-8c8ebef35e55.jpg'),Path('biryani_or_pulao/pulao/38a84c67-016b-4ef5-a674-5b6b65d815d0.jpg'),Path('biryani_or_pulao/pulao/a6179b3a-7e12-4267-a77b-d1fc70d5fb7b.jpg'),Path('biryani_or_pulao/pulao/d1ab5d5d-bce3-4083-96e9-cdb300dd721c.jpg'),Path('biryani_or_pulao/pulao/cb02fb12-199d-455c-acb1-4dc4ebbda448.jpg'),Path('biryani_or_pulao/pulao/57dec288-4917-4c03-82c6-f05d1481e2e4.jpg'),Path('biryani_or_pulao/pulao/84b35647-ca34-4aef-955a-7cefa92dd77e.jpg'),Path('biryani_or_pulao/pulao/5e44b883-1fa1-47c9-a94b-89230684e4ae.jpg')...]
Using the DuckDuck API, we downloaded a total dataset of 365 food images, consisting of 174
biryani images and 191
pulao images.
Remove dead link images from the dataset.
failed = verify_images(get_image_files(path))
failed.map(Path.unlink)
len(failed)
18
Let's create and check our dataset of biryani and pulao images,
dls = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=RandomSplitter(valid_pct=0.2, seed=42),
get_y=parent_label,
item_tfms=[Resize(192, method='squish')]
).dataloaders(path)
dls.show_batch(max_n=6)
Let's train it using the ResNet-18 architecture for 3 epochs
learn = vision_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(5)
<IPython.core.display.HTML object>
<IPython.core.display.HTML object>
<IPython.core.display.HTML object>
<IPython.core.display.HTML object>
Let's begin our predictions.
is_biryani,_,probs = learn.predict(PILImage.create('biryani.jpg'))
print(f"This is a: {is_biryani}.")
print(f"Probability it's a biryani: {probs[0]:.4f}\nProbability it's a pulao image: {probs[1]:.4f}")
<IPython.core.display.HTML object>
<IPython.core.display.HTML object>
This is a: biryani.
Probability it's a biryani: 0.8207
Probability it's a pulao image: 0.1793
Contents
- Let's build an image classifier model to distinguish between biryani and pulao using minimal code using fastai
- Downloading Images with the DuckDuckGo Search API.
- Download and display a sample biryani image
- Download and display the sample pulao image
- Download a few sample images and store them in a directory
- Using the DuckDuck API, we downloaded a total dataset of 365 food images, consisting of `174` biryani images and `191` pulao images.
- Remove dead link images from the dataset.
- Let's create and check our dataset of biryani and pulao images,
- Let's begin our predictions.
Contents
- Let's build an image classifier model to distinguish between biryani and pulao using minimal code using fastai
- Downloading Images with the DuckDuckGo Search API.
- Download and display a sample biryani image
- Download and display the sample pulao image
- Download a few sample images and store them in a directory
- Using the DuckDuck API, we downloaded a total dataset of 365 food images, consisting of `174` biryani images and `191` pulao images.
- Remove dead link images from the dataset.
- Let's create and check our dataset of biryani and pulao images,
- Let's begin our predictions.