OCR Project
Loading...
Searching...
No Matches
cnn.h File Reference

Convolutional Neural Network for A–Z character recognition. More...

#include <stddef.h>
Include dependency graph for cnn.h:
This graph shows which files directly or indirectly include this file:

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

CNNcnn_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.

Detailed Description

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.

Macro Definition Documentation

◆ CNN_BATCH_SIZE

#define CNN_BATCH_SIZE   32

Training batch size.

◆ CNN_CONV_H

#define CNN_CONV_H   (CNN_IMG_H - CNN_KERNEL_H + 1) /* 26 */

Derived conv output size (no padding, stride 1).

◆ CNN_CONV_W

#define CNN_CONV_W   (CNN_IMG_W - CNN_KERNEL_W + 1) /* 26 */

◆ CNN_FLAT_SIZE

#define CNN_FLAT_SIZE   (CNN_N_FILTERS * CNN_POOL_OUT_H * CNN_POOL_OUT_W) /* 2704 */

Flattened size after pooling.

◆ CNN_HIDDEN_SIZE

#define CNN_HIDDEN_SIZE   128

Hidden dense layer size.

◆ CNN_IMG_H

#define CNN_IMG_H   56

Input image height and width (pixels).

◆ CNN_IMG_W

#define CNN_IMG_W   56

◆ CNN_KERNEL_H

#define CNN_KERNEL_H   3

◆ CNN_KERNEL_W

#define CNN_KERNEL_W   3

◆ CNN_LR

#define CNN_LR   0.001f

Learning rate for SGD.

◆ CNN_MOMENTUM

#define CNN_MOMENTUM   0.9f

Momentum coefficient.

◆ CNN_N_CLASSES

#define CNN_N_CLASSES   26

Number of output classes (A–Z).

◆ CNN_N_FILTERS

#define CNN_N_FILTERS   16

Convolution: number of filters and kernel size.

◆ CNN_POOL_H

#define CNN_POOL_H   4

Max-pooling window and stride (4×4 keeps flat size at 2704 with 56×56 input).

◆ CNN_POOL_OUT_H

#define CNN_POOL_OUT_H   (CNN_CONV_H / CNN_POOL_H) /* 13 */

Derived pool output size: floor(54/4) = 13.

◆ CNN_POOL_OUT_W

#define CNN_POOL_OUT_W   (CNN_CONV_W / CNN_POOL_W) /* 13 */

◆ CNN_POOL_W

#define CNN_POOL_W   4

Function Documentation

◆ cnn_backward()

void cnn_backward ( CNN * net,
int label )

Compute gradients via backpropagation.

Must be called after cnn_forward(). Accumulates gradients into net->grads.

Parameters
netCNN after a forward pass.
labelTrue class index in [0, CNN_N_CLASSES).
Note
This is a stub: the full implementation is deferred.
Here is the caller graph for this function:

◆ cnn_create()

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.

Returns
Pointer to a heap-allocated CNN, or NULL on allocation failure.
Note
The caller must free the returned pointer with cnn_free().
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cnn_forward()

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.

Parameters
netInitialised CNN.
imageFlat array of CNN_IMG_H * CNN_IMG_W normalised float pixels, row-major order.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cnn_free()

void cnn_free ( CNN * net)

Free all memory associated with a CNN.

Parameters
netPointer returned by cnn_create(). No-op if NULL.
Here is the caller graph for this function:

◆ cnn_loss()

float cnn_loss ( const CNN * net,
int label )

Compute cross-entropy loss for the current forward-pass output.

loss = -log(output[label])

Parameters
netCNN after cnn_forward().
labelTrue class index.
Returns
Scalar cross-entropy loss (≥ 0).
Here is the caller graph for this function:

◆ cnn_predict()

int cnn_predict ( CNN * net,
const float * image )

Predict the most likely class for a single image.

Runs cnn_forward() internally.

Parameters
netInitialised CNN with trained weights.
imageFlat float array, same layout as cnn_forward().
Returns
Class index in [0, CNN_N_CLASSES), i.e. 0 = 'A', 25 = 'Z'.
Here is the call graph for this function:

◆ cnn_update()

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.

Parameters
netCNN with filled net->grads from cnn_backward().
Note
Resets net->grads to zero after the update.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cnn_zero_grads()

void cnn_zero_grads ( CNN * net)

Zero all gradient accumulators in net->grads.

Parameters
netCNN whose gradients should be cleared.
Here is the caller graph for this function: