Spaces:
Runtime error
Runtime error
File size: 2,073 Bytes
e18a750 |
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import torch
import torch.nn as nn
class ResNet(nn.Module):
def __init__(self, in_channels: int, num_classes: int):
"""ResNet9"""
super().__init__()
self.conv1 = ConvBlock(in_channels, 64)
self.conv2 = ConvBlock(64, 128, pool=True)
self.res1 = nn.Sequential(
ConvBlock(128, 128),
ConvBlock(128, 128)
)
self.conv3 = ConvBlock(128, 256)
self.conv4 = ConvBlock(256, 512, pool=True)
self.res2 = nn.Sequential(
ConvBlock(512, 512),
ConvBlock(512, 512)
)
self.classifier = nn.Sequential(
nn.MaxPool2d(kernel_size=(4, 4)),
nn.AdaptiveAvgPool2d(1),
nn.Flatten(),
nn.Linear(512, 128),
nn.Dropout(0.25),
nn.Linear(128, num_classes),
nn.Dropout(0.25),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.conv1(x)
x = self.conv2(x)
x = self.res1(x) + x #skip
x = self.conv3(x)
x = self.conv4(x)
x = self.res2(x) + x #skip
prediction = self.classifier(x)
return prediction
class ConvBlock(nn.Module):
def __init__(self, in_channels: int, out_channels: int, pool: bool = False, pool_no: int = 2):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.pool = pool
self.pool_no = pool_no
if self.pool:
self.pool_block = nn.Sequential(
nn.ReLU(inplace=True),
nn.MaxPool2d(self.pool_no)
)
else:
self.pool_block = nn.Sequential(
nn.ReLU(inplace=True),
)
self.block = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels),
self.pool_block
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.block(x)
return x |