skills/cv/2d-to-3d-model-conversion/SKILL.md
Converts a pretrained 2D CNN into a 3D CNN by recursively replacing Conv2d/BN2d/Pool2d layers and inflating kernel weights along the depth axis.
npx skillsauth add wenmin-wu/ds-skills cv-2d-to-3d-model-conversionInstall 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.
Pretrained 3D CNNs are scarce, but pretrained 2D models (ImageNet) are abundant. This technique recursively walks a 2D model, replaces Conv2d with Conv3d, BatchNorm2d with BatchNorm3d, and pooling layers with their 3D counterparts. Crucially, 2D kernel weights are inflated by repeating along the new depth dimension, preserving learned spatial features. This gives 3D volumetric models (CT, MRI) a strong initialization without training from scratch.
import torch
import torch.nn as nn
def convert_3d(module):
module_output = module
if isinstance(module, nn.BatchNorm2d):
module_output = nn.BatchNorm3d(module.num_features, module.eps,
module.momentum, module.affine,
module.track_running_stats)
if module.affine:
with torch.no_grad():
module_output.weight = module.weight
module_output.bias = module.bias
module_output.running_mean = module.running_mean
module_output.running_var = module.running_var
elif isinstance(module, nn.Conv2d):
module_output = nn.Conv3d(
module.in_channels, module.out_channels,
kernel_size=module.kernel_size[0], stride=module.stride[0],
padding=module.padding[0], dilation=module.dilation[0],
groups=module.groups, bias=module.bias is not None)
# Inflate: repeat 2D weights along depth
module_output.weight = nn.Parameter(
module.weight.unsqueeze(-1).repeat(1, 1, 1, 1, module.kernel_size[0]))
elif isinstance(module, nn.MaxPool2d):
module_output = nn.MaxPool3d(
kernel_size=module.kernel_size, stride=module.stride,
padding=module.padding)
for name, child in module.named_children():
module_output.add_module(name, convert_3d(child))
return module_output
# Usage: inflate a pretrained EfficientNet to 3D
model_2d = torchvision.models.efficientnet_v2_s(weights="IMAGENET1K_V1")
model_3d = convert_3d(model_2d)
convert_3d(model) to recursively replace all 2D layers with 3D equivalentsdata-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