Sagemaker
model loading
machine learning
AWS
Python

How do you locally load model.tar.gz file from Sagemaker?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

There is no single universal way to “load a SageMaker model.tar.gz” locally, because the archive is just a packaging format. What matters is what the training job put inside it: a PyTorch state dict, a full TensorFlow SavedModel, a Scikit-learn pickle, an XGBoost booster, or something custom. The correct workflow is to extract the archive first, inspect the contents, and then load the model using the framework that originally produced it.

Step 1: Extract the Archive

A model.tar.gz is a gzipped tar archive. Start by unpacking it.

python
1import tarfile
2from pathlib import Path
3
4archive_path = Path("model.tar.gz")
5extract_dir = Path("extracted_model")
6
7extract_dir.mkdir(exist_ok=True)
8
9with tarfile.open(archive_path, "r:gz") as tar:
10    tar.extractall(path=extract_dir)
11
12print(list(extract_dir.rglob("*")))

This gives you the actual model files. Until you inspect that structure, you do not yet know what loader you need.

Common SageMaker Layout Patterns

Typical contents include things like:

  • 'model.pth'
  • 'model.joblib'
  • 'model.pkl'
  • 'saved_model.pb'
  • variables directories
  • tokenizer or preprocessing files
  • inference scripts such as code/inference.py

SageMaker itself does not force one universal model format. It mainly standardizes how artifacts are packed and shipped.

Loading a TensorFlow SavedModel

If the extracted directory contains saved_model.pb, it is probably a TensorFlow SavedModel.

python
1import tensorflow as tf
2from pathlib import Path
3
4model_path = Path("extracted_model")
5model = tf.saved_model.load(str(model_path))
6
7print(model)

If the training script exported a Keras-style saved model, this is the right direction. Sometimes you may instead need:

python
keras_model = tf.keras.models.load_model("extracted_model")

depending on how the artifact was created.

Loading a PyTorch Model

If the archive contains a .pt or .pth file, you probably need PyTorch.

python
1import torch
2
3model_file = "extracted_model/model.pth"
4
5state = torch.load(model_file, map_location="cpu")
6print(type(state))

At this point, the key question is whether the file contains:

  • a raw model object
  • a state_dict
  • a checkpoint dictionary

If it is a state_dict, you also need the original model class definition locally:

python
model = MyModel()
model.load_state_dict(state)
model.eval()

This is why “just load the SageMaker archive” is often not enough by itself.

Loading a Scikit-Learn or Joblib Artifact

If you find .pkl or .joblib files, the artifact may come from Scikit-learn or a related Python workflow.

python
1import joblib
2
3model = joblib.load("extracted_model/model.joblib")
4print(model)

Or:

python
1import pickle
2
3with open("extracted_model/model.pkl", "rb") as f:
4    model = pickle.load(f)
5
6print(model)

This requires a compatible Python environment and matching library versions.

Do Not Ignore code/ or Inference Scripts

Many SageMaker model bundles include supporting code such as:

  • 'inference.py'
  • preprocessing logic
  • custom handlers
  • tokenizer assets

If local inference fails even after loading the core model file, check whether the original deployment depended on helper code in the archive. The “model” may actually be a model plus custom serving logic.

Match the Training Environment

Local loading often fails because the artifact format is correct but the local environment is not compatible. Common issues include:

  • wrong framework version
  • missing custom classes
  • incompatible pickle protocol
  • missing tokenizer or vocabulary files
  • different device assumptions such as GPU-only checkpoints

If possible, match the training or inference container versions rather than guessing from scratch.

Minimal Inspection Workflow

A practical process is:

  1. extract the archive
  2. inspect filenames and directories
  3. identify the underlying framework
  4. reproduce or approximate the original environment
  5. load the artifact with the framework-native loader

This is more reliable than searching for a generic “SageMaker loader,” because SageMaker packaging is not itself the model format.

Security Note

Be careful with pickle-like formats from untrusted sources. Loading Python pickles, joblib artifacts, or arbitrary Torch objects can execute code during deserialization. Only load such artifacts from sources you trust.

That risk is not unique to SageMaker, but SageMaker archives often contain exactly these kinds of framework artifacts.

Common Pitfalls

The biggest mistake is assuming model.tar.gz is a framework-neutral model format. It is just an archive. Another is trying to load the artifact without first inspecting its contents. Developers also often forget that a PyTorch state_dict needs the model class definition, or that a SavedModel and a Keras .h5 file are loaded differently. Finally, version mismatch between the local environment and the original training or serving environment is a frequent cause of confusing load errors.

Summary

  • A SageMaker model.tar.gz must be extracted before it can be understood or loaded.
  • The loading method depends on the framework-specific files inside the archive.
  • Use TensorFlow, PyTorch, Scikit-learn, or another framework loader based on the extracted contents.
  • Check for helper code and environment-specific dependencies, not just the main model file.
  • The archive format is generic packaging; the real model format is determined by what the training job saved.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.