Spaces:
Sleeping
Sleeping
File size: 1,375 Bytes
a9640f8 | 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 34 35 36 37 38 39 40 41 42 | import torch
import torch.nn as nn
class ImprovedCNN(nn.Module):
def __init__(self, input_channels, hidden_units, num_classes=4):
super().__init__()
self.block1 = nn.Sequential(
nn.Conv2d(in_channels=input_channels, out_channels=hidden_units, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(hidden_units),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2)
)
self.block2 = nn.Sequential(
nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units*2, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(hidden_units*2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2)
)
self.block3 = nn.Sequential(
nn.Conv2d(in_channels=hidden_units*2, out_channels=hidden_units*4, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(hidden_units*4),
nn.ReLU(),
nn.AdaptiveAvgPool2d(output_size=(4, 4))
)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(hidden_units*4*4*4, 256),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(256, 128)
)
def forward(self, x):
x = self.block1(x)
x = self.block2(x)
x = self.block3(x)
x = self.classifier(x)
return x
|