This project is a Convolutional Neural Network (CNN) designed to classify nuts as "good" or "bad." The neural network is trained on images of nuts and can then predict the quality of new images.
The model was developed as a diploma project and can be adapted for classifying other products or industrial tasks (e.g., quality control in manufacturing or object recognition in customs inspection).
The model achieved 94.80% accuracy on a test dataset of 5000 images.
- Python 3.8+
- TensorFlow + Keras
- NumPy
- ImageDataGenerator for image loading
Before starting, install the required dependencies:
pip install tensorflow numpy kerasA total of 5000 images of nuts were taken on a green background, divided into two classes:
- 2500 images of good nuts
- 2500 images of bad nuts
Example images (good nut/bad nut):
Create a data/ directory with the following subdirectories:
data/
├── train/ # 4000 images (80%)
│ ├── good/ # 2000 images
│ ├── bad/ # 2000 images
├── val/ # 500 images (10%)
│ ├── good/ # 250 images
│ ├── bad/ # 250 images
├── test/ # 500 images (10%)
│ ├── good/ # 250 images
│ ├── bad/ # 250 images
Run the Python script to train the model:
python Nut_CNN.pyAfter training, the model will be saved in the file nut_classifier.h5. The .h5 file format is used to store trained model weights in Keras.
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
# Load the pre-trained model
model = load_model("nut_classifier.h5")
# Load an image
img = image.load_img("path/to/image.jpg", target_size=(150, 150))
# Convert image to an array and normalize
x = image.img_to_array(img) / 255.0
x = np.expand_dims(x, axis=0)
# Predict the class of the image
prediction = model.predict(x)[0][0]
# Display the result
print("Good nut" if prediction > 0.5 else "Bad nut")- Prepare your dataset by organizing images into
train/,val/, andtest/folders. - Update the script with your dataset paths.
- Run the training process.
- Use the trained model for predictions on new images.
- You can add hyperparameter configuration via a config file.
- Improve data augmentation for better generalization.
- Adding more layers requires testing for potential accuracy improvement or degradation.
This project is licensed under the MIT license.
💡If you find this project useful, you can support my work — even a small donation makes a difference
- Crypto donation: USDT (TRC20):
TCECqH8ZxXGCQuWZeto1nV9nawbeeV4fG8 - Crypto donation: Bitcoin (BTC):
bc1q3lvprzayxd3qulk0epk5dh58zx36mfev76wj30 - Crypto donation: Ethereum (ETH):
0x80DbC00Fd91bAb3D4FE6E6441Dae0719e6bF5c9e - International card (Visa/Mastercard):
https://www.donationalerts.com/r/johngear

