import torch import torch.nn as nn import torch.nn.functional as F import…
- User
import torch import torch.nn as nn import torch.nn.functional as F import segmentation_models_pytorch as smp
class AdaptiveKernelHypernet(nn.Module): """ Hypernetwork that predicts optimal kernels for Laplacian pyramid decomposition Based on HipyrNet architecture but adapted for denoising """ def init(self, kernel_size=5, input_channels=3): super(AdaptiveKernelHypernet, self).init()
codeself.kernel_size = kernel_size self.kernel_params = kernel_size * kernel_size # Feature extraction with irregular kernels (inspired by HipyrNet) self.conv_layers = nn.Sequential( nn.Conv2d(input_channels, 16, kernel_size=(3, 1), stride=1, padding=(1, 0)), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2), # 28x28 -> 14x14 nn.Conv2d(16, 32, kernel_size=(1, 3), stride=1, padding=(0, 1)), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2), # 14x14 -> 7x7 nn.Conv2d(32, 64, kernel_size=(3, 1), stride=1, padding=(1, 0)), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2), # 7x7 -> 3x3 nn.Conv2d(64, 128, kernel_size=(1, 3), stride=1, padding=(0, 1)), nn.ReLU(inplace=True), # LAST POOLING REMOVED nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1), nn.ReLU(inplace=True), nn.AdaptiveAvgPool2d((self.kernel_size, self.kernel_size)), nn.Conv2d(256, 3, kernel_size=1, stride=1, padding=0), ) # Normalization layer to ensure kernel sums appropriately self.kernel_norm = nn.Softmax(dim=-1) def forward(self, image): """ Generate adaptive kernel for input image Args: image: Input image [B, C, H, W] Returns: kernel: Adaptive kernel [B, 1, kernel_size, kernel_size] """ # Extract features and generate kernel x = self.conv_layers(image) # [B, 1, kernel_size, kernel_size] # Normalize kernel to ensure proper convolution properties batch_size = x.shape[0] kernel_flat = x.view(batch_size*3, -1) kernel_normalized = self.kernel_norm(kernel_flat) kernel = kernel_normalized.view(batch_size, 3, self.kernel_size, self.kernel_size) return kernel - Assistant
- User
ok so no obvious error
- Assistant
Right — nothing here jumps out as a syntax or shape-mismatch error. Functionally, it should run as long as:
Your train_loader and val_loader really do yield (clean_img, adv_img, label) in that order.
The encoder_name you pass is supported by segmentation_models_pytorch and has imagenet weights available.
Input image sizes are consistent across the pyramid and adapters.
The main “risks” are performance and convergence, not outright breakage — e.g., the per-sample convolution loop will slow things down, and the way you feed features into the UNet decoder is unconventional, so training might be less stable or slower to converge. If you want, I can streamline the convolution step and suggest a slightly more “decoder-friendly” feature ordering so you don’t lose efficiency.
- User
so will it learn to reform my images, if i test it on adv
- Assistant
Yes — in principle, with the setup you’ve got, it will learn to take your adversarial/noisy image and push it back toward the clean version during testing, because:
outputs from the UNet are trained to match clean_img via pythonCopyEditl1_loss = criterion(outputs, clean_img)
That’s the main driver of “restoration.”
The kernel-matching loss forces the hypernetwork to produce kernels from the adversarial input that look like the kernels from the clean image, so the filtering behaviour also becomes more “clean-image–like.”
During inference, you’ll feed only adv_img into the model, and it will still run the pyramid decomposition → UNet → reconstruction pipeline.
That said, whether it works well depends heavily on:
Quality of adversarial/clean pairs – If your training data covers the same attack type you’ll test on, it should generalize better.
Training stability – Your current loop should converge, but the unusual feature feeding into the UNet might make learning slower.