Skip to content
Back to skills

Cnn Encoder Spatial Feature Map

ASecurity

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.

  • 61 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 12, 2026
developmentpython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill cnn-encoder-spatial-feature-map --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Cnn Encoder Spatial Feature Map?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Cnn Encoder Spatial Feature Map
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-cnn-encoder-spatial-feature-map/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-cnn-encoder-spatial-feature-map)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: cv-cnn-encoder-spatial-feature-map
description: >
  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.
---
# 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

```python
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

- **Model choice**: ResNet-34/50 gives 7x7=49 spatial positions for 224x224 input; EfficientNet varies
- **Image size**: Larger input = more spatial positions = richer attention but more memory
- **Adaptive pooling**: Optionally add `nn.AdaptiveAvgPool2d((H, W))` before flatten for fixed spatial dims
- **Fine-tuning**: Freeze early layers, train later layers with the decoder

## References

- [InChI / Resnet + LSTM with attention / starter](https://www.kaggle.com/code/yasufuminakama/inchi-resnet-lstm-with-attention-starter)
- [Pytorch ResNet+LSTM with attention](https://www.kaggle.com/code/pasewark/pytorch-resnet-lstm-with-attention)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…