skills/cv/gem-pooling/SKILL.md
Replaces global average pooling with Generalized Mean (GeM) pooling, using a learnable or fixed exponent to emphasize high-activation regions.
npx skillsauth add wenmin-wu/ds-skills cv-gem-poolingInstall 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.
Global Average Pooling (GAP) treats all spatial locations equally, diluting strong local signals in large feature maps. Generalized Mean (GeM) pooling raises activations to power p before averaging, then takes the p-th root — higher p values emphasize peak activations (approaching max pooling at p→∞). With p=1 it's average pooling; p=3 is a common default that boosts discriminative regions. Used extensively in retrieval (image search, metric learning) and medical imaging where lesions occupy small regions.
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.parameter import Parameter
import timm
class GeM(nn.Module):
def __init__(self, p=3.0, eps=1e-6, p_trainable=False):
super().__init__()
self.p = Parameter(torch.ones(1) * p) if p_trainable else p
self.eps = eps
def forward(self, x):
return F.avg_pool2d(
x.clamp(min=self.eps).pow(self.p),
(x.size(-2), x.size(-1))
).pow(1.0 / self.p)
# Plug into any timm backbone
backbone = timm.create_model('seresnext50_32x4d', pretrained=True,
num_classes=0, global_pool='')
pool = GeM(p=3.0, p_trainable=True)
head = nn.Linear(backbone.num_features, num_classes)
# Forward
features = backbone(images) # (B, C, H, W)
pooled = pool(features).squeeze() # (B, C)
logits = head(pooled) # (B, num_classes)
global_pool='' and num_classes=0 to get raw feature mapsp_trainable=True for the model to learn optimal pooling aggressiondata-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