skills/cv/3d-encoder-2d-decoder-segmentation/SKILL.md
3D ResNet encoder extracts volumetric features, pools depth dimension, then feeds into a 2D UNet/FPN decoder for segmentation
npx skillsauth add wenmin-wu/ds-skills cv-3d-encoder-2d-decoder-segmentationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
For volumetric data where the target is a 2D segmentation mask (e.g., ink detection on CT slices, organ segmentation from MRI), a hybrid architecture uses a 3D CNN encoder to capture inter-slice context, then pools the depth dimension from each feature map and feeds the resulting 2D feature maps into a standard 2D UNet/FPN decoder. This combines 3D spatial understanding with efficient 2D decoding.
import torch
import torch.nn as nn
import torch.nn.functional as F
class HybridSegModel(nn.Module):
def __init__(self, encoder_3d, encoder_dims, upscale=4):
super().__init__()
self.encoder = encoder_3d # 3D ResNet returning multi-scale features
self.decoder = FPNDecoder(encoder_dims, upscale)
def forward(self, x):
if x.ndim == 4:
x = x.unsqueeze(1) # (B, 1, D, H, W)
feat_maps_3d = self.encoder(x) # list of (B, C, D', H', W')
feat_maps_2d = [f.mean(dim=2) for f in feat_maps_3d] # pool depth
return self.decoder(feat_maps_2d)
class FPNDecoder(nn.Module):
def __init__(self, dims, upscale):
super().__init__()
self.convs = nn.ModuleList([
nn.Sequential(
nn.Conv2d(dims[i]+dims[i-1], dims[i-1], 3, 1, 1, bias=False),
nn.BatchNorm2d(dims[i-1]), nn.ReLU(inplace=True))
for i in range(1, len(dims))])
self.logit = nn.Conv2d(dims[0], 1, 1)
self.up = nn.Upsample(scale_factor=upscale, mode='bilinear')
def forward(self, features):
for i in range(len(features)-1, 0, -1):
up = F.interpolate(features[i], scale_factor=2, mode='bilinear')
features[i-1] = self.convs[i-1](torch.cat([features[i-1], up], 1))
return self.up(self.logit(features[0]))
data-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF