Transfer Learning And Practical Training
Pretrained Models & Model Hubs
Chapter 1-2 assumed a pretrained model was already available. This chapter covers where to actually find one, and how to use it correctly and responsibly.
Jr Codex Deep Learning Notes
Level: Intermediate–Advanced Prerequisites: Chapter 2: Feature Extraction vs Fine-Tuning Time to complete: ~20 minutes
Table of Contents
- Where Pretrained Models Come From
- torchvision — Vision Models
- The HuggingFace Hub
- Reading a Model Card
- Matching Preprocessing to the Pretrained Model
- Licensing and Practical Constraints
- Summary & Next Steps
1. Where Pretrained Models Come From
Chapter 1-2 assumed a pretrained model was already available. This chapter covers where to actually find one, and how to use it correctly and responsibly.
The General Sources
─────────────────────────────────────────
torchvision: PyTorch's own library of pretrained
computer vision models (ResNet, VGG,
EfficientNet, ViT, and more) — Section
2
HuggingFace Hub: the dominant hub for NLP/LLM models
(BERT, GPT-family, and thousands of
community-contributed models) — also
increasingly hosts vision and audio
models — Section 3
Research paper papers introducing a new
repositories: architecture frequently
publish official pretrained
weights directly (e.g. on
GitHub) — less standardized,
but often the ONLY source
for cutting-edge research
models
─────────────────────────────────────────
2. torchvision — Vision Models
import torch
from torchvision import models
# List some commonly available pretrained architectures
resnet = models.resnet50(weights="IMAGENET1K_V2")
efficientnet = models.efficientnet_b0(weights="IMAGENET1K_V1")
vit = models.vit_b_16(weights="IMAGENET1K_V1") # Vision Transformer (Module 6, Ch.5)
# Inspecting what a model was trained on/for
weights = models.ResNet50_Weights.IMAGENET1K_V2
print(weights.meta["categories"][:5]) # the ORIGINAL 1000 ImageNet class names
print(weights.transforms()) # the EXACT preprocessing this model expects (Section 5)3. The HuggingFace Hub
The HuggingFace Hub hosts hundreds of thousands of pretrained models — overwhelmingly for NLP/LLM tasks, but increasingly for vision and audio as well — and is the primary resource used throughout the NLP & LLM Notes.
from transformers import AutoModel, AutoTokenizer
# Downloads (and CACHES locally) a pretrained model and its matching tokenizer
model_name = "bert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)
# The "Auto" classes automatically select the RIGHT model/tokenizer architecture,
# based on the model's published configuration — no need to know the exact
# architecture details in advanceWhy "Auto" Classes Matter
─────────────────────────────────────────
HuggingFace hosts models built on MANY different underlying
architectures (encoder-only, decoder-only, encoder-decoder
— Module 6, Ch.4, Section 7) — the AutoModel/AutoTokenizer
classes inspect the model's PUBLISHED CONFIGURATION and
automatically load the correct architecture-specific code,
sparing the user from needing to track this manually for
every model.
─────────────────────────────────────────
4. Reading a Model Card
Every well-maintained pretrained model on HuggingFace (and increasingly, torchvision) is published with a model card — documentation describing what the model was trained on, its intended use, known limitations, and performance benchmarks.
What to Check Before Using a Pretrained Model
─────────────────────────────────────────
Training data: what data was this trained on? Is it
reasonably similar to your intended
use case (Chapter 2, Section 6's
"task similarity" question)?
Intended use what was this model DESIGNED for?
& limitations: using a model outside its
documented intended use is a
common source of poor,
unreliable results
Evaluation what BENCHMARKS/metrics does the
benchmarks: model card report, and are
they measured the SAME way
you'll need to measure your
own task's performance
(ML Notes, Module 4, Ch.6)?
License: is this model's LICENSE
compatible with your
intended use, especially
for commercial
applications (Section 6)?
─────────────────────────────────────────
5. Matching Preprocessing to the Pretrained Model
A critical, easy-to-miss requirement: input data must be preprocessed exactly the way the original model's training data was preprocessed.
from torchvision import models, transforms
weights = models.ResNet50_Weights.IMAGENET1K_V2
correct_transform = weights.transforms() # the EXACT preprocessing ResNet50 expects — USE THIS DIRECTLY
# Manually reconstructing it (for illustration) shows why this matters:
manual_transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), # ImageNet's EXACT statistics
])Why Mismatched Preprocessing Silently Hurts Performance
─────────────────────────────────────────
A pretrained model's early layers (Chapter 1, Section 3)
learned to expect input in a SPECIFIC numerical range and
distribution — feeding it data normalized with DIFFERENT
statistics (or resized to a DIFFERENT resolution than it was
trained on) doesn't cause an ERROR, but silently produces
WORSE features and degraded downstream performance — a
common, hard-to-diagnose beginner mistake, directly echoing
Module 4, Chapter 3, Section 2's normalization discussion.
─────────────────────────────────────────
6. Licensing and Practical Constraints
Common License Categories (Always VERIFY the Specific Model's License)
─────────────────────────────────────────
Permissive (MIT, Apache 2.0): generally safe for commercial
use, with minimal
restrictions
Research-only/ explicitly PROHIBITS
non-commercial: commercial deployment —
common for some
cutting-edge research
models
Custom/restrictive some large model
licenses: providers impose
specific usage
restrictions (e.g.
usage caps, use-case
restrictions) — read
the ACTUAL license
text, not just the
category label
─────────────────────────────────────────
Always check a model's specific license before using it in any commercial or production context — this is a legal/business consideration a data scientist or ML engineer is often responsible for flagging, not something to assume is automatically fine.
7. Summary & Next Steps
Key Takeaways
- Pretrained models are primarily sourced from torchvision (vision), the HuggingFace Hub (NLP/LLM and increasingly other domains), or official research repositories.
- HuggingFace's "Auto" classes automatically load the correct model and tokenizer architecture based on a model's published configuration.
- A model card documents training data, intended use, benchmarks, and licensing — always review it before adopting a pretrained model for a new task.
- Input preprocessing must exactly match what the pretrained model was originally trained with; mismatches silently degrade performance rather than causing visible errors.
Concept Check
- Why is it important to use a model's exact published preprocessing transform rather than approximating it manually?
- What should you check in a model card before adopting a pretrained model for a new project?
- Why can't you assume a pretrained model's license automatically permits commercial use?
Next Chapter
→ Chapter 4: Practical Fine-Tuning Recipes
Jr Codex — 1-on-1 Personalized Coaching | Back to Module Index | Back to DL Index