Buckets:

download
raw
3.08 kB
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Dict, Any, List
from promethium.ml.models.base import PromethiumModel
from promethium.ml.models.registry import ModelRegistry
class ConvBlock(nn.Module):
"""(Conv => BN => ReLU) * 2"""
def __init__(self, in_ch, out_ch):
super().__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1),
nn.BatchNorm2d(out_ch),
nn.LeakyReLU(inplace=True),
nn.Conv2d(out_ch, out_ch, kernel_size=3, padding=1),
nn.BatchNorm2d(out_ch),
nn.LeakyReLU(inplace=True)
)
def forward(self, x):
return self.conv(x)
class UpSample(nn.Module):
"""Upscaling then double conv"""
def __init__(self, in_ch, out_ch, bilinear=True):
super().__init__()
if bilinear:
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
self.conv = ConvBlock(in_ch, out_ch) # in_ch is actually in_ch//2 * 2 after concat usually
else:
self.up = nn.ConvTranspose2d(in_ch, in_ch // 2, kernel_size=2, stride=2)
self.conv = ConvBlock(in_ch, out_ch)
def forward(self, x1, x2):
x1 = self.up(x1)
# Input is CHW
diffY = x2.size()[2] - x1.size()[2]
diffX = x2.size()[3] - x1.size()[3]
x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2,
diffY // 2, diffY - diffY // 2])
x = torch.cat([x2, x1], dim=1)
return self.conv(x)
@ModelRegistry.register("unet")
class UNet(PromethiumModel):
"""
Standard U-Net architecture.
Config:
n_channels: input channels
n_classes: output channels
bilinear: use bilinear interpolation for upsampling
"""
def __init__(self, config: Dict[str, Any]):
super().__init__(config)
n_channels = config.get("n_channels", 1)
n_classes = config.get("n_classes", 1)
bilinear = config.get("bilinear", True)
self.inc = ConvBlock(n_channels, 64)
self.down1 = nn.Sequential(nn.MaxPool2d(2), ConvBlock(64, 128))
self.down2 = nn.Sequential(nn.MaxPool2d(2), ConvBlock(128, 256))
self.down3 = nn.Sequential(nn.MaxPool2d(2), ConvBlock(256, 512))
factor = 2 if bilinear else 1
self.down4 = nn.Sequential(nn.MaxPool2d(2), ConvBlock(512, 1024 // factor))
self.up1 = UpSample(1024, 512 // factor, bilinear)
self.up2 = UpSample(512, 256 // factor, bilinear)
self.up3 = UpSample(256, 128 // factor, bilinear)
self.up4 = UpSample(128, 64, bilinear)
self.outc = nn.Conv2d(64, n_classes, kernel_size=1)
def forward(self, x):
x1 = self.inc(x)
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
logits = self.outc(x)
return logits

Xet Storage Details

Size:
3.08 kB
·
Xet hash:
d48a37926dec0e64b622a4449164a76d0ef03eefd21b5f8c1cfdb8e5dec8fdfb

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.