A complete implementation of a Hopfield Neural Network for pattern recognition with image upload capabilities and a beautiful web interface.
This project implements a Hopfield Neural Network that can:
- Train on predefined binary patterns (letters P and Q)
- Accept noisy or distorted images as input
- Recall the closest stored pattern using associative memory
- Provide both web interface and interactive demo
Hopfield_Project/
│── hopfield.py # Core Hopfield network implementation
│── app.py # Flask web application
│── requirements.txt # Python dependencies
│── README.md # This file
│── templates/
│ ├── index.html # Main upload page
│ ├── results.html # Results display page
│ ├── demo.html # Interactive demo page
│ ├── 404.html # Error page
│ └── 500.html # Server error page
│── static/ # Generated visualizations
│ └── result.png # Latest pattern comparison
└── uploads/ # Temporary uploaded files
- Python 3.9 or higher
- pip package manager
pip install -r requirements.txtOr install manually:
pip install numpy matplotlib pillow flask werkzeugpython app.pyThe application will be available at: http://localhost:5000
python hopfield.pyThis will run a test of the Hopfield network with noisy patterns and generate visualization files.
The Hopfield Network is a recurrent neural network that stores patterns as stable states in its weight matrix.
for pattern in patterns:
W += np.outer(pattern, pattern)
np.fill_diagonal(W, 0) # No self-connectionsMathematical representation:
W_ij = Σ_μ x_i^(μ) * x_j^(μ), W_ii = 0
for i in range(n):
net_input = np.dot(W[i], state)
state[i] = 1 if net_input >= 0 else -1Mathematical representation:
s_i(t+1) = sign(Σ_j W_ij * s_j(t))
img = Image.open(path).convert("L") # Grayscale
img = img.resize((5,5)) # 5x5 grid
binary = np.where(arr < 128, 1, -1) # Threshold
vector = binary.flatten() # Flatten to 1DThe network is pre-trained on two 5×5 binary patterns:
Pattern P:
1 1 1 1 -1
1 -1 -1 -1 1
1 1 1 1 -1
1 -1 -1 -1 -1
1 -1 -1 -1 -1
Pattern Q:
-1 1 1 1 -1
1 -1 -1 -1 1
1 -1 -1 -1 1
1 -1 -1 1 1
-1 1 1 1 1
- Beautiful, responsive design
- File upload with drag-and-drop support
- Visual display of training patterns
- File type validation and size limits
- Side-by-side comparison of input, recalled, and stored patterns
- Detailed similarity scores
- Convergence information and iteration count
- Technical details about the network
- Click-to-edit 5×5 pattern grid
- Real-time pattern recognition
- Preset patterns and noise generation
- Instant feedback with similarity scores
GET /api/patterns- Get training patternsPOST /api/recall- Recall pattern from JSON input
-
Upload an Image:
- Go to
http://localhost:5000 - Click "Choose File" and select an image
- Click "Recognize Pattern"
- View results with detailed analysis
- Go to
-
Interactive Demo:
- Go to
http://localhost:5000/demo - Click cells to create patterns
- Use preset patterns or add noise
- Click "Recall Pattern" to see results
- Go to
-
Test with Sample Images:
- Use the provided sample images in
noisy_samples/folder - Try different noise levels: clean, blur, salt & pepper
- Compare how the network handles different types of degradation
- Use the provided sample images in
The noisy_samples/ folder contains test images for evaluating network performance:
| Image | Description | Expected Accuracy |
|---|---|---|
P_clean.png |
Perfect P pattern | ~100% |
P_light_noise.png |
P with 10% noise | ~90-95% |
P_moderate_noise.png |
P with 20% noise | ~80-90% |
P_blur.png |
Blurred P pattern | ~85-95% |
P_saltpepper.png |
P with salt & pepper noise | ~70-85% |
P_random_noise.png |
P with random noise | ~75-90% |
Q_clean.png |
Perfect Q pattern | ~100% |
Q_light_noise.png |
Q with 10% noise | ~90-95% |
Q_moderate_noise.png |
Q with 20% noise | ~80-90% |
Q_blur.png |
Blurred Q pattern | ~85-95% |
Q_saltpepper.png |
Q with salt & pepper noise | ~70-85% |
Q_random_noise.png |
Q with random noise | ~75-90% |
Note: These images are generated to exactly match the stored 7×7 patterns, ensuring optimal testing results.
from hopfield import HopfieldNetwork, create_training_patterns
# Initialize network
patterns = create_training_patterns()
network = HopfieldNetwork(size=25)
network.train(list(patterns.values()))
# Create noisy input
noisy_input = add_noise_to_pattern(patterns['P'], noise_level=0.2)
# Recall pattern
recalled, converged, iterations = network.recall(noisy_input)
print(f"Converged: {converged}, Iterations: {iterations}")
# Calculate similarity
similarity = network.pattern_similarity(recalled, patterns['P'])
print(f"Similarity to P: {similarity:.2f}")- ✅ Hebbian learning rule implementation
- ✅ Asynchronous neuron updates
- ✅ Energy function calculation
- ✅ Pattern similarity metrics
- ✅ Convergence detection
- ✅ Automatic image preprocessing
- ✅ Resize to 5×5 grid
- ✅ Grayscale conversion
- ✅ Binary thresholding
- ✅ Support for multiple image formats
- ✅ Modern, responsive design
- ✅ File upload with validation
- ✅ Real-time pattern editing
- ✅ Beautiful visualizations
- ✅ Error handling and user feedback
- ✅ Pattern comparison plots
- ✅ Similarity bar charts
- ✅ Interactive grid displays
- ✅ Color-coded results
- Grid Size: 5×5 (25 neurons)
- Patterns: P and Q letters
- Update Rule: Asynchronous
- Activation: Sign function
- Learning: Hebbian rule
- Target Size: 5×5 pixels
- Threshold: 128 (grayscale)
- Supported Formats: PNG, JPG, JPEG, GIF, BMP, TIFF
- Max File Size: 16MB
- Host: 0.0.0.0 (all interfaces)
- Port: 5000
- Debug Mode: Enabled in development
-
Import Errors:
pip install --upgrade numpy matplotlib pillow flask
-
Port Already in Use:
- Change port in
app.py:app.run(port=5001) - Or kill existing process:
lsof -ti:5000 | xargs kill
- Change port in
-
Image Processing Errors:
- Ensure image file is not corrupted
- Try different image formats
- Check file size (max 16MB)
-
Network Not Converging:
- This is normal for very noisy inputs
- Try reducing noise level
- Check pattern similarity scores
- Use smaller images for faster processing
- Clear browser cache if interface issues occur
- Restart Flask server if memory usage is high
The network minimizes the energy function:
E = -½ Σᵢⱼ wᵢⱼ sᵢ sⱼ
Theoretical capacity: ~0.15N patterns (N = number of neurons)
- For 25 neurons: ~3-4 patterns maximum
- Current implementation: 2 patterns (well within capacity)
- Guaranteed for stored patterns (noise-free)
- May converge to spurious states with noise
- Maximum iterations: 100 (configurable)
Feel free to contribute improvements:
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is open source and available under the MIT License.
This implementation is perfect for:
- Neural network courses
- Pattern recognition studies
- Machine learning demonstrations
- Interactive learning experiences
- Hopfield, J. J. (1982). Neural networks and physical systems with emergent collective computational abilities.
- Hertz, J., Krogh, A., & Palmer, R. G. (1991). Introduction to the theory of neural computation.
Happy Pattern Recognition! 🧠✨