Skip to content

Latest commit

 

History

History
353 lines (260 loc) · 8.18 KB

File metadata and controls

353 lines (260 loc) · 8.18 KB

NeuralForge - Usage Examples

🎯 Complete Usage Examples

Example 1: Train and Test CIFAR-10 (30 minutes)

# Step 1: Train ResNet18 on CIFAR-10
python train.py --dataset cifar10 --model resnet18 --epochs 20 --batch-size 128 --lr 0.001

# Step 2: Test the model interactively
python tests/test_model.py --dataset cifar10 --mode interactive

# In interactive mode:
>>> random 20        # Test 20 random images
>>> accuracy         # Calculate full accuracy
>>> sample 100       # Test specific image
>>> exit

Expected Results:

  • Training time: ~15-20 minutes (RTX 3060 Ti)
  • Accuracy: ~80-85% after 20 epochs
  • Model saved: ./models/best_model.pt

Example 2: Quick Test with Synthetic Data (2 minutes)

# Fast training for testing the framework
python train.py --dataset synthetic --num-samples 500 --epochs 3 --batch-size 32

# Quick random testing
python tests/test_model.py --dataset synthetic --mode random --samples 10

Use Case: Testing your setup, debugging, quick experiments


Example 3: MNIST Digit Recognition (5 minutes)

# Train on MNIST
python train.py --dataset mnist --model simple --epochs 10 --batch-size 64

# Test accuracy
python tests/test_model.py --dataset mnist --mode accuracy

Expected Results:

  • Training time: ~3-5 minutes
  • Accuracy: ~98-99%
  • Perfect for learning and demonstrations

Example 4: Fashion-MNIST (15 minutes)

# Train ResNet on Fashion-MNIST
python train.py --dataset fashion_mnist --model resnet18 --epochs 20 --batch-size 128

# Interactive testing
python tests/test_model.py --dataset fashion_mnist --mode interactive
>>> random 50
>>> accuracy

Classes: T-shirt, Trouser, Pullover, Dress, Coat, Sandal, Shirt, Sneaker, Bag, Ankle boot


Example 5: Neural Architecture Search (1 hour)

# Run evolutionary NAS
python examples/neural_architecture_search.py

What it does:

  • Searches for optimal architecture
  • Uses evolutionary algorithm
  • Tests 15 architectures over 20 generations
  • Outputs best architecture with parameters

Expected Output:

Best Architecture Found:
Fitness: 0.7234
Accuracy: 78.45%
Parameters: 1,234,567
FLOPs: 98,765,432

Example 6: Custom Training Script

# examples/my_custom_training.py
import sys
sys.path.insert(0, '.')

import torch
import torch.nn as nn
from src.python.neuralforge import Trainer, Config
from src.python.neuralforge.data.real_datasets import get_dataset
from src.python.neuralforge.data.dataset import DataLoaderBuilder
from src.python.neuralforge.models.resnet import ResNet18
from src.python.neuralforge.optim.optimizers import AdamW

# Configuration
config = Config()
config.batch_size = 128
config.epochs = 50
config.learning_rate = 0.001

# Load CIFAR-10
train_dataset = get_dataset('cifar10', train=True, download=True)
val_dataset = get_dataset('cifar10', train=False, download=True)

# Data loaders
loader_builder = DataLoaderBuilder(config)
train_loader = loader_builder.build_train_loader(train_dataset)
val_loader = loader_builder.build_val_loader(val_dataset)

# Model
model = ResNet18(num_classes=10)

# Training
criterion = nn.CrossEntropyLoss()
optimizer = AdamW(model.parameters(), lr=config.learning_rate)

trainer = Trainer(model, train_loader, val_loader, optimizer, criterion, config)
trainer.train()

print(f"Best loss: {trainer.best_val_loss:.4f}")

Run it:

python examples/my_custom_training.py

Example 7: Test Your Own Images

# Train a model first
python train.py --dataset cifar10 --epochs 20

# Test with your own images
python tests/test_model.py --dataset cifar10 --mode interactive

# In interactive mode:
>>> image ./my_photos/cat.jpg
Custom Image: ./my_photos/cat.jpg
Predicted:       cat
Confidence:      94.3%

Top-5 Predictions:
  1. cat             94.3%
  2. dog             3.2%
  3. deer            1.5%
  4. bird            0.7%
  5. frog            0.3%

Requirements:

  • Image should contain objects similar to training classes
  • Will be automatically resized to match model input

Example 8: Compare Multiple Datasets

# Train on different datasets
python train.py --dataset mnist --model simple --epochs 10
mv ./models/best_model.pt ./models/mnist_best.pt

python train.py --dataset fashion_mnist --model resnet18 --epochs 20
mv ./models/best_model.pt ./models/fashion_best.pt

python train.py --dataset cifar10 --model resnet18 --epochs 30
mv ./models/best_model.pt ./models/cifar10_best.pt

# Test each
python tests/test_model.py --model ./models/mnist_best.pt --dataset mnist --mode accuracy
python tests/test_model.py --model ./models/fashion_best.pt --dataset fashion_mnist --mode accuracy
python tests/test_model.py --model ./models/cifar10_best.pt --dataset cifar10 --mode accuracy

Example 9: Monitor Training in Real-Time

Terminal 1 - Start Training:

python train.py --dataset cifar10 --epochs 100 --batch-size 128

Terminal 2 - Watch Logs:

# Windows
Get-Content ./logs/*.log -Wait -Tail 20

# Linux/Mac
tail -f ./logs/*.log

Example 10: Batch Testing

# test_batch.py
import sys
sys.path.insert(0, '.')

import torch
from src.python.neuralforge.data.real_datasets import get_dataset
from src.python.neuralforge.models.resnet import ResNet18

# Load model and dataset
model = ResNet18(num_classes=10)
checkpoint = torch.load('./models/best_model.pt')
model.load_state_dict(checkpoint['model_state_dict'])
model.eval()

dataset = get_dataset('cifar10', train=False)

# Test first 100 samples
correct = 0
for i in range(100):
    image, label = dataset[i]
    with torch.no_grad():
        output = model(image.unsqueeze(0))
        pred = output.argmax(1).item()
        if pred == label:
            correct += 1

print(f"Accuracy on 100 samples: {correct}%")

📊 Real Training Results

From RTX 3060 Ti

CIFAR-10 (ResNet18, 50 epochs):

Epoch 50/50 | Train Loss: 0.3521 | Train Acc: 87.82% | Val Loss: 0.5123 | Val Acc: 84.31%
Training completed in 0.45 hours

MNIST (Simple CNN, 10 epochs):

Epoch 10/10 | Train Loss: 0.0234 | Train Acc: 99.21% | Val Loss: 0.0312 | Val Acc: 98.89%
Training completed in 0.08 hours

Fashion-MNIST (ResNet18, 30 epochs):

Epoch 30/30 | Train Loss: 0.2145 | Train Acc: 92.15% | Val Loss: 0.2834 | Val Acc: 90.42%
Training completed in 0.25 hours

💡 Pro Tips

1. Speed Up Training

# Use larger batch size (if GPU memory allows)
python train.py --dataset cifar10 --batch-size 256

# Reduce image size for faster experiments
config.image_size = 32  # Instead of 224

2. Save GPU Memory

# Smaller batch size
python train.py --dataset cifar10 --batch-size 64

# Disable AMP if issues
config.use_amp = False

3. Best Practices

# Always validate before full training
python tests/quick_test.py

# Start with few epochs
python train.py --dataset cifar10 --epochs 5

# Monitor GPU usage
nvidia-smi -l 1

4. Reproducible Results

# Set seed for reproducibility
python train.py --dataset cifar10 --seed 42

🎓 Learning Path

Beginner

  1. python tests/quick_test.py - Validate setup
  2. python train.py --dataset synthetic --epochs 3 - Quick test
  3. python train.py --dataset mnist --epochs 10 - Real dataset

Intermediate

  1. python train.py --dataset cifar10 --epochs 20 - More complex
  2. python tests/test_model.py --mode interactive - Test models
  3. python examples/train_cifar10.py - Custom script

Advanced

  1. python examples/neural_architecture_search.py - NAS
  2. Create custom models in src/python/neuralforge/nn/
  3. Implement custom CUDA kernels in src/cuda/

🚀 Next Steps

After running these examples:

  1. Experiment with hyperparameters - learning rate, batch size, epochs
  2. Try different models - ResNet, EfficientNet, ViT
  3. Create custom architectures - Build your own networks
  4. Implement new features - Add your own datasets, layers
  5. Optimize performance - Profile, tune, accelerate

📚 More Resources

  • QUICKSTART.md - Getting started guide
  • DOCUMENTATION.md - Full API reference
  • DATASETS.md - Dataset information
  • FEATURES.md - Complete feature list

Happy experimenting! 🎉