CP3501: Deep Learning

Week 1: Your First Deep Learning Model

Building an Image Classifier in 5 Steps

James Cook University | TR1 2026

1 / 30

Today's Goal: Build Your First AI

By the end of today, you will train a working image classifier in less than 5 minutes.

We'll build a model that can:

This is the same approach used in production systems at major tech companies.

2 / 30

Learning Objectives

After this session, you will be able to:

3 / 30

The Deep Learning Workflow

Every deep learning project follows these 5 steps:

1. Get Data

Collect images

2. Prepare

Organize & label

3. Model

Choose architecture

4. Train

Learn patterns

5. Deploy

Use it!

We'll walk through each step with a concrete example today.

4 / 30

PART 1

Build It

Let's build our first image classifier

5 / 30

Step 1: Get Data

We'll use DuckDuckGo image search to download training images automatically.

# Install the search library !pip install -Uqq duckduckgo_search # Import necessary functions from duckduckgo_search import DDGS from fastai.vision.all import * # Define what we want to classify searches = 'forest', 'bird' # Create folders for our images path = Path('bird_or_not')

What's happening: We're defining two categories (forest, bird) and setting up folders to store the images we'll download.

6 / 30

Step 1: Get Data (continued)

# Download 200 images for each category for search in searches: dest = (path/search) dest.mkdir(exist_ok=True, parents=True) download_images(dest, urls=DDGS().images(search, max_results=200))

Result: You now have 400 images (200 birds, 200 forests) stored in organized folders.

Key Insight: Deep learning needs data. For image classification with transfer learning, 200 images per category is often sufficient to achieve good results.

7 / 30

Step 2: Prepare Data with DataBlock

The DataBlock tells the model how to understand your data by answering 5 questions:

Q1: What types of data?

→ Images as input, Categories as output

Q2: Where is the data?

→ In the 'bird_or_not' folder

Q3: How to get labels?

→ From the parent folder name (bird or forest)

Q4: How to split for validation?

→ Randomly take 20% for testing

Q5: How to transform images?

→ Resize to 224×224 pixels, apply augmentation

8 / 30

Step 2: DataBlock Code

dbs = DataBlock( blocks=(ImageBlock, CategoryBlock), # Q1: Input=Images, Output=Categories get_items=get_image_files, # Q2: Get all image files splitter=RandomSplitter(valid_pct=0.2, seed=42), # Q4: 20% for validation get_y=parent_label, # Q3: Label from folder name item_tfms=[Resize(224, ResizeMethod.Squish)] # Q5: Resize to 224×224 ) # Create the DataLoaders (batches of data for training) dls = dbs.dataloaders(path, bs=32) # bs=32 means process 32 images at a time

What happens now: Your data is ready to feed into the model. The images are organized into batches, resized appropriately, and split into training (80%) and validation (20%) sets.

9 / 30

Quiz 1: Understanding DataBlock

If you want to classify 3 categories (dogs, cats, birds) instead of 2, which part of the DataBlock code needs to change?
  • A. The blocks parameter needs to list three categories
  • B. The splitter needs to be changed to split data three ways
  • C. Only the folder structure changes (add a 'cats' folder) - no code changes needed
  • D. The CategoryBlock needs to be changed to ThreeCategoryBlock
10 / 30

Step 3: Create the Model

We'll use transfer learning - starting with a model pre-trained on millions of images.

# Create a learner with ResNet18 architecture learn = vision_learner(dls, resnet18, metrics=error_rate)

What is ResNet18?

A neural network with 18 layers that has already learned to recognize basic image features (edges, textures, shapes) from 1.2 million images in ImageNet.

What is error_rate?

A metric that tells us what percentage of images our model gets wrong. Lower error_rate = better model performance.

11 / 30

How Transfer Learning Works

Pre-trained Model Layer 1: Edges Layer 2: Textures Layer 3: Patterns Layer 4: Parts Classify 1000 objects Adapt Your Model Layer 1: Edges ✓ Layer 2: Textures ✓ Layer 3: Patterns ✓ Layer 4: Parts ✓ Classify bird/forest

Key Point: We keep the learned features (edges, textures, patterns) and only retrain the final layer for our specific task.

12 / 30

Step 4: Train the Model

# Fine-tune the model for 3 epochs (3 complete passes through data) learn.fine_tune(3)

This single line of code:

Training Time: On a GPU, this typically takes 2-3 minutes. Without GPU, it may take 10-15 minutes.

13 / 30

Understanding Training Output

During training, you'll see a table like this:

Epoch Train Loss Valid Loss Error Rate Time
0 0.823145 0.345632 0.125000 00:42
1 0.412389 0.198234 0.087500 00:38
2 0.298176 0.156789 0.062500 00:39

Train Loss: How wrong the model is on training data (lower is better)

Valid Loss: How wrong the model is on validation data (lower is better)

Error Rate: Percentage of images classified incorrectly (0.062500 = 6.25% wrong = 93.75% accurate!)

14 / 30

Quiz 2: Interpreting Results

You train a model and get these results: Train Loss = 0.12, Valid Loss = 0.89, Error Rate = 0.35. What's the most likely problem?
  • A. The model needs more training epochs
  • B. The model is overfitting (memorizing training data, not generalizing)
  • C. The model is performing perfectly
  • D. The learning rate is too high
15 / 30

Step 5: Use Your Model

# Make a prediction on a new image img = PILImage.create('test_image.jpg') prediction, idx, probabilities = learn.predict(img) print(f"This is a: {prediction}") print(f"Probability: {probabilities[idx]:.2%}")

Example Output:

This is a: bird
Probability: 94.73%

Congratulations! You've just built, trained, and used a deep learning model. This same workflow applies to countless real-world applications.

16 / 30

PART 2

Understand It

What's really happening under the hood?

17 / 30

The Training Loop: How Models Learn

Input Image Neural Network Prediction Calculate Loss (how wrong?) Update Weights True Label: "bird" Repeat thousands of times...

Each iteration through this loop is called a training step. An epoch is one complete pass through all training images.

18 / 30

What is a Neural Network?

A neural network is a mathematical function with millions of adjustable parameters (weights).

The Analogy

Think of it like a complex curve-fitting machine:

  • Inputs: pixel values from images
  • Outputs: probability for each category
  • Weights: adjusted during training to match inputs to correct outputs

ResNet18 Specifically

  • 18 layers deep
  • ~11 million parameters
  • Trained on 1.2M images
  • Top-5 accuracy: 93.6% on ImageNet

Why it works: Deep networks can learn hierarchical representations - simple features in early layers (edges) combine to form complex features in later layers (objects).

19 / 30

Understanding Loss Functions

A loss function measures how wrong the model's predictions are.

Example: Predicting "bird"

Good Prediction

Model outputs:

bird: 95%

forest: 5%

Loss = 0.051 (low)

Bad Prediction

Model outputs:

bird: 20%

forest: 80%

Loss = 1.609 (high)

Goal of training: Adjust the weights to minimize the loss across all training examples.

20 / 30

Quiz 3: Transfer Learning Concept

Why does transfer learning work so well for image classification?
  • A. It's faster because we skip the training phase entirely
  • B. Early layers learn general features (edges, textures) useful for many tasks, so we only need to retrain the final classification layer
  • C. ImageNet contains all possible images, so the model already knows the answer
  • D. It only works if your task is very similar to ImageNet's 1000 categories
21 / 30

When to Use Deep Learning

Deep learning excels when:

Good Fit ✓

  • Pattern recognition in complex data
  • Large amounts of data available
  • Difficult to manually define rules
  • Images, text, audio, video
  • Prediction or classification tasks
Examples: Medical image diagnosis, speech recognition, recommendation systems, autonomous driving

Poor Fit ✗

  • Very small datasets (<100 examples)
  • Requires strict logical rules
  • Needs explicit explanations
  • Perfect accuracy required
  • Simple statistical relationships
Examples: Tax calculations, database queries, simple sorting, rule-based systems
22 / 30

PART 3

Apply It

Practical guidance for your own projects

23 / 30

Myth Busting: What You DON'T Need

MYTH #1: You need advanced mathematics
REALITY:

Week 1-3: No math at all. Week 4-7: Basic derivatives (high school calculus). Week 8+: More advanced topics for those interested. You can build and deploy models without deep mathematical knowledge.

MYTH #2: You need massive datasets
REALITY:

Transfer learning allows excellent results with 200-500 images per category. Research shows 99%+ accuracy achieved on specialized tasks with just 200 images using pre-trained models.

MYTH #3: You need expensive hardware
REALITY:

Free options include Google Colab (free GPU access) and Kaggle (30 hours/week free GPU). Training our bird classifier: GPU = 3 minutes, CPU = 15 minutes.

24 / 30

How to Know If Your Model is Working

Good Signs ✓

Training loss decreasing steadily Validation loss similar to training loss Error rate < 15% for 2-class problem Predictions make sense on test images

Warning Signs ⚠

Training loss very low, validation loss high (overfitting) Both losses not decreasing (underfitting) Error rate > 40% (random guessing) Weird predictions on simple test cases

Rule of thumb: For a 2-class problem, aim for <10% error rate. For 10 classes, <20% error rate is reasonable to start.

25 / 30

Quiz 4: Debugging Model Performance

Your model achieves 99% accuracy on training data but only 60% on validation data. What should you try FIRST?
  • A. Train for many more epochs
  • B. Add data augmentation or reduce model complexity (the model is overfitting)
  • C. Use a larger, more complex model
  • D. Decrease the learning rate
26 / 30

Troubleshooting Common Errors

Error: "CUDA out of memory"

Solution: Reduce batch size. Change bs=32 to bs=16 or bs=8 in your DataLoaders.

Error: "No module named 'fastai'"

Solution: Install the library: !pip install fastai

Error: "Cannot find images" or "0 images found"

Solution: Check your folder structure. Images should be in subfolders named after their categories (e.g., bird_or_not/bird/, bird_or_not/forest/).

Error: Model accuracy is ~50% and not improving

Solution: Your model is randomly guessing. Check: (1) Labels are correct, (2) Images match labels, (3) Enough training epochs, (4) Learning rate not too high.

27 / 30

Adapting This Code for Your Own Project

To classify your own images, change only these parts:

# 1. Define YOUR categories searches = 'category1', 'category2', 'category3' # ← Change this # 2. Choose YOUR folder name path = Path('my_classifier') # ← Change this # Everything else stays the same! dbs = DataBlock( blocks=(ImageBlock, CategoryBlock), get_items=get_image_files, splitter=RandomSplitter(valid_pct=0.2, seed=42), get_y=parent_label, item_tfms=[Resize(224, ResizeMethod.Squish)] ) dls = dbs.dataloaders(path, bs=32) learn = vision_learner(dls, resnet18, metrics=error_rate) learn.fine_tune(3)

Ideas: Classify plant species, identify defects in manufacturing, sort documents, recognize handwritten digits, detect medical conditions from X-rays.

28 / 30

Quiz 5: Real-World Application

A hospital wants to classify chest X-rays as "normal" or "pneumonia". They have 300 X-rays of each type. Is deep learning appropriate here?
  • A. No - too few images for deep learning
  • B. No - medical applications require perfect accuracy which DL cannot provide
  • C. Yes - transfer learning can work well with 300 images per class, but should be used as an assistive tool with human verification
  • D. No - X-rays are too different from natural images for transfer learning
29 / 30

Your Assignment: Build Your Own Classifier

Due before next week's class

Train a classifier on a topic of YOUR choice. It must classify at least 2 categories.

Requirements:

  • Use the code from today's notebook
  • Choose your own categories (be creative!)
  • Download at least 150 images per category
  • Train the model and record your error rate
  • Test on 3-5 new images you find online
  • Post your results on the discussion forum

Resources:

• Course notebook: Available on LearnJCU

• Free GPU access: Google Colab or Kaggle

• FastAI documentation: docs.fast.ai

• Discussion forum: For questions and sharing results

30 / 30