|
OCR Project
|
Convolutional Neural Network for A–Z character recognition. More...
#include <stddef.h>

Go to the source code of this file.
Classes | |
| struct | CNNWeights |
| Learnable parameters of the CNN. More... | |
| struct | CNNActivations |
| Intermediate activations produced by the forward pass. More... | |
| struct | CNN |
| Full CNN state: weights, gradients, momentum buffers, activations. More... | |
Macros | |
| #define | CNN_IMG_H 56 |
| #define | CNN_IMG_W 56 |
| #define | CNN_N_FILTERS 16 |
| #define | CNN_KERNEL_H 3 |
| #define | CNN_KERNEL_W 3 |
| #define | CNN_CONV_H (CNN_IMG_H - CNN_KERNEL_H + 1) /* 26 */ |
| #define | CNN_CONV_W (CNN_IMG_W - CNN_KERNEL_W + 1) /* 26 */ |
| #define | CNN_POOL_H 4 |
| #define | CNN_POOL_W 4 |
| #define | CNN_POOL_OUT_H (CNN_CONV_H / CNN_POOL_H) /* 13 */ |
| #define | CNN_POOL_OUT_W (CNN_CONV_W / CNN_POOL_W) /* 13 */ |
| #define | CNN_FLAT_SIZE (CNN_N_FILTERS * CNN_POOL_OUT_H * CNN_POOL_OUT_W) /* 2704 */ |
| #define | CNN_HIDDEN_SIZE 128 |
| #define | CNN_N_CLASSES 26 |
| #define | CNN_LR 0.001f |
| #define | CNN_MOMENTUM 0.9f |
| #define | CNN_BATCH_SIZE 32 |
Functions | |
| CNN * | cnn_create (void) |
| Allocate and initialise a CNN with He-initialised weights. | |
| void | cnn_free (CNN *net) |
| Free all memory associated with a CNN. | |
| void | cnn_forward (CNN *net, const float *image) |
| Run a full forward pass and populate net->act. | |
| void | cnn_backward (CNN *net, int label) |
| Compute gradients via backpropagation. | |
| void | cnn_update (CNN *net) |
| Apply one SGD-with-momentum update step to the weights. | |
| void | cnn_zero_grads (CNN *net) |
| Zero all gradient accumulators in net->grads. | |
| int | cnn_predict (CNN *net, const float *image) |
| Predict the most likely class for a single image. | |
| float | cnn_loss (const CNN *net, int label) |
| Compute cross-entropy loss for the current forward-pass output. | |
Convolutional Neural Network for A–Z character recognition.
Architecture: Input 28×28 (float, 0–1) → Conv2D 16 filters 3×3, stride 1, no padding → 16×26×26 → ReLU → MaxPool 2×2, stride 2 → 16×13×13 → Flatten → 2704 → Dense 2704 → 128 → ReLU → Dense 128 → 26 → Softmax → P(A)…P(Z)
Training: He initialisation, SGD with momentum, cross-entropy loss.
| #define CNN_BATCH_SIZE 32 |
Training batch size.
| #define CNN_CONV_H (CNN_IMG_H - CNN_KERNEL_H + 1) /* 26 */ |
Derived conv output size (no padding, stride 1).
| #define CNN_CONV_W (CNN_IMG_W - CNN_KERNEL_W + 1) /* 26 */ |
| #define CNN_FLAT_SIZE (CNN_N_FILTERS * CNN_POOL_OUT_H * CNN_POOL_OUT_W) /* 2704 */ |
Flattened size after pooling.
| #define CNN_HIDDEN_SIZE 128 |
Hidden dense layer size.
| #define CNN_IMG_H 56 |
Input image height and width (pixels).
| #define CNN_IMG_W 56 |
| #define CNN_KERNEL_H 3 |
| #define CNN_KERNEL_W 3 |
| #define CNN_LR 0.001f |
Learning rate for SGD.
| #define CNN_MOMENTUM 0.9f |
Momentum coefficient.
| #define CNN_N_CLASSES 26 |
Number of output classes (A–Z).
| #define CNN_N_FILTERS 16 |
Convolution: number of filters and kernel size.
| #define CNN_POOL_H 4 |
Max-pooling window and stride (4×4 keeps flat size at 2704 with 56×56 input).
| #define CNN_POOL_OUT_H (CNN_CONV_H / CNN_POOL_H) /* 13 */ |
Derived pool output size: floor(54/4) = 13.
| #define CNN_POOL_OUT_W (CNN_CONV_W / CNN_POOL_W) /* 13 */ |
| #define CNN_POOL_W 4 |
| void cnn_backward | ( | CNN * | net, |
| int | label ) |
Compute gradients via backpropagation.
Must be called after cnn_forward(). Accumulates gradients into net->grads.
| net | CNN after a forward pass. |
| label | True class index in [0, CNN_N_CLASSES). |

| CNN * cnn_create | ( | void | ) |
Allocate and initialise a CNN with He-initialised weights.
All conv kernels and dense weights are drawn from a zero-mean normal distribution with variance 2/fan_in (He, 2015). Biases are zeroed.


| void cnn_forward | ( | CNN * | net, |
| const float * | image ) |
Run a full forward pass and populate net->act.
Stages: Conv2D → ReLU → MaxPool → Flatten → Dense → ReLU → Dense → Softmax.
| net | Initialised CNN. |
| image | Flat array of CNN_IMG_H * CNN_IMG_W normalised float pixels, row-major order. |


| void cnn_free | ( | CNN * | net | ) |
Free all memory associated with a CNN.
| net | Pointer returned by cnn_create(). No-op if NULL. |

| float cnn_loss | ( | const CNN * | net, |
| int | label ) |
Compute cross-entropy loss for the current forward-pass output.
loss = -log(output[label])
| net | CNN after cnn_forward(). |
| label | True class index. |

| int cnn_predict | ( | CNN * | net, |
| const float * | image ) |
Predict the most likely class for a single image.
Runs cnn_forward() internally.
| net | Initialised CNN with trained weights. |
| image | Flat float array, same layout as cnn_forward(). |

| void cnn_update | ( | CNN * | net | ) |
Apply one SGD-with-momentum update step to the weights.
w_new = w - lr * (β * v + g) where v is the momentum velocity and g is the gradient.
| net | CNN with filled net->grads from cnn_backward(). |

