API Reference
This page documents the public Python API exposed by the ccexam package.
Data (src.data)
get_loaders
from src.data import get_loaders
train_loader, test_loader = get_loaders(
dataset: str = "mnist",
batch_size: int = 64,
data_dir: str = "datasets",
num_workers: int = 2,
download: bool = True,
)
Returns (train_loader, test_loader) as torch.utils.data.DataLoader objects for a registered dataset. Dataset files are downloaded automatically on first use.
Example
from src.data import get_loaders
train_loader, test_loader = get_loaders(dataset="svhn", batch_size=128)
for images, labels in train_loader:
...
Data modules
Each dataset has a dedicated data module that can also be used directly:
Class |
Dataset |
Default normalization |
|---|---|---|
|
MNIST |
mean=0.1307, std=0.3081 |
|
USPS |
mean=0.2471, std=0.2994 |
|
SVHN |
mean=(0.4377, 0.4438, 0.4728), std=(0.1980, 0.2010, 0.1970) |
All modules expose .train_loader(), .val_loader(), and .test_loader() methods.
Models (src.models)
All model classes are torch.nn.Module subclasses callable with an image batch tensor.
MNISTNet
CNN for 28×28 grayscale MNIST digits. Two convolutional blocks followed by a two-layer classifier head (128 hidden units, 10 outputs).
from src.models import MNISTNet
model = MNISTNet()
logits = model(batch) # batch shape: (N, 1, 28, 28)
USPSNet
Same architecture as MNISTNet, adapted for 16×16 grayscale USPS digits.
from src.models import USPSNet
model = USPSNet()
logits = model(batch) # batch shape: (N, 1, 16, 16)
SVHNNet
Deeper CNN for 32×32 RGB SVHN digits. Three convolutional blocks with batch normalization, followed by a dropout-regularized classifier head.
from src.models import SVHNNet
model = SVHNNet(num_classes=10, dropout=0.3)
logits = model(batch) # batch shape: (N, 3, 32, 32)
Parameter |
Default |
Description |
|---|---|---|
|
|
Number of output classes |
|
|
Dropout probability in the classifier head |
Training (src.training)
train
from src.training import train
metrics = train(dataset: str = "mnist", **kwargs) -> dict
Convenience entry point. Selects the correct data module and model from DATASET_REGISTRY and runs training.
Keyword argument |
Default |
Description |
|---|---|---|
|
|
Number of training epochs |
|
|
Learning rate (Adam optimizer) |
|
|
Mini-batch size |
|
registry default |
Where to save the |
|
|
Root directory for dataset downloads |
|
auto |
|
|
|
Custom |
Returns a dict with keys loss, accuracy, val_loss, val_accuracy, epochs, checkpoint_path.
Example
from src.training import train
metrics = train(dataset="usps", epochs=5, batch_size=128)
print(metrics["accuracy"])
DATASET_REGISTRY
Maps dataset names to DatasetSpec objects (data module class, model class, default checkpoint path).
Key |
Model |
Default checkpoint (file location) |
CLI / API argument |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Paths are resolved relative to
src/viaSRC_ROOT. Pass only theweights/<name>.pthportion when using--checkpoint-pathorcheckpoint_path=.
Evaluation (src.evaluation)
evaluate
from src.evaluation import evaluate
evaluate(
inference: BaseInference,
dataloader: Iterable,
metrics: dict[str, Callable[[Any, Any], float]] | None = None,
) -> dict
Run a trained model over every batch in dataloader and report classification metrics together with the average inference speed. Runs under torch.no_grad.
Parameter |
Type |
Description |
|---|---|---|
|
|
Inference instance from |
|
|
Yields |
|
|
Mapping of metric name to |
Returns a dict with:
"precision"— macro-averaged precision"recall"— macro-averaged recall"speed_ms"— average forward-pass time in milliseconds per sample (data-loading time excluded)
Example
from src.data import DATA_MODULES
from src.evaluation import evaluate
from src.inference import InferenceFactory
inference = InferenceFactory.create("svhn", checkpoint_path="weights/svhn.pth", device="cpu")
test_loader = DATA_MODULES["svhn"](data_dir="datasets", batch_size=256).test_loader()
results = evaluate(inference, test_loader)
print(results) # {"precision": 0.91, "recall": 0.90, "speed_ms": 1.42}
CLI usage
src.evaluation is also runnable as a module to evaluate a registered dataset on its test set:
python -m src.evaluation --dataset svhn --checkpoint-path weights/svhn.pth --device cpu
Argument |
Default |
Description |
|---|---|---|
|
|
One of |
|
registry default |
Checkpoint to load |
|
|
Evaluation batch size |
|
|
Root directory containing the datasets |
|
|
DataLoader worker count |
|
|
|
|
|
Logging level |
DEFAULT_METRICS
Module-level dict of the default metric callables used when metrics=None is passed to evaluate. Override or extend to add metrics like F1 or accuracy:
from src.evaluation import DEFAULT_METRICS, evaluate
from sklearn.metrics import f1_score
custom = {**DEFAULT_METRICS, "f1": lambda y, p: f1_score(y, p, average="macro")}
results = evaluate(inference, test_loader, metrics=custom)
Inference (src.inference)
run_inference
from src.inference import run_inference
run_inference(
model: str,
input_path: str | Path,
checkpoint_path: str | Path | None = None,
device: str | None = None,
) -> int | list[int]
Run inference on one image or a folder of images.
Parameter |
Type |
Description |
|---|---|---|
|
|
Dataset alias: |
|
|
Path to a single image file or a directory of images |
|
|
Override the default checkpoint file (optional) |
|
|
Torch device, e.g. |
Returns a single int label when input_path is a file, or a list[int] (sorted by filename) when it is a directory.
Example
from src.inference import run_inference
label = run_inference(model="usps", input_path="digit.png")
print(label) # e.g. 7
labels = run_inference(model="mnist", input_path="folder/")
print(labels) # e.g. [3, 1, 4, 1, 5]
InferenceFactory
from src.inference import InferenceFactory
predictor = InferenceFactory.create(model_name: str, **kwargs)
predictor.predict(image) # -> int
Creates a configured Inference instance for the given model name. Accepts the same keyword arguments as run_inference (device, checkpoint_path, etc.).
INFERENCE_REGISTRY
Dictionary mapping model/dataset names to InferenceSpec objects that define the model class, default checkpoint path, image size, and normalization constants.
Key |
Model class |
Image size |
Channels |
|---|---|---|---|
|
|
28×28 |
Grayscale |
|
|
16×16 |
Grayscale |
|
|
32×32 |
RGB |
write_results
from src.inference import write_results
write_results(results: dict[Path, int], output_path: str | Path) -> Path
Write a {image_path: label} dictionary to a .csv or .txt file. The output file is never overwritten — a numbered copy is created instead (predictions_1.csv, …).