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

Binary serialisation and deserialisation of CNN weights. More...

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

Go to the source code of this file.

Macros

#define MODEL_MAGIC   "OCRC"
#define MODEL_VERSION   ((uint32_t)1)

Functions

int model_save (const CNN *net, const char *path)
 Save the weights of a trained CNN to a binary file.
int model_load (CNN *net, const char *path)
 Load CNN weights from a binary file into net.
int model_find_latest (const char *dir, char *out_path, size_t out_len)
 Find the most recently modified .bin file inside a directory.

Detailed Description

Binary serialisation and deserialisation of CNN weights.

The model file format is a flat binary dump of the CNNWeights struct, preceded by a 4-byte magic number and a 4-byte version field:

[magic: 4 bytes 'OCRC'] [version: uint32_t] [CNNWeights: sizeof(CNNWeights) bytes]

This allows fast save/load with a single fwrite/fread call and easy detection of incompatible files.

Macro Definition Documentation

◆ MODEL_MAGIC

#define MODEL_MAGIC   "OCRC"

Magic bytes written at the start of every model file.

◆ MODEL_VERSION

#define MODEL_VERSION   ((uint32_t)1)

Current model file format version. Increment when CNNWeights changes.

Function Documentation

◆ model_find_latest()

int model_find_latest ( const char * dir,
char * out_path,
size_t out_len )

Find the most recently modified .bin file inside a directory.

Iterates over all regular files with a ".bin" suffix in dir and returns the one with the highest mtime.

Parameters
dirDirectory to search (e.g. "models/").
out_pathBuffer that receives the full path of the chosen file.
out_lenSize of out_path in bytes.
Returns
0 if a file was found and written to out_path, -1 if the directory is empty or cannot be opened.
Here is the caller graph for this function:

◆ model_load()

int model_load ( CNN * net,
const char * path )

Load CNN weights from a binary file into net.

Validates the magic number and version before reading. If validation fails, net is left unchanged.

Parameters
netCNN to load weights into. Must not be NULL.
pathSource file path.
Returns
0 on success, -1 on I/O error or format mismatch.
Here is the caller graph for this function:

◆ model_save()

int model_save ( const CNN * net,
const char * path )

Save the weights of a trained CNN to a binary file.

Creates or overwrites the file at path. The directory must exist.

Parameters
netCNN whose weights should be saved. Must not be NULL.
pathDestination file path (e.g. "models/my_model.bin").
Returns
0 on success, -1 on I/O error (message printed to stderr).
Here is the caller graph for this function: