A neural network model that predicts Bezier curve sequences for geometric shapes. The model learns to generate diverse curve predictions for different input shapes, solving the issue where all shapes would produce identical predictions.
This project trains a transformer-based model to:
- Predict Bezier curve parameters (6 numbers per segment) for shapes
- Classify curve types (line, quadratic, cubic)
- Determine stop positions for variable-length sequences
- Generate diverse outputs for different input shapes
# Navigate to project directory
cd shape-predictor
# Create virtual environment
python -m venv shape_predictor_env
# Activate virtual environment
# Windows:
shape_predictor_env\Scripts\activate
# Linux/Mac:
source shape_predictor_env/bin/activate
# Upgrade pip and install dependencies
python -m pip install --upgrade pip
pip install -r requirements.txt# Create 5 simple shapes for training
python create_simple_shapes.pyThis creates:
- 🔴 Red circle
- 🔵 Blue square
- 🟢 Green triangle
- 🟡 Yellow diamond
- 🟣 Purple pentagon
# Train for 50 epochs on the 5 simple shapes
python boundedShapePredSOTA.pyTraining will:
- Run for 50 epochs (or until convergence)
- Save best model to
bezier_checkpoints/best_model.pth - Save final model to
bezier_checkpoints/final_model.pth - Display training progress with loss metrics
# Run comprehensive evaluation
python evaluate_model.pyThis will:
- Load the best trained model
- Test on all 5 shapes
- Generate visualizations in
evaluation_results/ - Check prediction diversity (main issue we solved)
- Display detailed metrics
# Quick functionality test (no ML dependencies needed)
python test_shapes.pyshape-predictor/
├── README.md # This file
├── requirements.txt # Python dependencies
├── .gitignore # Git ignore rules
├── boundedShapePredSOTA.py # Main training script
├── evaluate_model.py # Model evaluation script
├── create_simple_shapes.py # Generate training data
├── test_shapes.py # Basic functionality test
├── contour.py # Contour processing utilities
├── dataset/ # Training data
│ ├── json/ # Shape metadata
│ ├── images/ # Shape images
│ └── masks/ # Shape masks
├── bezier_checkpoints/ # Trained models
│ ├── best_model.pth # Best performing model
│ └── final_model.pth # Final epoch model
├── evaluation_results/ # Generated visualizations
└── shape_predictor_env/ # Virtual environment
- Python: 3.9+ (tested on 3.9 and 3.11)
- PyTorch: 2.0+
- Dependencies: See
requirements.txt
torch>=2.0.0- Neural network frameworktransformers>=4.20.0- FLAN-T5 text encoderopencv-python>=4.5.0- Image/contour processingmatplotlib>=3.5.0- Visualizationscipy>=1.8.0- Bezier curve fittingpillow>=8.0.0- Image handlingnumpy>=1.21.0- Numerical operations
The model uses a transformer-based architecture with:
- Text Encoder: FLAN-T5-base for processing shape descriptions
- Shape Encoder: Processes parent shape Bezier sequences
- Fusion Module: Combines text and shape features
- Decoder: Predicts child shape parameters
- Output Heads:
- Curve parameters (6 per segment)
- Curve types (line/quadratic/cubic)
- Stop positions
Original Issue: All curves predicted for a shape were identical regardless of input.
Solution:
- Fixed model initialization to encourage diversity
- Improved training with better loss weighting
- Added proper evaluation to verify diverse outputs
Verification: Run python evaluate_model.py to see diverse predictions.
- Dataset: 5 simple geometric shapes
- Epochs: 50 (early stopping if loss < 1e-6)
- Batch Size: 2
- Learning Rate: 1e-3
- Loss Components:
- Curve parameter L1 loss (λ=500)
- Stop prediction loss (λ=10)
- Length prediction loss (λ=20)
- Type classification loss (λ=100)
The evaluation script provides:
- Curve L1 Error: Accuracy of curve parameters
- Type Accuracy: Curve type classification accuracy
- Endpoint Error: Shape endpoint prediction accuracy
- Diversity Analysis: Checks for prediction variance
- Visual Comparisons: Ground truth vs predictions
-
"No module named 'torch'"
# Make sure virtual environment is activated shape_predictor_env\Scripts\activate pip install -r requirements.txt
-
"Checkpoint not found"
# Train the model first python boundedShapePredSOTA.py -
"scipy not available"
pip install scipy
-
Memory issues
- Use CPU instead of GPU: model automatically detects available device
- Reduce batch size in training script if needed
- Python 3.9: ✅ Fully supported
- Python 3.10+: ✅ Supported
- Python 3.8:
⚠️ May work but not tested
python boundedShapePredSOTA.py# Edit boundedShapePredSOTA.py
train_model_batched(
dataset_path="dataset",
num_epochs=100, # Train longer
learning_rate=5e-4, # Different learning rate
batch_size=4, # Larger batch
)import torch
from boundedShapePredSOTA import PolygonPredictor, PolygonConfig
# Load model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
cfg = PolygonConfig()
model = PolygonPredictor(cfg=cfg).to(device)
checkpoint = torch.load("bezier_checkpoints/best_model.pth", map_location=device)
model.load_state_dict(checkpoint["model_state_dict"], strict=False)
model.eval()
# Use for predictions...After training, the model achieves:
- ✅ Diverse predictions for different shapes
- ✅ 100% type accuracy on test shapes
- ✅ Reasonable endpoint accuracy (~0.47 average error)
- ✅ No more identical outputs (original problem solved)
This project is provided as-is for educational and research purposes.
To contribute:
- Fork the repository
- Create a feature branch
- Make your changes
- Test with
python test_shapes.py - Submit a pull request
For questions or issues, please check the troubleshooting section above or create an issue in the repository.