smaranjitghose
commited on
Commit
β’
3148b97
1
Parent(s):
71cab96
Updated Readme
Browse files- .dockerignore +4 -0
- Dockerfile +23 -0
- README.md +141 -29
- app.py +45 -0
- app.yaml +9 -0
- assets/banner_img.png +0 -0
- {images β assets}/cheetah.jpg +0 -0
- {images β assets}/jaguar.jpg +0 -0
- {images β assets}/leopard.jpg +0 -0
- {images β assets}/lion.jpg +0 -0
- assets/streamlit_app.png +0 -0
- {images β assets}/tiger.jpg +0 -0
- notebooks/Big_Cat_Classifier.ipynb +0 -0
- notebooks/Big_Cat_Classifier_Inference.ipynb +0 -0
- requirements.txt +0 -0
- demo.txt β src/__init__.py +0 -0
- src/__pycache__/__init__.cpython-38.pyc +0 -0
- src/__pycache__/big_cat_classifier.cpython-38.pyc +0 -0
- src/__pycache__/test.cpython-38.pyc +0 -0
- src/big_cat_classifier.py +20 -0
.dockerignore
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
data/*
|
2 |
+
README.MD
|
3 |
+
notebooks/*
|
4 |
+
.gitignore
|
Dockerfile
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Get the base image
|
2 |
+
FROM python:3.8-slim-buster
|
3 |
+
|
4 |
+
#Expose port 8080
|
5 |
+
# EXPOSE 8080
|
6 |
+
|
7 |
+
#Copy Requirements.txt file into app directory
|
8 |
+
COPY requirements.txt app/requirements.txt
|
9 |
+
|
10 |
+
#install all requirements in requirements.txt
|
11 |
+
RUN pip install -r app/requirements.txt
|
12 |
+
|
13 |
+
#Copy all files in current directory into app directory
|
14 |
+
COPY . /app
|
15 |
+
|
16 |
+
#Change Working Directory to app directory
|
17 |
+
WORKDIR /app
|
18 |
+
|
19 |
+
# Run the application on port 8080 for Local Host or GCP
|
20 |
+
# ENTRYPOINT ["streamlit", "run", "app.py", "--server.port 8080", "--server.address 0.0.0.0"]
|
21 |
+
|
22 |
+
# Heroku Startup Command
|
23 |
+
CMD streamlit run app.py --server.port $PORT
|
README.md
CHANGED
@@ -1,43 +1,47 @@
|
|
1 |
---
|
2 |
tags:
|
3 |
-
- image-classification
|
4 |
-
- pytorch
|
5 |
-
- huggingpics
|
6 |
metrics:
|
7 |
-
- accuracy
|
8 |
|
9 |
model-index:
|
10 |
-
- name: big-cat-classifier
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
---
|
20 |
|
|
|
21 |
|
|
|
22 |
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|:---:|:--------|-------------------------------|
|
29 |
-
| 0 | Cheetah |![cheetah](./images/cheetah.jpg) |
|
30 |
-
| 1 | Jaguar |![jaguar](./images/jaguar.jpg) |
|
31 |
-
| 2 | Leopard |![leopard](./images/leopard.jpg) |
|
32 |
-
| 3 | Lion |![lion](./images/lion.jpg) |
|
33 |
-
| 4 | Tiger |![tiger](./images/tiger.jpg) |
|
34 |
|
|
|
35 |
|
36 |
-
|
37 |
-
> - Since jaguars and leopards have similar appearances, the model might confuse the two. These [[1](https://www.nationalgeographic.com/animals/article/animals-big-cats-jaguars-leopards)] [[2](https://safarisafricana.com/jaguar-v-leopard/)] two articles throw some light on the difference between the two species.
|
38 |
-
> - Theoretically the model should be able to accurately identify geographical population variants of each species. However, in practical scenarios this may not be true as during the training phases this was not kept in mind while collecting the dataset.
|
39 |
-
> - For example: images of Bengal Tigers, Siberian Tigers, Indochinese Tigers, and Malayan Tigers should be identified as Tigers
|
40 |
-
> - Lastly, the performance of the model in categorizing certain rare variants in the populations of big cats such as white tigers, snow leopards, or black panther has not been determined exclusively. Although some of the tests performed gave satisfactory results.
|
41 |
|
42 |
## Usage
|
43 |
|
@@ -58,13 +62,121 @@ def identify_big_cat(img_path:str)->str:
|
|
58 |
logits = outputs.logits
|
59 |
predicted_class_idx = logits.argmax(-1).item()
|
60 |
return model_panthera.config.id2label[predicted_class_idx]
|
61 |
-
|
62 |
|
63 |
our_big_cat = identify_big_cat("path_of_the_image")
|
64 |
print(f"Predicted species: {our_big_cat}" )
|
65 |
```
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
## Reference and Acknowledgement:
|
68 |
|
69 |
[Hugging Pics](https://github.com/nateraw/huggingpics)
|
70 |
-
|
|
|
1 |
---
|
2 |
tags:
|
3 |
+
- image-classification
|
4 |
+
- pytorch
|
5 |
+
- huggingpics
|
6 |
metrics:
|
7 |
+
- accuracy
|
8 |
|
9 |
model-index:
|
10 |
+
- name: big-cat-classifier
|
11 |
+
results:
|
12 |
+
- task:
|
13 |
+
name: Image Classification
|
14 |
+
type: image-classification
|
15 |
+
metrics:
|
16 |
+
- name: Accuracy
|
17 |
+
type: accuracy
|
18 |
+
value: 0.9107142686843872
|
19 |
---
|
20 |
|
21 |
+
![Big Cat Classifier](./assets/banner_img.png)
|
22 |
|
23 |
+
An image classifier built using Vision Transformers that categories images of the big cats into the following classes:
|
24 |
|
25 |
+
| Class | Big Cat | Sample Image |
|
26 |
+
| :---: | :------ | -------------------------------- |
|
27 |
+
| 0 | Cheetah | ![cheetah](./assets/cheetah.jpg) |
|
28 |
+
| 1 | Jaguar | ![jaguar](./assets/jaguar.jpg) |
|
29 |
+
| 2 | Leopard | ![leopard](./assets/leopard.jpg) |
|
30 |
+
| 3 | Lion | ![lion](./assets/lion.jpg) |
|
31 |
+
| 4 | Tiger | ![tiger](./assets/tiger.jpg) |
|
32 |
|
33 |
+
> **Note**:
|
34 |
+
>
|
35 |
+
> - Since jaguars and leopards have similar appearances, the model might confuse the two. These [[1](https://www.nationalgeographic.com/animals/article/animals-big-cats-jaguars-leopards)] [[2](https://safarisafricana.com/jaguar-v-leopard/)] two articles throw some light on the difference between the two species.
|
36 |
+
> - Theoretically the model should be able to accurately identify geographical population variants of each species. However, in practical scenarios this may not be true as during the training phases this was not kept in mind while collecting the dataset.
|
37 |
+
> - For example: images of Bengal Tigers, Siberian Tigers, Indochinese Tigers, and Malayan Tigers should be identified as Tigers
|
38 |
+
> - Lastly, the performance of the model in categorizing certain rare variants in the populations of big cats such as white tigers, snow leopards, or black panther has not been determined exclusively. Although some of the tests performed gave satisfactory results.
|
39 |
|
40 |
+
### Training and Inference
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
**Training**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/smaranjitghose/Big_Cat_Classifier/blob/master/notebooks/Big_Cat_Classifier.ipynb)
|
43 |
|
44 |
+
**Inference**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/smaranjitghose/Big_Cat_Classifier/blob/master/notebooks/Big_Cat_Classifier_Inference.ipynb)
|
|
|
|
|
|
|
|
|
45 |
|
46 |
## Usage
|
47 |
|
|
|
62 |
logits = outputs.logits
|
63 |
predicted_class_idx = logits.argmax(-1).item()
|
64 |
return model_panthera.config.id2label[predicted_class_idx]
|
65 |
+
|
66 |
|
67 |
our_big_cat = identify_big_cat("path_of_the_image")
|
68 |
print(f"Predicted species: {our_big_cat}" )
|
69 |
```
|
70 |
|
71 |
+
## Hosted API:
|
72 |
+
|
73 |
+
Check it out [here](https://huggingface.co/smaranjitghose/big-cat-classifier)
|
74 |
+
|
75 |
+
## Library App Usage:
|
76 |
+
|
77 |
+
- Clone this repository
|
78 |
+
|
79 |
+
```
|
80 |
+
git clone https://github.com/smaranjitghose/Big_Cat_Classifier.git
|
81 |
+
```
|
82 |
+
|
83 |
+
- Move inside the cloned repository
|
84 |
+
|
85 |
+
```
|
86 |
+
cd Big_Cat_Classifier
|
87 |
+
```
|
88 |
+
|
89 |
+
- Now follow either of following two routes:
|
90 |
+
|
91 |
+
A) Without using Docker:
|
92 |
+
|
93 |
+
**Make sure you have installed the latest stable version [Python 3](https://www.python.org/downloads/) and added it to PATH**
|
94 |
+
|
95 |
+
- Install the python dependencies
|
96 |
+
|
97 |
+
```
|
98 |
+
pip install -r requirements.txt
|
99 |
+
```
|
100 |
+
|
101 |
+
- Start the streamlit app on local server
|
102 |
+
|
103 |
+
```
|
104 |
+
streamlit run app.py
|
105 |
+
```
|
106 |
+
|
107 |
+
B) Using Docker:
|
108 |
+
|
109 |
+
**Make sure you have installed [Docker](https://docs.docker.com/engine/install/)**
|
110 |
+
|
111 |
+
- Build the Docker Image
|
112 |
+
|
113 |
+
```
|
114 |
+
docker build -t smaranjitghose/big-cat-classifier:latest .
|
115 |
+
```
|
116 |
+
|
117 |
+
- Check if the image is available
|
118 |
+
|
119 |
+
```
|
120 |
+
docker images
|
121 |
+
```
|
122 |
+
|
123 |
+
- Create a Docker container from the image and Run it
|
124 |
+
|
125 |
+
```
|
126 |
+
docker run -t -i -p 8080:8080 --name "big-cat-classifier" smaranjitghose/big-cat-classifier
|
127 |
+
```
|
128 |
+
|
129 |
+
- Open your browser and visit `localhost:8080`
|
130 |
+
|
131 |
+
![Streamlit App](./assets/streamlit_app.png)
|
132 |
+
|
133 |
+
## Hosting
|
134 |
+
|
135 |
+
1. Heroku
|
136 |
+
|
137 |
+
- Remove the lines that exposed the particular port in the docker container
|
138 |
+
- Make sure the startup command is exposed with a variable Port Number
|
139 |
+
|
140 |
+
```
|
141 |
+
ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=$PORT"]
|
142 |
+
```
|
143 |
+
|
144 |
+
- Login to Heroku
|
145 |
+
|
146 |
+
```
|
147 |
+
heroku login -i
|
148 |
+
```
|
149 |
+
|
150 |
+
- Create a new Heroku app
|
151 |
+
|
152 |
+
```
|
153 |
+
heroku create
|
154 |
+
```
|
155 |
+
|
156 |
+
- Login in to Container Registry
|
157 |
+
|
158 |
+
```
|
159 |
+
heroku container:login
|
160 |
+
```
|
161 |
+
|
162 |
+
- Build the Docker image and push it to Container Registry
|
163 |
+
|
164 |
+
```
|
165 |
+
heroku container:push web
|
166 |
+
```
|
167 |
+
|
168 |
+
- Release the app
|
169 |
+
|
170 |
+
```
|
171 |
+
heroku container:release web
|
172 |
+
```
|
173 |
+
|
174 |
+
- Check the hosted version and dashboard
|
175 |
+
|
176 |
+
```
|
177 |
+
heroku open
|
178 |
+
```
|
179 |
+
|
180 |
## Reference and Acknowledgement:
|
181 |
|
182 |
[Hugging Pics](https://github.com/nateraw/huggingpics)
|
|
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
from PIL import Image, ImageOps
|
5 |
+
|
6 |
+
from src import big_cat_classifier
|
7 |
+
|
8 |
+
|
9 |
+
def main():
|
10 |
+
st.set_page_config(
|
11 |
+
page_title="Big Cat Classifier",
|
12 |
+
layout="centered",
|
13 |
+
initial_sidebar_state="collapsed",
|
14 |
+
)
|
15 |
+
banner_img = Image.open("./assets/banner_img.png")
|
16 |
+
st.image(banner_img)
|
17 |
+
st.title("Coded with β€οΈ by Smaranjit Ghose")
|
18 |
+
st.text("")
|
19 |
+
st.text("")
|
20 |
+
st.text("")
|
21 |
+
|
22 |
+
uploaded_file = st.file_uploader("Choose an image..", type=["jpg", "png", "jpeg"])
|
23 |
+
|
24 |
+
if st.button("Predict"):
|
25 |
+
if uploaded_file is not None:
|
26 |
+
try:
|
27 |
+
img = Image.open(uploaded_file)
|
28 |
+
st.subheader("Your Image:")
|
29 |
+
st.image(img)
|
30 |
+
st.write("")
|
31 |
+
st.write("")
|
32 |
+
|
33 |
+
with st.spinner("Our AI forest officer has started analyzing...."):
|
34 |
+
label = big_cat_classifier.classifier(uploaded_file)
|
35 |
+
time.sleep(5)
|
36 |
+
st.success(f"We think this is an image of a {label}")
|
37 |
+
|
38 |
+
except:
|
39 |
+
st.error("We apologize something went wrong ππ½ββοΈ")
|
40 |
+
else:
|
41 |
+
st.error("Can you please upload an image ππ½ββοΈ")
|
42 |
+
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
main()
|
app.yaml
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
service: big-cat-classifier
|
2 |
+
runtime: custom
|
3 |
+
env: flex
|
4 |
+
manual_scaling:
|
5 |
+
instances: 1
|
6 |
+
resources:
|
7 |
+
cpu: 1
|
8 |
+
memory_gb: 0.5
|
9 |
+
disk_size_gb: 10
|
assets/banner_img.png
ADDED
{images β assets}/cheetah.jpg
RENAMED
File without changes
|
{images β assets}/jaguar.jpg
RENAMED
File without changes
|
{images β assets}/leopard.jpg
RENAMED
File without changes
|
{images β assets}/lion.jpg
RENAMED
File without changes
|
assets/streamlit_app.png
ADDED
{images β assets}/tiger.jpg
RENAMED
File without changes
|
notebooks/Big_Cat_Classifier.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
notebooks/Big_Cat_Classifier_Inference.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
requirements.txt
ADDED
Binary file (68 Bytes). View file
|
|
demo.txt β src/__init__.py
RENAMED
File without changes
|
src/__pycache__/__init__.cpython-38.pyc
ADDED
Binary file (135 Bytes). View file
|
|
src/__pycache__/big_cat_classifier.cpython-38.pyc
ADDED
Binary file (861 Bytes). View file
|
|
src/__pycache__/test.cpython-38.pyc
ADDED
Binary file (282 Bytes). View file
|
|
src/big_cat_classifier.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
3 |
+
|
4 |
+
|
5 |
+
def classifier(img_path: str) -> str:
|
6 |
+
"""
|
7 |
+
Function that reads an image of a big cat (belonging to Panthera family) and returns the corresponding species
|
8 |
+
"""
|
9 |
+
img = Image.open(img_path)
|
10 |
+
model_panthera = ViTForImageClassification.from_pretrained(
|
11 |
+
"smaranjitghose/big-cat-classifier"
|
12 |
+
)
|
13 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(
|
14 |
+
"smaranjitghose/big-cat-classifier"
|
15 |
+
)
|
16 |
+
inputs = feature_extractor(images=img, return_tensors="pt")
|
17 |
+
outputs = model_panthera(**inputs)
|
18 |
+
logits = outputs.logits
|
19 |
+
predicted_class_idx = logits.argmax(-1).item()
|
20 |
+
return model_panthera.config.id2label[predicted_class_idx]
|