Cheetah Vs Leopard
This project is to create a Image Classification Model to distinguish between 'Cheetah' and 'Leopard'.
- Cheetah in News in India
- Reason for Extinction
- Importing modules to download images
- Difference between "Cheetah" and "Leopard".
- Black tear lines in "Cheeetah" face.
- Download Data
- Let's Train our model(Resnet50)
- Confusion Matrix
- Prediction
Cheetah in News in India
Now a days, In India "Cheetah" has been making News. The reason for this is that "Cheetah" has not been seen in India for nearly about 70 years. Cheetah is an extinct animal from 1950's. But, Now Indian Government has brought big cats from the southern African nation of Namibia to India. They are part of a reintroduction program and will live in a large open space. Cheetahs once lived in India, but died out about 70 years ago. Scientists hope the cats will learn to hunt deer and other wild animals in the area. And they hope the cheetahs will reproduce. Link: https://economictimes.indiatimes.com/news/india/project-cheetah-india-welcomes-cheetahs-after-7-decades-of-extinction/videoshow/94261539.cms
Reason for Extinction
The reasons for the extinction are hunting, human population growth and killing Cheetah due to misunderstanging it with "Leopard". In this project I want to focus on this third reason, misundertood "Cheetah" with "Leopard" Yes, people misunderstood it with "Leopard", since leopard often found to be attacking on Humans but "Cheetah" does not. But, they kind of look same. In this project I will try to build a good working model to distinguish between cheetah and leopard using Fastai Libraries.
!!pip install -Uqq fastai duckduckgo_search
from duckduckgo_search import ddg_images
from fastai.vision.all import *
from fastcore.all import *
from fastdownload import download_url
def search_images(term, max_images=1):
print(f"Searching for {term}")
return L(ddg_images(term, max_results=max_images)).itemgot("image")
def get_image(term, max_n=2):
im = download_url(search_images(term=term, max_images=max_n)[max_n-1], f"{term}.jpg")
return im
Image.open(get_image("Cheetah_face", max_n=1)).to_thumb(256, 256)
Let's have a look in Leopard face.
Image.open(get_image("leopard", max_n=1)).to_thumb(256, 256)
Image.open(get_image("leopard_skin", max_n=1)).to_thumb(256, 256)
Image.open(get_image("cheetah_skin", max_n=5)).to_thumb(256, 256)
searches = "Cheetah", "Leopard"
path = Path("cheetah_or_not")
for o in searches:
dest = (path/o)
dest.mkdir(exist_ok=True, parents=True)
download_images(dest, urls=search_images(f"{o} photo", max_images=200))
Lets get the list containing image files in path.
fns = get_image_files(path)
fns
Deleting files failed to open.
failed = verify_images(get_image_files(path))
failed.map(Path.unlink)
len(failed)
Let's create a template for the dataloaders with DataBlock.
template = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
get_y=parent_label,
splitter= RandomSplitter(valid_pct=0.2, seed=42),
item_tfms=Resize(224)
)
dls = template.dataloaders(path)
dls.show_batch(max_n=5, nrows=1)
model_01 = vision_learner(dls, resnet50, metrics=error_rate)
model_01.fine_tune(2)
interp = ClassificationInterpretation.from_learner(model_01)
interp.plot_confusion_matrix()
As we can see everytime this model predicted correctly except for wher it was 'Cheetah' and it predicted 'Leopard'.
def Predict(im):
pred, pred_idx, probs = model_01.predict(im)
return f"Prediction: {pred}, Probabilty: {probs[pred_idx]}"
Now, Let's get an image and predict.
img = get_image("cheetah")
Image.open(img).to_thumb(256, 256)
Let's model predict the above image.
Predict(img)
Wow ! 100% accurate. That is great. Awesome model. Imagine having high tech "goggles" and we can apply deep learnings model to it. That would be really cool.
Thanks for reading. I hope enjoy this project.