Establish the shared model inputs

This step prepares one set of model weights, labels, image, tensor shape, and preprocessing settings for every runtime path. This gives the rest of the module the same inputs to work from, so later differences come from the runtime path or precision rather than from a different input tensor.

Prepare the model assets

On the development machine, install the requirements for Module 2:

    

        
        
python -m pip install -r module-2-cnn-inference-with-quantization/requirements.txt

    

Run the following command on the development machine:

    

        
        
module-2-cnn-inference-with-quantization/scripts/aot/prepare_mobilenetv2.py \
  --out module-2-cnn-inference-with-quantization/artifacts/mobilenetv2

    

The script downloads the MobileNetV2 weights through TorchVision (a PyTorch library that provides common computer vision datasets, image transforms, and pretrained vision models), saves the state dictionary, writes the ImageNet labels and model_spec.json, and attempts to download a CC0 Labrador image from Wikimedia Commons for benchmarking.

You should expect to see output similar to this:

    

        
        
Downloading sample image to module-2-cnn-inference-with-quantization/artifacts/mobilenetv2/sample.jpg...
Wrote module-2-cnn-inference-with-quantization/artifacts/mobilenetv2/mobilenet_v2.pth
Wrote module-2-cnn-inference-with-quantization/artifacts/mobilenetv2/imagenet_labels.json
Wrote module-2-cnn-inference-with-quantization/artifacts/mobilenetv2/sample.jpg
Wrote module-2-cnn-inference-with-quantization/artifacts/mobilenetv2/model_spec.json

    

The four files have distinct roles:

  • mobilenet_v2.pth: the local TorchVision MobileNetV2 state dictionary
  • imagenet_labels.json: the ImageNet class names used to interpret the output logits
  • sample.jpg: the sample image used by the benchmark and demo commands
  • model_spec.json: the image size, layout, and preprocessing settings used by export, benchmarking, quantization, and the demo

Understand the saved model data

The preparation script loads the pre-trained model and saves its weights and labels locally:

    

        
        
weights = MobileNet_V2_Weights.IMAGENET1K_V1
model = mobilenet_v2(weights=weights).eval()
torch.save(model.state_dict(), weights_path)

labels = weights.meta["categories"]
labels_path.write_text(json.dumps(labels, indent=2) + "\n")

    

TorchVision supplies both the pre-trained weights and the ImageNet categories.

The script records the image size and preprocessing in model_spec.json:

    

        
        
"input": {
    "shape": [1, 3, 224, 224],
    "dtype": "float32",
    "layout": "nchw",
    "preprocessing": {
        "resize": 256,
        "center_crop": 224,
        "mean": [0.485, 0.456, 0.406],
        "std": [0.229, 0.224, 0.225],
    },
}

    

The shape [1, 3, 224, 224] represents one RGB image in NCHW order: batch, channels, height, and width. The resize, crop, mean, and standard deviation are the ImageNet preprocessing values used by the pre-trained model.

Module 1 used model_spec.json for token inputs such as input_ids and attention_mask. Module 2 uses it for image tensor shape and preprocessing. In both modules, the file keeps the development machine and Raspberry Pi code using the same model inputs.

Check prepared assets

Before moving on, confirm that the preparation step produced:

  • mobilenet_v2.pth
  • imagenet_labels.json
  • sample.jpg
  • model_spec.json

If any file is missing, rerun prepare_mobilenetv2.py before continuing.

Previous page Complete and continue