49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
"""
|
|
TinyCNN model for HLS deployment on PYNQ-Z2.
|
|
|
|
This model is intentionally small and hardware-friendly:
|
|
- input: 1 x 64 x 64 grayscale image, values in [0, 1]
|
|
- output: 10 logits for light-lobe classes
|
|
"""
|
|
import torch
|
|
import torch.nn as nn
|
|
|
|
|
|
class TinyCNN(nn.Module):
|
|
"""A compact CNN that maps cleanly to a simple HLS accelerator."""
|
|
|
|
def __init__(self, num_classes=10):
|
|
super().__init__()
|
|
self.features = nn.Sequential(
|
|
nn.Conv2d(1, 8, kernel_size=3, padding=1),
|
|
nn.ReLU(inplace=True),
|
|
nn.MaxPool2d(kernel_size=2, stride=2),
|
|
nn.Conv2d(8, 16, kernel_size=3, padding=1),
|
|
nn.ReLU(inplace=True),
|
|
nn.MaxPool2d(kernel_size=2, stride=2),
|
|
nn.Conv2d(16, 32, kernel_size=3, padding=1),
|
|
nn.ReLU(inplace=True),
|
|
nn.MaxPool2d(kernel_size=2, stride=2),
|
|
)
|
|
self.pool = nn.AdaptiveAvgPool2d((1, 1))
|
|
self.classifier = nn.Linear(32, num_classes)
|
|
|
|
def forward(self, x):
|
|
x = self.features(x)
|
|
x = self.pool(x)
|
|
x = torch.flatten(x, 1)
|
|
return self.classifier(x)
|
|
|
|
|
|
def create_tiny_cnn(num_classes=10):
|
|
return TinyCNN(num_classes=num_classes)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
model = create_tiny_cnn()
|
|
sample = torch.randn(1, 1, 64, 64)
|
|
output = model(sample)
|
|
params = sum(p.numel() for p in model.parameters())
|
|
print(f"Output shape: {tuple(output.shape)}")
|
|
print(f"Parameters: {params}")
|