1 Commits

Author SHA1 Message Date
2902927b72 added result, 18.5276 2026-01-24 17:56:32 +01:00
10 changed files with 89 additions and 51 deletions

View File

@@ -15,13 +15,25 @@ def init_weights(m):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, (nn.BatchNorm2d, nn.InstanceNorm2d)):
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.GroupNorm):
if m.weight is not None:
nn.init.constant_(m.weight, 1)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
def _make_norm(num_channels: int) -> nn.Module:
"""Batch-size independent normalization (works well for batch_size=1 eval)."""
# Choose a group count that divides num_channels.
num_groups = min(32, num_channels)
while num_groups > 1 and (num_channels % num_groups) != 0:
num_groups //= 2
return nn.GroupNorm(num_groups=num_groups, num_channels=num_channels)
class ChannelAttention(nn.Module):
"""Channel attention module (squeeze-and-excitation style)"""
def __init__(self, channels, reduction=16):
@@ -71,36 +83,55 @@ class CBAM(nn.Module):
class ConvBlock(nn.Module):
"""Convolutional block with Conv2d -> InstanceNorm2d -> GELU"""
def __init__(self, in_channels, out_channels, kernel_size=3, padding=1, dropout=0.0, dilation=1):
"""Convolutional block with Conv2d -> BatchNorm -> LeakyReLU"""
def __init__(self, in_channels, out_channels, kernel_size=3, padding=1, dropout=0.0):
super().__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, padding=padding, dilation=dilation)
# InstanceNorm is preferred for style/inpainting tasks
self.bn = nn.InstanceNorm2d(out_channels, affine=True)
self.act = nn.GELU()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, padding=padding)
self.bn = _make_norm(out_channels)
self.relu = nn.LeakyReLU(0.1, inplace=True)
self.dropout = nn.Dropout2d(dropout) if dropout > 0 else nn.Identity()
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):
"""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__()
self.conv1 = nn.Conv2d(channels, channels, 3, padding=dilation, dilation=dilation)
self.bn1 = nn.InstanceNorm2d(channels, affine=True)
self.conv2 = nn.Conv2d(channels, channels, 3, padding=dilation, dilation=dilation)
self.bn2 = nn.InstanceNorm2d(channels, affine=True)
self.act = nn.GELU()
self.conv1 = nn.Conv2d(channels, channels, 3, padding=1)
self.bn1 = _make_norm(channels)
self.conv2 = nn.Conv2d(channels, channels, 3, padding=1)
self.bn2 = _make_norm(channels)
self.relu = nn.LeakyReLU(0.1, inplace=True)
self.dropout = nn.Dropout2d(dropout) if dropout > 0 else nn.Identity()
def forward(self, 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.bn2(self.conv2(out))
out = out + residual
return self.act(out)
return self.relu(out)
class GatedConvBlock(nn.Module):
"""Gated convolution block (helps the network condition on the mask channel)."""
def __init__(self, in_channels, out_channels, kernel_size=3, padding=1, dropout=0.0):
super().__init__()
self.feature = nn.Conv2d(in_channels, out_channels, kernel_size, padding=padding)
self.gate = nn.Conv2d(in_channels, out_channels, kernel_size, padding=padding)
self.norm = _make_norm(out_channels)
self.act = nn.LeakyReLU(0.1, inplace=True)
self.dropout = nn.Dropout2d(dropout) if dropout > 0 else nn.Identity()
def forward(self, x):
feat = self.feature(x)
gate = torch.sigmoid(self.gate(x))
out = feat * gate
out = self.norm(out)
out = self.act(out)
out = self.dropout(out)
return out
class DownBlock(nn.Module):
@@ -150,7 +181,7 @@ class MyModel(nn.Module):
# Initial convolution with larger receptive field
self.init_conv = nn.Sequential(
ConvBlock(n_in_channels, base_channels, kernel_size=7, padding=3),
GatedConvBlock(n_in_channels, base_channels, kernel_size=7, padding=3, dropout=dropout),
ConvBlock(base_channels, base_channels),
ResidualConvBlock(base_channels)
)
@@ -164,9 +195,9 @@ class MyModel(nn.Module):
# Bottleneck with multiple residual blocks
self.bottleneck = nn.Sequential(
ConvBlock(base_channels * 16, base_channels * 16, dropout=dropout),
ResidualConvBlock(base_channels * 16, dropout=dropout, dilation=2),
ResidualConvBlock(base_channels * 16, dropout=dropout, dilation=4),
ResidualConvBlock(base_channels * 16, dropout=dropout, dilation=8),
ResidualConvBlock(base_channels * 16, dropout=dropout),
ResidualConvBlock(base_channels * 16, dropout=dropout),
ResidualConvBlock(base_channels * 16, dropout=dropout),
CBAM(base_channels * 16)
)
@@ -186,7 +217,7 @@ class MyModel(nn.Module):
# Output layer with smooth transition
self.output = nn.Sequential(
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.Sigmoid() # Ensure output is in [0, 1] range
)

View File

@@ -26,22 +26,31 @@ def create_arrays_from_image(image_array: np.ndarray, offset: tuple, spacing: tu
return image_array, known_array
def resize(img: Image, augment: bool = False):
transforms_list = [
def resize(img: Image):
resize_transforms = transforms.Compose([
transforms.Resize((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)
def augment_geometric(img: Image.Image) -> Image.Image:
"""Lightweight, label-preserving augmentation (safe for train/val/test splits)."""
# Horizontal flip
if random.random() < 0.5:
img = img.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
# Vertical flip (less frequent)
if random.random() < 0.2:
img = img.transpose(Image.Transpose.FLIP_TOP_BOTTOM)
# 90-degree rotations (no interpolation artifacts)
r = random.random()
if r < 0.25:
img = img.transpose(Image.Transpose.ROTATE_90)
elif r < 0.5:
img = img.transpose(Image.Transpose.ROTATE_180)
elif r < 0.75:
img = img.transpose(Image.Transpose.ROTATE_270)
return img
def preprocess(input_array: np.ndarray):
input_array = np.asarray(input_array, dtype=np.float32) / 255.0
return input_array
@@ -51,9 +60,8 @@ class ImageDataset(torch.utils.data.Dataset):
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.augment = augment
def __len__(self):
return len(self.imagefiles)
@@ -61,13 +69,17 @@ class ImageDataset(torch.utils.data.Dataset):
def __getitem__(self, idx:int):
index = int(idx)
image = Image.open(self.imagefiles[index])
image = np.asarray(resize(image, self.augment))
image = Image.open(self.imagefiles[index]).convert("RGB")
image = augment_geometric(image)
image = np.asarray(resize(image))
image = preprocess(image)
spacing_x = random.randint(2,6)
spacing_y = random.randint(2,6)
offset_x = random.randint(0,8)
offset_y = random.randint(0,8)
# Sample a grid-mask similar in density to the challenge testset (~8% known pixels).
# IMPORTANT: offset ranges must be tied to spacing to avoid accidental distribution shift.
spacing_x = random.randint(4, 6)
spacing_y = random.randint(2, 4)
offset_x = random.randint(0, spacing_x - 1)
offset_y = random.randint(0, spacing_y - 1)
spacing = (spacing_x, spacing_y)
offset = (offset_x, offset_y)
input_array, known_array = create_arrays_from_image(image.copy(), offset, spacing)

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")
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_test = int(n_total * testset_ratio)
n_valid = int(n_total * validset_ratio)
n_train = n_total - n_test - n_valid
indices = np.random.permutation(n_total)
# Create datasets with and without augmentation
train_dataset_source = datasets.ImageDataset(datafolder=data_path, augment=True)
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])
dataset_train = Subset(image_dataset, indices=indices[0:n_train])
dataset_valid = Subset(image_dataset, indices=indices[n_train:n_train + n_valid])
dataset_test = Subset(image_dataset, indices=indices[n_train + n_valid:n_total])
assert len(image_dataset) == len(dataset_train) + len(dataset_test) + len(dataset_valid)