-
Notifications
You must be signed in to change notification settings - Fork 0
/
lenet_cifar.py
33 lines (28 loc) · 1.04 KB
/
lenet_cifar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from timm.models.registry import register_model
from torch import nn
from torch.nn import functional as F
# Define a simple CNN model
class LeNet(nn.Module):
def __init__(self, num_classes: int = 10):
super().__init__()
self.conv1 = nn.Conv2d(3, 6, kernel_size=5, stride=1, padding=2) # 28*28->32*32-->28*28
self.pool1 = nn.AvgPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(6, 16, kernel_size=5, stride=1)
self.pool2 = nn.AvgPool2d(kernel_size=2, stride=2)
self.flatten1 = nn.Flatten()
self.fc1 = nn.Linear(24 * 24, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, num_classes)
def forward(self, x):
x = F.relu(self.conv1(x))
x = self.pool1(x)
x = F.relu(self.conv2(x))
x = self.pool2(x)
x = self.flatten1(x)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
@register_model
def lenet_cifar(num_classes: int) -> nn.Module:
return LeNet(num_classes=num_classes)