# How to Build Your Own Image Recognition Model as Your First AI Project > After a month buried in AI maths, I wanted to build something real. This is the full journey of training a CNN on CIFAR-10 and serving it as a web app, including every question I had and every mistake I made. - Author: Yadunandan Singh (https://yadunandan-singh.pages.dev/) - Published: 2025-09-24 · updated 2026-10-19 - URL: https://yadunandan-singh.pages.dev/blog/first-image-recognition-model/ - Topics: Machine Learning, Computer Vision, Beginners - First published on Medium: https://medium.com/@yadunandan-ai-dev/how-can-you-build-your-own-image-recognition-model-as-your-first-ai-project-090a20c3ec3e ## Summary Yadunandan Singh documents his first hands-on AI project: a convolutional neural network trained on CIFAR-10 with TensorFlow and Keras and served through a Flask web app. The guide covers how images become 32 × 32 × 3 tensors, normalisation, data augmentation, three Conv2D and pooling blocks, dropout, and the compile and fit settings, plus a 1,563-epoch, eight-hour experiment in which strong augmentation and dropout kept validation accuracy improving instead of overfitting. ## Key takeaways - A colour image is a 32 × 32 × 3 tensor of pixel values; scale them to 0–1 before training. - Data augmentation and dropout are the main defences against overfitting. - Stack Conv2D and MaxPooling2D blocks to detect patterns, then Flatten, Dense and softmax to classify. - Use sparse categorical cross-entropy for integer labels and categorical cross-entropy for one-hot labels. - Serve the model by giving uploads exactly the same preprocessing as the training data. --- For a solid month I was deep in the maths behind AI: equations, formulas, algorithms, one practice session after another. Then a question started bothering me. **What good is all this theory if I can't use it to build something?** That changed my plan. I swapped endless practice for a new rule: a hands-on project every weekend. This was the first. I built an image classifier from scratch and served it as a web app you can drop a photo into. It didn't just teach me how to code a model. It taught me how to *think* like an AI engineer. This post documents that whole journey: every question I had, every mistake I made, and every lesson I took from it, so your path can be smoother than mine. All you need is some basic Python. The final code is on [GitHub](https://github.com/YADUNANDAN-SINGH/AI-Image-Recognition-webapp). ## Part 1: How a computer "sees" an image Computers work in numbers. So how does a rich, colourful photo become something a computer can reason about? Start with a black-and-white picture. To a computer it's a grid of tiny dots called **pixels**, and each pixel is a single number for its brightness, from **0 (pure black)** to **255 (pure white)**. A picture is just a big matrix of numbers. Colour adds one twist. Instead of one number per pixel, there are three: one each for **red**, **green** and **blue (RGB)**, each from 0 to 255. Mixing them gives millions of colours. So a small 32 × 32 colour image isn't a 2D grid any more; it's a 3D block of numbers with the shape **32 × 32 × 3**. In AI, a block of numbers like this is called a **tensor**. The dataset I used, CIFAR-10, is exactly that: 60,000 tiny 32 × 32 colour images in ten classes: aeroplanes, cars, birds, cats, deer, dogs, frogs, horses, ships and lorries. ### My first question: why divide by 255.0 and not 255? My first instinct was to feed those raw numbers straight into a model. But raw data is rarely in the right shape for learning, and the first preparation step is **normalisation**. Neural networks learn best when their inputs are small, so we scale every pixel from the 0–255 range down to 0–1 by dividing by 255: ```python (x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 ``` Which led to my question: why `255.0`, with the decimal point? Training works by making millions of tiny, precise adjustments, and that needs fractional values, so the result *must* be floating point: 191 should become 0.749, not 0. In Python 3 the `/` operator always returns a float, so `/ 255` would actually work too. But in languages with integer division, or with Python's own `//`, 191 divided by 255 is 0, and your whole image silently turns black. Writing `255.0` makes the intent impossible to miss. ## Part 2: Designing the model's brain With the image as a tensor, how do we teach a model to understand it? We use a **convolutional neural network (CNN)**. A CNN doesn't try to understand the whole image at once. Its trick is to first learn small, reusable patterns (edges, corners, textures) and then learn how those patterns combine into bigger things: an ear, a wheel, a wing. ### My toolkit: TensorFlow and Keras At first I couldn't tell these two apart. The way I think of it now: **TensorFlow** is the workshop, the engine that does the heavy maths. **Keras** is a friendly toolkit inside that workshop, which lets you build a model by stacking layers like LEGO bricks. ### Building the model, one brick at a time **1. The foundation: a `Sequential` model.** Think of the flat green LEGO base plate you start with. It doesn't do anything by itself, but it holds every layer you add, in order, one on top of the next. **2. The anti-memorisation machine: data augmentation.** A huge trap in AI is **overfitting**: the model memorises the training images instead of learning what makes a cat a cat. To fight it, my model starts with three augmentation layers, `RandomFlip`, `RandomRotation` and `RandomZoom`. During training they feed the network slightly altered versions of every image, so it can never just memorise the exact pixels. **3. The eyes: `Conv2D` and `MaxPooling2D`.** This is the core of the CNN. - `Conv2D` is the pattern detector. My question here was what the numbers in it, like 32 and 64, mean. They're the number of **filters**: `filters=32` means hiring 32 detectives, each trained to find one particular visual pattern. - `MaxPooling2D` is the efficiency manager. It shrinks the output of the layer before it, keeping only the strongest signals, which makes the model faster and helps it recognise a pattern wherever it appears in the image. **A mistake I made:** at first I tried to put `MaxPooling2D` *inside* the `Conv2D` call. It doesn't work that way. In a `Sequential` model each layer is its own brick, added separately, one after another. **4. The brain: `Flatten`, `Dense` and `Dropout`.** - `Flatten` is a simple but essential bridge: it unrolls the grid of detected patterns into one long line of numbers. - `Dense` is a classic decision-making layer that weighs all of those signals together. - `Dropout` is a second weapon against overfitting. During training it randomly switches off half of the connections, so the model can't lean too heavily on any single one. - The final `Dense` layer has one unit per class (10 for CIFAR-10) and a **softmax** activation, which turns raw scores into probabilities that add up to 1. Here's the complete model from my project: ```python model = Sequential([ layers.RandomFlip("horizontal", input_shape=(32, 32, 3)), layers.RandomRotation(0.1), layers.RandomZoom(0.1), layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu'), layers.MaxPooling2D(pool_size=(2, 2)), layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'), layers.MaxPooling2D(pool_size=(2, 2)), layers.Conv2D(filters=128, kernel_size=(3, 3), activation='relu'), layers.MaxPooling2D(pool_size=(2, 2)), layers.Flatten(), layers.Dense(units=128, activation='relu'), layers.Dropout(0.5), layers.Dense(units=10, activation='softmax') ]) ``` Augmentation first, then three rounds of "look for patterns, then shrink", then the decision-making layers. ## Part 3: Bringing the model to life With the architecture designed, it was time for the exciting part: training, where an empty network becomes a working pattern detector. But first the model needs a learning strategy. ### The pre-flight check: compile `model.compile()` configures how the model learns. The terms confused me at first, so here they are in plain words: ```python model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) ``` - **Optimizer** is the engine of learning: the algorithm that decides how to adjust the model's internal weights to reduce its mistakes. `adam` is a smart, efficient, all-round choice. - **Loss** is the grading system that measures how wrong each prediction is; the optimizer's whole job is to push it down. I used `sparse_categorical_crossentropy` because my labels are plain integers, like 3 for "cat". With one-hot labels, like `[0,0,0,1,0,0,0,0,0,0]`, you'd use `categorical_crossentropy` instead. - **Metrics** are for us humans. The loss is what the model learns from, but **accuracy**, the share of images classified correctly, is much easier to read. ### Liftoff: fit Training starts with `model.fit()`, and two more terms matter here: - An **epoch** is one complete study session, in which the model sees every training image once. - A **batch** is the small group of images the model looks at before each update; Keras uses 32 by default. With 50,000 training images, that's 50,000 ÷ 32 ≈ **1,563 steps** per epoch. When `fit()` runs, the terminal comes alive with a report card for every epoch. A line such as: ```output Epoch 32/70 … accuracy: 0.8423 - val_accuracy: 0.7263 ``` says that after 32 epochs the model gets 84% of the *training* images right, and about 73% of images it has never seen (`val_accuracy`). That second number is the one that tells you whether it has actually learned. ## My big experiment: 1,563 epochs The concept I most wanted to understand was **overfitting**. An overfit model is like a student who memorises the textbook's answers: near-perfect on questions it has seen, lost on anything new. I'd started out asking, "How do I get 100% accuracy?" Theory says that training for far too long is the quickest way to overfit, and I wanted to see that happen for myself. So I ran an extreme experiment: **1,563 epochs**, about eight hours of training, fully expecting the model to fall apart. ![Terminal output from Keras showing epochs 1 to 3 of 1,563, each 1,563 steps at about 10 ms per step, with accuracy and validation accuracy rising.](https://yadunandan-singh.pages.dev/blog/first-image-recognition-model/training-log.webp "The start of the long run: epoch 3 of 1,563, with validation accuracy already climbing.") It didn't. The validation accuracy kept improving slowly, even after hundreds of epochs. The lesson surprised me. Data augmentation and dropout made a strong anti-memorisation team. Training was so varied, with a new flip, rotation and zoom of every image, and half the connections dropped at every step, that the model *couldn't* simply memorise the data. Instead it benefited from the extra time and learned more general patterns. No textbook could have shown me that as clearly as watching it happen. ## Part 4: Serving it as a web app A model in a notebook isn't a product, so the last step was to put it behind a website. The backend is **Flask**. It loads the saved model once and exposes a `/predict` endpoint. Every uploaded image goes through the same preparation as the training data, because a model only understands inputs that look like what it learned from: ```python img = Image.open(io.BytesIO(image_data)) if img.mode != 'RGB': img = img.convert('RGB') img = img.resize((32, 32), Image.Resampling.LANCZOS) # same size as CIFAR-10 img_array = np.array(img, dtype=np.float32) / 255.0 # same 0–1 scale as training img_array = np.expand_dims(img_array, axis=0) # a "batch" of one image ``` The endpoint returns the model's **top three guesses with their confidence**, and the frontend, plain HTML, CSS and JavaScript, lets you drag and drop an image and see the result straight away. ## What I'd tell you before you start This project took me from the depths of theory to a working app, and the most important thing it taught me is simple: **the best way to understand AI is to build it, break it and fix it yourself.** Pick a small dataset, write every layer yourself, and run an experiment you expect to fail. You'll learn more from the result than from another week of reading. And if you want a gentler next step into the maths, my post on [building a matrix transformation visualiser](https://yadunandan-singh.pages.dev/blog/linear-algebra-transformation-visualizer/) is where I went next.