The Professional
ML Lifecycle & Architectures

A mission-critical reference for structuring, training, and deploying real-world ML systems. Every domain from Computer Vision to Generative AI, each with pipeline diagrams, production folder structures, artifact checkpoints, file extension glossary, and expert engineering tips.

Computer
Vision
NLP &
LLMs
Tabular
ML
Generative
AI
Time
Series
Recomm.
Systems
Graph
Neural Nets
Audio &
Speech
Anomaly
Detection
Multi-
modal AI
Computer Vision NLP / LLMs Tabular ML Generative AI Time Series Recommendation Graph NNs Audio & Speech Anomaly Detection Multi-modal Pro Practices Resources

Computer Vision Architecture

Computer Vision teaches machines to interpret and understand visual information from images and video. It is the backbone of self-driving cars, medical imaging diagnostics, quality control in manufacturing, and real-time security systems. The core challenge is converting raw pixel arrays — tensors of shape (H, W, C) — into structured predictions: bounding boxes, segmentation masks, or class labels.

The lifecycle of a CV project is highly iterative. Labeling is expensive and annotation quality is the single largest determinant of model performance. A poorly labelled dataset with 100k images will consistently underperform a well-labelled dataset with 10k images. This is why Human-in-the-Loop (HITL) labeling pipelines — where model predictions are reviewed and corrected by humans — are now the professional standard.

End-to-End Pipeline

Raw Data
Images / Videos
JPG, PNG, MP4
Unlabeled
ingest
HITL Labeling
CVAT.ai / Roboflow
Bounding Boxes
Polygons / Masks
annotate
Augmentation
Albumentations
Flip, Crop, Rotate
Normalize
transform
Model Training
YOLOv8 / ResNet
PyTorch / TF
Kaggle GPU
train
Artifact
best.pt / .weights
.onnx (export)
Model Card
deploy
FastAPI + OpenCV
REST Inference
/predict endpoint
Docker container

Detailed Workflow

01
Data Collection & Ingestion
Source images from open datasets (COCO, Open Images, ImageNet) or collect domain-specific data. Organise raw images into a /data/raw/ directory. Never modify raw data — all transformations produce new artefacts. Maintain a manifest.csv with image paths, capture dates, and source metadata for traceability. Minimum class balance: no class should have fewer than 10% of the dominant class's samples.
02
Human-in-the-Loop (HITL) Labeling with CVAT.ai or Roboflow
CVAT.ai (self-hosted, free) supports bounding boxes, polygons, semantic segmentation, and keypoint annotation. Use its AI-assisted labeling: upload a SAM (Segment Anything Model) checkpoint to auto-generate masks that humans then verify and correct — reducing annotation time by 60–80%. Roboflow (freemium, hosted) handles labeling, augmentation, versioning, and export to YOLO/COCO format in one workflow. For HITL: train an initial model on 10% of data, run inference on the remaining 90%, then have annotators only correct model predictions rather than label from scratch. This is called Active Learning.
03
Data Augmentation with Albumentations
Augmentation artificially expands dataset diversity and prevents overfitting to lighting conditions, viewpoints, and scales. Always apply augmentation only to the training split — never to validation or test. Apply augmentations in the dataset's __getitem__ method so they're generated on-the-fly (saves disk space). Critical augmentations for CV: horizontal/vertical flip, random crop, brightness/contrast jitter, Gaussian blur, and for detection: mosaic and mixup (used in YOLOv8 by default).
04
Model Training: YOLO vs. PyTorch Transfer Learning
YOLOv8 (Ultralytics) is the production standard for object detection: single-stage, real-time, and trivial to fine-tune. Run yolo train data=config.yaml model=yolov8n.pt epochs=100 imgsz=640 to fine-tune on a custom dataset. PyTorch + torchvision gives more architectural control: use resnet50 pretrained on ImageNet, freeze backbone layers, and replace the final fully-connected layer with your class count. Monitor with W&B or TensorBoard. Use Kaggle's free T4 GPU (30hr/week) for training.
05
Evaluation: mAP, IoU, Precision-Recall
mAP@0.5 (mean Average Precision at IoU threshold 0.5) is the standard detection metric. IoU (Intersection over Union) measures bounding box quality: $\text{IoU} = \frac{|\text{predicted} \cap \text{ground truth}|}{|\text{predicted} \cup \text{ground truth}|}$. A threshold of 0.5 means a detection is only counted as correct if it overlaps the ground truth by at least 50%. For segmentation, use mIoU (mean IoU across all classes).
06
Export & Inference with OpenCV + FastAPI
Export to ONNX for cross-framework deployment: model.export(format='onnx'). ONNX models run with onnxruntime — no PyTorch dependency at inference time. Use OpenCV for pre/post-processing: reading frames, resizing, drawing detection outputs. Wrap in FastAPI: accept multipart image uploads, preprocess, run inference, return JSON with bounding boxes and confidence scores. Deploy the FastAPI app in a Docker container.

Production Folder Structure

This structure follows the YOLO convention, compatible with both Ultralytics and standard PyTorch training loops. The separation of /images and /labels mirrors how YOLO tools expect data on disk.

Bash — Project Tree
cv-project/
├── .env                         # API keys, model endpoints — NEVER commit
├── .gitignore                   # Ignore /data, /weights, __pycache__
├── requirements.txt             # Pinned: torch==2.2.0, ultralytics==8.2.0
├── config.yaml                  # YOLO dataset config (path, nc, names)
│
├── data/
│   ├── raw/                     # Original images — READ ONLY, never modify
│   │   ├── images/
│   │   └── manifest.csv         # filename, source, capture_date, split
│   ├── processed/               # After augmentation/resize
│   │   ├── train/
│   │   │   ├── images/          # .jpg / .png — 640x640 normalized
│   │   │   └── labels/          # .txt — YOLO format: class cx cy w h
│   │   ├── val/
│   │   │   ├── images/
│   │   │   └── labels/
│   │   └── test/
│   │       ├── images/
│   │       └── labels/
│   └── README.md                # Dataset card: source, license, version
│
├── src/
│   ├── dataset.py               # PyTorch Dataset class, augmentation pipeline
│   ├── train.py                 # Training loop, optimizer, scheduler
│   ├── evaluate.py              # mAP, IoU, confusion matrix
│   ├── inference.py             # Load model, preprocess, postprocess
│   └── utils.py                 # draw_boxes(), iou(), xywh2xyxy()
│
├── api/
│   ├── app.py                   # FastAPI app — /predict, /health endpoints
│   ├── schemas.py               # Pydantic request/response models
│   └── Dockerfile               # Production container
│
├── weights/                     # Trained model artefacts — in .gitignore
│   ├── best.pt                  # Best checkpoint (lowest val loss)
│   ├── last.pt                  # Final epoch checkpoint
│   └── best.onnx                # Exported for deployment
│
├── inference/
│   ├── test_images/             # Sample images for smoke testing
│   └── results/                 # Output images with drawn detections
│
└── notebooks/
    ├── 01_eda.ipynb             # Dataset exploration, class distribution
    ├── 02_training.ipynb        # Training experiments
    └── 03_evaluation.ipynb      # mAP curves, error analysis
💡
Why separate /images and /labels? YOLO tools auto-discover label files by replacing /images/ in the path with /labels/. If your structure deviates, the dataloader silently skips annotations. Additionally, separating raw from processed data ensures you always have an uncorrupted source to re-augment from if you change your preprocessing strategy.

Artifacts, Checkpoints & File Formats

.ptPyTorch state_dict — Contains only the model's weights tensor dictionary. Requires the model architecture to be defined separately before loading. Use torch.save(model.state_dict(), 'best.pt') and load with model.load_state_dict(torch.load('best.pt')). This is the preferred format for PyTorch projects — portable and architecture-agnostic.
.pthPyTorch full checkpoint — Saves the entire object: model, optimizer state, epoch number, loss history. Use for resuming interrupted training: torch.save({'epoch': epoch, 'model': model.state_dict(), 'optimizer': opt.state_dict(), 'loss': loss}, 'checkpoint.pth'). Larger file but contains everything needed to continue training exactly where it stopped.
.weightsDarknet/YOLO legacy format — Used by YOLOv3/v4. Binary format, not a standard serialization. YOLOv5+ uses .pt instead. If you receive a .weights file, convert it: python convert_weights.py --cfg yolov4.cfg --weights yolov4.weights --output yolov4.pt.
.onnxOpen Neural Network Exchange — Cross-framework, production-ready format. Export from PyTorch with torch.onnx.export(model, dummy_input, 'model.onnx'). Run on any hardware (CPU/GPU/NPU) using onnxruntime. Zero PyTorch dependency at inference — critical for lightweight deployment. Validate with onnx.checker.check_model(model).
.tfliteTensorFlow Lite — Edge/Mobile — 4-8x smaller than the original model through quantisation. Deploy on Android (via TFLite Interpreter), iOS (Core ML), Raspberry Pi, or Coral TPU. Convert: converter = tf.lite.TFLiteConverter.from_saved_model(path); converter.optimizations = [tf.lite.Optimize.DEFAULT]. INT8 quantisation reduces model size by 75% with <1% accuracy drop.
.engineTensorRT Engine — GPU Optimized — NVIDIA's inference optimizer. Fuses layers, optimizes memory layout for the specific GPU it's compiled on. Not portable between GPU models. Gives 2–10x speed improvement over vanilla ONNX on NVIDIA hardware. Used in production servers and Jetson devices. Build with trtexec --onnx=model.onnx --saveEngine=model.engine --fp16.

Professional Engineering Tips

🔐 .env for API Keys

Store Roboflow API keys, W&B tokens, and cloud credentials in .env. Load with from dotenv import load_dotenv; load_dotenv(). Your .gitignore must include .env — one accidental commit exposes your credentials to the entire internet permanently (git history is forever).

📦 .gitignore for Datasets

Add data/raw/, data/processed/, weights/, *.pt, *.onnx to .gitignore. Use DVC (Data Version Control) instead: dvc add data/processed/ pushes data to S3/GDrive while tracking metadata in git. Never store multi-GB datasets in GitHub.

🧪 Reproducibility

Always seed everything: torch.manual_seed(42); np.random.seed(42); random.seed(42). For full determinism on GPU: torch.backends.cudnn.deterministic = True. Log the exact commit hash, dataset version, and hyperparameters to W&B so any experiment can be perfectly reproduced 6 months later.

🐳 Docker for Inference

Use FROM python:3.11-slim as base, not FROM pytorch/pytorch (adds 3GB). Install only onnxruntime-gpu and opencv-python-headless for a lean inference container. Copy weights in the Dockerfile: COPY weights/best.onnx /app/weights/. The resulting image should be under 1GB.

Domain Resources

Ultralytics YOLOv8State-of-the-art object detection, segmentation, pose estimation. Trivial fine-tuning API.Free
OpenCVImage/video I/O, preprocessing, drawing detections, geometric transforms.Free
CVAT.aiSelf-hostable annotation platform. SAM-assisted labeling, team workflows, COCO/YOLO export.Freemium
RoboflowHosted labeling + augmentation + dataset versioning + one-click YOLO export.Freemium
Kaggle NotebooksFree T4/P100 GPU (30hr/week). Pre-installed PyTorch, CUDA, Albumentations.Free
AlbumentationsFast image augmentation library. Handles bounding box / mask coordinate transforms automatically.Free

NLP / LLM Architecture

Natural Language Processing has been completely transformed by the Transformer architecture. The paradigm shift from task-specific models to fine-tuning pre-trained Large Language Models means that most production NLP today works like this: take a model that has read the entire internet (GPT, BERT, LLaMA, Mistral), then fine-tune it on your domain-specific data using a fraction of the compute. This is called Transfer Learning for NLP.

The architectural journey of text: Raw TextTokenization (split into subword tokens via BPE/WordPiece) → Embeddings (each token becomes a high-dimensional vector, e.g., 768-dim for BERT-base) → Transformer Layers (attention + feed-forward) → Inference (classification head, generation head, or embedding extraction). Understanding this pipeline lets you intervene at exactly the right stage.

End-to-End Pipeline

Raw Text
PDFs, HTML, CSVs
Scraped text
Unlabeled corpus
label
Label Studio
NER spans
Sentiment labels
RLHF preference
tokenize
Tokenizer
BPE / WordPiece
input_ids
attention_mask
fine-tune
Fine-tuning
LoRA / QLoRA
Hugging Face
Trainer API
save
Artifact
.safetensors
tokenizer.json
config.json
serve
HF Inference / vLLM
REST API
Streaming tokens
LangChain chain

Detailed Workflow

01
Text Collection & Cleaning
Use trafilatura for web scraping, pdfminer.six for PDFs, beautifulsoup4 for HTML. Cleaning pipeline: remove HTML tags, normalize Unicode (unicodedata.normalize('NFKC', text)), strip boilerplate, deduplicate with MinHash LSH (use datasketch). Near-duplicates destroy evaluation metrics — a model that memorises training samples scores perfectly on test if the test set has duplicates.
02
Data Labeling with Label Studio (NER, Sentiment, Classification)
Label Studio (open source, self-hosted) provides a configurable annotation interface for NER (Named Entity Recognition), text classification, relation extraction, and RLHF preference pairs. Install: pip install label-studio && label-studio start. Configure a NER task with a simple XML template: highlight spans and assign entity types (PERSON, ORG, PRODUCT). Export as CONLL-2003 format for token-classification models. Inter-annotator agreement (Cohen's Kappa) should exceed 0.8 before training.
03
Tokenization: The Bridge Between Text and Tensors
The tokenizer converts raw strings to integer IDs that the model understands. Always use the tokenizer that was used to train the base model — never substitute another. BERT uses WordPiece (30k vocabulary), GPT uses BPE (50k), LLaMA uses SentencePiece (32k). Key outputs: input_ids (token integers), attention_mask (1 for real tokens, 0 for padding), token_type_ids (segment IDs for BERT). Save the tokenizer alongside your model: tokenizer.save_pretrained('./checkpoints/v1/').
04
Fine-tuning with LoRA / QLoRA (Parameter-Efficient Fine-Tuning)
Full fine-tuning of a 7B parameter model requires 56GB of GPU VRAM — impractical for most. LoRA (Low-Rank Adaptation) freezes the original weights and injects trainable rank-decomposition matrices into attention layers. This trains only 0.1–1% of parameters while achieving comparable performance. QLoRA additionally quantizes the base model to 4-bit (using bitsandbytes), enabling fine-tuning of LLaMA-7B on a single 16GB GPU. Use the peft library: model = get_peft_model(model, lora_config).
05
RAG Architecture (Retrieval-Augmented Generation)
Instead of fine-tuning, RAG retrieves relevant documents at inference time and injects them into the prompt context. Architecture: (1) Chunk documents → (2) Embed chunks with text-embedding-3-small or all-MiniLM-L6-v2(3) Store in a vector database (Chroma, Pinecone, Weaviate) → (4) At query time: embed query, retrieve top-k chunks by cosine similarity, inject into system prompt → (5) LLM generates grounded response. LangChain and LlamaIndex provide orchestration.
06
Experiment Tracking with Weights & Biases
Log every training run: wandb.init(project="my-llm", config=hyperparams). Log metrics: wandb.log({"train/loss": loss, "eval/perplexity": ppl}). W&B automatically tracks your system metrics (GPU utilisation, memory), code version (git commit hash), and environment. Compare runs visually to identify which hyperparameter changes actually improved the model. Create Model Registry entries for promoted checkpoints.

Fine-Tuning Folder Structure

Bash — NLP/LLM Project Tree
nlp-project/
├── .env                          # HF_TOKEN, WANDB_API_KEY, OPENAI_API_KEY
├── .gitignore                    # /datasets, /checkpoints, *.bin, *.safetensors
├── requirements.txt              # transformers, peft, bitsandbytes, datasets, wandb
│
├── configs/
│   ├── lora_config.yaml          # r=16, lora_alpha=32, target_modules=[q_proj,v_proj]
│   ├── training_args.yaml        # lr=2e-4, epochs=3, batch_size=4, fp16=True
│   └── data_config.yaml          # max_seq_length=2048, split ratios
│
├── datasets/                     # In .gitignore — track with DVC
│   ├── raw/                      # Original text files, CSVs, JSONL
│   ├── labeled/                  # Label Studio exports (NER, classification)
│   │   ├── train.jsonl           # {"text": "...", "label": "POSITIVE"}
│   │   ├── val.jsonl
│   │   └── test.jsonl
│   └── processed/                # After tokenization + formatting
│       └── tokenized_train/      # HuggingFace datasets on-disk cache
│
├── src/
│   ├── data/
│   │   ├── loader.py             # load_dataset(), preprocessing_fn()
│   │   └── collator.py           # DataCollatorForSeq2Seq, padding logic
│   ├── models/
│   │   ├── base_model.py         # Load pretrained + apply LoRA config
│   │   └── rag_pipeline.py       # Retriever + LLM chain
│   └── training/
│       ├── trainer.py            # HuggingFace Trainer wrapper
│       └── evaluate.py           # ROUGE, BLEU, perplexity, exact match
│
├── tokenizer/                    # Saved tokenizer files
│   ├── tokenizer.json            # Vocabulary + merges rules
│   ├── tokenizer_config.json     # Special tokens, model_max_length
│   └── special_tokens_map.json   # [PAD], [CLS], [SEP], [MASK]
│
├── checkpoints/                  # In .gitignore — track with DVC/HF Hub
│   ├── checkpoint-500/           # Mid-training checkpoint (auto-saved)
│   ├── checkpoint-1000/
│   └── final/                    # Best model checkpoint
│       ├── adapter_model.safetensors  # LoRA weights only (~50MB)
│       ├── adapter_config.json        # LoRA configuration
│       └── trainer_state.json         # Training history
│
├── api/
│   ├── app.py                    # FastAPI: /chat, /embed, /classify endpoints
│   ├── inference.py              # Load merged model, tokenizer, generate()
│   └── Dockerfile
│
└── notebooks/
    ├── 01_data_exploration.ipynb
    ├── 02_fine_tuning.ipynb
    └── 03_evaluation.ipynb

Artifacts, Checkpoints & File Formats

.safetensorsHuggingFace Safetensors (Recommended) — The modern replacement for .bin. Safe to load from untrusted sources (unlike pickle-based formats). Supports lazy loading (read only the tensors you need), memory-mapped for zero-copy loading, and verified with a checksum. Always prefer this format: model.save_pretrained('./output', safe_serialization=True).
.binHuggingFace legacy PyTorch binary — Older format used by most models on HuggingFace Hub before 2023. Uses torch.save() under the hood (pickle-based). Large models are split into shards: pytorch_model-00001-of-00003.bin. Functional but prefer safetensors for new projects. Security risk if loading from untrusted sources.
.ggufGGML Unified Format — CPU Quantized Inference — Used by llama.cpp and Ollama for running LLMs on CPU without CUDA. Stores the model in quantized format: Q4_K_M (4-bit mixed quantization) reduces a 7B model from ~14GB to ~4GB with minimal quality loss. Essential for local deployment on laptops. Convert from HuggingFace format: python convert_hf_to_gguf.py --model llama3.
tokenizer.jsonTokenizer vocabulary and merge rules — Contains the full BPE merge rules, vocabulary mapping (token → ID), and special token definitions. Never ship a model without its tokenizer. A model loaded with the wrong tokenizer produces nonsensical outputs. The tokenizer must match the exact one used during pretraining.
adapter_model.safetensorsLoRA adapter weights — Contains only the low-rank matrices injected by LoRA, not the full base model. Typically 10–100MB vs. 14GB for the base. Load separately: model = PeftModel.from_pretrained(base_model, './adapter/'). This means you can fine-tune 10 different tasks and share the same base model weights — storing only the small adapters.

Professional Engineering Tips

🔑 HF_TOKEN in .env

Most gated models (LLaMA-3, Mistral) require a HuggingFace token. Store it as HF_TOKEN=hf_xxx in .env and load with login(token=os.getenv('HF_TOKEN')). Never hardcode tokens in notebooks — Jupyter notebooks save outputs to git history.

📊 Track Perplexity, Not Just Loss

Loss decreasing doesn't mean the model is improving — it might be overfitting. Track perplexity ($e^{\text{loss}}$) on a held-out evaluation set. For chat models, also run MT-Bench or MMLU to measure general capability degradation from fine-tuning.

🗂️ Dataset Versioning with DVC

Text datasets change (new annotations, cleaned versions). Track with DVC: dvc add datasets/labeled/train.jsonl pushes data to S3 and stores a .dvc pointer in git. You can check out the exact dataset version from any git commit.

⚡ Flash Attention for Speed

Enable Flash Attention 2 for 2-4x training speedup: model = AutoModelForCausalLM.from_pretrained(name, attn_implementation="flash_attention_2"). Requires Ampere or newer GPU (A100, RTX 3090+). Reduces memory from $O(n^2)$ to $O(n)$ with respect to sequence length.

Domain Resources

Hugging Face HubModel hub (500k+ models), datasets, Spaces. The npm registry of NLP.Freemium
LangChainLLM orchestration: chains, agents, RAG, memory. Production-ready abstractions.Free
Weights & BiasesExperiment tracking, hyperparameter sweeps, model registry, artifact versioning.Freemium
Label StudioOpen-source annotation platform. NER, classification, sequence-to-sequence, preference.Free
vLLMHigh-throughput LLM serving with PagedAttention. 24x faster than naive HuggingFace inference.Free

Tabular / Classic ML Architecture

Despite the hype around deep learning, the majority of production ML systems in finance, healthcare, and e-commerce run on tabular data with tree-based models. Gradient boosted decision trees (XGBoost, LightGBM, CatBoost) consistently outperform neural networks on tabular tasks. The real complexity isn't the model — it's the feature engineering pipeline.

A production tabular ML system is a scikit-learn Pipeline with carefully ordered transformations: imputation → encoding → scaling → feature selection → model. Wrapping this in a Pipeline object is non-negotiable — it ensures preprocessing is always fitted on training data only and applied consistently at inference time, preventing the #1 source of production ML bugs: data leakage.

End-to-End Pipeline

Raw Tabular Data
CSV / Parquet / SQL
Mixed types
Missing values
profile
EDA & Profiling
ydata-profiling
Distributions
Correlation heatmap
engineer
Feature Engineering
Impute / Encode
Scale / Select
sklearn Pipeline
train
XGBoost / LGBM
Optuna tuning
Cross-validation
Feature importance
save
Pipeline Artifact
.joblib / .pkl
Full pipeline
+ metadata.json
serve
FastAPI REST
/predict endpoint
Input validation
Docker + CI/CD

Detailed Workflow

01
Automated EDA with ydata-profiling
Before any code, run from ydata_profiling import ProfileReport; profile = ProfileReport(df, title="EDA"); profile.to_file("eda_report.html"). This generates a comprehensive report: distributions, missing value patterns, correlations, duplicate detection, and interaction plots — in one command. Read the report before writing a single preprocessing line. It tells you which features are constants (drop them), which are highly correlated (consider dropping one), and which have MNAR (Missing Not At Random) patterns.
02
Building a Production scikit-learn Pipeline
The Pipeline is the most important engineering decision in tabular ML. It chains all transformations into a single object that can be fit, saved, and loaded atomically. Use ColumnTransformer for heterogeneous features: apply StandardScaler to numeric columns, OneHotEncoder to categorical columns, and SimpleImputer to handle nulls — all within a single Pipeline(steps=[...]). The final step is always the model. This prevents any possible data leakage because the entire transformation chain is fit only on training data.
03
Feature Engineering — The Real Value Add
Domain expertise drives feature engineering. Common patterns: datetime decomposition (extract year, month, day-of-week, is_weekend, quarter, days_since_event), ratio features (revenue / visits = conversion_rate), log transforms on skewed distributions (np.log1p(x)), interaction features (feature1 × feature2), target encoding with K-fold smoothing for high-cardinality categoricals. Use FeatureEngine or custom transformers that inherit from BaseEstimator, TransformerMixin to make them Pipeline-compatible.
04
Model Selection: Why Gradient Boosting Wins on Tabular Data
Gradient boosted trees handle mixed data types natively, are robust to outliers, capture non-linear interactions automatically, and provide interpretable feature importances. LightGBM trains 10x faster than XGBoost on large datasets (leaf-wise splitting vs. depth-wise). CatBoost handles categorical features natively without encoding. XGBoost has the most mature regularisation and is the competition standard. Always benchmark all three with identical hyperparameter budgets before committing to one.
05
SHAP for Model Explainability
Use SHAP (SHapley Additive exPlanations) to explain individual predictions: explainer = shap.TreeExplainer(model); shap_values = explainer.shap_values(X_test). SHAP summary plots show global feature importance. SHAP force plots show why the model made a specific prediction. This is often a regulatory requirement in finance (GDPR Article 22: right to explanation) and healthcare. Tree-based SHAP is exact (not approximate) and runs in O(n log n).

Production Pipeline Folder Structure

Bash — Tabular ML Project Tree
tabular-ml-project/
├── .env                          # DB connection strings, API keys
├── .gitignore                    # /data/raw, /data/processed, *.pkl, *.joblib
├── pyproject.toml                # PEP 517 build, locked dependencies
│
├── data/
│   ├── raw/                      # Original data — READ ONLY
│   │   ├── train.csv
│   │   ├── test.csv
│   │   └── data_dictionary.md    # Schema: column name, type, description, nulls
│   ├── processed/                # Cleaned + engineered features
│   │   ├── X_train.parquet       # Parquet > CSV: 5-10x smaller, preserves dtypes
│   │   ├── X_val.parquet
│   │   ├── y_train.parquet
│   │   └── y_val.parquet
│   └── external/                 # Lookup tables, reference data
│
├── features/
│   ├── transformers.py           # Custom sklearn transformers (inherit BaseEstimator)
│   ├── datetime_features.py      # DatetimeFeatureExtractor
│   ├── target_encoder.py         # KFoldTargetEncoder
│   └── column_config.py         # NUMERIC_COLS, CAT_COLS, TARGET, ID_COLS lists
│
├── preprocessing/
│   ├── pipeline.py               # build_pipeline() → sklearn Pipeline object
│   ├── validation.py             # Great Expectations: schema, distribution checks
│   └── split.py                  # train_test_split with stratification logic
│
├── models/
│   ├── train_xgb.py              # XGBoost + Optuna hyperparameter search
│   ├── train_lgbm.py             # LightGBM equivalent
│   ├── train_catboost.py         # CatBoost equivalent
│   ├── stacking.py               # Stacking ensemble: StackingClassifier
│   └── baseline.py               # DummyClassifier, LogisticRegression baselines
│
├── evaluation/
│   ├── metrics.py                # AUC, F1, calibration, lift curves
│   ├── shap_analysis.py          # SHAP summary, force, dependency plots
│   └── fairness.py               # Demographic parity, equal opportunity checks
│
├── artifacts/                    # In .gitignore — track with MLflow/DVC
│   ├── pipeline_v1.joblib        # Complete sklearn Pipeline (preprocessor + model)
│   ├── metadata_v1.json          # {"version":"1.0","train_auc":0.94,"features":42}
│   └── shap_explainer_v1.pkl     # Pre-computed SHAP explainer for fast inference
│
└── api/
    ├── app.py                    # FastAPI: /predict, /explain endpoints
    ├── schemas.py                # Pydantic input validation (catches wrong dtypes)
    └── Dockerfile

Artifacts & File Formats

.joblibjoblib — Preferred for sklearn models — Uses memory-mapped NumPy arrays under the hood, making it 3-5x faster than pickle for objects containing large arrays (like trained Random Forests or XGBoost models). joblib.dump(pipeline, 'pipeline.joblib', compress=3). compress=3 gives ~50% size reduction with minimal speed penalty. Always save the full Pipeline, never just the model.
.pklPython pickle — Python's native serialization protocol. Works for any Python object. Use pickle.HIGHEST_PROTOCOL for smallest file size. Limitation: pickle files are Python-version and sometimes library-version sensitive. A .pkl saved with scikit-learn 1.3 may not load in scikit-learn 1.1. Always record the exact library version in metadata.json.
.parquetApache Parquet — Columnar data format — The production standard for storing processed features. 5-10x smaller than CSV, preserves dtypes exactly (a float32 stays float32, not rounded to 6 decimal places), supports columnar reads (read only the columns you need), and works natively with Pandas, Spark, and DuckDB. Use df.to_parquet('features.parquet', index=False).
metadata.jsonModel card and version manifest — Always ship alongside every model artefact. Contains: version, training date, feature list with types, training metrics (AUC, F1, calibration), data version hash, scikit-learn version, Python version. Load this at API startup to validate the model version: if the version doesn't match what the API expects, raise an error immediately rather than serving wrong predictions silently.

Domain Resources

UCI ML Repository650+ real-world datasets: medical, financial, IoT, text. Canonical ML benchmark datasets.Free
ydata-profilingOne-line EDA reports: distributions, correlations, missing values, interactions.Free
SHAPModel-agnostic explainability. Exact TreeExplainer for gradient boosting, KernelExplainer for any model.Free
OptunaBayesian hyperparameter optimisation. Tree-structured Parzen Estimator, built-in pruning, W&B integration.Free
Great ExpectationsData quality validation: schema checks, distribution drift, null thresholds. CI/CD for data pipelines.Freemium

Generative AI Architecture (Diffusion & GANs)

Generative AI learns the underlying data distribution and samples from it to create new content — images, audio, video, code, molecules. Two paradigms dominate: Diffusion Models (Stable Diffusion, DALL-E, Flux) which iteratively denoise random noise into structured output, and Generative Adversarial Networks (GANs) where a generator and discriminator play an adversarial game.

The key conceptual insight for diffusion models is the latent space: the VAE (Variational Autoencoder) compresses a 512×512×3 image (786k values) into a 64×64×4 latent tensor (16k values) — a 49x compression. All the denoising happens in this compressed space, making it computationally feasible. The U-Net (or DiT Transformer) learns to predict the noise added at each timestep. At inference, you start from pure Gaussian noise and iteratively remove predicted noise, guided by CLIP text embeddings.

Diffusion Model Architecture Flow

Training Images
Captioned dataset
LAION / custom
PNG / WebP
encode
VAE Encoder
Image → Latent
z ∈ ℝ^{64×64×4}
Compress 49x
denoise
U-Net / DiT
Noise prediction
1000 timesteps
CLIP guidance
decode
VAE Decoder
Latent → Image
512×512×3
Generated output
save
Artifact
.safetensors
LoRA adapter
model_index.json
host
Replicate / ComfyUI
API endpoint
Serverless GPU
Webhook results

Detailed Workflow

01
The Forward Diffusion Process (Training)
Forward diffusion gradually adds Gaussian noise to a training image over $T = 1000$ timesteps: $q(x_t | x_{t-1}) = \mathcal{N}(x_t; \sqrt{1-\beta_t}x_{t-1}, \beta_t I)$ where $\beta_t$ is a noise schedule (linear, cosine, or exponential). At $t = T$, the image is pure Gaussian noise. The U-Net learns to predict the noise $\epsilon$ that was added at each timestep, minimizing: $\mathcal{L} = ||\epsilon - \epsilon_\theta(x_t, t, c)||^2$ where $c$ is the CLIP text conditioning.
02
DreamBooth & LoRA Fine-tuning for Custom Subjects
To fine-tune Stable Diffusion on your own subject (a specific person's face, a product, a unique art style): DreamBooth fine-tunes the entire U-Net on 3-20 images. LoRA for diffusion injects low-rank matrices into the cross-attention layers — faster, smaller (10-100MB adapter vs. 4GB full model), and more portable. Use kohya-ss/sd-scripts for LoRA training. Dataset requirement: 10-30 high-quality images with descriptive captions. Use a unique trigger word: "photo of [V] person".
03
GANs: Generator vs. Discriminator
The GAN framework: Generator $G$ maps random noise $z \sim \mathcal{N}(0,I)$ to realistic images. Discriminator $D$ learns to distinguish real from generated. They're trained adversarially: $\min_G \max_D \mathbb{E}_{x \sim p_{data}}[\log D(x)] + \mathbb{E}_{z \sim p_z}[\log(1 - D(G(z)))]$. Modern architectures: StyleGAN3 for high-res face generation, DCGAN for simple datasets, CycleGAN for unpaired image-to-image translation (photos → paintings), Pix2Pix for paired translation (sketches → photos).
04
Deployment: Replicate, Modal, and ComfyUI
Replicate lets you push a Docker container with your model and run it on serverless GPUs. Define a cog.yaml and predict.py; cog push r8.im/username/model deploys it globally. Billed per second of GPU time. Modal provides a Python-native serverless GPU platform — write a function, decorate with @app.function(gpu="A10G"), and it scales to zero. ComfyUI provides a node-based workflow editor for chaining diffusion operations visually, with API mode for programmatic access.

Generative AI Folder Structure

Bash — GenAI / Diffusion Project Tree
genai-project/
├── .env                          # HF_TOKEN, REPLICATE_API_TOKEN
├── .gitignore                    # /models, /datasets, *.safetensors, *.ckpt
│
├── datasets/                     # DVC-tracked, not in git
│   ├── raw_images/               # Original training images
│   ├── captions/                 # BLIP-2 auto-captions or manual .txt files
│   └── preprocessed/            # Resized to 512x512 / 1024x1024 + buckets
│
├── training/
│   ├── lora_config.yaml          # network_dim=32, alpha=16, scheduler=cosine
│   ├── train_dreambooth.py       # DreamBooth fine-tuning script
│   ├── train_lora.py             # LoRA fine-tuning (kohya-style)
│   └── caption_generator.py     # BLIP-2 auto-captioning pipeline
│
├── models/                       # In .gitignore
│   ├── base/                     # Base model (e.g., SD1.5, SDXL, Flux.1)
│   │   └── model_index.json      # Diffusers model manifest
│   ├── lora/                     # Trained LoRA adapters
│   │   ├── subject_lora.safetensors
│   │   └── style_lora.safetensors
│   └── vae/                      # Optional improved VAE
│
├── inference/
│   ├── generate.py               # StableDiffusionPipeline, prompt engineering
│   ├── img2img.py                # Image-to-image with denoising strength
│   └── controlnet.py            # ControlNet: pose/depth/edge conditioning
│
├── evaluation/
│   ├── fid_score.py              # Fréchet Inception Distance (lower = better)
│   ├── clip_score.py             # Text-image alignment (higher = better)
│   └── sample_grid.py           # Generate evaluation sample grids
│
├── api/
│   ├── predict.py                # Cog prediction class (for Replicate)
│   ├── cog.yaml                  # Replicate container config
│   └── modal_app.py              # Modal serverless deployment
│
└── notebooks/
    ├── 01_data_prep.ipynb
    ├── 02_training_lora.ipynb
    └── 03_inference_experiments.ipynb

Artifacts & File Formats

.safetensorsFull model weights (Diffusers format) — Stable Diffusion models saved with Diffusers library. Structure: model_index.json (manifest), unet/, vae/, text_encoder/ subdirectories each containing diffusion_pytorch_model.safetensors and config.json. Load with StableDiffusionPipeline.from_pretrained('./model_dir/').
.ckptStable Diffusion legacy checkpoint — Single-file format from CompVis/Stability AI. Contains the full U-Net, VAE, and text encoder in one file (2-8GB). Security risk: ckpt files use pickle and can execute arbitrary code. Always prefer .safetensors. Convert: python convert_original_stable_diffusion_to_diffusers.py --checkpoint_path model.ckpt --dump_path ./output_dir/.
model_index.jsonDiffusers pipeline manifest — Defines the pipeline architecture: which components exist (unet, vae, text_encoder, scheduler), their class names, and configuration keys. Required for from_pretrained() to correctly reconstruct the full pipeline. Always include when distributing a model.

Domain Resources

HuggingFace DiffusersPython library for SOTA diffusion models (SD, SDXL, Flux, ControlNet). Fine-tuning and inference.Free
CivitaiCommunity repository for Stable Diffusion models, LoRAs, embeddings. Browse and download fine-tuned models.Free
ReplicateServerless model hosting with per-second billing. Push Docker+model, get REST API instantly.Freemium
ModalPython-native serverless GPU compute. @app.function(gpu="A10G") scales to zero.Freemium
ComfyUINode-based diffusion workflow UI. API mode for programmatic generation. Local or server deployment.Free

Time Series & Forecasting Architecture

Time series data has a critical property that breaks standard ML assumptions: temporal autocorrelation. Observations are not independent — yesterday's sales predict today's sales. This invalidates random train/test splits (leaks future data into training) and requires specialized models that explicitly model temporal dependencies. Applications span demand forecasting, stock prediction, anomaly detection in telemetry, weather prediction, and energy consumption modeling.

Raw Time Series
Irregular timestamps
Missing values
Multiple seasonality
Preprocessing
Resample / Impute
Decompose (STL)
Stationarity test
Feature Engineering
Lag features
Rolling statistics
Calendar features
Model Training
Prophet / N-BEATS
LSTM / TFT
LightGBM + lags
Artifact
.pkl / .h5 / .json
Scaler state
Forecast config
Scheduled API
Airflow / Cron
FastAPI /forecast
Monitoring drift

Key Concepts & Workflow

01
Temporal Train/Test Split — Never Use random_state
Time series splits must respect temporal order. Use the last N periods as the test set: train = df[df['date'] < '2024-01-01']; test = df[df['date'] >= '2024-01-01']. For cross-validation, use TimeSeriesSplit(n_splits=5) — each fold's test set is chronologically after its training set. Never shuffle time series data. For multi-step ahead validation, use a walk-forward validation scheme.
02
Feature Engineering: Lags, Rolling Stats, Calendar Features
Lag features: df['sales_lag_1'] = df['sales'].shift(1) — yesterday's sales. Rolling statistics: df['sales_roll_7'] = df['sales'].rolling(7).mean() — 7-day moving average. Calendar features: day of week, month, quarter, is_holiday, days_to_holiday, fiscal_week. These transform a time-dependent problem into a standard regression problem solvable by LightGBM — often outperforming LSTM by significant margins on structured time series.
03
Model Selection: Statistical vs. ML vs. Deep Learning
ARIMA/SARIMA: classic statistical models for stationary series with clear seasonality. Use auto_arima() from pmdarima to auto-select (p,d,q). Prophet (Meta): decomposes into trend + seasonality + holidays. Robust to missing data. Ideal for business forecasting with multiple seasonalities. LightGBM + lag features: consistently wins on tabular time series. N-BEATS / N-HiTS (Nixtla): pure deep learning, no feature engineering needed. TFT (Temporal Fusion Transformer): attention-based, handles multivariate series with static covariates.
04
Evaluation Metrics: MAPE, SMAPE, WAPE, and Interval Coverage
MAPE (Mean Absolute Percentage Error): intuitive but explodes near zero. SMAPE (Symmetric MAPE): bounded in [0%, 200%]. WAPE (Weighted APE = MAE/sum(actuals)): best for intermittent demand (zeros). Always report prediction intervals, not just point forecasts: a forecast without uncertainty bounds is misleading. Use conformal prediction or quantile regression to produce calibrated intervals. Check interval coverage: 90% interval should contain actuals 90% of the time.

Folder Structure

Bash — Time Series Project Tree
timeseries-project/
├── data/
│   ├── raw/                      # Original CSV exports, SQL dumps
│   ├── processed/
│   │   ├── cleaned.parquet       # Resampled, imputed, timezone-normalized
│   │   └── features.parquet      # Lag features + rolling stats + calendar
│   └── external/                 # Weather, holidays, macro indicators
│
├── src/
│   ├── preprocessing/
│   │   ├── resampler.py          # Irregular → regular grid (resample/interpolate)
│   │   ├── stationarity.py       # ADF test, KPSS test, differencing
│   │   └── decompose.py          # STL decomposition: trend + seasonal + residual
│   ├── features/
│   │   ├── lag_features.py       # create_lag_features(df, lags=[1,7,28,365])
│   │   ├── rolling_features.py   # Rolling mean, std, min, max
│   │   └── calendar_features.py  # Datetime → day_of_week, is_holiday, etc.
│   └── models/
│       ├── prophet_model.py      # Prophet wrapper + hyperparameter tuning
│       ├── lgbm_model.py         # LightGBM with TimeSeriesSplit CV
│       ├── lstm_model.py         # Keras LSTM with sliding window
│       └── ensemble.py           # Weighted average of model forecasts
│
├── artifacts/
│   ├── lgbm_model.joblib         # LightGBM model + feature names
│   ├── scaler.joblib             # MinMaxScaler state for LSTM inputs
│   ├── prophet_params.json       # Fitted Prophet component parameters
│   └── forecast_config.json      # horizon=30, freq="D", confidence=0.95
│
└── api/
    ├── app.py                    # FastAPI: /forecast?horizon=30
    └── scheduler.py              # APScheduler: retrain weekly

Domain Resources

StatsForecast (Nixtla)Lightning-fast ARIMA, ETS, Theta models. 100x faster than statsmodels. Parallelized.Free
NeuralForecast (Nixtla)N-BEATS, N-HiTS, TFT, PatchTST implementations. PyTorch Lightning based.Free
Prophet (Meta)Additive forecasting with trend, seasonality, and holidays. Great for business KPIs.Free
DartsUnified time series library: statistical, ML, and deep learning models with identical API.Free

Recommendation System Architecture

Recommendation systems are among the highest-ROI ML applications. Netflix's recommendation system saves $1B/year in churn prevention. Amazon attributes 35% of revenue to recommendations. The core problem: given a user-item interaction matrix (sparse, often 99%+ empty), predict which items a user will engage with next. Three paradigms dominate: Collaborative Filtering (similar users like similar items), Content-Based Filtering (items similar to what you liked), and Hybrid Systems combining both with a two-tower neural network.

Interaction Data
Clicks, ratings
Purchase history
Dwell time
Matrix Building
User-Item sparse matrix
Implicit feedback
Negative sampling
Embedding Training
ALS / SVD++
Two-Tower NN
BPR optimization
ANN Indexing
FAISS / ScaNN
Approximate NN
Sub-ms retrieval
Artifacts
user_embeddings.npy
item_embeddings.npy
faiss_index.bin
Real-time API
/recommend?user_id
<5ms p99 latency
A/B tested

Key Concepts

01
Implicit vs. Explicit Feedback
Explicit feedback (star ratings, thumbs up/down) is rare but high-quality. Implicit feedback (clicks, purchases, dwell time, scroll depth) is abundant but noisy. A click doesn't mean the user liked something — they might have clicked to confirm they didn't want it. For implicit feedback, use Bayesian Personalised Ranking (BPR) which models relative preferences: "user A preferred item X over item Y" rather than absolute ratings. Weight by recency and confidence: a purchase 5 minutes ago signals stronger preference than a click 6 months ago.
02
Matrix Factorization with ALS
Alternating Least Squares decomposes the user-item matrix $R \in \mathbb{R}^{m \times n}$ into $R \approx U V^T$ where $U \in \mathbb{R}^{m \times k}$ (user embeddings) and $V \in \mathbb{R}^{n \times k}$ (item embeddings). $k$ (latent dimensions) = 64–512 in production. Recommendation: find items with highest $u_i \cdot v_j$ score. Use implicit library for implicit feedback ALS: model = implicit.als.AlternatingLeastSquares(factors=128). Scales to millions of users/items.
03
Two-Tower Architecture (Modern Industry Standard)
Train two neural networks: a user tower that encodes user features into an embedding, and an item tower that encodes item features. Trained with contrastive loss: minimize distance between embeddings of interacted user-item pairs, maximize distance for non-interacted pairs. At inference: precompute all item embeddings, index with FAISS, retrieve top-k with dot product search in <5ms. Used by YouTube, Pinterest, TikTok. Implemented with TensorFlow Recommenders (TFRS).
04
Cold Start Problem
New users with no history cannot be embedded by collaborative filtering. Solutions: (1) Content-based warm start: use item metadata (category, price, description) to bootstrap recommendations. (2) Onboarding questions: collect 3-5 explicit preferences. (3) Popularity baseline: recommend globally popular items stratified by category/demographic. (4) Meta-learning: train a model to quickly adapt to a new user from just a few interactions (MAML-based approaches).

Artifacts & File Formats

.npyNumPy binary array — Stores user/item embedding matrices. np.save('user_embeddings.npy', U) / np.load('user_embeddings.npy'). Much faster than CSV (no text parsing). Store alongside a user_id → row_index mapping in a separate file for O(1) lookup.
faiss_index.binFAISS vector index — Facebook AI Similarity Search stores item embeddings for approximate nearest-neighbor retrieval. Index types: IndexFlatL2 (exact, slow), IndexIVFFlat (approximate, fast), IndexHNSWFlat (graph-based, fastest). Save/load: faiss.write_index(index, 'index.bin'); faiss.read_index('index.bin').
.npzNumPy compressed archive — Stores sparse interaction matrix in COO/CSR format. scipy.sparse.save_npz('interactions.npz', sparse_matrix). Enables loading just the structure without deserializing the entire matrix.

Domain Resources

implicitFast ALS, BPR, LMF for implicit feedback. GPU-accelerated. Production-proven.Free
FAISSBillion-scale approximate nearest neighbor search. GPU-accelerated. Sub-millisecond retrieval.Free
TensorFlow RecommendersTwo-tower architecture, multi-task learning, retrieval + ranking models.Free

Graph Neural Network Architecture

Most real-world data has relational structure that standard ML ignores: molecules (atoms + bonds), social networks (users + friendships), knowledge graphs (entities + relationships), citation networks (papers + references), fraud detection (transactions + accounts). Graph Neural Networks process this structure by iteratively aggregating information from neighbouring nodes — "message passing" — learning representations that capture both node features and graph topology.

Raw Graph Data
Node features
Edge list
Edge weights/types
Graph Construction
NetworkX → PyG
Data() object
Adjacency matrix
GNN Training
Message Passing
GCN / GAT / SAGE
Node / Link / Graph
Evaluation
Accuracy / F1
AUC-ROC
Hits@K (links)
Artifact
.pt (model)
node_embeddings.pt
graph_schema.json
GraphQL / REST API
Node classification
Link prediction
Real-time scoring

Key Concepts

01
Message Passing Neural Networks (MPNNs)
All GNNs share the same computational framework: for each node $v$, aggregate messages from neighbours $\mathcal{N}(v)$, then update the node's embedding: $h_v^{(k)} = \text{UPDATE}\left(h_v^{(k-1)}, \text{AGGREGATE}\left(\{h_u^{(k-1)} : u \in \mathcal{N}(v)\}\right)\right)$. After $K$ layers, each node's embedding captures information from its $K$-hop neighbourhood. GCN uses mean aggregation, GAT uses attention-weighted aggregation (learns which neighbours to focus on), GraphSAGE samples a fixed-size neighbourhood for scalability.
02
Three Learning Tasks on Graphs
Node classification: predict the label of each node (e.g., classify accounts as fraudulent/legitimate). Link prediction: predict whether an edge exists between two nodes (e.g., predict friendship or drug-disease interaction). Use negative_sampling(edge_index) to generate training negatives. Graph classification: predict a label for the entire graph (e.g., classify molecules as toxic/non-toxic). Requires a readout function (global mean/max pool) to aggregate node embeddings into a single graph-level vector.
03
Scalability: Mini-batch Training with Neighbour Sampling
Full-graph training is infeasible for graphs with millions of nodes (OOM). GraphSAGE solves this: for each target node, sample a fixed number of neighbours at each hop (e.g., 25→10 for 2-hop). This bounds the computation tree size. Use PyG's NeighborLoader: loader = NeighborLoader(data, num_neighbors=[25, 10], batch_size=1024, input_nodes=train_idx). Scales to billion-edge graphs with constant memory per batch.

Folder Structure

Bash — GNN Project Tree
gnn-project/
├── data/
│   ├── raw/
│   │   ├── nodes.csv             # node_id, feature_1, ..., feature_n, label
│   │   └── edges.csv             # src_id, dst_id, edge_weight, edge_type
│   └── processed/
│       └── graph.pt              # PyG Data() object saved with torch.save()
├── src/
│   ├── dataset.py                # Build PyG Data() from raw CSVs
│   ├── models/
│   │   ├── gcn.py                # GCNConv layers
│   │   ├── gat.py                # GATConv with multi-head attention
│   │   └── sage.py               # SAGEConv for inductive learning
│   ├── train.py                  # Training loop with NeighborLoader
│   └── evaluate.py               # Node accuracy, link AUC, Hits@K
├── artifacts/
│   ├── gnn_model.pt              # Trained GNN model state_dict
│   └── node_embeddings.pt        # Pre-computed embeddings for all nodes
└── api/
    └── app.py                    # FastAPI: /classify_node, /predict_link

Domain Resources

PyTorch Geometric (PyG)The dominant GNN library. 100+ layers, 70+ benchmark datasets, mini-batch loaders.Free
Deep Graph Library (DGL)Framework-agnostic (PyTorch/TensorFlow/MXNet). Strong on heterogeneous graphs.Free
OGB (Stanford)Open Graph Benchmark: large-scale graph datasets and leaderboards. OGB-LSC for billion-scale.Free

Audio & Speech Architecture

Audio ML is fundamentally a signal processing + deep learning problem. Raw audio is a 1D time-domain signal sampled at 16,000-44,100 Hz. The key insight: neural networks don't operate on raw waveforms efficiently. Instead, audio is converted to a mel spectrogram — a 2D time-frequency representation that mimics the logarithmic sensitivity of the human cochlea. This turns audio classification into image classification, enabling use of CNNs and Vision Transformers.

Raw Audio
.wav / .mp3 / .flac
16kHz mono
Variable length
Mel Spectrogram
STFT → Mel filterbank
Log amplitude
(T, n_mels) tensor
Annotation
Audacity labels
Speaker diarization
Transcript alignment
Fine-tune Whisper
ASR / Classification
wav2vec2 / HuBERT
Conformer
Artifact
.pt / .safetensors
processor config
vocab.json
Streaming API
WebSocket / gRPC
Real-time chunks
Sub-second latency

Key Concepts

01
From Waveform to Mel Spectrogram
Pipeline: (1) STFT (Short-Time Fourier Transform): apply a sliding Hann window (e.g., 25ms hop 10ms) and compute FFT at each position → complex spectrogram. (2) Mel filterbank: apply triangular filters spaced on the mel scale (logarithmic frequency) → captures perceptually relevant features. (3) Log amplitude: convert power to dB scale. Code: librosa.feature.melspectrogram(y=audio, sr=16000, n_mels=128, hop_length=160). Result: a (128, T) matrix that CNNs can process like an image.
02
Fine-tuning OpenAI Whisper for Custom Domains
Whisper is a Transformer encoder-decoder trained on 680k hours of audio. Fine-tune for domain-specific vocabulary (medical terms, accents, noise conditions) with as little as 1 hour of labeled audio. Use HuggingFace WhisperForConditionalGeneration. Format: audio → 80-channel log-mel spectrogram → Whisper processor → tokenized transcript. Training: minimize cross-entropy on predicted token sequences. CER (Character Error Rate) and WER (Word Error Rate) are the evaluation metrics.
03
Audio Classification with CNN on Mel Spectrograms
Treat the mel spectrogram as an image: spec = librosa.feature.melspectrogram(...) → resize to (224, 224) → pass through EfficientNet-B0 pretrained on ImageNet → fine-tune on audio classes. This works because ImageNet pretrained features capture texture patterns analogous to spectral patterns. Used for: environmental sound classification (ESC-50), music genre recognition, keyword spotting, emotion recognition. Achieves near-SOTA with minimal audio-specific engineering.

Artifacts & File Formats

.wavWaveform Audio File Format — Uncompressed PCM audio. The standard format for ML training data — no compression artifacts. Always store training data as 16kHz mono WAV. librosa.load(path, sr=16000, mono=True) resamples to 16kHz on load. 1 hour of 16kHz mono WAV ≈ 112MB.
vocab.jsonASR vocabulary mapping — Maps characters or subword tokens to integer IDs for ASR models. Always save alongside the model: processor.save_pretrained('./whisper_finetuned/'). Includes special tokens: <pad>, <bos>, <eos>, language tokens (<|en|>), task tokens (<|transcribe|>).

Domain Resources

librosaAudio loading, mel spectrograms, MFCCs, beat tracking, pitch estimation.Free
OpenAI WhisperSOTA multilingual ASR. Available via HuggingFace. Fine-tune on domain audio.Free
wav2vec 2.0Self-supervised speech representation learning. Excellent for low-resource ASR fine-tuning.Free

Anomaly Detection Architecture

Anomaly detection (also called outlier detection or novelty detection) identifies data points that deviate significantly from the expected pattern. Unlike standard classification, anomalies are by definition rare — often constituting 0.1–1% of data. This extreme class imbalance makes standard classifiers fail. The solution: train only on normal data and flag anything that doesn't fit the learned normality model. Applications: financial fraud, network intrusion, industrial equipment failure prediction, medical outlier detection.

Normal Data Only
Labeled "normal"
Historical baseline
Clean operational data
Feature Engineering
Statistical features
Rolling z-scores
Frequency domain
Normality Model
Isolation Forest
Autoencoder
One-Class SVM
Threshold Calibration
Precision-Recall
Contamination rate
Business cost matrix
Artifact
.joblib
threshold.json
scaler.joblib
Alert API
Scoring endpoint
PagerDuty / Slack
Grafana dashboard

Algorithm Comparison

Isolation ForestRandom trees isolate anomalies in fewer splits. Score = average path length. $O(n \log n)$, fast. Best for tabular data with no labeled anomalies. Contamination param = expected anomaly rate. IsolationForest(contamination=0.01).
AutoencoderNeural network trained to reconstruct normal data. Anomaly score = reconstruction error. High error = anomaly. Excellent for high-dimensional data (images, signals). Architecture: encoder → bottleneck → decoder. Use MSE as reconstruction loss.
Local Outlier FactorDensity-based. Compares local density of a point to its neighbours. LOF > 1 indicates outlier. Detects local anomalies invisible to global methods. $O(n^2)$ — slow for large datasets. Use LocalOutlierFactor(n_neighbors=20, novelty=True).
One-Class SVMFits a hypersphere around normal data in kernel space. Points outside = anomalies. Works in high dimensions. Use RBF kernel. Sensitive to hyperparameter $\nu$ (upper bound on outlier fraction). Best for low-to-medium dimensional data.
ECOD (PyOD)Empirical-Cumulative-distribution-based Outlier Detection. Parameter-free, O(n log n), interpretable. Best zero-shot anomaly detector — no tuning needed. Available via pyod.models.ecod import ECOD.

Domain Resources

PyOD40+ anomaly detection algorithms with unified sklearn-compatible API. ECOD, COPOD, SUOD.Free
Anomaly Detection ResourcesComprehensive survey of algorithms, datasets, and papers curated by PyOD authors.Free

Multi-modal AI Architecture

Multi-modal models process and relate information from multiple data types simultaneously — images + text, audio + video, graphs + text. The breakthrough was CLIP (Contrastive Language-Image Pretraining): by training an image encoder and a text encoder jointly on 400M image-caption pairs, it learns a shared embedding space where "a photo of a cat" is near an image of a cat. This enables zero-shot image classification, visual question answering, and image-text retrieval without task-specific training.

Multi-modal Data
Images + Captions
Video + Transcripts
Documents + Tables
Modal Processing
ViT image patches
BPE text tokens
Cross-modal align
CLIP / BLIP-2
Contrastive loss
Shared embedding
Cross-attention
Evaluation
Zero-shot accuracy
Recall@K
VQA accuracy
Artifact
.pt / .safetensors
vision_config.json
text_config.json
Multi-modal API
Image + query input
Structured JSON out
Vision-language chat

Key Architectures

01
CLIP: Zero-Shot Image Classification
CLIP trains a Vision Transformer (ViT) image encoder and a Transformer text encoder with contrastive loss: for a batch of (image, text) pairs, push embeddings of matching pairs together and pull non-matching pairs apart. The resulting shared space enables zero-shot classification: encode the image, encode each class label as "a photo of a {class}", find the highest cosine similarity. Use: from transformers import CLIPModel, CLIPProcessor. Fine-tune on domain images with only a few hundred examples.
02
LLaVA / BLIP-2: Vision-Language Chat Models
These models can answer questions about images in natural language. Architecture: image → CLIP ViT encoder → linear projection → LLM token stream. The projection layer maps visual features into the LLM's embedding space. BLIP-2 uses a Q-Former (cross-attention between image tokens and learned query embeddings) as the bridge. LLaVA uses a simpler linear projection and is easier to fine-tune on custom image-instruction pairs. Use: from transformers import LlavaForConditionalGeneration.
03
Building Multi-modal Search (CLIP + FAISS)
Build a semantic image search engine: (1) Encode all images with CLIP ViT → embeddings ∈ ℝ^512. (2) Index with FAISS. (3) At query time: encode the text query with CLIP text encoder. (4) Retrieve top-k images by cosine similarity. This is the exact architecture behind Pinterest visual search, Google Image Search, and e-commerce visual similarity. Works out of the box with zero fine-tuning on the new image domain — CLIP's generalization is remarkable.

Domain Resources

CLIP (OpenAI)Zero-shot image classification, visual search, image-text alignment. HuggingFace ready.Free
LLaVAVisual instruction tuning. Answer any question about an image. Multiple size variants.Free
BLIP-2Bootstrapped language-image pretraining. Q-Former architecture. Visual QA and captioning.Free

Professional Engineering Practices

These practices apply to every ML domain. Skipping any of them is technical debt that will cause a production incident. Apply them from day one, not as an afterthought.

🔐 Secrets Management with .env Files

Every ML project touches external services: Hugging Face, W&B, AWS, Roboflow, OpenAI. Their API keys must never be committed to git — even to private repos, because git history is permanent and repos can be made public accidentally.

Bash — .env + Python
# .env — NEVER commit this file
HF_TOKEN=hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
WANDB_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
OPENAI_API_KEY=sk-proj-xxxxxxxxxxxxxxxxxxxxxxx
AWS_ACCESS_KEY_ID=AKIAXXXXXXXXXXXXXXXX
AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxx
REPLICATE_API_TOKEN=r8_xxxxxxxxxxxxxxxxxxxxxxx
ROBOFLOW_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
DATABASE_URL=postgresql://user:pass@localhost:5432/mldb

# Load in Python
# pip install python-dotenv
from dotenv import load_dotenv
import os
load_dotenv()  # reads .env from current directory
hf_token = os.getenv("HF_TOKEN")

# In Docker: pass as environment variables
# docker run -e HF_TOKEN=$HF_TOKEN my-ml-image
# Or use Docker secrets for production
⚠️
If you accidentally commit a secret: Rotate the key immediately (don't just delete the commit — git history persists). Use git-filter-repo to purge the commit from history, then force push. GitHub's secret scanning will flag it automatically and email you if it detects committed credentials.

📦 The Definitive ML .gitignore

Bash — .gitignore for ML Projects
# ── Secrets & credentials
.env
.env.*
*.pem
secrets/

# ── Python
__pycache__/
*.py[cod]
*.so
*.egg
*.egg-info/
dist/
build/
.venv/
venv/
env/

# ── Jupyter
.ipynb_checkpoints/
*.ipynb         # Optional: only if using .py scripts, not notebooks

# ── ML data (track with DVC instead)
data/raw/
data/processed/
datasets/
*.csv           # Large CSVs only — small reference CSVs are OK in git
*.parquet
*.h5
*.hdf5

# ── Model weights (track with DVC or MLflow)
*.pt
*.pth
*.pkl
*.joblib
*.safetensors
*.bin
*.onnx
*.gguf
*.ckpt
*.weights
*.engine
*.tflite
weights/
models/
checkpoints/
artifacts/

# ── Experiment outputs
runs/           # TensorBoard logs
wandb/          # W&B local runs
mlruns/         # MLflow tracking
outputs/
results/
logs/

# ── OS & Editor
.DS_Store
Thumbs.db
.idea/
.vscode/        # Optional: some teams commit .vscode/settings.json
*.swp

🗂️ Data & Model Versioning with DVC

Git tracks code. DVC tracks data and models. Every dvc add creates a lightweight pointer file (.dvc) that git tracks, while the actual large file is pushed to remote storage (S3, GCS, GDrive, SSH). Checking out any git commit gives you the corresponding data version automatically.

Bash — DVC Setup & Usage
# Install and initialise
pip install dvc[s3]   # or dvc[gcs], dvc[gdrive]
dvc init              # creates .dvc/ directory

# Configure remote storage (S3 example)
dvc remote add -d myremote s3://my-ml-bucket/zerotoml
dvc remote modify myremote profile default  # use AWS CLI profile

# Track large files
dvc add data/processed/train.parquet        # creates data/processed/train.parquet.dvc
dvc add weights/best.pt
git add data/processed/train.parquet.dvc weights/best.pt.dvc .gitignore
git commit -m "Track processed data and model weights with DVC"

# Push data to remote
dvc push                                     # uploads to S3

# On another machine / CI pipeline
git clone https://github.com/Muhammad-waqas1/zerotoml
dvc pull                                     # downloads tracked files from S3

# Switch to a previous data version
git checkout v1.0.0
dvc checkout                                 # downloads the data from that commit

🐳 Production Dockerfile Template for ML APIs

Docker — Production ML API
# ── Stage 1: dependency install (cached layer)
FROM python:3.11-slim AS base

WORKDIR /app

# System deps — install before copying code (better layer caching)
RUN apt-get update && apt-get install -y --no-install-recommends \
    curl libgomp1 libglib2.0-0 \
 && rm -rf /var/lib/apt/lists/*

# Install Python deps first (changes less often than code)
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# ── Stage 2: application
COPY src/ ./src/
COPY api/ ./api/
COPY artifacts/ ./artifacts/          # Model weights baked into image

# Security: non-root user
RUN useradd -m -u 1000 mluser && chown -R mluser /app
USER mluser

EXPOSE 8000

HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
  CMD curl -f http://localhost:8000/health || exit 1

CMD ["uvicorn", "api.app:app", "--host", "0.0.0.0", "--port", "8000", \
     "--workers", "2", "--log-level", "info"]

📊 Experiment Tracking: The Minimum Viable Setup

Python — W&B + MLflow Dual Tracking
import wandb
import mlflow
import os, subprocess

# ── Weights & Biases (cloud, great UI, hyperparameter sweeps)
wandb.init(
    project="zerotoml-cv",
    name=f"yolov8-exp-{run_id}",
    config={
        "model":        "yolov8n",
        "epochs":       100,
        "img_size":     640,
        "batch_size":   16,
        "learning_rate": 0.01,
        "dataset_version": "v3.2",  # Always version your dataset!
        "git_commit":   subprocess.check_output(["git", "rev-parse", "HEAD"]).decode().strip(),
    },
    tags=["baseline", "augmentation-v2"],
)

# Log metrics each epoch
for epoch in range(epochs):
    metrics = train_one_epoch(model, dataloader)
    wandb.log({
        "train/loss": metrics["loss"],
        "val/mAP50":  metrics["map50"],
        "val/precision": metrics["precision"],
        "lr":         scheduler.get_last_lr()[0],
    })

# Save model artifact to W&B
artifact = wandb.Artifact("yolov8-trained", type="model")
artifact.add_file("weights/best.pt")
wandb.log_artifact(artifact)
wandb.finish()

# ── MLflow (self-hosted, great for enterprise)
mlflow.set_tracking_uri("http://mlflow-server:5000")
with mlflow.start_run(run_name="yolov8-baseline"):
    mlflow.log_params({"epochs": 100, "img_size": 640})
    mlflow.log_metrics({"val_map50": 0.847, "val_precision": 0.891})
    mlflow.log_artifact("weights/best.pt", artifact_path="model")
    mlflow.set_tag("model_type", "object_detection")

Professional Tools & Resources

Every tool categorized by function, pricing model, and primary use case. Updated for 2025.

Tool / Platform Category Primary Use Pricing Best For
DATA LABELING & ANNOTATION
CVAT.ai Labeling CV annotation (bbox, polygon, segmentation, keypoints) Freemium Self-hosted teams, YOLO/COCO export
Roboflow Labeling Labeling + augmentation + dataset versioning + deploy Freemium End-to-end CV pipeline, quick iteration
Label Studio Labeling NLP (NER, classification), CV, audio, time-series Free Multi-modal labeling, self-hosted, RLHF
Scale AI Labeling Managed human labeling at scale Paid Production annotation, autonomous vehicles
Prodigy Labeling Active learning annotation, NLP + CV Paid spaCy ecosystem, small team annotation
Argilla Labeling LLM data labeling, RLHF preference collection Free Fine-tuning LLMs with human feedback
COMPUTE & GPU ACCESS
Kaggle Notebooks Compute Free GPU/TPU for training Free 30hr/week T4 GPU, pre-installed ML stack
Google Colab Compute Notebook GPU compute Freemium Prototyping, free T4 with limits
Lambda Labs Compute On-demand GPU cloud (H100, A100, 3090) Paid LLM training, cheapest H100 hourly rate
Vast.ai Compute Peer-to-peer GPU marketplace Paid Cheapest GPU rates, flexible configs
Modal Compute Serverless Python GPU functions Freemium Zero-to-scale GPU jobs, $30/mo free credit
RunPod Compute GPU cloud with persistent storage Paid LLM fine-tuning, stable long-running jobs
EXPERIMENT TRACKING & MODEL REGISTRY
Weights & Biases Tracking Experiment tracking, hyperparameter sweeps, model registry Freemium Best visualization, sweep automation, free tier for academics
MLflow Tracking Self-hosted experiment tracking + model serving Free Enterprise, on-premise, multi-framework
Neptune.ai Tracking Metadata store for ML experiments Freemium Large teams, compliance logging
DVC Tracking Data + model versioning for git Free Dataset versioning, pipeline reproducibility
Comet ML Tracking Experiment tracking + model production monitoring Freemium Production drift monitoring included
MODEL HOSTING & INFERENCE
HF Inference Endpoints Hosting Deploy any HF model as dedicated endpoint Paid NLP/LLM APIs, auto-scaling, GPU
Replicate Hosting Deploy ML models via Docker + cog Freemium Diffusion models, per-second billing
Streamlit Community Cloud Hosting Deploy Streamlit ML dashboards Free ML demos, data apps, internal tools
Render Hosting Docker container deployment from GitHub Freemium FastAPI ML APIs, auto-deploy from GitHub
vLLM Hosting High-throughput LLM inference server Free Self-hosted LLM serving, OpenAI-compatible API
Ollama Hosting Run LLMs locally (GGUF models) Free Local development, privacy-sensitive deployments
ML FRAMEWORKS & LIBRARIES
PyTorch Framework Deep learning research + production Free Research, flexibility, 80% of papers
scikit-learn Framework Classical ML: Pipeline, GridSearch, metrics Free Tabular ML, preprocessing pipelines
XGBoost Framework Gradient boosted trees, GPU support Free Tabular competition standard
LightGBM Framework Fast gradient boosting, leaf-wise splitting Free Large tabular datasets, 10x faster than XGBoost
fastai Framework High-level PyTorch API for CV/NLP/tabular Free Rapid prototyping, learning PyTorch patterns
Keras / TensorFlow Framework High-level DL, production TF Serving Free TFLite mobile export, TPU training
DATASETS & DATA TOOLS
UCI ML Repository Data 650+ structured datasets for ML benchmarking Free Tabular ML benchmarking, research
HF Datasets Data 70k+ ML datasets, streaming, one-line load Free NLP datasets, multi-modal, benchmark
Kaggle Datasets Data 200k+ community datasets across domains Free Competition data, real-world messy datasets
Great Expectations Data Data quality validation, schema enforcement Freemium Production data pipelines, CI/CD for data
MLOPS & DEPLOYMENT INFRASTRUCTURE
FastAPI MLOps Production REST API framework for ML services Free ML inference APIs, typed endpoints, auto-docs
BentoML MLOps ML model serving framework Freemium Multi-model APIs, adaptive batching
Evidently AI MLOps ML monitoring, drift detection, data quality Freemium Production model monitoring, drift alerts
Apache Airflow MLOps Pipeline orchestration and scheduling Free ETL pipelines, scheduled retraining
Prefect MLOps Modern data pipeline orchestration Freemium ML workflow automation, retry logic
Pricing legend: Free— Open source or permanently free tier Freemium— Free tier exists, paid for scale/features Paid— Commercial only or requires payment

Ready to Build?

From Architecture to Production

The best way to learn these architectures is to build with them. Start with the ZeroToML curriculum to build the mathematical and practical foundations, then apply the lifecycle patterns from this guide to your own projects.