File size: 2,515 Bytes
f38aa9b
 
 
 
 
 
 
 
 
 
 
 
ee3b201
 
 
 
 
f38aa9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee3b201
f38aa9b
ee3b201
f38aa9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee3b201
 
 
 
 
f38aa9b
ee3b201
 
 
 
f38aa9b
ee3b201
 
f38aa9b
ee3b201
 
 
 
 
 
f38aa9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
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)