projekt_ml/model.py
2025-01-19 16:27:39 +01:00

81 lines
2.4 KiB
Python

import torch.nn as nn
import torch.nn.functional as F
class CNNv1(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 16, 3, 1)
self.conv2 = nn.Conv2d(16, 32, 3, 1)
self.fc1 = nn.Linear(32 * 54 * 54, 128)
self.fc2 = nn.Linear(128, 2)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2, 2)
x = x.view(-1, 32 * 54 * 54)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
class CNNv2(nn.Module):
def __init__(self) -> None:
super().__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.bn1 = nn.BatchNorm2d(32)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.bn2 = nn.BatchNorm2d(64)
self.conv3 = nn.Conv2d(64, 128, 3, 1)
self.bn3 = nn.BatchNorm2d(128)
self.fc1 = nn.Linear(128 * 26 * 26, 256)
self.dropout = nn.Dropout(0.5)
self.fc2 = nn.Linear(256, 2)
def forward(self, x):
x = F.relu(self.bn1(self.conv1(x)))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.bn2(self.conv2(x)))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.bn3(self.conv3(x)))
x = F.max_pool2d(x, 2, 2)
x = x.view(-1, 128 * 26 * 26)
x = F.relu(self.fc1(x))
x = self.dropout(x)
x = self.fc2(x)
return x
class CNNv3(nn.Module):
def __init__(self) -> None:
super().__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.bn1 = nn.BatchNorm2d(32)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.bn2 = nn.BatchNorm2d(64)
self.conv3 = nn.Conv2d(64, 128, 3, 1)
self.bn3 = nn.BatchNorm2d(128)
self.conv4 = nn.Conv2d(128, 256, 3, 1)
self.bn4 = nn.BatchNorm2d(256)
self.global_avg_pool = nn.AdaptiveAvgPool2d((1, 1))
self.fc1 = nn.Linear(256, 128)
self.dropout = nn.Dropout(0.5)
self.fc2 = nn.Linear(128, 2)
def forward(self, x):
x = F.relu(self.bn1(self.conv1(x)))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.bn2(self.conv2(x)))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.bn3(self.conv3(x)))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.bn4(self.conv4(x)))
x = self.global_avg_pool(x)
x = x.view(-1, 256)
x = F.relu(self.fc1(x))
x = self.dropout(x)
x = self.fc2(x)
return x