Download model.py from FolashadeO/week4: direct link, hf CLI and curl.
- Browser
- Download file 2.19 kB
-
https://huggingface.co/FolashadeO/week4/resolve/main/model.py
- Command line
-
hf download hf://FolashadeO/week4/model.py
-
curl -L -o model.py https://huggingface.co/FolashadeO/week4/resolve/main/model.py
2.19 kB
| import torch | |
| import torch.nn as nn | |
| class DoubleConv(nn.Module): | |
| def __init__(self, in_channels, out_channels): | |
| super().__init__() | |
| self.double_conv = nn.Sequential( | |
| nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, bias=False), | |
| nn.BatchNorm2d(out_channels), | |
| nn.ReLU(inplace=True), | |
| nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1, bias=False), | |
| nn.BatchNorm2d(out_channels), | |
| nn.ReLU(inplace=True) | |
| ) | |
| def forward(self, x): return self.double_conv(x) | |
| class UNet(nn.Module): | |
| def __init__(self, in_channels: int = 3, out_channels: int = 1): | |
| super().__init__() | |
| self.inc = DoubleConv(in_channels, 64) | |
| self.down1 = nn.Sequential(nn.MaxPool2d(2), DoubleConv(64, 128)) | |
| self.down2 = nn.Sequential(nn.MaxPool2d(2), DoubleConv(128, 256)) | |
| self.down3 = nn.Sequential(nn.MaxPool2d(2), DoubleConv(256, 512)) | |
| self.down4 = nn.MaxPool2d(2) | |
| self.bottleneck = DoubleConv(512, 1024) | |
| self.up1 = nn.ConvTranspose2d(1024, 512, kernel_size=2, stride=2) | |
| self.conv1 = DoubleConv(1024, 512) | |
| self.up2 = nn.ConvTranspose2d(512, 256, kernel_size=2, stride=2) | |
| self.conv2 = DoubleConv(512, 256) | |
| self.up3 = nn.ConvTranspose2d(256, 128, kernel_size=2, stride=2) | |
| self.conv3 = DoubleConv(256, 128) | |
| self.up4 = nn.ConvTranspose2d(128, 64, kernel_size=2, stride=2) | |
| self.conv4 = DoubleConv(128, 64) | |
| self.outc = nn.Conv2d(64, out_channels, kernel_size=1) | |
| def forward(self, x): | |
| x1 = self.inc(x) | |
| x2 = self.down1(x1) | |
| x3 = self.down2(x2) | |
| x4 = self.down3(x3) | |
| b = self.down4(x4) | |
| b = self.bottleneck(b) | |
| x = self.up1(b) | |
| x = torch.cat([x, x4], dim=1) | |
| x = self.conv1(x) | |
| x = self.up2(x) | |
| x = torch.cat([x, x3], dim=1) | |
| x = self.conv2(x) | |
| x = self.up3(x) | |
| x = torch.cat([x, x2], dim=1) | |
| x = self.conv3(x) | |
| x = self.up4(x) | |
| x = torch.cat([x, x1], dim=1) | |
| x = self.conv4(x) | |
| return self.outc(x) | |