File size: 5,070 Bytes
b992f8b
 
3148b97
 
 
b992f8b
3148b97
b992f8b
 
3148b97
 
 
 
 
 
 
 
 
b992f8b
 
3148b97
b992f8b
3148b97
b992f8b
3148b97
 
 
 
 
 
 
394b63e
3148b97
 
 
 
 
 
394b63e
3148b97
394b63e
3148b97
394b63e
3148b97
394b63e
e66e641
394b63e
e66e641
 
 
 
 
 
 
 
 
 
 
 
84a6cc5
e66e641
 
 
 
3148b97
e66e641
 
 
 
 
3148b97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e66e641
394b63e
84a6cc5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
---
tags:
  - image-classification
  - pytorch
  - huggingpics
metrics:
  - accuracy

model-index:
  - name: big-cat-classifier
    results:
      - task:
          name: Image Classification
          type: image-classification
        metrics:
          - name: Accuracy
            type: accuracy
            value: 0.9107142686843872
---

![Big Cat Classifier](./assets/banner_img.png)

An image classifier built using Vision Transformers that categories images of the big cats into the following classes:

| Class | Big Cat | Sample Image                     |
| :---: | :------ | -------------------------------- |
|   0   | Cheetah | ![cheetah](./assets/cheetah.jpg) |
|   1   | Jaguar  | ![jaguar](./assets/jaguar.jpg)   |
|   2   | Leopard | ![leopard](./assets/leopard.jpg) |
|   3   | Lion    | ![lion](./assets/lion.jpg)       |
|   4   | Tiger   | ![tiger](./assets/tiger.jpg)     |

> **Note**:
>
> - 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.
> - 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.
>   - For example: images of Bengal Tigers, Siberian Tigers, Indochinese Tigers, and Malayan Tigers should be identified as Tigers
> - 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.

### Training and Inference

**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)

**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)

## Usage

```python
from PIL import Image
import matplotlib.pyplot as plt
from transformers import ViTFeatureExtractor, ViTForImageClassification

def identify_big_cat(img_path:str)->str:
  """
  Function that reads an image of a big cat (belonging to Panthera family) and returns the corresponding species
  """
  img = Image.open(img_path)
  model_panthera = ViTForImageClassification.from_pretrained("smaranjitghose/big-cat-classifier")
  feature_extractor = ViTFeatureExtractor.from_pretrained('smaranjitghose/big-cat-classifier')
  inputs = feature_extractor(images=img, return_tensors="pt")
  outputs = model_panthera(**inputs)
  logits = outputs.logits
  predicted_class_idx = logits.argmax(-1).item()
  return model_panthera.config.id2label[predicted_class_idx]


 our_big_cat = identify_big_cat("path_of_the_image")
 print(f"Predicted species: {our_big_cat}" )
```

## Hosted API:

Check it out [here](https://huggingface.co/smaranjitghose/big-cat-classifier)

## Library App Usage:

- Clone this repository

```
git clone https://github.com/smaranjitghose/Big_Cat_Classifier.git
```

- Move inside the cloned repository

```
cd Big_Cat_Classifier
```

- Now follow either of following two routes:

A) Without using Docker:

**Make sure you have installed the latest stable version [Python 3](https://www.python.org/downloads/) and added it to PATH**

- Install the python dependencies

```
pip install -r requirements.txt
```

- Start the streamlit app on local server

```
streamlit run app.py
```

B) Using Docker:

**Make sure you have installed [Docker](https://docs.docker.com/engine/install/)**

- Build the Docker Image

```
docker build -t smaranjitghose/big-cat-classifier:latest .
```

- Check if the image is available

```
docker images
```

- Create a Docker container from the image and Run it

```
docker run -t -i -p 8080:8080 --name "big-cat-classifier" smaranjitghose/big-cat-classifier
```

- Open your browser and visit `localhost:8080`

![Streamlit App](./assets/streamlit_app.png)

## Hosting

1. Heroku

- Remove the lines that exposed the particular port in the docker container
- Make sure the startup command is exposed with a variable Port Number

```
ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=$PORT"]
```

- Login to Heroku

```
heroku login -i
```

- Create a new Heroku app

```
heroku create
```

- Login in to Container Registry

```
heroku container:login
```

- Build the Docker image and push it to Container Registry

```
heroku container:push web
```

- Release the app

```
heroku container:release web
```

- Check the hosted version and dashboard

```
heroku open
```

## Reference and Acknowledgement:

[Hugging Pics](https://github.com/nateraw/huggingpics)