Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Pretrained encoder and target-task contract

Last updated: 5 Oct 20265 min read
tutorial
AdvancedBy AITrove Editorial

Transfer learning reuses a representation learned elsewhere, but the target task still needs its own label, input, split and evaluation contract.

Separate representation from decision

A pretrained image encoder turns a parcel photo into a feature vector. A new head predicts whether the parcel needs damage review. The source model’s original label set does not define the target label or intervention. Write the target unit, photograph timing, positive class and review action before extracting features. The image-unit guide covers the difference between a photo, parcel and shipment as an evaluation unit.

Pin preprocessing with the weights

The encoder expects an input shape, channel order, pixel range, normalization and crop rule. Changing any of these can change embeddings even when the weights stay fixed. Store the complete preprocessing contract with the encoder version and head. The code compares an observed input against a declared contract; it is a validation guard, not a substitute for checking image quality. Pixel geometry and serving parity address adjacent failures.

Establish the target split before adaptation

Photos of the same parcel, burst capture or depot incident belong on one side of each split. A frozen encoder does not prevent leakage if near-duplicate target photos appear in both training and evaluation. Use a later depot or period for an honest target test where deployment differs. Grouped evaluation is the relevant boundary.

Check that the source can be used

Track the source checkpoint identifier, permitted use, training-data description where available, known exclusions and security review. A source representation can be technically useful yet unsuitable because of licensing, privacy or input-data restrictions. Do not assert an unknown source dataset has no overlap with target evaluation images. The provenance audit records what can and cannot be verified.

Define a baseline before claiming transfer

Compare a frozen encoder plus small head against an operational rule and a target-only model where feasible. Use the same target test, threshold and review cost for each. A lower training loss says little about the new depot. The linear probe gives a cheap first adaptation; the slice audit checks whether any gain survives target variation.

Implementation

python
encoder_contract = {
    "checkpoint_id": "parcel-vision-v4",
    "height": 224,
    "width": 224,
    "channels": 3,
    "pixel_min": 0.0,
    "pixel_max": 1.0,
    "target_positive": "damage-review",
}
incoming_image = {"height": 224, "width": 224, "channels": 3,
                  "pixel_min": 0.0, "pixel_max": 1.0}

def validate_encoder_input(contract, observed):
    required = ("height", "width", "channels", "pixel_min", "pixel_max")
    mismatches = [field for field in required
                  if contract[field] != observed.get(field)]
    return mismatches

assert validate_encoder_input(encoder_contract, incoming_image) == []
assert validate_encoder_input(
    encoder_contract, {**incoming_image, "channels": 1}
) == ["channels"]

Performance and operating cost

Checking K contract fields is O(K) time and O(K) mismatch storage. Extracting an embedding costs the encoder’s inference compute and its input tensor memory; caching vectors trades storage for repeated inference. The largest hidden cost is collecting an independent, correctly labeled target test cohort.

Common Mistakes

  • Do not reuse the source task’s label meaning as the target label.
  • Do not change normalization or crop rules between training and serving.
  • Do not claim source-data independence when the pretraining manifest is unavailable.

Read next

Continue the workflow: Self-supervised objectives and valid views.

ai-data
machine-learning
Storage details