AI-Project/architecture.py

54 lines
2.0 KiB
Python
Raw Normal View History

2024-07-03 00:42:21 +02:00
import torch
import torch.nn
class MyCNN(torch.nn.Module):
def __init__(self,
input_channels: int):
2024-07-03 00:42:21 +02:00
super().__init__()
2024-07-06 19:12:58 +02:00
self.layers = torch.nn.Sequential(
2024-07-15 23:16:06 +02:00
torch.nn.Conv2d(in_channels=input_channels, out_channels=32, kernel_size=3, padding='same', bias=False),
torch.nn.BatchNorm2d(32),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, padding='same', bias=False),
torch.nn.BatchNorm2d(32),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=3, padding=1),
torch.nn.Dropout2d(0.1),
torch.nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding='same', bias=False),
torch.nn.BatchNorm2d(64),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding='same', bias=False),
2024-07-06 19:12:58 +02:00
torch.nn.BatchNorm2d(64),
torch.nn.ReLU(),
2024-07-15 23:16:06 +02:00
torch.nn.MaxPool2d(kernel_size=3, padding=1),
2024-07-06 19:12:58 +02:00
2024-07-15 23:16:06 +02:00
torch.nn.Dropout2d(0.1),
2024-07-06 19:12:58 +02:00
torch.nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding='same', bias=False),
torch.nn.BatchNorm2d(128),
torch.nn.ReLU(),
2024-07-15 23:16:06 +02:00
torch.nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding='same', bias=False),
torch.nn.BatchNorm2d(128),
2024-07-06 19:12:58 +02:00
torch.nn.ReLU(),
2024-07-15 23:16:06 +02:00
torch.nn.MaxPool2d(kernel_size=3, padding=1),
2024-07-06 19:12:58 +02:00
torch.nn.Flatten(),
2024-07-15 23:16:06 +02:00
torch.nn.Dropout(0.25),
torch.nn.Linear(in_features=2048, out_features=1024),
2024-07-06 19:12:58 +02:00
torch.nn.ReLU(),
2024-07-15 23:16:06 +02:00
torch.nn.Linear(in_features=1024, out_features=512),
2024-07-06 19:12:58 +02:00
torch.nn.ReLU(),
2024-07-15 23:16:06 +02:00
torch.nn.Linear(in_features=512, out_features=20, bias=False)
2024-07-06 19:12:58 +02:00
)
2024-07-03 00:42:21 +02:00
def forward(self, input_images: torch.Tensor) -> torch.Tensor:
2024-07-06 19:12:58 +02:00
return self.layers(input_images)
def __repr__(self):
return str(self.layers)
2024-07-03 00:42:21 +02:00
model = MyCNN(input_channels=1)