5 Commits

Author SHA1 Message Date
77b8b9b3f6 remove pycache 2026-01-31 19:05:45 +01:00
7d4caaf501 added fifth submission 2026-01-26 14:03:08 +01:00
248ffb8faf Add fourth submission 2026-01-25 22:20:37 +01:00
1771377121 Add second and third submissions 2026-01-25 20:41:25 +01:00
eaf45f5c72 added first submission 2026-01-24 22:47:28 +01:00
17 changed files with 32 additions and 50 deletions

View File

@@ -2,3 +2,4 @@ data/*
*.zip *.zip
*.jpg *.jpg
*.pt *.pt
__pycache__/

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -15,10 +15,8 @@ def init_weights(m):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None: if m.bias is not None:
nn.init.constant_(m.bias, 0) nn.init.constant_(m.bias, 0)
elif isinstance(m, (nn.BatchNorm2d, nn.InstanceNorm2d)): elif isinstance(m, nn.BatchNorm2d):
if m.weight is not None:
nn.init.constant_(m.weight, 1) nn.init.constant_(m.weight, 1)
if m.bias is not None:
nn.init.constant_(m.bias, 0) nn.init.constant_(m.bias, 0)
@@ -71,36 +69,35 @@ class CBAM(nn.Module):
class ConvBlock(nn.Module): class ConvBlock(nn.Module):
"""Convolutional block with Conv2d -> InstanceNorm2d -> GELU""" """Convolutional block with Conv2d -> BatchNorm -> LeakyReLU"""
def __init__(self, in_channels, out_channels, kernel_size=3, padding=1, dropout=0.0, dilation=1): def __init__(self, in_channels, out_channels, kernel_size=3, padding=1, dropout=0.0):
super().__init__() super().__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, padding=padding, dilation=dilation) self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, padding=padding)
# InstanceNorm is preferred for style/inpainting tasks self.bn = nn.BatchNorm2d(out_channels)
self.bn = nn.InstanceNorm2d(out_channels, affine=True) self.relu = nn.LeakyReLU(0.1, inplace=True)
self.act = nn.GELU()
self.dropout = nn.Dropout2d(dropout) if dropout > 0 else nn.Identity() self.dropout = nn.Dropout2d(dropout) if dropout > 0 else nn.Identity()
def forward(self, x): def forward(self, x):
return self.dropout(self.act(self.bn(self.conv(x)))) return self.dropout(self.relu(self.bn(self.conv(x))))
class ResidualConvBlock(nn.Module): class ResidualConvBlock(nn.Module):
"""Residual convolutional block for better gradient flow""" """Residual convolutional block for better gradient flow"""
def __init__(self, channels, dropout=0.0, dilation=1): def __init__(self, channels, dropout=0.0):
super().__init__() super().__init__()
self.conv1 = nn.Conv2d(channels, channels, 3, padding=dilation, dilation=dilation) self.conv1 = nn.Conv2d(channels, channels, 3, padding=1)
self.bn1 = nn.InstanceNorm2d(channels, affine=True) self.bn1 = nn.BatchNorm2d(channels)
self.conv2 = nn.Conv2d(channels, channels, 3, padding=dilation, dilation=dilation) self.conv2 = nn.Conv2d(channels, channels, 3, padding=1)
self.bn2 = nn.InstanceNorm2d(channels, affine=True) self.bn2 = nn.BatchNorm2d(channels)
self.act = nn.GELU() self.relu = nn.LeakyReLU(0.1, inplace=True)
self.dropout = nn.Dropout2d(dropout) if dropout > 0 else nn.Identity() self.dropout = nn.Dropout2d(dropout) if dropout > 0 else nn.Identity()
def forward(self, x): def forward(self, x):
residual = x residual = x
out = self.act(self.bn1(self.conv1(x))) out = self.relu(self.bn1(self.conv1(x)))
out = self.dropout(out) out = self.dropout(out)
out = self.bn2(self.conv2(out)) out = self.bn2(self.conv2(out))
out = out + residual out = out + residual
return self.act(out) return self.relu(out)
class DownBlock(nn.Module): class DownBlock(nn.Module):
@@ -164,9 +161,9 @@ class MyModel(nn.Module):
# Bottleneck with multiple residual blocks # Bottleneck with multiple residual blocks
self.bottleneck = nn.Sequential( self.bottleneck = nn.Sequential(
ConvBlock(base_channels * 16, base_channels * 16, dropout=dropout), ConvBlock(base_channels * 16, base_channels * 16, dropout=dropout),
ResidualConvBlock(base_channels * 16, dropout=dropout, dilation=2), ResidualConvBlock(base_channels * 16, dropout=dropout),
ResidualConvBlock(base_channels * 16, dropout=dropout, dilation=4), ResidualConvBlock(base_channels * 16, dropout=dropout),
ResidualConvBlock(base_channels * 16, dropout=dropout, dilation=8), ResidualConvBlock(base_channels * 16, dropout=dropout),
CBAM(base_channels * 16) CBAM(base_channels * 16)
) )
@@ -186,7 +183,7 @@ class MyModel(nn.Module):
# Output layer with smooth transition # Output layer with smooth transition
self.output = nn.Sequential( self.output = nn.Sequential(
nn.Conv2d(base_channels, base_channels // 2, kernel_size=3, padding=1), nn.Conv2d(base_channels, base_channels // 2, kernel_size=3, padding=1),
nn.GELU(), nn.LeakyReLU(0.1, inplace=True),
nn.Conv2d(base_channels // 2, 3, kernel_size=1), nn.Conv2d(base_channels // 2, 3, kernel_size=1),
nn.Sigmoid() # Ensure output is in [0, 1] range nn.Sigmoid() # Ensure output is in [0, 1] range
) )

View File

@@ -26,21 +26,11 @@ def create_arrays_from_image(image_array: np.ndarray, offset: tuple, spacing: tu
return image_array, known_array return image_array, known_array
def resize(img: Image, augment: bool = False): def resize(img: Image):
transforms_list = [ resize_transforms = transforms.Compose([
transforms.Resize((IMAGE_DIMENSION, IMAGE_DIMENSION)), transforms.Resize((IMAGE_DIMENSION, IMAGE_DIMENSION)),
transforms.CenterCrop((IMAGE_DIMENSION, IMAGE_DIMENSION)) transforms.CenterCrop((IMAGE_DIMENSION, IMAGE_DIMENSION))
] ])
if augment:
transforms_list = [
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1, hue=0.05),
transforms.RandomRotation(10),
] + transforms_list
resize_transforms = transforms.Compose(transforms_list)
return resize_transforms(img) return resize_transforms(img)
def preprocess(input_array: np.ndarray): def preprocess(input_array: np.ndarray):
input_array = np.asarray(input_array, dtype=np.float32) / 255.0 input_array = np.asarray(input_array, dtype=np.float32) / 255.0
@@ -51,9 +41,8 @@ class ImageDataset(torch.utils.data.Dataset):
Dataset class for loading images from a folder Dataset class for loading images from a folder
""" """
def __init__(self, datafolder: str, augment: bool = False): def __init__(self, datafolder: str):
self.imagefiles = sorted(glob.glob(os.path.join(datafolder,"**","*.jpg"),recursive=True)) self.imagefiles = sorted(glob.glob(os.path.join(datafolder,"**","*.jpg"),recursive=True))
self.augment = augment
def __len__(self): def __len__(self):
return len(self.imagefiles) return len(self.imagefiles)
@@ -62,7 +51,7 @@ class ImageDataset(torch.utils.data.Dataset):
index = int(idx) index = int(idx)
image = Image.open(self.imagefiles[index]) image = Image.open(self.imagefiles[index])
image = np.asarray(resize(image, self.augment)) image = np.asarray(resize(image))
image = preprocess(image) image = preprocess(image)
spacing_x = random.randint(2,6) spacing_x = random.randint(2,6)
spacing_y = random.randint(2,6) spacing_y = random.randint(2,6)

View File

@@ -84,21 +84,16 @@ def train(seed, testset_ratio, validset_ratio, data_path, results_path, early_st
plotpath = os.path.join(results_path, "plots") plotpath = os.path.join(results_path, "plots")
os.makedirs(plotpath, exist_ok=True) os.makedirs(plotpath, exist_ok=True)
image_dataset = datasets.ImageDataset(datafolder=data_path, augment=False) image_dataset = datasets.ImageDataset(datafolder=data_path)
n_total = len(image_dataset) n_total = len(image_dataset)
n_test = int(n_total * testset_ratio) n_test = int(n_total * testset_ratio)
n_valid = int(n_total * validset_ratio) n_valid = int(n_total * validset_ratio)
n_train = n_total - n_test - n_valid n_train = n_total - n_test - n_valid
indices = np.random.permutation(n_total) indices = np.random.permutation(n_total)
dataset_train = Subset(image_dataset, indices=indices[0:n_train])
# Create datasets with and without augmentation dataset_valid = Subset(image_dataset, indices=indices[n_train:n_train + n_valid])
train_dataset_source = datasets.ImageDataset(datafolder=data_path, augment=True) dataset_test = Subset(image_dataset, indices=indices[n_train + n_valid:n_total])
val_test_dataset_source = datasets.ImageDataset(datafolder=data_path, augment=False)
dataset_train = Subset(train_dataset_source, indices=indices[0:n_train])
dataset_valid = Subset(val_test_dataset_source, indices=indices[n_train:n_train + n_valid])
dataset_test = Subset(val_test_dataset_source, indices=indices[n_train + n_valid:n_total])
assert len(image_dataset) == len(dataset_train) + len(dataset_test) + len(dataset_valid) assert len(image_dataset) == len(dataset_train) + len(dataset_test) + len(dataset_valid)