DEV Community

Cover image for From Pixels to Predictions: Building Your First ML Classifier A dev-friendly intro to image classification using deep learning.
Darkstalker
Darkstalker

Posted on

From Pixels to Predictions: Building Your First ML Classifier A dev-friendly intro to image classification using deep learning.

"Machine learning isn’t magic. It’s just math applied with care."

If you've ever wondered how an app can look at a picture and decide whether it's a dog or not welcome. This post is a gentle but thorough walkthrough of how you'd actually build a simple image classifier.

We’ll also sprinkle in a few mental models, best practices, and resources so you build good priors for your ML journey.

Step 1: Frame the Problem

We want to answer: "Given an image, is this a dog?"

That’s a binary classification problem. We're not trying to recognize the breed, or count how many dogs just yes or no.

This is a supervised learning task: the model learns from labeled examples ("dog" / "not dog").

ML Tip: Ask yourself is this binary, multiclass, or multilabel? Are labels ambiguous? What kind of mistakes do you care about more?

Step 2: Get and Understand the Data

You can’t train a model without good data.

Use a dataset like:
Stanford Dogs Dataset
Kaggle Dogs vs. Cats

Clean it up:
Resize images
Normalize pixel values
Make sure you have balanced examples (dogs and non-dogs)

Split into:
Train (e.g. 70%)
Validation (15%)
Test (15%)

Data is the real currency of ML. Always treat it with respect.

Step 3: Choose a Model

We need a model that works well on images. Enter: Convolutional Neural Networks (CNNs).

To keep it simple, use transfer learning:
Load a pretrained CNN (e.g., ResNet-18)
Replace the final layer
Fine-tune it on your dataset

PyTorch makes this easy:
import torchvision.models as models
model = models.resnet18(pretrained=True)
model.fc = nn.Linear(model.fc.in_features, 1) # Binary classifier

Step 4: Train the Model

Use Binary Cross-Entropy Loss
Optimizer: Adam (lr=1e-4)
Batch size: 32 or 64
Train for 10–20 epochs

Log:
Loss
Accuracy
Precision/Recall/F1
Use early stopping and save the best model.

Training is where your model learns, but evaluation is where you learn.

Step 5: Evaluate and Improve

Don't just look at accuracy. Dive deeper:
Confusion matrix
Precision/Recall
Examples it got wrong (and why!)

Create a test set with edge cases:
Dogs in costumes
Blurry images
Not-quite-a-dog images (foxes, wolves, etc.)

Step 6: Deploy the Model

Use something like FastAPI:
Load the trained model

Create an endpoint: /predict
Accept image uploads
Return prediction: yes/no + confidence

{
  "input": "dog.jpg",
  "output": {
    "class": "dog",
    "confidence": 0.94
  }
}
Enter fullscreen mode Exit fullscreen mode

Bonus: Dockerize it, add logging, and boom — you've got an ML microservice.

Mental Models for ML

"Bad data beats good models." Clean your data.
"All models are wrong, some are useful." Focus on real-world value.
"Every ML model is a hypothesis." Validate it against reality.

Continue Learning
If you're excited to go deeper, here are some curated resources:

Foundational
Deep Learning with PyTorch (free book):https://www.bing.com/ck/a?!&&p=b95a304773387304460e4addd2d0d3061188c99a88cf69fc3c4060a16a5a1d4eJmltdHM9MTc0OTQyNzIwMA&ptn=3&ver=2&hsh=4&fclid=11f78f6f-4226-66ca-395a-9afb432267e8&psq=Deep+Learning+with+PyTorch&u=a1aHR0cHM6Ly93d3cubGVhcm5weXRvcmNoLmlvLw&ntb=1

C0S231n: Convolutional Neural Networks for Visual Recognition
Practical: https://www.bing.com/ck/a?!&&p=abb2ed38507c82f845bab881da4858194c16f0c26d1ca28ed4ca061d16f4f246JmltdHM9MTc0OTQyNzIwMA&ptn=3&ver=2&hsh=4&fclid=11f78f6f-4226-66ca-395a-9afb432267e8&psq=C0S231n%3a+Convolutional+Neural+Networks+for+Visual+Recognition+Practical&u=a1aHR0cHM6Ly9jczIzMW4uc3RhbmZvcmQuZWR1Lw&ntb=1

fast.ai's Practical Deep Learning: https://www.bing.com/ck/a?!&&p=555d78b7e1231143e269ce360005c8f207d3c261ef88ee5043e2daa2136b378aJmltdHM9MTc0OTQyNzIwMA&ptn=3&ver=2&hsh=4&fclid=11f78f6f-4226-66ca-395a-9afb432267e8&psq=fast.ai%27s+Practical+Deep+Learning&u=a1aHR0cHM6Ly9jb3Vyc2UuZmFzdC5haS8&ntb=1

Kaggle Learn: Computer Vision:https://www.bing.com/ck/a?!&&p=dfe4d5b9f4f515e41c5ef93e0e8617bc67e89e4985b1b1210282cff8d7489f64JmltdHM9MTc0OTQyNzIwMA&ptn=3&ver=2&hsh=4&fclid=11f78f6f-4226-66ca-395a-9afb432267e8&psq=Kaggle+Learn%3a+Computer+Vision+&u=a1aHR0cHM6Ly93d3cua2FnZ2xlLmNvbS9sZWFybi9jb21wdXRlci12aXNpb24&ntb=1

Papers with Code – Image Classification: https://www.bing.com/ck/a?!&&p=23f1094ad7f47bdc825716ee4fa1585f249318f01941d9f4e474e02967984c48JmltdHM9MTc0OTQyNzIwMA&ptn=3&ver=2&hsh=4&fclid=11f78f6f-4226-66ca-395a-9afb432267e8&psq=Papers+with+Code+%e2%80%93+Image+Classification&u=a1aHR0cHM6Ly9wYXBlcnN3aXRoY29kZS5jb20vdGFzay9pbWFnZS1jbGFzc2lmaWNhdGlvbg&ntb=1

Distill.pub – Understanding Neural Networkshttps://www.bing.com/ck/a?!&&p=6fe476bbb2ab923280fe0db4ee1734aaa5d5bf6ce662b6348b1c59e3ca5a4234JmltdHM9MTc0OTQyNzIwMA&ptn=3&ver=2&hsh=4&fclid=11f78f6f-4226-66ca-395a-9afb432267e8&psq=Distill.pub+%e2%80%93+Understanding+Neural+Networks&u=a1aHR0cHM6Ly9kaXN0aWxsLnB1Yi8yMDIxL2dubi1pbnRyby8&ntb=1

A Visual Intro to ML: https://www.bing.com/ck/a?!&&p=98c6efc8a7ef7a95069de441e56c9f510fceec87fe54dbe923d95f31d808ed26JmltdHM9MTc0OTQyNzIwMA&ptn=3&ver=2&hsh=4&fclid=11f78f6f-4226-66ca-395a-9afb432267e8&psq=A+Visual+Intro+to+ML&u=a1aHR0cDovL3d3dy5yMmQzLnVzL3Zpc3VhbC1pbnRyby10by1tYWNoaW5lLWxlYXJuaW5nLXBhcnQtMS8&ntb=1

TL;DR
Step:
What You Do

  1. Define the problem (classification? regression?)

  2. Get clean, labeled data

  3. Choose a model (CNNs + transfer learning)

  4. Train and evaluate rigorously

5.Deploy with a web API

Final Thought
Learning ML is a journey — one where the map is fuzzy, but the destination is worth it. Start simple, stay curious, and keep shipping things that work.

If this helped you, consider following me for more ML breakdowns and engineering insights. Questions or feedback? Drop them in the comments!

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more