Slide 1 of 24

Week 3: Machine Learning Recap & Model Deployment

CP3501/CP5701 Deep Learning

Part 1: Machine Learning & Deep Learning Fundamentals

Part 2: Deploying Your Models to Production

Information Technology | College of Science and Engineering

James Cook University

What is Machine Learning?

Traditional Programming

Rules + Data Computer Program Answers

Humans write explicit rules:
if temperature > 30: "Hot"

Machine Learning

Data + Answers Learning Algorithm Rules

Algorithm discovers rules from examples:
Learns patterns automatically

Key Idea: Instead of programming rules, we provide examples and let the computer learn the patterns.

Two Main Types of Machine Learning Tasks

Regression

Predicting continuous numbers

Size (sqm) Price ($)
  • House prices
  • Temperature forecast
  • Stock prices

Classification

Predicting categories/classes

Cat Dog Pointy ears Floppy ears Decision Boundary
  • Image classification (cat/dog)
  • Spam detection
  • Disease diagnosis

Knowledge Checkpoint 1

Question 1: Which of these is a REGRESSION problem?

  • A) Classifying emails as spam or not spam
  • B) Identifying whether an image contains a cat or dog
  • C) Predicting tomorrow's maximum temperature
  • D) Diagnosing whether a patient has diabetes (yes/no)
Correct! C is regression.

Why? Temperature is a continuous number (23.5°C, 24.1°C, etc.)
A, B, D are classification because they predict categories (spam/not spam, cat/dog, yes/no)

Question 2: What's the fundamental difference between traditional programming and ML?

  • A) ML is always more accurate
  • B) ML learns rules from data; traditional programming uses hand-coded rules
  • C) ML doesn't require any data
  • D) Traditional programming is faster
Correct! B is the key difference.

ML discovers patterns from examples, while traditional programming requires humans to explicitly write every rule.

Traditional Machine Learning vs Deep Learning

Aspect Traditional ML Deep Learning
Feature Engineering Manual - humans decide what features matter
(e.g., "ear shape", "fur texture")
Automatic - learns features from raw data
Data Requirements Works with smaller datasets
(hundreds to thousands)
Needs large datasets
(thousands to millions)
Computation Can run on regular CPU Usually needs GPU for training
Interpretability Easier to understand why it made a decision Often a "black box"
Performance Good for structured/tabular data Excels at images, text, audio
Examples Decision Trees, Random Forest, SVM Convolutional Neural Networks (CNNs), Transformers
When to use Deep Learning? Complex unstructured data (images, text, audio) + large datasets + sufficient compute resources

How Do Neural Networks Work?

Inspired by the Brain (simplified)

Input Layer Pixel 1 Pixel 2 Pixel 3 Hidden Layer Output Layer Cat Dog

Each Connection Has a Weight

  • Thick red lines = strong influence
  • Thin gray lines = weak influence
  • Learning = adjusting these weights

Learning Process

  • Show example: cat image
  • Network makes prediction
  • Compare to correct answer
  • Adjust weights to improve
  • Repeat thousands of times

Why "Deep" Learning?

Deep = Many layers of neurons

Simple Features Complex Features Layer 1 Edges Lines Layer 2 Shapes Textures Layer 3 Ears Eyes Output CAT 95%
Key Insight: Each layer learns increasingly complex patterns. Early layers detect simple features (edges), later layers detect complex features (ears, eyes), final layer makes the decision (cat/dog).

How We Train Neural Networks

1. Forward Pass Input image → Network Makes prediction 2. Calculate Error Prediction: Dog (80%) Actual: Cat → ERROR! 3. Backpropagation Adjust weights to reduce error 4. Repeat Thousands of examples Loss Function Measures how wrong we are Loss = (Predicted - Actual)² Goal: Minimize this loss by adjusting weights

Knowledge Checkpoint 2

Question 3: What is the main advantage of Deep Learning over traditional ML?

  • A) Always requires less data
  • B) Automatically learns features from raw data (no manual feature engineering)
  • C) Runs faster on any computer
  • D) Easier to understand and interpret
Correct! B is the key advantage.

Traditional ML requires humans to manually decide which features matter (e.g., "ear shape", "fur color"). Deep Learning discovers these features automatically from the raw pixels.

Question 4: In the training process diagram, what does "Backpropagation" do?

  • A) Makes predictions on new data
  • B) Adjusts the network weights to reduce the error
  • C) Loads the training data into memory
  • D) Displays the results to the user
Correct! B is what backpropagation does.

It's the mathematical process of propagating the error backwards through the network and adjusting weights to minimize future errors.

Transfer Learning: The Secret Weapon

Traditional Approach

Train from scratch Random weights Needs HUGE dataset (millions of images) Days/weeks of training

Transfer Learning ✓

Start with pre-trained model (ImageNet: 14M images) Fine-tune on YOUR data (hundreds of images OK!) Minutes/hours of training
Analogy: Instead of learning a new language from scratch, you start by knowing grammar and vocabulary, then just learn domain-specific terms. Pre-trained models already know "how to see" (edges, shapes, textures) - we just teach them to recognize our specific categories.
This is why FastAI works so well with small datasets! It uses pre-trained models as a starting point.

What You've Already Built (Weeks 1-2)

1 Loaded a pre-trained model (ResNet, trained on ImageNet)
2 Prepared your dataset (images organized in folders)
3 Fine-tuned the model using learn.fine_tune()
4 Evaluated performance with accuracy metrics
5 Made predictions on new images in Jupyter notebooks
The Problem: Your model only exists in your Kaggle/Colab notebook.

How can others use it? How can you add it to your portfolio?

Part 2: Model Deployment

From Notebook to Production

Current State Model in notebook Only you can use it Requires coding knowledge Deploy Goal Web app anyone can use No coding required Portfolio-ready! What We'll Learn Today: 1. Export your trained model 2. Create a simple web interface (Gradio) 3. Deploy to Hugging Face Spaces (free hosting!) 4. Share your work with the world

The Only 3 Files You Need

1 model.pkl - Your trained model (the "brain")
learn.export('model.pkl')  # Creates this file
2 app.py - The web interface code
import gradio as gr
from fastai.vision.all import *

learn = load_learner('model.pkl')

def classify_image(img):
    pred, idx, probs = learn.predict(img)
    return {learn.dls.vocab[i]: float(probs[i]) for i in range(len(probs))}

demo = gr.Interface(
    fn=classify_image,
    inputs=gr.Image(),
    outputs=gr.Label()
)
demo.launch()
3 requirements.txt - List of libraries needed
fastai
gradio

Deployment Workflow (Simple Method)

1 Train and Export (Kaggle/Colab)
learn.fine_tune(3)
learn.export('model.pkl')  # Download this file
2 Create Hugging Face Space
  • Go to huggingface.co/spaces
  • Click "Create new Space"
  • Choose "Gradio" as the SDK
  • Select "Public" and "Apache 2.0" license
3 Upload Files via Web Interface
  • Click "Files" → "Add file"
  • Upload model.pkl
  • Create app.py (copy code from slide 13)
  • Create requirements.txt
4 Wait for Build (2-5 minutes)

Hugging Face automatically installs libraries and starts your app

Your app is now live! Share the URL with anyone.

Understanding app.py (Line by Line)

import gradio as gr                    # 1. Import web interface library
from fastai.vision.all import *        # 2. Import FastAI

learn = load_learner('model.pkl')      # 3. Load your trained model

def classify_image(img):               # 4. Define what happens when user uploads
    pred, idx, probs = learn.predict(img)  #    - Get prediction from model
    return {                           #    - Return results as dictionary
        learn.dls.vocab[i]: float(probs[i]) 
        for i in range(len(probs))
    }

demo = gr.Interface(                   # 5. Create the web interface
    fn=classify_image,                 #    - Which function to call
    inputs=gr.Image(),                 #    - Input: image upload box
    outputs=gr.Label(),                #    - Output: labels with percentages
    title="My Image Classifier",       #    - Customize title
    description="Upload a photo to classify it"
)

demo.launch()                          # 6. Start the web server
That's it! Just 15 lines of code to create a web app. Gradio handles all the HTML/CSS/JavaScript for you.

Knowledge Checkpoint 3

Question 5: What does learn.export('model.pkl') do?

  • A) Sends your model to Hugging Face automatically
  • B) Saves your trained model to a file you can download
  • C) Deletes your model from memory
  • D) Converts your model to JavaScript
Correct! B is what export() does.

It packages your trained model (weights, architecture, preprocessing) into a single .pkl file that you can download and deploy elsewhere.

Question 6: Which file contains the actual trained neural network?

  • A) app.py
  • B) model.pkl
  • C) requirements.txt
  • D) All of the above
Correct! B is the model file.

model.pkl = trained weights (the "brain")
app.py = interface code (how to use the brain)
requirements.txt = list of libraries to install

Common Deployment Errors (and Fixes)

Error Message What It Means How to Fix
FileNotFoundError: model.pkl App can't find your model file Check that model.pkl is in the same directory as app.py
ModuleNotFoundError: No module named 'fastai' FastAI library not installed Add fastai to requirements.txt
Application startup failed Syntax error in app.py Check for typos, missing colons, incorrect indentation
Model was trained with different FastAI version Version mismatch Specify version in requirements.txt: fastai==2.7.14
Image not displaying predictions Return format issue Make sure classify_image() returns a dictionary
Debugging Tip: Check the "Logs" tab in your Hugging Face Space to see detailed error messages.

Making Your App Look Professional

Add these parameters to gr.Interface():

demo = gr.Interface(
    fn=classify_image,
    inputs=gr.Image(),
    outputs=gr.Label(num_top_classes=3),  # Show top 3 predictions
    
    # Customization options:
    title="🐱🐶 Pet Classifier",
    description="Upload a photo of a cat or dog to see predictions",
    article="Built with FastAI and Gradio. Model trained on Oxford-IIIT Pets dataset.",
    
    examples=[                            # Sample images users can try
        "example_cat.jpg",
        "example_dog.jpg"
    ],
    
    theme="default",                      # Try: "default", "huggingface", "grass"
    allow_flagging="never"                # Disable flagging button
)

Add a README.md

---
title: Pet Classifier
emoji: 🐱
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 4.0.0
app_file: app.py
---

# Pet Classifier

This model classifies images of cats and dogs...

Pro Tips

  • Add example images to help users
  • Write clear descriptions
  • Explain model limitations
  • Include training details
  • Add your name/contact

Alternative: Git Workflow (Advanced)

For making frequent updates

1 Clone your Space
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE
cd YOUR_SPACE
2 Add your files
cp /path/to/model.pkl .
# Create app.py and requirements.txt
3 Commit and push
git add model.pkl app.py requirements.txt
git commit -m "Initial deployment"
git push
Why use Git? Makes it easy to update your app, track changes, and collaborate. But web interface is simpler for beginners!

From Model to Portfolio Piece

What makes a good portfolio project?

Must Haves

  • Working deployed app
  • Clear problem statement
  • Example inputs/outputs
  • Model accuracy reported
  • Known limitations listed

Nice to Haves

  • Custom dataset you collected
  • Comparison of different models
  • Error analysis
  • Blog post explaining approach
  • Source code on GitHub

Connection to Assessment 3 (Group Project)

Your project MUST be deployed. This demonstrates:
- SLO2: Building and tuning models
- SLO3: End-to-end solutions with reproducible code
- SLO4: Documenting decisions and limitations

Today's Practical Exercise

Deploy Your Week 2 Model

1 Export your model (if you haven't already)

Run learn.export('model.pkl') in your notebook and download the file

2 Create Hugging Face account

Go to huggingface.co and sign up (free)

3 Create a new Space

Spaces → Create new Space → Choose Gradio

4 Upload files
  • model.pkl (your trained model)
  • app.py (copy from slide 13, modify categories)
  • requirements.txt (fastai and gradio)
5 Test and customize

Upload test images, add title/description, create README

Resources & Next Steps

Documentation

  • Gradio Docs: gradio.app/docs
  • FastAI Docs: docs.fast.ai
  • HF Spaces Guide: huggingface.co/docs/hub/spaces

Example Spaces

  • Browse huggingface.co/spaces
  • Click "Files" to see code
  • Duplicate and modify

For Your Project

  • Choose a unique dataset
  • Document your process
  • Deploy early, iterate often
  • Ask for feedback
  • Handle edge cases

Getting Help

  • Check Space logs for errors
  • Search HF forums
  • Ask in workshop sessions
  • Share link for debugging
Remember: Deployment is not a one-time task. You'll iterate on your model and app as you get user feedback and discover edge cases.

Final Knowledge Check

Question 7: You deploy your model but it crashes with "ModuleNotFoundError: No module named 'fastai'". What's missing?

  • A) model.pkl file
  • B) 'fastai' in requirements.txt
  • C) Internet connection
  • D) Hugging Face account
Correct! B - you need to specify dependencies.

requirements.txt tells Hugging Face which Python libraries to install. Without it, FastAI won't be available when your app runs.

Question 8: What's the main benefit of Transfer Learning?

  • A) Models train slower but are more accurate
  • B) You can achieve good results with much less data and training time
  • C) You don't need any training data at all
  • D) Models work without GPUs
Correct! B is the key benefit.

Pre-trained models already know general features (edges, shapes, textures). You just fine-tune them on your specific task with a smaller dataset.

Week 3 Summary

Part 1: ML/DL Fundamentals

  • Machine Learning: Learning patterns from data instead of programming rules
  • Regression: Predict numbers
  • Classification: Predict categories
  • Deep Learning: Many layers, automatic feature learning
  • Transfer Learning: Start with pre-trained model, fine-tune on your data

Part 2: Model Deployment

  • Export: learn.export('model.pkl')
  • 3 Files: model.pkl, app.py, requirements.txt
  • Gradio: Simple web interface creation
  • Hugging Face Spaces: Free hosting platform
  • Portfolio: Deployed app demonstrates your skills

Next Week Preview

We'll dive deeper into what's happening inside neural networks:
- How are images represented as numbers?
- What exactly are tensors?
- How does backpropagation work mathematically?
- Understanding loss functions and optimizers

Today's Workshop: Deploy your Week 2 model to Hugging Face Spaces!