KameliaZaman commited on
Commit
6bc9e67
·
verified ·
1 Parent(s): dfd5176

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +282 -47
README.md CHANGED
@@ -12,79 +12,314 @@ license: mit
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
14
 
15
- # Butterfly Classification using CNN
16
-
17
- - **Author:** Kamelia Zaman Moon
18
- - **Project link:** https://huggingface.co/spaces/KameliaZaman/Butterfly-Classification-using-CNN
19
- - **Language(s):** Python
20
- - **License:** MIT
21
- - **Contact:** [email protected]
22
-
23
- ## Table of Contents
24
- - [Introduction](#introduction)
25
- - [Model Architecture](#model-architecture)
26
- - [How-to Guide](#how-to-guide)
27
- - [License](#license)
28
- - [Contributors](#contributors)
29
-
30
- ## 1. Introduction
31
- This project focuses on classifying butterfly images using a Convolutional Neural Network (CNN). The dataset includes images of butterflies and moths, with the goal of training a model to accurately predict the species of a given butterfly image.
32
-
33
- ## 2. Model Architecture
34
- The CNN model used for this project is based on the ResNet50V2 architecture. The model is trained on a dataset consisting of training, validation, and test sets. The key components of the model architecture include convolutional layers, dropout for regularization, global average pooling, and fully connected layers. The model is compiled using the Adam optimizer and sparse categorical crossentropy loss.
35
- ```
36
- ── model_checkpoint_manual_effnet.h5 - generated model.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- ── Butterfly classification with CNN.ipynb - preprocesses input, trains, saves and evaluates the model.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- ── app.py - this module starts the app interface.
41
 
42
- ── README.md - readme file of this project.
43
 
44
- ── requirements.txt - list of required packages.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  ```
46
 
47
- ## 3. How-to Guide
48
- ### 3.1. Data Preparation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  - The dataset is loaded from a CSV file containing information about the butterflies and moths.
50
  - Image paths are constructed based on the dataset information.
51
  - The dataset is split into training, validation, and test sets.
52
 
53
- ### 3.2. Exploratory Data Analysis (EDA)
54
  - Visualizations are created to explore the distribution of labels in the dataset.
55
 
56
- ### 3.3. Image Data Generation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  - Image data generators are used to augment the training data.
58
  - Training and validation data generators are created.
59
 
60
- ### 3.4. Model Training
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  - The ResNet50V2-based model is constructed and compiled.
62
  - The model is trained on the augmented training data, and its performance is monitored using validation data.
63
  - Callbacks for reducing learning rate and early stopping are employed during training.
64
 
65
- ### 3.5. Model Evaluation
 
 
 
 
 
 
 
 
 
 
66
  - The trained model is evaluated on the test set to measure its accuracy.
67
 
68
- ### 3.6. Deployment
 
 
69
  - Gradio is utilized for deploying the trained model.
70
  - Users can input an image, and the model will predict the butterfly species.
71
 
72
- ```bash
73
- # clone project
74
- git clone https://huggingface.co/spaces/KameliaZaman/Butterfly-Classification-using-CNN/tree/main
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
- # go inside the project directory
77
- cd Butterfly-Classification-using-CNN
78
 
79
- # install the required packages
80
- pip install -r requirements.txt
81
 
82
- # run the gradio app
83
- python app.py
84
- ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
- ## 4. License
87
- This project is licensed under the [MIT License](LICENSE).
88
 
89
- ## 5. Contributors
90
- - Kamelia Zaman Moon - kamelia.stu2017@juniv.edu
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
14
 
15
+ <a name="readme-top"></a>
16
+
17
+ <div align="center">
18
+ <img src="https://huggingface.co/KameliaZaman/Butterfly-Classification-Using-CNN/resolve/main/assets/logo.png" alt="Logo" width="500" height="500">
19
+
20
+ <h3 align="center">Butterfly Classification using CNN</h3>
21
+
22
+ <p align="center">
23
+ Butterfly image classification using ResNet50V2
24
+ <br />
25
+ <a href="https://huggingface.co/spaces/KameliaZaman/Butterfly-Classification-using-CNN">View Demo</a>
26
+ </p>
27
+ </div>
28
+
29
+ <!-- TABLE OF CONTENTS -->
30
+ <details>
31
+ <summary>Table of Contents</summary>
32
+ <ol>
33
+ <li>
34
+ <a href="#about-the-project">About The Project</a>
35
+ <ul>
36
+ <li><a href="#built-with">Built With</a></li>
37
+ </ul>
38
+ </li>
39
+ <li>
40
+ <a href="#getting-started">Getting Started</a>
41
+ <ul>
42
+ <li><a href="#dependencies">Dependencies</a></li>
43
+ <li><a href="#installation">Installation</a></li>
44
+ </ul>
45
+ </li>
46
+ <li><a href="#usage">Usage</a></li>
47
+ <li><a href="#contributing">Contributing</a></li>
48
+ <li><a href="#license">License</a></li>
49
+ <li><a href="#contact">Contact</a></li>
50
+ </ol>
51
+ </details>
52
+
53
+ <!-- ABOUT THE PROJECT -->
54
+ ## About The Project
55
+
56
+ <img src="https://huggingface.co/KameliaZaman/Butterfly-Classification-Using-CNN/resolve/main/assets/About.png" alt="Logo" width="500" height="500">
57
+
58
+ The project aims to develop a butterfly image classification system utilizing the ResNet50V2 architecture. The goal is to accurately identify different species of butterflies from images, leveraging the deep learning capabilities of ResNet50V2. This involves training the model on a large dataset of butterfly images, fine-tuning its parameters, and optimizing its performance to achieve high accuracy in classifying various butterfly species. Ultimately, the project seeks to provide a reliable tool for researchers, conservationists, and enthusiasts to easily identify and catalog different butterfly species, aiding in biodiversity studies and conservation efforts.
59
+
60
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
61
+
62
+ ### Built With
63
+
64
+ * [![Python][Python]][Python-url]
65
+ * [![TensorFlow][TensorFlow]][TensorFlow-url]
66
+ * [![OpenCV][OpenCV]][OpenCV-url]
67
+ * [![NumPy][NumPy]][NumPy-url]
68
+ * [![Pandas][Pandas]][Pandas-url]
69
+ * [![Matplotlib][Matplotlib]][Matplotlib-url]
70
+ * [![Plotly][Plotly]][Plotly-url]
71
+
72
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
73
+
74
+ <!-- GETTING STARTED -->
75
+ ## Getting Started
76
+
77
+ Please follow these simple steps to setup this project locally.
78
+
79
+ ### Dependencies
80
+
81
+ Here are the list all libraries, packages and other dependencies that need to be installed to run this project.
82
 
83
+ For example, this is how you would list them:
84
+ * TensorFlow 2.16.1
85
+ ```sh
86
+ conda install -c conda-forge tensorflow
87
+ ```
88
+ * OpenCV 4.9.0
89
+ ```sh
90
+ conda install -c conda-forge opencv
91
+ ```
92
+ * Gradio 4.24.0
93
+ ```sh
94
+ conda install -c conda-forge gradio
95
+ ```
96
+ * NumPy 1.26.4
97
+ ```sh
98
+ conda install -c conda-forge numpy
99
+ ```
100
 
101
+ ### Alternative: Export Environment
102
 
103
+ Alternatively, clone the project repository, install it and have all dependencies needed.
104
 
105
+ ```sh
106
+ conda env export > requirements.txt
107
+ ```
108
+
109
+ Recreate it using:
110
+
111
+ ```sh
112
+ conda env create -f requirements.txt
113
+ ```
114
+
115
+ ### Installation
116
+
117
+ ```sh
118
+ # clone project
119
+ git clone https://huggingface.co/spaces/KameliaZaman/Butterfly-Classification-using-CNN/tree/main
120
+
121
+ # go inside the project directory
122
+ cd Butterfly-Classification-using-CNN
123
+
124
+ # install the required packages
125
+ pip install -r requirements.txt
126
+
127
+ # run the gradio app
128
+ python app.py
129
  ```
130
 
131
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
132
+
133
+ <!-- USAGE EXAMPLES -->
134
+ ## Usage
135
+
136
+ #### Dataset
137
+
138
+ Dataset is from "https://www.kaggle.com/datasets/gpiosenka/butterfly-images40-species" which contains train, test and validation sets for 100 butterfly or moth species.
139
+
140
+ #### Model Architecture
141
+
142
+ ResNet50V2 was used to to train the model. Adam optimizer was applied with a learning rate of 0.0001.
143
+
144
+ <img src="https://huggingface.co/KameliaZaman/Butterfly-Classification-Using-CNN/resolve/main/assets/arch.png" alt="Logo" width="500" height="500">
145
+
146
+ #### Data Preparation
147
  - The dataset is loaded from a CSV file containing information about the butterflies and moths.
148
  - Image paths are constructed based on the dataset information.
149
  - The dataset is split into training, validation, and test sets.
150
 
151
+ #### Exploratory Data Analysis (EDA)
152
  - Visualizations are created to explore the distribution of labels in the dataset.
153
 
154
+ ```sh
155
+ label_counts = df['labels'].value_counts()[:10]
156
+
157
+ fig = px.bar(x=label_counts.index,
158
+ y=label_counts.values,
159
+ color=label_counts.values,
160
+ text=label_counts.values,
161
+ color_continuous_scale='Blues')
162
+
163
+ fig.update_layout(
164
+ title_text='Top 10 Labels Distribution',
165
+ template='plotly_white',
166
+ xaxis=dict(
167
+ title='Label',
168
+ ),
169
+ yaxis=dict(
170
+ title='Count',
171
+ )
172
+ )
173
+
174
+ fig.update_traces(marker_line_color='black',
175
+ marker_line_width=1.5,
176
+ opacity=0.8)
177
+
178
+ fig.show()
179
+ ```
180
+
181
+ <img src="https://huggingface.co/KameliaZaman/Butterfly-Classification-Using-CNN/resolve/main/assets/eda.png" alt="Logo" width="500" height="500">
182
+
183
+ #### Image Data Generation
184
  - Image data generators are used to augment the training data.
185
  - Training and validation data generators are created.
186
 
187
+ ```sh
188
+ train_gen = ImageDataGenerator(horizontal_flip=True, vertical_flip=True, rescale=1/255.)
189
+ val_gen = ImageDataGenerator(rescale=1/255.)
190
+
191
+ BATCH_SIZE = 64
192
+ SEED = 56
193
+ IMAGE_SIZE = (244, 244)
194
+
195
+ train_flow_gen = train_gen.flow_from_directory(directory=train_dir,
196
+ class_mode='sparse',
197
+ batch_size=BATCH_SIZE,
198
+ target_size=IMAGE_SIZE,
199
+ seed=SEED)
200
+
201
+ val_flow_gen = val_gen.flow_from_directory(directory=val_dir,
202
+ class_mode='sparse',
203
+ batch_size=BATCH_SIZE,
204
+ target_size=IMAGE_SIZE,
205
+ seed=SEED)
206
+ ```
207
+
208
+ #### Model Training
209
  - The ResNet50V2-based model is constructed and compiled.
210
  - The model is trained on the augmented training data, and its performance is monitored using validation data.
211
  - Callbacks for reducing learning rate and early stopping are employed during training.
212
 
213
+ ```sh
214
+ resnet_model.fit(train_flow_gen, epochs=15,
215
+ steps_per_epoch=int(np.ceil(train_df.shape[0]/BATCH_SIZE)),
216
+ validation_data=val_flow_gen,
217
+ validation_steps=int(np.ceil(val_df.shape[0]/BATCH_SIZE)),
218
+ callbacks=[rlr_cb, early_cb])
219
+ ```
220
+
221
+ <img src="https://huggingface.co/KameliaZaman/Butterfly-Classification-Using-CNN/resolve/main/assets/train_acc.png" alt="Logo" width="500" height="500">
222
+
223
+ #### Model Evaluation
224
  - The trained model is evaluated on the test set to measure its accuracy.
225
 
226
+ <img src="https://huggingface.co/KameliaZaman/Butterfly-Classification-Using-CNN/resolve/main/assets/test_acc.png" alt="Logo" width="500" height="500">
227
+
228
+ #### Deployment
229
  - Gradio is utilized for deploying the trained model.
230
  - Users can input an image, and the model will predict the butterfly species.
231
 
232
+ ```sh
233
+ import gradio as gr
234
+ import tensorflow as tf
235
+ from tensorflow.keras.models import load_model
236
+ import numpy as np
237
+ import cv2
238
+
239
+ model_path = './model_checkpoint_manual_resnet.h5'
240
+ model = load_model(model_path)
241
+
242
+ class_names = ['ADONIS', 'AFRICAN GIANT SWALLOWTAIL', 'AMERICAN SNOOT', 'AN 88', 'APPOLLO', 'ARCIGERA FLOWER MOTH', 'ATALA', 'ATLAS MOTH', 'BANDED ORANGE HELICONIAN', 'BANDED PEACOCK']
243
+
244
+ def preprocess_image(img):
245
+ if isinstance(img, str):
246
+ # Load and preprocess the image
247
+ img = cv2.imread(img)
248
+ img = cv2.resize(img, (224, 224))
249
+ img = img / 255.0 # Normalize pixel values
250
+ img = np.expand_dims(img, axis=0) # Add batch dimension
251
+ elif isinstance(img, np.ndarray):
252
+ img = cv2.resize(img, (224, 224))
253
+ img = img / 255.0 # Normalize pixel values
254
+ img = np.expand_dims(img, axis=0) # Add batch dimension
255
+ else:
256
+ raise ValueError("Unsupported input type. Please provide a file path or a NumPy array.")
257
+ return img
258
+
259
+ def classify_image(img):
260
+ img = preprocess_image(img)
261
+ predictions = model.predict(img)
262
+ predicted_class = np.argmax(predictions)
263
+ predicted_class_name = class_names[predicted_class]
264
+
265
+ return f"Predicted Class: {predicted_class_name}"
266
+
267
+ iface = gr.Interface(fn=classify_image,
268
+ inputs="image",
269
+ outputs="text",
270
+ live=True)
271
+
272
+ iface.launch()
273
+ ```
274
 
275
+ <img src="https://huggingface.co/KameliaZaman/Butterfly-Classification-Using-CNN/resolve/main/assets/About.png" alt="Logo" width="500" height="500">
 
276
 
277
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
 
278
 
279
+ <!-- CONTRIBUTING -->
280
+ ## Contributing
281
+
282
+ Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.
283
+
284
+ If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
285
+ Don't forget to give the project a star! Thanks again!
286
+
287
+ 1. Fork the Project
288
+ 2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
289
+ 3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
290
+ 4. Push to the Branch (`git push origin feature/AmazingFeature`)
291
+ 5. Open a Pull Request
292
+
293
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
294
+
295
+ <!-- LICENSE -->
296
+ ## License
297
+
298
+ Distributed under the MIT License. See [MIT License](LICENSE) for more information.
299
+
300
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
301
+
302
+ <!-- CONTACT -->
303
+ ## Contact
304
+
305
+ Kamelia Zaman Moon - [email protected]
306
+
307
+ Project Link: [https://huggingface.co/spaces/KameliaZaman/Butterfly-Classification-using-CNN](https://huggingface.co/spaces/KameliaZaman/Butterfly-Classification-using-CNN/tree/main)
308
+
309
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
310
 
 
 
311
 
312
+ [Python]: https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54
313
+ [Python-url]: https://www.python.org/
314
+ [TensorFlow]: https://img.shields.io/badge/TensorFlow-%23FF6F00.svg?style=for-the-badge&logo=TensorFlow&logoColor=white
315
+ [TensorFlow-url]: https://tensorflow.org/
316
+ [OpenCV]: https://img.shields.io/badge/opencv-%23white.svg?style=for-the-badge&logo=opencv&logoColor=white
317
+ [OpenCV-url]: https://opencv.org/
318
+ [NumPy]: https://img.shields.io/badge/numpy-%23013243.svg?style=for-the-badge&logo=numpy&logoColor=white
319
+ [NumPy-url]: https://numpy.org/
320
+ [Pandas]: https://img.shields.io/badge/pandas-%23150458.svg?style=for-the-badge&logo=pandas&logoColor=white
321
+ [Pandas-url]: https://pandas.pydata.org/
322
+ [Matplotlib]: https://img.shields.io/badge/Matplotlib-%23ffffff.svg?style=for-the-badge&logo=Matplotlib&logoColor=black
323
+ [Matplotlib-url]: https://matplotlib.org/
324
+ [Plotly]: https://img.shields.io/badge/Plotly-%233F4F75.svg?style=for-the-badge&logo=plotly&logoColor=white
325
+ [Plotly-url]: https://plotly.com/