A deep learning approach for seed classification using transfer learning with multiple pre-trained convolutional neural networks for comparative analysis.
This project implements seed classification using a two-stage approach: feature extraction from pre-trained CNN models followed by custom classification layers. The study compares the performance of approximately 9 different pre-trained models including DenseNet121, InceptionV3, VGG19, ResNet, NASNet, MobileNet, and others.
- Feature Extraction: Extract features using pre-trained CNN models (without top classification layers)
- Classification: Train custom dense neural networks on the extracted features
- DenseNet121
- InceptionV3
- VGG19
- ResNet
- NASNet
- MobileNet
- Additional models (approximately 9 total for comprehensive comparison)
Your dataset should be organized in the following structure:
your_dataset_folder/
├── train/
│ ├── seed_class_1/
│ │ ├── image1.jpg
│ │ ├── image2.jpg
│ │ └── ...
│ ├── seed_class_2/
│ ├── seed_class_3/
│ ├── seed_class_4/
│ └── seed_class_5/
└── test/
├── seed_class_1/
├── seed_class_2/
├── seed_class_3/
├── seed_class_4/
└── seed_class_5/
- Training Samples: 4,000 images total
- Testing Samples: 1,000 images total
- Image Requirements: Any format (JPG, PNG, etc.)
- Number of Classes: 5 seed types
- Recommended: 800 images per class for training, 200 per class for testing
Install required libraries:
tensorflow
keras
numpy
scikit-learn
joblib
matplotlib
pillow
- Organize your custom dataset in the folder structure shown above
- Update dataset paths in the code:
- Replace
train_dirwith your training folder path - Replace
test_dirwith your testing folder path
- Replace
- Data splitting: If you have a single dataset folder, you need to split it into train/test sets manually or programmatically
- Image format: Ensure all images are in standard formats (JPG, PNG, etc.)
- Specify your dataset path: Update the
train_dirandtest_dirvariables to point to your custom dataset location - Example: If your dataset is in
/home/user/seed_dataset/, set paths accordingly
- Manual Split: Organize your data into separate train and test folders before running the code
- Train/Test Ratio: Recommended 80:20 split (4000 train, 1000 test images)
- Class Balance: Ensure each class has adequate representation in both train and test sets
- Sample Count: Modify
sample_countparameter based on your actual dataset size - Batch Size: Adjust
batch_size(default 32) based on your system memory - Number of Classes: Update the final Dense layer if you have different number of seed classes
- DenseNet121 pre-trained model loaded without top classification layers
- Input images resized to 224×224×3 pixels
- Features extracted using global average pooling from DenseNet121
- Single feature extraction process with batch-wise processing for memory efficiency
- Input Layer: 1024-dimensional feature vectors from DenseNet121
- Multiple Classifier Designs: Different architectures inspired by various models (InceptionV3, VGG19, ResNet, NASNet, MobileNet, etc.)
- Hidden Layers: Various combinations of Dense layers with ReLU activation, BatchNormalization, and Dropout
- Output Layer: Softmax activation for multi-class classification (5 seed classes)
- Optimization: Adam optimizer with categorical crossentropy loss
- Organize your custom seed dataset in the required folder structure
- Ensure proper train/test split
- Verify image formats and quality
- Load DenseNet121 pre-trained model for feature extraction
- Extract features from your custom dataset using DenseNet121
- Save extracted features as single joblib file (
densenet_features.joblib)
- Load DenseNet121 extracted features from joblib file
- Train multiple different classifiers (InceptionV3-style, VGG19-style, ResNet-style, etc.) on same features
- Save each trained model as
.kerasfile in their respective folders - Apply different classification architectures to same feature set for comparison
- Evaluate each model on test set
- Compare performance metrics across all models
- Analyze results for best performing architecture
- Single Feature Extraction: Features extracted only using DenseNet121 model
- Feature Caching: DenseNet121 features saved as single
.joblibfile for reuse - Consistent Preprocessing: Same image preprocessing applied once
- Batch Processing: Memory-efficient feature extraction from DenseNet121
- DenseNet121 features used as input for all classifier models
- Multiple classifier architectures trained on same DenseNet121 features
- Identical train/test splits and features for fair comparison
- Different classification approaches (InceptionV3-style, VGG19-style, ResNet-style, etc.) applied to same feature set
- Update file paths to match your custom dataset location
- Verify data split - ensure you have separate train and test folders
- Check dataset size - adjust sample counts in the code accordingly
- Validate image formats - ensure all images can be loaded properly
- Large datasets: Consider processing in smaller batches during feature extraction
- Feature storage: Single joblib file can be large for big datasets
- GPU memory: Monitor GPU usage during DenseNet121 feature extraction
- Model storage: Each trained classifier saved separately as
.kerasfiles for easy access
- Number of classes: Modify final Dense layer output units in all classifier architectures
- Dataset size: Update sample_count parameters for feature extraction
- Model architecture: Adjust hidden layer sizes for different classifiers if needed
- Training parameters: Tune epochs, learning rate, etc. for each model
- Model saving: Ensure proper folder structure for saving each trained
.kerasmodel
- Feature files (
.joblib) for each pre-trained model - Trained classifier models for each architecture
- Performance metrics and comparison results
- Accuracy comparison across all models
- Training time and computational efficiency analysis
- Model performance on your specific seed types
- Best performing architecture recommendation
- Agricultural Technology: Custom seed variety identification
- Transfer Learning Research: Architecture comparison for domain-specific tasks
- Computer Vision: Performance analysis on specialized datasets
Configure your custom dataset paths and ensure proper data splitting before running the experiments.
After completing the model comparison and selecting the best performing classifier, the chosen model can be deployed as a web application for real-time seed classification.
The deployment involves creating a Flask web application that:
- Loads the best performing trained model (
.kerasfile) - Uses DenseNet121 for feature extraction from uploaded images
- Provides predictions through a user-friendly web interface
- Returns classification results with confidence scores
- Flask Server: Handles HTTP requests and file uploads
- Feature Extraction: DenseNet121 model for preprocessing images
- Classification: Best performing trained classifier model
- Image Processing: Handles image upload, validation, and preprocessing
- File Upload: Simple interface for uploading seed images
- Results Display: Shows predicted class and confidence scores
- Error Handling: User-friendly error messages for invalid inputs
- DenseNet121 Feature Extractor: Same preprocessing pipeline as training
- Trained Classifier: Loads the best performing
.kerasmodel from comparison study - Consistent Pipeline: Identical feature extraction and classification process
- Supported Formats: PNG, JPG, JPEG image files
- File Size Limit: 16MB maximum upload size
- Image Preprocessing: Automatic resizing to 224×224 pixels
- Security: Secure filename handling and file validation
- Primary Prediction: Most likely seed class with confidence score
- All Probabilities: Complete probability distribution across all classes
- Class Labels: Human-readable class names (Broken, Immature, Intact, Skin-Damaged, Spotted)
flask
tensorflow
keras
numpy
pillow
werkzeug
flask_app/
├── app.py # Main Flask application
├── inception_v3.keras # Best performing trained model
├── templates/
│ └── index.html # Frontend interface
├── uploads/ # Temporary image storage
└── static/ # CSS/JS files (optional)
- Model Selection: Choose best performing model from comparison study
- Model Path: Update model loading path in Flask app
- Class Labels: Verify class names match your dataset classes
- Upload Directory: Ensure upload folder exists and has write permissions
- Analyze comparison results from all trained models
- Select model with highest accuracy or best performance metrics
- Copy the chosen
.kerasfile to Flask application directory
- Install required Flask dependencies
- Configure upload folder and file size limits
- Set allowed file extensions for image uploads
- Update class labels to match your seed types
- Load DenseNet121 model for feature extraction (same as training)
- Load selected trained classifier model
- Implement prediction pipeline matching training preprocessing
- Create HTML template for file upload
- Implement JavaScript for handling responses
- Add error handling and user feedback
- Style interface for better user experience
- Start Flask development server
- Navigate to web interface in browser
- Upload seed image through file selector
- View prediction results with confidence scores
- Image Upload: User selects and uploads seed image
- Preprocessing: Image resized and normalized
- Feature Extraction: DenseNet121 extracts features
- Classification: Trained model predicts seed class
- Results: Class label and confidence returned to user
- Model Caching: Load models once at startup
- Batch Processing: Handle multiple simultaneous requests
- File Cleanup: Remove uploaded files after processing
- Error Logging: Comprehensive error tracking and logging
- File Validation: Strict file type and size checking
- Secure Uploads: Prevent malicious file uploads
- Input Sanitization: Clean and validate all user inputs
- Access Control: Consider authentication for production use
- Docker Deployment: Containerize application for easy deployment
- Cloud Hosting: Deploy on cloud platforms (AWS, GCP, Azure)
- Load Balancing: Handle multiple concurrent users
- Database Integration: Store prediction history and analytics
- Simple Upload: Drag-and-drop or click to upload images
- Real-time Results: Immediate classification feedback
- Confidence Scores: Numerical confidence for predictions
- All Classes: Complete probability distribution display
- Primary Class: Most likely seed type
- Confidence Level: Prediction certainty percentage
- Alternative Classes: Other possible classifications with probabilities
- Processing Time: Quick response for real-time use
This deployment phase transforms your research comparison into a practical tool for automated seed classification, making the technology accessible to end users through an intuitive web interface.