<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nikita Gupta</title>
    <description>The latest articles on DEV Community by Nikita Gupta (@nikitagpt05).</description>
    <link>https://dev.to/nikitagpt05</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4017938%2F1fa055fc-20e2-40ea-9490-1918c73241a8.jpg</url>
      <title>DEV Community: Nikita Gupta</title>
      <link>https://dev.to/nikitagpt05</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nikitagpt05"/>
    <language>en</language>
    <item>
      <title>Why 97% Accuracy is a Lie: My Journey from ANN to CNN</title>
      <dc:creator>Nikita Gupta</dc:creator>
      <pubDate>Mon, 06 Jul 2026 15:34:15 +0000</pubDate>
      <link>https://dev.to/nikitagpt05/why-97-accuracy-is-a-lie-my-journey-from-ann-to-cnn-458</link>
      <guid>https://dev.to/nikitagpt05/why-97-accuracy-is-a-lie-my-journey-from-ann-to-cnn-458</guid>
      <description>&lt;p&gt;You know the feeling. Your model trains perfectly, the validation loss drops, your accuracy hits a 97.5% on the MNIST dataset and it feels like it might work fine.&lt;/p&gt;

&lt;p&gt;Then you build a real-time UI, test it yourself, and your model completely falls apart.&lt;/p&gt;

&lt;p&gt;That is exactly what happened with my latest project: a real-time handwritten digit recognizer built with PyTorch and Pygame.Here is how my "perfect" Artificial Neural Network (ANN) failed in the real world, and the engineering steps I took to fix it.&lt;/p&gt;

&lt;p&gt;The Setup&lt;br&gt;
The goal was simple. Build a black Pygame canvas where a user can draw a number (0-9) with their mouse, and have a PyTorch model predict the number the moment they release the click.&lt;/p&gt;

&lt;p&gt;I started with a standard flattened ANN. It trained. But the second I drew a number on the screen, it guessed wrong almost every time.Especially numbers like 6,7,8,9.&lt;/p&gt;

&lt;p&gt;The Problem: Domain Shift&lt;br&gt;
The issue was Domain Shift.&lt;/p&gt;

&lt;p&gt;An ANN flattens a 28x28 image into a 1D array of 784 pixels. It doesn't actually understand shapes; it just memorizes where bright pixels should be.&lt;/p&gt;

&lt;p&gt;The Dataset: The MNIST training data has perfectly centered, thin pen strokes.&lt;br&gt;
The Real World: My Pygame digital brush was thick. My hand jittered. The numbers weren't perfectly centered.&lt;/p&gt;

&lt;p&gt;Because the lines were thicker, the pixels didn't perfectly align with the ANN's expectations, and it completely failed to recognize the numbers.&lt;/p&gt;

&lt;p&gt;The Experiment: The "Model Arena"&lt;br&gt;
To fix this, I decided to upgrade to a Convolutional Neural Network (CNN), which actually looks for edges, curves, and loops using spatial filters.&lt;/p&gt;

&lt;p&gt;But I wanted to prove it was better. So, I built a Model Arena: a script that ran both the ANN and the new CNN side-by-side on the exact same drawing. I even built an Ensemble feature to average their probabilities.&lt;/p&gt;

&lt;p&gt;The result? The CNN broke too. It started guessing the number 8 for almost everything.&lt;/p&gt;

&lt;p&gt;The Fix: Normalization &amp;amp; Optimization&lt;br&gt;
It turns out, the architecture wasn't the only issue. It was the math.&lt;/p&gt;

&lt;p&gt;During training, I normalized the PyTorch dataset to a range of [-1, 1]. But my Pygame image pre-processing was scaling the pixels from [0, 1]. The CNN was receiving numbers it didn't expect, panicking, and defaulting to 8 (the mathematical center of the dataset).&lt;/p&gt;

&lt;p&gt;Once I spotted the bug, the fix was incredibly satisfying. I updated the Pygame tensor math.&lt;br&gt;
I also reduced the brush thickness to mimic a real pen, and dropped the random rotation in my training data augmentation from 15° to 5° so the shapes wouldn't distort too heavily.&lt;/p&gt;

&lt;p&gt;The Takeaway&lt;br&gt;
After those fixes, the CNN's accuracy skyrocketed. It easily recognized my live handwriting, completely outperforming the ANN (which I ended up deleting entirely).&lt;/p&gt;

&lt;p&gt;The biggest lesson? A high accuracy score on a pristine dataset means absolutely nothing if your live environment doesn't perfectly match your training environment.&lt;/p&gt;

&lt;p&gt;You can check out the final, fully optimized CNN architecture on my GitHub:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/nikitagpt05" rel="noopener noreferrer"&gt;
        nikitagpt05
      &lt;/a&gt; / &lt;a href="https://github.com/nikitagpt05/Handwritten-Digit-Recognizer" rel="noopener noreferrer"&gt;
        Handwritten-Digit-Recognizer
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Handwritten Digit Recognizer (PyTorch &amp;amp; Pygame)&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;This is a real-time app that guesses the numbers you draw on the screen. It is built with PyTorch and Pygame. This project shows how a machine learning model can perform perfectly on standard training data but struggle with real-world inputs, and how changing the underlying architecture fixes those issues.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;How the Project Evolved&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;I went through three main testing phases to get the accuracy right for live, interactive drawings.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. The First Attempt (ANN)&lt;/strong&gt;
I started by building a basic Artificial Neural Network (ANN). It scored about 97.5% accuracy on the standard MNIST dataset. But when I drew numbers with my mouse in the actual app, it failed constantly. An ANN flattens an image into a single line of pixels, so it does not actually understand shapes, edges, or curves. If I used a thick digital brush or drew the number slightly off-center…&lt;/p&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/nikitagpt05/Handwritten-Digit-Recognizer" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Have you ever had a model fail in production after passing all your tests? Let me know in the comments!&lt;/p&gt;

</description>
      <category>deeplearning</category>
      <category>machinelearning</category>
      <category>python</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
