--- license: mit tags: - image-classification - mnist - cnn - resnet - pytorch - experiment pipeline_tag: image-classification --- # a0 — 순수 CNN vs Residual CNN (MNIST) 깊이를 늘린 순수 CNN의 한계(degradation 문제)를 확인하고, residual connection이 이를 해결하는지 검증한 실험 프로젝트. PyTorch로 처음부터 구현. ## Model Description - **Task**: MNIST 손글씨 숫자 분류 (28×28, 10 classes) - **Architecture**: 채널 64 고정의 conv 스택. `depth` = 블록 수(블록당 conv 2개) - `PlainBlock`: Conv → BN → ReLU → Conv → BN → ReLU - `ResBlock`: 위 구조 + skip connection (`out + x`) - **학습**: Adam (lr=1e-3) + CosineAnnealingLR, batch=128, 15 epochs - 가중치는 `runs/depthN[_res][_nobn]/model.pt` (PyTorch state_dict)에 저장 ## Results ### BatchNorm 있음 — best val accuracy | depth (blocks) | plain CNN | + residual | |---|---|---| | 5 | **99.70%** | — | | 10 | 99.62% | 99.58% | | 15 | 99.60% | 99.52% | | 20 | 99.58% | 99.66% | | 40 | 98.89% (불안정) | 99.56% | ### BatchNorm 없음 — best val accuracy | depth (blocks) | plain CNN | + residual | |---|---|---| | 20 | **11.35% (붕괴)** | 99.39% | | 40 | **11.35% (붕괴)** | 99.40% | 자세한 분석은 [RESULTS.md](RESULTS.md), 데이터 출처는 [DATA.md](DATA.md) 참조. ## How to use ```python import torch from src.model import CNN model = CNN(depth=20, use_residual=True, bn=False) model.load_state_dict(torch.load("runs/depth20_res_nobn/model.pt", weights_only=True)) model.eval() ``` ## Training details ```bash # 예: depth20 plain CNN, BN 없음 python src/train.py --depth 20 --no-bn --epochs 15 # 예: depth20 residual CNN, BN 없음 python src/train.py --depth 20 --residual --no-bn --epochs 15 ``` 주요 옵션: `--depth`(블록 수), `--residual`, `--no-bn`, `--channels`, `--epochs`, `--batch-size`, `--lr` ## 구조 ``` src/ model.py # PlainBlock / ResBlock / CNN 빌더 train.py # MNIST 학습 스크립트 plot_results.py # runs/ 의 history.json → 비교 그래프 생성 runs/ # 실험별 history.json + model.pt 가중치 results/ # 시각화 PNG data/ # MNIST (git 제외, torchvision 자동 다운로드) ``` ## 재현 환경 ```bash python -m venv .venv .venv/Scripts/pip install torch torchvision --index-url https://download.pytorch.org/whl/cu128 ``` 테스트 환경: PyTorch 2.11.0+cu128, NVIDIA RTX 5070 Ti (CUDA 12.8)