Cnn encoder spatial feature map

From wenmin-wu/ds-skills · View interactive version

Cnn encoder spatial feature map is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Strips the global pool and FC head from a pretrained CNN to expose spatial feature maps (H x W x C) for attention-based decoding.

How to use this skill

Skill file

CNN Encoder Spatial Feature Map

Overview

For image-to-sequence tasks (captioning, OCR, molecular translation), the CNN must output spatial feature maps rather than a single vector. Replace the global pooling and FC head with nn.Identity(), then permute/reshape the output to (batch, H*W, C). Each spatial position becomes an "input token" the decoder can attend to.

Quick Start

import timm
import torch.nn as nn

class SpatialEncoder(nn.Module):
    def __init__(self, model_name="resnet34", pretrained=True):
        super().__init__()
        self.cnn = timm.create_model(model_name, pretrained=pretrained)
        self.n_features = self.cnn.fc.in_features
        self.cnn.global_pool = nn.Identity()
        self.cnn.fc = nn.Identity()

    def forward(self, x):
        features = self.cnn(x)                      # (B, C, H, W)
        features = features.permute(0, 2, 3, 1)     # (B, H, W, C)
        B, H, W, C = features.shape
        features = features.view(B, H * W, C)       # (B, num_pixels, C)
        return features

Workflow

  1. Load any pretrained CNN via timm/torchvision
  2. Replace global_pool and fc with nn.Identity()
  3. Forward pass yields (B, C, H, W) spatial tensor
  4. Permute and reshape to (B, num_pixels, C) for attention input
  5. Feed spatial features into attention-based decoder

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.