architectures
orchard.architectures
¶
Architectures Factory Package.
Implements the Factory Pattern to decouple model instantiation from the main execution logic. Routes requests to specific architecture definitions and ensures models are correctly adapted to the dataset geometry (channels and classes) resolved at runtime.
build_convnext_tiny(num_classes, in_channels, *, pretrained)
¶
Constructs ConvNeXt-Tiny adapted for image classification datasets.
Workflow
- Load pretrained weights from ImageNet (if enabled)
- Modify first conv layer to accept custom input channels
- Apply weight morphing for channel compression (if grayscale)
- Replace classification head with dataset-specific linear layer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_classes
|
int
|
Number of dataset classes for classification head |
required |
in_channels
|
int
|
Input channels (1=Grayscale, 3=RGB) |
required |
pretrained
|
bool
|
Whether to load ImageNet pretrained weights |
required |
Returns:
| Type | Description |
|---|---|
Module
|
Adapted ConvNeXt-Tiny model (device placement handled by factory). |
Source code in orchard/architectures/convnext_tiny.py
build_efficientnet_b0(num_classes, in_channels, *, pretrained)
¶
Constructs EfficientNet-B0 adapted for image classification datasets.
Workflow
- Load pretrained weights from ImageNet (if enabled)
- Modify first conv layer to accept custom input channels
- Apply weight morphing for channel compression (if grayscale)
- Replace classification head with dataset-specific linear layer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_classes
|
int
|
Number of dataset classes for classification head |
required |
in_channels
|
int
|
Input channels (1=Grayscale, 3=RGB) |
required |
pretrained
|
bool
|
Whether to load ImageNet pretrained weights |
required |
Returns:
| Type | Description |
|---|---|
Module
|
Adapted EfficientNet-B0 model (device placement handled by factory). |
Source code in orchard/architectures/efficientnet_b0.py
get_model(device, dataset_cfg, arch_cfg, verbose=True)
¶
Factory function to resolve, instantiate, and prepare architectures.
It maps configuration identifiers to specific builder functions via an internal registry. Structural parameters like input channels and class cardinality are derived from the 'effective' geometry resolved by the DatasetConfig.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device
|
Hardware accelerator target. |
required |
dataset_cfg
|
DatasetConfig
|
Dataset sub-config with resolved metadata. |
required |
arch_cfg
|
ArchitectureConfig
|
Architecture sub-config with model selection. |
required |
verbose
|
bool
|
If True, emit builder-internal INFO logging. |
True
|
Returns:
| Type | Description |
|---|---|
Module
|
nn.Module: The instantiated model synchronized with the target device. |
Example
model = get_model(device, dataset_cfg=cfg.dataset, arch_cfg=cfg.architecture)
Raises:
| Type | Description |
|---|---|
ValueError
|
If the requested architecture is not found in the registry. |
Source code in orchard/architectures/factory.py
build_mini_cnn(num_classes, in_channels, *, dropout)
¶
Constructs MiniCNN for low-resolution image classification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_classes
|
int
|
Number of dataset classes |
required |
in_channels
|
int
|
Input channels (1=Grayscale, 3=RGB) |
required |
dropout
|
float
|
Dropout probability before final FC layer |
required |
Returns:
| Type | Description |
|---|---|
Module
|
MiniCNN model (device placement handled by factory). |
Source code in orchard/architectures/mini_cnn.py
build_resnet_18(num_classes, in_channels, *, pretrained, resolution)
¶
Constructs ResNet-18 with resolution-aware architectural adaptation.
At 28x28/32x32, performs stem surgery to preserve spatial resolution. At 64x64, 128x128, and 224x224, uses the standard ResNet-18 architecture.
Workflow
- Load ImageNet pretrained ResNet-18 (if enabled)
- Apply resolution-specific stem adaptation
- Replace classification head with dataset-specific linear layer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_classes
|
int
|
Number of dataset classes |
required |
in_channels
|
int
|
Input channels (1=Grayscale, 3=RGB) |
required |
pretrained
|
bool
|
Whether to load ImageNet pretrained weights |
required |
resolution
|
int
|
Input image resolution (28, 32, 64, 128, or 224) |
required |
Returns:
| Type | Description |
|---|---|
Module
|
Adapted ResNet-18 (device placement handled by factory). |
Source code in orchard/architectures/resnet_18.py
build_timm_model(num_classes, in_channels, *, arch_cfg)
¶
Construct any timm-registered model with automatic adaptation.
timm.create_model handles:
- Pretrained weight loading (from HuggingFace Hub or torch.hub)
- Classification head replacement (num_classes)
- Input channel adaptation with weight morphing (in_chans)
- Dropout rate injection (drop_rate)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_classes
|
int
|
Number of output classes for the classification head. |
required |
in_channels
|
int
|
Number of input channels (1=grayscale, 3=RGB). |
required |
arch_cfg
|
ArchitectureConfig
|
Architecture sub-config with name, pretrained, dropout. |
required |
Returns:
| Type | Description |
|---|---|
Module
|
Adapted timm model (device placement handled by factory). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the timm model identifier is not found in the registry. |
Source code in orchard/architectures/timm_backbone.py
build_vit_tiny(num_classes, in_channels, *, pretrained, weight_variant=None)
¶
Constructs Vision Transformer Tiny adapted for image classification datasets.
Workflow
- Resolve pretrained weight variant from config (if enabled)
- Load model via timm with automatic head replacement
- Modify patch embedding layer for custom input channels
- Apply weight morphing for channel compression (if grayscale)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_classes
|
int
|
Number of dataset classes for classification head |
required |
in_channels
|
int
|
Input channels (1=Grayscale, 3=RGB) |
required |
pretrained
|
bool
|
Whether to load pretrained weights |
required |
weight_variant
|
str | None
|
Specific timm weight variant identifier |
None
|
Returns:
| Type | Description |
|---|---|
Module
|
Adapted ViT-Tiny model (device placement handled by factory). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If weight variant is invalid or incompatible with pretrained flag |