Views
No views yet

conda install -c conda-forge tensorflowconda install -c conda-forge opencvconda install -c conda-forge gradioconda install -c conda-forge numpyconda env export > requirements.txtconda env create -f requirements.txt1# clone project
2git clone https://huggingface.co/spaces/KameliaZaman/Butterfly-Classification-using-CNN/tree/main
3
4# go inside the project directory
5cd Butterfly-Classification-using-CNN
6
7# install the required packages
8pip install -r requirements.txt
9
10# run the gradio app
11python app.py 
1label_counts = df['labels'].value_counts()[:10]
2
3fig = px.bar(x=label_counts.index,
4 y=label_counts.values,
5 color=label_counts.values,
6 text=label_counts.values,
7 color_continuous_scale='Blues')
8
9fig.update_layout(
10 title_text='Top 10 Labels Distribution',
11 template='plotly_white',
12 xaxis=dict(
13 title='Label',
14 ),
15 yaxis=dict(
16 title='Count',
17 )
18)
19
20fig.update_traces(marker_line_color='black',
21 marker_line_width=1.5,
22 opacity=0.8)
23
24fig.show()
1train_gen = ImageDataGenerator(horizontal_flip=True, vertical_flip=True, rescale=1/255.)
2val_gen = ImageDataGenerator(rescale=1/255.)
3
4BATCH_SIZE = 64
5SEED = 56
6IMAGE_SIZE = (244, 244)
7
8train_flow_gen = train_gen.flow_from_directory(directory=train_dir,
9 class_mode='sparse',
10 batch_size=BATCH_SIZE,
11 target_size=IMAGE_SIZE,
12 seed=SEED)
13
14val_flow_gen = val_gen.flow_from_directory(directory=val_dir,
15 class_mode='sparse',
16 batch_size=BATCH_SIZE,
17 target_size=IMAGE_SIZE,
18 seed=SEED)1resnet_model.fit(train_flow_gen, epochs=15,
2 steps_per_epoch=int(np.ceil(train_df.shape[0]/BATCH_SIZE)),
3 validation_data=val_flow_gen,
4 validation_steps=int(np.ceil(val_df.shape[0]/BATCH_SIZE)),
5 callbacks=[rlr_cb, early_cb])

1import gradio as gr
2import tensorflow as tf
3from tensorflow.keras.models import load_model
4import numpy as np
5import cv2
6
7model_path = './model_checkpoint_manual_resnet.h5'
8model = load_model(model_path)
9
10class_names = ['ADONIS', 'AFRICAN GIANT SWALLOWTAIL', 'AMERICAN SNOOT', 'AN 88', 'APPOLLO', 'ARCIGERA FLOWER MOTH', 'ATALA', 'ATLAS MOTH', 'BANDED ORANGE HELICONIAN', 'BANDED PEACOCK']
11
12def preprocess_image(img):
13 if isinstance(img, str):
14 # Load and preprocess the image
15 img = cv2.imread(img)
16 img = cv2.resize(img, (224, 224))
17 img = img / 255.0 # Normalize pixel values
18 img = np.expand_dims(img, axis=0) # Add batch dimension
19 elif isinstance(img, np.ndarray):
20 img = cv2.resize(img, (224, 224))
21 img = img / 255.0 # Normalize pixel values
22 img = np.expand_dims(img, axis=0) # Add batch dimension
23 else:
24 raise ValueError("Unsupported input type. Please provide a file path or a NumPy array.")
25 return img
26
27def classify_image(img):
28 img = preprocess_image(img)
29 predictions = model.predict(img)
30 predicted_class = np.argmax(predictions)
31 predicted_class_name = class_names[predicted_class]
32
33 return f"Predicted Class: {predicted_class_name}"
34
35iface = gr.Interface(fn=classify_image,
36 inputs="image",
37 outputs="text",
38 live=True)
39
40iface.launch()
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)