Skip to content

Dewick75/CNN-Model-to-detect-flowers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌺 Flower Classification CNN

A clean, simple CNN for classifying flower images. Focus on image selection testing only.

Project Overview Complete flower classification system with interactive testing

πŸ“ Files

β”œβ”€β”€ CNN.ipynb                    # Clean Jupyter notebook (8 cells)
β”œβ”€β”€ flower_classifier_final.py  # Complete Python script
β”œβ”€β”€ test_image_selection.py     # Test file dialog
β”œβ”€β”€ Dataset/                     # Image dataset
β”‚   β”œβ”€β”€ Anthurm/                # 50 images
β”‚   β”œβ”€β”€ Rose/                   # 50 images
β”‚   └── Sunflower/              # 50 images
β”œβ”€β”€ screenshots/                 # Documentation images
β”‚   β”œβ”€β”€ 01_project_overview.png
β”‚   β”œβ”€β”€ 02_training_process.png
β”‚   β”œβ”€β”€ 03_file_selection.png
β”‚   β”œβ”€β”€ 04_prediction_results.png
β”‚   └── 05_accuracy_graphs.png

πŸš€ Quick Start

Option 1: Jupyter Notebook (Recommended)

  1. Open CNN.ipynb in VS Code
  2. Run cells 1-8 sequentially (Shift+Enter)
  3. Cell 8 opens file dialog to select your image

Training Process Model training in progress showing accuracy improvements

Option 2: Complete Python Script

python flower_classifier_final.py

Option 3: Test File Dialog Only

python test_image_selection.py

File Selection Dialog Interactive file dialog for selecting flower images

πŸ“š Notebook Structure

The CNN.ipynb notebook contains 9 cells:

πŸ”§ Cell 1: Setup & Dataset Check

  • Imports all required libraries
  • Checks dataset availability
  • Shows dataset statistics

πŸ“Š Cell 2: Data Preparation

  • Sets up data generators
  • Configures image preprocessing
  • Splits data into training/validation

πŸ—οΈ Cell 3: Model Architecture

  • Builds CNN model with:
    • 2 Convolutional layers
    • 2 MaxPooling layers
    • Dense layers for classification

🎯 Cell 4: Model Training

  • Trains the model for 10 epochs
  • Shows training progress
  • Displays final accuracy

πŸ’Ύ Cell 5: Save Model

  • Saves trained model as flower_classifier_model.h5
  • Extracts class names for testing

πŸ“ˆ Cell 6: Training Visualization

  • Plots training/validation accuracy
  • Plots training/validation loss
  • Shows training summary

Training Accuracy Graphs Training and validation accuracy/loss curves showing model performance

πŸ› οΈ Cell 7: Image Selection Function

  • Defines interactive testing function
  • File dialog for image selection
  • Results visualization

πŸ§ͺ Cell 8: Test Your Image

  • Execute prediction function
  • Select and classify your own images

🎯 Features

✨ Interactive Testing Options

  1. πŸ“ File Selection: Choose your own images using a file dialog

πŸ” Detailed Results

  • Confidence scores for all classes
  • Visual display of images with predictions
  • Side-by-side visualization with confidence bars

Prediction Results Example prediction showing selected image and confidence scores for all flower classes

πŸ›‘οΈ Error Handling

  • Checks for missing dependencies
  • Validates dataset structure
  • Handles invalid image files
  • User-friendly error messages

πŸ“‹ Requirements

  • Python 3.7+
  • TensorFlow 2.x
  • Matplotlib
  • NumPy
  • Scikit-learn (for evaluation metrics)
  • Seaborn (for confusion matrix)
  • Tkinter (for file dialog - usually included with Python)

🎨 Model Architecture

Input (64x64x3)
    ↓
Conv2D (32 filters, 3x3) + ReLU
    ↓
MaxPooling2D (2x2)
    ↓
Conv2D (64 filters, 3x3) + ReLU
    ↓
MaxPooling2D (2x2)
    ↓
Flatten
    ↓
Dense (128 units) + ReLU
    ↓
Dense (3 units) + Softmax
    ↓
Output (3 classes)

πŸ“Š Expected Results

  • Training Accuracy: ~98-100%
  • Validation Accuracy: ~93-97%
  • Training Time: 1-2 minutes (10 epochs)
  • Model Size: ~1.5MB

πŸ”§ Troubleshooting

Common Issues:

  1. "Dataset not found"

    • Ensure Dataset folder is in the same directory
    • Check folder structure matches the expected format
  2. "Import Error"

    • Install missing packages: pip install tensorflow matplotlib numpy scikit-learn seaborn
  3. "Kernel not found"

    • Install Jupyter extension in VS Code
    • Select the correct Python interpreter
  4. "File dialog not opening"

    • Tkinter might not be installed: pip install tk

🎯 Usage Tips

  1. Run cells in order - Each cell depends on previous ones
  2. Wait for training - Cell 4 takes 1-2 minutes to complete
  3. Test with your images - Use Cell 9 to test with your own flower photos
  4. Check accuracy - Use Cell 6 to see detailed performance metrics

πŸ”¬ What Happened Here - Technical Deep Dive

οΏ½ The Journey: From Broken Code to Working System

Original Problem:

  • Had a Google Colab notebook that couldn't run in VS Code
  • Code used Colab-specific file upload functions
  • Testing functionality was completely broken
  • Code was cluttered with unnecessary comments

What We Fixed:

  • βœ… Removed Google Colab dependencies (google.colab.files)
  • βœ… Implemented native file dialog using tkinter
  • βœ… Simplified code structure and removed clutter
  • βœ… Added proper error handling and validation
  • βœ… Enhanced visualization with side-by-side results
  • βœ… Focused on user preference (image selection only)

🧠 How the Technology Works

1. Convolutional Neural Network (CNN) Architecture

Input Image (64x64x3 RGB)
    ↓
Conv2D(32 filters, 3x3) β†’ Detects edges, corners, basic shapes
    ↓
MaxPooling2D(2x2) β†’ Reduces size, keeps important features
    ↓
Conv2D(64 filters, 3x3) β†’ Detects complex patterns like petals
    ↓
MaxPooling2D(2x2) β†’ Further size reduction
    ↓
Flatten β†’ Converts 2D features to 1D vector
    ↓
Dense(128) β†’ Learns feature combinations
    ↓
Dense(3, softmax) β†’ Outputs probabilities [Anthurm, Rose, Sunflower]

Why This Works:

  • Convolution: Finds patterns regardless of position in image
  • Pooling: Makes model robust to small variations
  • Multiple layers: Learns hierarchy from simple to complex features
  • Dense layers: Combines all learned features for final decision

2. Training Process Breakdown

What Happens During Training:

  1. Forward Pass: Image β†’ CNN β†’ Prediction
  2. Loss Calculation: Compare prediction vs actual label
  3. Backpropagation: Calculate how to adjust weights
  4. Weight Update: Improve model based on errors
  5. Repeat: For all images and epochs

Epoch-by-Epoch Learning:

  • Epochs 1-3: Model learns basic features (edges, colors)
  • Epochs 4-6: Recognizes shapes and patterns (petals, centers)
  • Epochs 7-10: Fine-tunes decision boundaries between classes

3. Image Processing Pipeline

# Step 1: Load image
img = image.load_img(img_path, target_size=(64, 64))

# Step 2: Convert to array
img_array = image.img_to_array(img)  # Shape: (64, 64, 3)

# Step 3: Normalize pixels
img_array = img_array / 255.0  # Convert 0-255 to 0-1

# Step 4: Add batch dimension
img_array = np.expand_dims(img_array, axis=0)  # Shape: (1, 64, 64, 3)

# Step 5: Predict
prediction = model.predict(img_array)  # Output: [0.1, 0.8, 0.1]

Why Each Step Matters:

  • Resizing: Standardizes input size for CNN
  • Normalization: Helps model train faster and more stable
  • Batch dimension: CNN expects multiple images, even if just one
  • Prediction: Returns probability for each class

πŸ“Š Step-by-Step Code Explanation

Cell 1: Setup & Imports

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import tkinter as tk
from tkinter import filedialog

What happens:

  • TensorFlow: Provides deep learning framework
  • ImageDataGenerator: Handles image loading and preprocessing
  • Tkinter: Creates native file dialog for image selection
  • Other imports: NumPy for arrays, Matplotlib for visualization

Cell 2: Data Preparation

train_datagen = ImageDataGenerator(
    rescale=1./255,           # Normalize pixels to 0-1
    validation_split=0.2      # 20% for validation
)

What happens:

  • Rescaling: Converts pixel values from 0-255 to 0-1 range
  • Validation split: Automatically reserves 20% of data for testing
  • Flow from directory: Automatically loads images and creates labels
  • Batch processing: Groups images for efficient GPU processing

Cell 3: Model Architecture

model = Sequential([
    Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)),
    MaxPooling2D(pool_size=(2, 2)),
    Conv2D(64, (3, 3), activation='relu'),
    MaxPooling2D(pool_size=(2, 2)),
    Flatten(),
    Dense(128, activation='relu'),
    Dense(3, activation='softmax')
])

Layer-by-layer breakdown:

  • Conv2D(32): 32 filters detect basic features β†’ Output: 62x62x32
  • MaxPool2D: Reduces size by half β†’ Output: 31x31x32
  • Conv2D(64): 64 filters detect complex features β†’ Output: 29x29x64
  • MaxPool2D: Further reduction β†’ Output: 14x14x64
  • Flatten: 2D β†’ 1D β†’ Output: 12,544 neurons
  • Dense(128): Feature combinations β†’ Output: 128 neurons
  • Dense(3): Final classification β†’ Output: 3 probabilities

Cell 4: Training

history = model.fit(
    train_generator,
    epochs=10,
    validation_data=validation_generator
)

What happens:

  • 10 epochs: Model sees all training data 10 times
  • Batch processing: Processes 32 images at a time
  • Validation: Tests on unseen data after each epoch
  • History tracking: Records accuracy and loss for plotting

Cell 5: Model Saving

model.save('flower_classifier_model.h5')
class_names = list(train_generator.class_indices.keys())

What happens:

  • Save model: Stores complete architecture + trained weights
  • Extract classes: Gets ['Anthurm', 'Rose', 'Sunflower'] for later use
  • H5 format: Efficient binary format for neural networks

Cell 6: Visualization

plt.plot(history.history['accuracy'], label='Training')
plt.plot(history.history['val_accuracy'], label='Validation')

What happens:

  • Training curves: Shows how accuracy improved over epochs
  • Validation tracking: Ensures model isn't overfitting
  • Loss curves: Shows how error decreased during training

Cell 7: Image Selection Function

def select_and_predict_image():
    root = tk.Tk()
    root.withdraw()
    img_path = filedialog.askopenfilename(...)

What happens:

  • Tkinter setup: Creates hidden window for file dialog
  • File dialog: Opens native OS file picker
  • Image loading: Loads and preprocesses selected image
  • Prediction: Runs image through trained CNN
  • Visualization: Shows image + confidence bars side-by-side

Cell 8: Testing

select_and_predict_image()

What happens:

  • File dialog opens: User selects flower image
  • Image preprocessing: Resize, normalize, add batch dimension
  • CNN prediction: Forward pass through trained network
  • Results display: Image + probability bars + console output

πŸ”§ Technology Stack Deep Dive

TensorFlow/Keras

  • Purpose: Deep learning framework
  • Why chosen: Industry standard, excellent documentation
  • Key features: GPU acceleration, automatic differentiation
  • In our project: Builds and trains CNN model

ImageDataGenerator

  • Purpose: Efficient image loading and preprocessing
  • Why chosen: Handles large datasets without memory issues
  • Key features: Automatic resizing, normalization, augmentation
  • In our project: Loads flower images in batches

Tkinter File Dialog

  • Purpose: Native file selection interface
  • Why chosen: Cross-platform, built into Python
  • Key features: OS-native appearance, file type filtering
  • In our project: Replaces Google Colab file upload

Matplotlib Visualization

  • Purpose: Display images and graphs
  • Why chosen: Integrates well with Jupyter notebooks
  • Key features: Subplots, customizable styling
  • In our project: Shows prediction results and training curves

NumPy Arrays

  • Purpose: Efficient numerical operations
  • Why chosen: Foundation for all ML libraries
  • Key features: Fast array operations, broadcasting
  • In our project: Image data manipulation and processing

πŸŽ“ Learning Outcomes

What You Learn:

  1. Deep Learning: How CNNs work for image classification
  2. Computer Vision: Image preprocessing and feature extraction
  3. Software Engineering: Clean code, error handling, user interfaces
  4. Data Science: Train/validation splits, performance metrics
  5. Python Programming: Libraries integration, file handling

Skills Developed:

  • Building neural networks from scratch
  • Data preprocessing and visualization
  • Creating interactive user interfaces
  • Model training and evaluation
  • Debugging and troubleshooting

Real-World Applications:

  • Agriculture: Automated plant disease detection
  • Botany: Species identification for research
  • Mobile Apps: Plant identification applications
  • E-commerce: Automatic product categorization
  • Education: Interactive learning tools

�🌟 Next Steps

  • Add more flower classes
  • Implement data augmentation
  • Try transfer learning with pre-trained models
  • Deploy as a web application
  • Add real-time camera classification

You can collect same vertion of google colab codes from this link- https://colab.research.google.com/drive/16VEieLiiel4eTJ2I8aeueMUnGI5EmoF2?usp=sharing

Happy Flower Classification! πŸŒΊπŸ€–

This project demonstrates the complete journey from a broken Colab notebook to a production-ready flower classification system with clean code, proper error handling, and user-friendly interface.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published