File size: 3,394 Bytes
5d00c1b | 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | # CNN Architecture (Mathematical Formulation)
We consider an input image
$$
x \in \mathbb{R}^{3 \times 256 \times 256}
$$
with 3 color channels (RGB). Shapes are written as (channels Γ height Γ width).
---
## 1. Convolution Block 1 (16 channels)
**Conv1 (3β16, kernel=3, padding=1, stride=1):**
$$
y^{(1)}_{k,i,j}
=\sum_{c=1}^{3}\sum_{u=-1}^{1}\sum_{v=-1}^{1}
W^{(1)}_{k,c,u,v}\; x_{c,\,i+u,\,j+v} + b^{(1)}_{k}
$$
Output:
$$
Y^{(1)} \in \mathbb{R}^{16 \times 256 \times 256}
$$
**BatchNorm1:**
$$
\hat{y}^{(1)}_{k,i,j}=\frac{y^{(1)}_{k,i,j}-\mu_k}{\sqrt{\sigma_k^2+\varepsilon}},\qquad
z^{(1)}_{k,i,j}=\gamma_k \hat{y}^{(1)}_{k,i,j}+\beta_k
$$
**ReLU1:**
$$
a^{(1)}_{k,i,j} = \max(0, z^{(1)}_{k,i,j})
$$
**MaxPool1 (2Γ2, stride=2):**
$$
p^{(1)}_{k,i,j} = \max_{0 \le u,v < 2} a^{(1)}_{k,\,2i+u,\,2j+v}
$$
Shape: $16 \times 128 \times 128$
---
## 2. Convolution Block 2 (32 channels)
**Conv2 (16β32):**
$$
y^{(2)}_{k,i,j}
=\sum_{c=1}^{16}\sum_{u=-1}^{1}\sum_{v=-1}^{1}
W^{(2)}_{k,c,u,v}\; p^{(1)}_{c,\,i+u,\,j+v} + b^{(2)}_{k}
$$
Then BN2 β ReLU2 β MaxPool2.
Shape after pooling: $32 \times 64 \times 64$.
---
## 3. Convolution Block 3 (64 channels)
**Conv3 (32β64):**
$$
y^{(3)}_{k,i,j}
=\sum_{c=1}^{32}\sum_{u=-1}^{1}\sum_{v=-1}^{1}
W^{(3)}_{k,c,u,v}\; p^{(2)}_{c,\,i+u,\,j+v} + b^{(3)}_{k}
$$
Then BN3 β ReLU3 β MaxPool3.
Shape after pooling: $64 \times 32 \times 32$.
---
## 4. Flatten
$$
h = \mathrm{vec}\bigl(p^{(3)}\bigr) \in \mathbb{R}^{64 \cdot 32 \cdot 32} = \mathbb{R}^{65536}
$$
---
## 5. Fully Connected Head
**FC1 (65536β256) + ReLU:**
$$
u_1 = W_1 h + b_1,\quad
a_1 = \max(0, u_1)
$$
**Dropout (p=0.5):**
$$
m \sim \text{Bernoulli}(0.5)^{256},\quad
\tilde{a}_1 = \frac{m \odot a_1}{0.5}
$$
**FC2 (256β64) + ReLU:**
$$
u_2 = W_2 \tilde{a}_1 + b_2,\quad
a_2 = \max(0, u_2)
$$
**FC3 (64β4) logits:**
$$
z = W_3 a_2 + b_3 \in \mathbb{R}^4
$$
---
## 6. Prediction & Loss
**Softmax (conceptual):**
$$
p_c = \frac{e^{z_c}}{\sum_{j=1}^4 e^{z_j}}, \quad c = 1,\dots,4
$$
**Predicted class:**
$$
\hat{y} = \arg\max_c z_c
$$
**Cross-Entropy Loss** for true class $y$:
$$
\mathcal{L}(z,y) = -\log p_y
= -z_y + \log\!\Bigl(\sum_{j=1}^{4} e^{z_j}\Bigr)
$$
---
## 7. Why These Pieces Matter
- **Deeper convs (16β32β64):** extract features hierarchically (edges β textures β scenes).
- **ReLU:** avoids vanishing gradients, speeds training.
- **BatchNorm:** normalizes activations, stabilizes training, regularizes.
- **MaxPool:** adds translation invariance, reduces computation.
- **Dropout:** prevents overfitting by randomly dropping neurons.
- **FC head:** compresses learned features into logits for the 4 classes.
# CNN Architecture Diagram
```text
Input (3 Γ 256 Γ 256)
β
βΌ
[Conv1: 3β16, 3Γ3 + BN + ReLU]
β
βΌ
MaxPool 2Γ2 β (16 Γ 128 Γ 128)
β
βΌ
[Conv2: 16β32, 3Γ3 + BN + ReLU]
β
βΌ
MaxPool 2Γ2 β (32 Γ 64 Γ 64)
β
βΌ
[Conv3: 32β64, 3Γ3 + BN + ReLU]
β
βΌ
MaxPool 2Γ2 β (64 Γ 32 Γ 32)
β
βΌ
Flatten β 65536
β
βΌ
[FC1: 65536β256 + ReLU + Dropout]
β
βΌ
[FC2: 256β64 + ReLU]
β
βΌ
[FC3: 64β4 logits]
β
βΌ
Softmax β {Sea, Forest, Urban, Field}
|