# DenseCNN attention-map gallery

The published gallery uses Grad-CAM on the final DenseCNN feature block. Each
sample contains the original CIFAR-10 image, the gradient-weighted heatmap, and
the heatmap overlaid on the image.

## Published examples

| Test index | True class | Predicted class | Result |
|---:|---|---|---|
| 0 | cat | cat | correct |
| 1 | ship | ship | correct |
| 2 | ship | ship | correct |
| 3957 | truck | airplane | incorrect |
| 5808 | dog | cat | incorrect |
| 1580 | frog | bird | incorrect |

![DenseCNN Grad-CAM gallery](assets/attention/densecnn_gradcam_gallery.png)

## Regenerate an example

MPS is required:

```bash
.venv/bin/python scripts/visualize_densecnn.py \
  --checkpoint checkpoints/densecnn.pt \
  --index 3957 \
  --output docs/assets/attention/densecnn_3957.png
```

The implementation hooks the final feature block, backpropagates the predicted
class score, averages gradients spatially to obtain channel weights, and
combines the weighted activations into a normalized 32×32 heatmap.

Grad-CAM is a qualitative diagnostic. A bright region means the model output
was sensitive to features there; it does not prove that the region caused the
decision or that the model learned a human concept.

## Top-10 channel analysis

Showing all final-block channels would be too dense to review. The companion
tool ranks channels by the absolute value of:

`mean gradient for predicted class × mean channel activation`

Positive contributions support the predicted class; negative contributions
suppress it. The heatmap itself shows where that channel activates.

```bash
.venv/bin/python scripts/visualize_densecnn_channels.py \
  --checkpoint checkpoints/densecnn.pt \
  --index 3957 --top-k 10 \
  --output docs/assets/attention/densecnn_channels_3957.png
```

### Good cases

![Correct cat top channels](assets/attention/densecnn_channels_0.png)

- **Cat → cat (99.0%):** the leading channels overlap on the head and body,
  while most foreground and background regions are suppressed. Complementary
  object-level cues support a stable prediction.

![Correct ship top channels](assets/attention/densecnn_channels_1.png)

- **Ship → ship (94.2%):** the dominant channel covers the full vessel. Other
  channels isolate the hull, waterline, and superstructure. The model combines
  global shape with localized maritime cues.

### Bad cases

![Truck misclassified as airplane top channels](assets/attention/densecnn_channels_3957.png)

- **Truck → airplane (98.7%):** localization is not the problem—the network
  focuses on the object. The top channels repeatedly encode an elongated
  horizontal silhouette and strong horizon boundary. At CIFAR resolution,
  weak wheel and road details fail to override airplane-like shape cues.

![Dog misclassified as cat top channels](assets/attention/densecnn_channels_5808.png)

- **Dog → cat (98.4%):** most channels correctly localize the animal, but they
  respond to shared compact-body, fur, and face textures. The error is class
  discrimination rather than foreground localization.

Across the gallery, the correct cases contain more complementary spatial cues.
The incorrect cases often contain many high-ranked channels repeating nearly
the same coarse cue. This is evidence of correlated feature reliance, but it is
still an interpretation of the maps rather than a causal proof.

## Compare DenseCNN with CNN Best

`CNN Best` is the reader-facing alias for `CNN-C + BN`, the highest-scoring
completed model among the original A/B/C
CNN baselines: 88.48% test accuracy versus DenseCNN's 92.53%. The comparison
tool uses the same CIFAR-10 indices for both networks and hooks each model's
final convolutional feature map, so differences are not caused by different
input samples.

![DenseCNN versus CNN-C + BN Grad-CAM](assets/attention/densecnn_vs_cnn_c_bn_gradcam.png)

| Index | True class | DenseCNN | CNN-C + BN | Observation |
|---:|---|---|---|---|
| 0 | cat | cat (99.0%) | cat (100.0%) | both correct; DenseCNN covers a broader object region |
| 1 | ship | ship (94.2%) | ship (100.0%) | both correct using different spatial evidence |
| 3957 | truck | airplane (98.7%) | airplane (100.0%) | shared high-confidence shape confusion |
| 5808 | dog | cat (98.4%) | dog (59.2%) | only CNN-C + BN is correct |

The dog case is an important counterexample to any claim that DenseCNN is
uniformly better. Its aggregate test accuracy is higher, but CNN-C + BN uses
more distributed evidence and succeeds on this particular image. Conversely,
both networks fail on the truck, suggesting that ambiguous low-resolution
shape cues can survive substantial architectural differences.

```bash
.venv/bin/python scripts/visualize_attention_comparison.py \
  --dense-checkpoint checkpoints/densecnn.pt \
  --cnn-checkpoint checkpoints/cnn_C_bn.pt \
  --indices 0 1 3957 5808 \
  --output docs/assets/attention/densecnn_vs_cnn_c_bn_gradcam.png
```

The command also writes a JSON sidecar containing the true class, prediction,
confidence, and correctness for both models. MPS is mandatory. Compare whether
each heatmap covers complementary object regions or collapses onto a single
texture, edge, or background cue; do not interpret a sharper map by itself as
proof that one model is better.
