<?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: Wojciech Bubółka</title>
    <description>The latest articles on DEV Community by Wojciech Bubółka (@lonwi).</description>
    <link>https://dev.to/lonwi</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%2F670609%2Faa5f872f-b078-4c74-b443-34af10d31caf.jpeg</url>
      <title>DEV Community: Wojciech Bubółka</title>
      <link>https://dev.to/lonwi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lonwi"/>
    <language>en</language>
    <item>
      <title>Why 92.5% Accuracy Still Failed My Ship Gates: Building a Safety-Critical Offline Mushroom Classifier</title>
      <dc:creator>Wojciech Bubółka</dc:creator>
      <pubDate>Sat, 28 Nov 2026 11:13:16 +0000</pubDate>
      <link>https://dev.to/lonwi/why-925-accuracy-still-failed-my-ship-gates-building-a-safety-critical-offline-mushroom-4n1n</link>
      <guid>https://dev.to/lonwi/why-925-accuracy-still-failed-my-ship-gates-building-a-safety-critical-offline-mushroom-4n1n</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Safety Disclaimer:&lt;/strong&gt; This project is an &lt;strong&gt;educational machine learning case study and taxonomic assistance tool&lt;/strong&gt;, &lt;strong&gt;NOT&lt;/strong&gt; an autonomous edibility oracle. A computer vision model operating solely on 256×256 RGB photographs &lt;strong&gt;must never&lt;/strong&gt; be used to decide whether a wild mushroom is safe to eat. In real-world foraging, look-alike species can differ only by microscopic spores, smell, bruising reactions, or habitat.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My offline mushroom classifier reached &lt;strong&gt;92.49% accuracy on the 1,424 of 1,891 multi-photo test observations (75.30%) that passed every gate&lt;/strong&gt;. It caught &lt;strong&gt;95.53%&lt;/strong&gt; of deadly Death Caps (&lt;em&gt;Amanita phalloides&lt;/em&gt;), and multi-photo fusion cut toxic-as-edible mistakes on the same observations from &lt;strong&gt;32&lt;/strong&gt; (first frame only, same single-frame gates) to &lt;strong&gt;9&lt;/strong&gt;. My own automated Ship Gates still returned &lt;strong&gt;&lt;code&gt;FAIL / BLOCKED&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That wasn't a setback. When a single wrong label can cause fatal liver failure, the most valuable thing a test pipeline can show you is where computer vision hits its physical limit, and where product UX safety has to take over.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Build Another Mushroom App?
&lt;/h2&gt;

&lt;p&gt;If you've ever found a mushroom you don't recognize in the middle of a forest with weak signal, you know how frustrating it gets. Ask a cloud AI chat what it is, and you wait ten seconds just to get a wall of text instead of a straight answer. Dedicated mushroom apps often come with paywalls, ads, clunky UIs, or "offline" modes that still freeze trying to load data with no internet. Even when they work, they rarely give me what I actually want on the spot: &lt;em&gt;is this worth checking closer, what exact features should I look at, how do I tell it apart from toxic look-alikes, plus local info like season, history, or how to cook it?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That's also why a local model makes sense even if it never says "Edible!". It isn't meant to decide for you. It's a fast &lt;strong&gt;field helper&lt;/strong&gt;. Its job is to take 67 species (for now), narrow them down to the top candidates in milliseconds with zero signal, and let a clean UI guide &lt;em&gt;you&lt;/em&gt; through checking the details and dangerous look-alikes manually.&lt;/p&gt;

&lt;p&gt;On a personal level, this isn't a commercial product. I've spent the last decade working as a frontend developer in SaaS, and between work and raising kids, I fell out of the loop with side projects. I wanted to learn by building something hands-on again: see how vision models, calibration, and on-device optimization actually work under the hood, test modern AI tooling, and find out how far I can push my own laptop without relying on a cloud API.&lt;/p&gt;

&lt;p&gt;That curiosity quickly turned into a much deeper safety challenge for &lt;strong&gt;Grzybobranie AI&lt;/strong&gt;, an offline-first React Native + Expo app running locally via &lt;code&gt;react-native-fast-tflite&lt;/code&gt;. &lt;em&gt;(Why **Grzybobranie AI&lt;/em&gt;&lt;em&gt;? It roughly translates to "mushroom picking", since I'm from Poland and focusing on local European species for now.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here is what happened when I tested my first laptop-trained model against strict release gates.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Constraints &amp;amp; Keeping the Test Set Untouched
&lt;/h2&gt;

&lt;p&gt;When building an offline app for mid-range Android phones with no mobile signal, plain accuracy is the wrong metric to chase. A 95% accurate model that confidently labels a deadly &lt;em&gt;Galerina marginata&lt;/em&gt; as an edible &lt;em&gt;Kuehneromyces mutabilis&lt;/em&gt; is a serious failure.&lt;/p&gt;

&lt;p&gt;To keep the project honest from day one, I set five strict rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Fixed set of 67 species in the catalog, 66 active classes (no "unknown" bucket):&lt;/strong&gt; The catalog has 67 species; after &lt;em&gt;A. verna&lt;/em&gt; is excluded (explained in section 2), the model has 66 active classes and no &lt;code&gt;"not_a_mushroom"&lt;/code&gt; or &lt;code&gt;"unknown"&lt;/code&gt; output class. Out-of-distribution (&lt;strong&gt;OOD&lt;/strong&gt;) photos like grass, shoes, or unlisted mushrooms have to be rejected mathematically using the model's raw scores (&lt;strong&gt;logits&lt;/strong&gt;, the raw numbers a neural network outputs before converting them into percentages).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tracking dangerous look-alikes:&lt;/strong&gt; 27 toxic or deadly &lt;code&gt;high_stakes&lt;/code&gt; species and 44 dangerous look-alike pairs are tracked explicitly in a dedicated config file.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hardware &amp;amp; speed limits:&lt;/strong&gt; Trained locally on an NVIDIA RTX 5070 Laptop GPU (8 GB &lt;strong&gt;VRAM&lt;/strong&gt;, &lt;em&gt;Video Random Access Memory&lt;/em&gt;) using &lt;code&gt;bfloat16&lt;/code&gt;, targeting mobile CPU inference under 2 seconds and a model file under 20 MB.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Untouched test set:&lt;/strong&gt; The main test split (13,051 images) and &lt;code&gt;ood_test&lt;/code&gt; (2,230 images) stayed 100% locked during training, calibration, threshold tuning, and multi-photo experiments. I only ran the final audit once, after freezing every single setting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fail-closed release gates (Ship Gates):&lt;/strong&gt; If any of these checks fail, the model does not ship to production:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt; toxic mushrooms confidently labeled as edible&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;≤ 5%&lt;/code&gt; confusion on any dangerous look-alike pair&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;≥ 85%&lt;/code&gt; &lt;strong&gt;recall&lt;/strong&gt; (out of all real toxic mushrooms in the test set, how many we actually caught) on every toxic/deadly species&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;≤ 2%&lt;/code&gt; of OOD images slipping through as "confident edible"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;≥ 99%&lt;/code&gt; &lt;strong&gt;Top-1 agreement&lt;/strong&gt; (the #1 predicted species matches) between PyTorch and TFLite&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  2. Breaking the I/O Bottleneck &amp;amp; Scaling to 256×256
&lt;/h2&gt;

&lt;p&gt;My first baseline used &lt;strong&gt;&lt;code&gt;mobilenetv4_conv_small&lt;/code&gt;&lt;/strong&gt; at &lt;code&gt;224×224&lt;/code&gt; across &lt;code&gt;60,413&lt;/code&gt; training images and &lt;code&gt;13,076&lt;/code&gt; validation images (all 67 classes; 158 of the 13,234 rows in &lt;code&gt;val.csv&lt;/code&gt; had no image file on disk, leaving 13,076 images). On the RTX 5070, 23 epochs took ~43.5 minutes while using barely &lt;strong&gt;~607 MB of VRAM&lt;/strong&gt; and keeping GPU utilization under &lt;strong&gt;5%&lt;/strong&gt;. The GPU wasn't even working; it was just waiting for my disk and CPU to read and decode JPEGs with 4 worker threads.&lt;/p&gt;

&lt;p&gt;Tuning PyTorch's &lt;code&gt;DataLoader&lt;/code&gt; (&lt;code&gt;num_workers=8&lt;/code&gt;, &lt;code&gt;pin_memory=True&lt;/code&gt;, &lt;code&gt;persistent_workers=True&lt;/code&gt;, &lt;code&gt;prefetch_factor=2&lt;/code&gt;) and bumping &lt;code&gt;batch_size&lt;/code&gt; to &lt;code&gt;128&lt;/code&gt; fixed the bottleneck. Throughput jumped to &lt;strong&gt;~1,000 images/sec&lt;/strong&gt; and GPU usage hit 78–80%. Even after more than doubling the model size (&lt;code&gt;8.52M&lt;/code&gt; parameters) and moving to native &lt;strong&gt;&lt;code&gt;256×256&lt;/code&gt;&lt;/strong&gt; resolution with &lt;strong&gt;&lt;code&gt;mobilenetv4_conv_medium&lt;/code&gt;&lt;/strong&gt;, all 23 epochs finished in &lt;strong&gt;49.5 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That extra resolution paid off immediately on tiny details like stem rings and gill patterns:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fu3zw87bgm862r3r6s1ee.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fu3zw87bgm862r3r6s1ee.jpg" alt="Comparison of Kuehneromyces mutabilis (Edible) vs Galerina marginata (Deadly)" width="800" height="422"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Comparison of Kuehneromyces mutabilis (left, edible; &lt;a href="https://www.inaturalist.org/observations/93749063" rel="noopener noreferrer"&gt;iNaturalist #93749063&lt;/a&gt; by &lt;code&gt;peter_gabler&lt;/code&gt;, &lt;a href="https://creativecommons.org/publicdomain/zero/1.0/" rel="noopener noreferrer"&gt;CC0&lt;/a&gt;) vs. Galerina marginata (right, deadly; &lt;a href="https://www.inaturalist.org/observations/322072507" rel="noopener noreferrer"&gt;iNaturalist #322072507&lt;/a&gt; by &lt;code&gt;phillymycobeth&lt;/code&gt;, &lt;a href="https://creativecommons.org/publicdomain/zero/1.0/" rel="noopener noreferrer"&gt;CC0&lt;/a&gt;). Both photos sourced from the validation split (CC0 1.0, Public Domain).&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Validation Overall Top-1:&lt;/strong&gt; &lt;code&gt;70.05%&lt;/code&gt; → &lt;strong&gt;&lt;code&gt;78.85%&lt;/code&gt; (+8.80 pp)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validation Overall Top-5&lt;/strong&gt; &lt;em&gt;(correct species somewhere in the top 5)&lt;/em&gt;: &lt;code&gt;89.17%&lt;/code&gt; → &lt;strong&gt;&lt;code&gt;93.42%&lt;/code&gt; (+4.25 pp)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validation Macro Top-1&lt;/strong&gt; &lt;em&gt;(average accuracy across all species equally, so rare mushrooms count just as much as common ones)&lt;/em&gt;: &lt;code&gt;67.07%&lt;/code&gt; → &lt;strong&gt;&lt;code&gt;75.97%&lt;/code&gt; (+8.90 pp)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Critical lethal pair (&lt;em&gt;Kuehneromyces mutabilis&lt;/em&gt; vs. &lt;em&gt;Galerina marginata&lt;/em&gt;):&lt;/strong&gt; validation confusion dropped from &lt;strong&gt;&lt;code&gt;11.90%&lt;/code&gt; down to &lt;code&gt;2.86%&lt;/code&gt;&lt;/strong&gt;, clearing the &lt;code&gt;5%&lt;/code&gt; bar on validation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F86fflwyaciex2x9kyrfx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F86fflwyaciex2x9kyrfx.png" alt="Classification accuracy and Death Cap recall progression" width="800" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Validation also exposed one deadly species with far too little training data: &lt;strong&gt;&lt;em&gt;Amanita verna&lt;/em&gt;&lt;/strong&gt; (&lt;code&gt;56&lt;/code&gt; training images, &lt;code&gt;11&lt;/code&gt; validation images) only reached &lt;strong&gt;18.18% recall&lt;/strong&gt;. In a safety-critical app, an 18% recall class is worse than not supporting that species at all. It gives a false sense of security. Instead of burning another training run, I turned off that class in post-processing and moved it to &lt;code&gt;uncoveredSpecies&lt;/code&gt; (leaving &lt;code&gt;66&lt;/code&gt; active classes). Under my confidence and OOD checks on validation, &lt;strong&gt;6 of 11&lt;/strong&gt; &lt;em&gt;A. verna&lt;/em&gt; photos were cleanly rejected and &lt;strong&gt;0 of 11&lt;/strong&gt; were mislabeled as edible.&lt;/p&gt;


&lt;h2&gt;
  
  
  3. Making Confidence Honest: Calibration &amp;amp; OOD Energy Gating
&lt;/h2&gt;

&lt;p&gt;If a model tells you it is &lt;strong&gt;80% confident&lt;/strong&gt;, it should actually be right about 80% of the time. Out of the box, mine wasn't.&lt;/p&gt;

&lt;p&gt;Training with &lt;code&gt;label_smoothing=0.1&lt;/code&gt; (a common trick that stops models from getting overconfident) actually swung things the other way and made raw percentages too cautious. On validation (&lt;code&gt;N = 13,065&lt;/code&gt;, which is 13,076 minus the 11 &lt;em&gt;A. verna&lt;/em&gt; validation images, because every evaluation from here on uses the 66-class active head), the &lt;strong&gt;Expected Calibration Error (ECE)&lt;/strong&gt; (which measures how far off those percentages are from reality) was &lt;strong&gt;&lt;code&gt;9.995%&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The fix is surprisingly simple: &lt;strong&gt;temperature scaling&lt;/strong&gt;. You fit a single multiplier (&lt;code&gt;T_single = 0.7848&lt;/code&gt;) on the validation logits before turning them into probabilities. That one number cut ECE by &lt;strong&gt;3.55×&lt;/strong&gt;, down to &lt;strong&gt;&lt;code&gt;2.815%&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fa03p57rakor4ez4dmtoh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fa03p57rakor4ez4dmtoh.png" alt="Reliability diagram before and after temperature calibration" width="800" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next came rejecting junk photos (boots, grass, dogs) and unknown mushroom species. Using &lt;strong&gt;energy scoring&lt;/strong&gt; (a technique from Liu et al., 2020), I collapsed the raw logits into a single number &lt;code&gt;E(x)&lt;/code&gt;: if the model isn't strongly excited about any known species, the energy stays high (closer to zero), and the photo gets rejected as OOD:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;compute_energy_vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;logits&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ndarray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ndarray&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Compute energy score E(x) = -T * logsumexp(z / T). Lower = more likely a known mushroom."""&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;logits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;scaled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;logits&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;temperature&lt;/span&gt;
    &lt;span class="n"&gt;max_s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scaled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;axis&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;keepdims&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;lse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;max_s&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scaled&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;max_s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;axis&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;keepdims&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;lse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;squeeze&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;axis&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My first cutoff (&lt;code&gt;theta = -1.5265&lt;/code&gt;, &lt;code&gt;minTop1Prob = 0.40&lt;/code&gt;) was way too lenient: &lt;strong&gt;&lt;code&gt;21.28%&lt;/code&gt;&lt;/strong&gt; of non-fungi photos and &lt;strong&gt;&lt;code&gt;31.16%&lt;/code&gt;&lt;/strong&gt; of unknown mushrooms on &lt;code&gt;ood_calib&lt;/code&gt; still slipped through as &lt;code&gt;Confident Edible&lt;/code&gt;. Testing stricter cutoffs strictly on &lt;code&gt;val.csv&lt;/code&gt; and &lt;code&gt;ood_calib&lt;/code&gt; landed on a much safer operating point (&lt;code&gt;theta = -2.2082&lt;/code&gt;, &lt;code&gt;minTop1Prob = 0.50&lt;/code&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Validation Keep Rate:&lt;/strong&gt; &lt;strong&gt;&lt;code&gt;81.39%&lt;/code&gt;&lt;/strong&gt; of valid photos accepted (accuracy on accepted photos rises to &lt;code&gt;88.30%&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Total &lt;code&gt;ood_calib&lt;/code&gt; Confident Edible:&lt;/strong&gt; &lt;strong&gt;&lt;code&gt;27.00%&lt;/code&gt; → &lt;code&gt;17.13%&lt;/code&gt; (&lt;code&gt;-36.5%&lt;/code&gt; drop)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-fungi (&lt;code&gt;ood_nonfungi&lt;/code&gt;) Confident Edible:&lt;/strong&gt; &lt;strong&gt;&lt;code&gt;21.28%&lt;/code&gt; → &lt;code&gt;10.85%&lt;/code&gt; (&lt;code&gt;-49.0%&lt;/code&gt; drop)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm90v58n16ugjsk6lund2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm90v58n16ugjsk6lund2.png" alt="Out-of-Distribution energy score distribution across val, ood_fungi, and ood_nonfungi" width="800" height="462"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  4. 4-Step Multi-Photo Logit Fusion
&lt;/h2&gt;

&lt;p&gt;A single top-down photo often hides key traits like stem rings, scales, or gill color. Grouping validation photos belonging to the same mushroom observation gave &lt;strong&gt;&lt;code&gt;1,803&lt;/code&gt; multi-photo observations (&lt;code&gt;4,463&lt;/code&gt; frames)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Simple majority voting breaks down on 2 photos (ties!) and throws away how confident each shot was. Instead, I built a 4-step fail-closed fusion pipeline:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fapb4entjdlra1kw7cjt6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fapb4entjdlra1kw7cjt6.png" alt="4-Step Multi-Photo Logit Fusion Pipeline" width="800" height="722"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Frame-Level OOD Filter:&lt;/strong&gt; Drop blurry or non-mushroom frames (&lt;code&gt;E(x_i) &amp;gt; -2.2082&lt;/code&gt;) before averaging, so one accidental shot of your shoe doesn't ruin the rest.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Disagreement Check:&lt;/strong&gt; Abort with &lt;code&gt;CONFLICT_DETECTED&lt;/code&gt; if two or more accepted frames are both confident (&lt;code&gt;p_top1 &amp;gt;= 0.70&lt;/code&gt;) yet point to different species (catching cases where a user snaps two different mushrooms side-by-side).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logit Averaging + Re-Calibration:&lt;/strong&gt; Average the raw logit vectors (&lt;code&gt;z_bar = mean(z_accepted)&lt;/code&gt;) and re-run the energy and probability gates.

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Hidden gotcha:&lt;/em&gt; Averaging multiple frames makes the combined prediction sharper and more confident, so reusing the single-photo temperature (&lt;code&gt;T_single = 0.7848&lt;/code&gt;) pushed calibration error right back up to &lt;strong&gt;&lt;code&gt;12.36%&lt;/code&gt;&lt;/strong&gt;. Fitting a separate multi-photo temperature (&lt;strong&gt;&lt;code&gt;T_multi = 0.5146&lt;/code&gt;&lt;/strong&gt;) brought ECE back down to &lt;strong&gt;&lt;code&gt;2.17%&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;High-Stakes Safety Veto:&lt;/strong&gt; Even if the combined Top-1 result says "edible", inspect &lt;strong&gt;every single input frame&lt;/strong&gt; (including rejected ones). If any toxic or deadly species scored &lt;code&gt;&amp;gt;= 20%&lt;/code&gt; on even one photo, block the edible verdict and flag the risk:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;veto_risks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;frames_to_check&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total_frames&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;veto_check_all_frames&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;accepted_frames&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;is_edible&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;f_idx&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;frames_to_check&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;hs_idx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hs_slug&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;high_stakes_indices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;p_toxic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame_probs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;f_idx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hs_idx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;p_toxic&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;high_stakes_veto_threshold&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;veto_risks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="s2"&gt;"slug"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;hs_slug&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="s2"&gt;"class_idx"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;hs_idx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="s2"&gt;"probability"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;p_toxic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="s2"&gt;"frame_idx"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;f_idx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On multi-photo validation observations, accuracy jumped from &lt;code&gt;74.88%&lt;/code&gt; (single frame) to &lt;strong&gt;&lt;code&gt;89.52%&lt;/code&gt; (+14.64 pp)&lt;/strong&gt;, while the safety veto stepped in and blocked &lt;strong&gt;107 observations&lt;/strong&gt; (&lt;code&gt;5.93%&lt;/code&gt;) that otherwise looked edible.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. The Quantization Trap: Why INT8 Broke Energy Gating
&lt;/h2&gt;

&lt;p&gt;Almost every mobile ML guide tells you to &lt;strong&gt;quantize&lt;/strong&gt; your model: shrink its weights from 32-bit floats (&lt;strong&gt;FP32&lt;/strong&gt;) down to 16-bit (&lt;strong&gt;FP16&lt;/strong&gt;) or 8-bit integers (&lt;strong&gt;INT8&lt;/strong&gt;) to save space on phones.&lt;/p&gt;

&lt;p&gt;Before touching the test set, I exported the model to LiteRT / TFLite in &lt;strong&gt;FP32&lt;/strong&gt; (&lt;code&gt;32.56 MB&lt;/code&gt;), &lt;strong&gt;FP16&lt;/strong&gt; (&lt;code&gt;16.49 MB&lt;/code&gt;), and &lt;strong&gt;INT8&lt;/strong&gt; (&lt;code&gt;9.17 MB&lt;/code&gt;), then compared their outputs against PyTorch across all &lt;code&gt;13,065&lt;/code&gt; validation images. &lt;strong&gt;MAE&lt;/strong&gt; (&lt;em&gt;Mean Absolute Error&lt;/em&gt;) below measures the average difference between raw PyTorch and TFLite logits:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric on &lt;code&gt;val.csv&lt;/code&gt; (&lt;code&gt;N = 13,065&lt;/code&gt;)&lt;/th&gt;
&lt;th&gt;PyTorch Baseline&lt;/th&gt;
&lt;th&gt;TFLite FP16&lt;/th&gt;
&lt;th&gt;TFLite INT8&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Top-1 Agreement vs. PyTorch (&lt;code&gt;&amp;gt;= 99%&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;100.00%&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;99.43% (PASS)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;93.89% (FAIL)&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;High-Stakes Top-1 Agreement&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;100.00%&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;99.54%&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;94.80%&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Logit MAE vs. PyTorch&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0.0000&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.0135&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.2022 (15× worse)&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Max Logit Error&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0.0000&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.3196&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;17.3796&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Logit Scale Ratio (Std)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1.0000&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.9995&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.9266 (-7.3% shrinkage)&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Energy Gate Keep Rate (&lt;code&gt;theta = -2.2082&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;81.39%&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;81.38% (0.01% drift)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;80.47% (0.92% drift)&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Why INT8 failed:&lt;/strong&gt; Shrinking to 8-bit integers compressed the overall scale of the logits by &lt;strong&gt;&lt;code&gt;-7.3%&lt;/code&gt;&lt;/strong&gt;, caused wild error spikes up to &lt;strong&gt;&lt;code&gt;17.38&lt;/code&gt;&lt;/strong&gt;, and dropped Top-1 agreement to &lt;code&gt;93.89%&lt;/code&gt;. Because the OOD energy gate depends directly on the size of those raw logits, INT8 silently shifted where the gate opened and closed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TFLite FP16&lt;/strong&gt; (&lt;code&gt;16.49 MB&lt;/code&gt;, &lt;code&gt;99.43%&lt;/code&gt; agreement, &lt;code&gt;0.0135&lt;/code&gt; MAE, &lt;code&gt;0.01%&lt;/code&gt; gate drift) was the clear winner. Saving 7 MB with INT8 isn't worth silently breaking safety calibration.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. Final Ship Gates Audit on Untouched &lt;code&gt;test.csv&lt;/code&gt; &amp;amp; &lt;code&gt;ood_test&lt;/code&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Caveat up front:&lt;/strong&gt; after this audit I found a preprocessing bug. &lt;code&gt;get_val_transforms&lt;/code&gt; ran &lt;code&gt;Resize(448) + CenterCrop(256)&lt;/code&gt;, so on a 4:3 phone photo the model saw only the central ~24.5% of the frame, while the app feeds the full image. The numbers below describe this pipeline, not the in-app experience. Part 2 covers the fix.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With every setting frozen (&lt;code&gt;T_single = 0.7848&lt;/code&gt;, &lt;code&gt;T_multi = 0.5146&lt;/code&gt;, &lt;code&gt;theta = -2.2082&lt;/code&gt;, &lt;code&gt;minTop1Prob = 0.50&lt;/code&gt;), I ran one clean audit on the untouched &lt;code&gt;test.csv&lt;/code&gt; (&lt;code&gt;N = 13,051&lt;/code&gt;) and &lt;code&gt;ood_test&lt;/code&gt; (&lt;code&gt;N = 2,230&lt;/code&gt;) using &lt;strong&gt;TFLite FP16&lt;/strong&gt;. 42 of the 44 configured pairs are evaluated, because the 2 pairs involving &lt;em&gt;amanita_verna&lt;/em&gt; (&lt;em&gt;agaricus_campestris&lt;/em&gt; and &lt;em&gt;calocybe_gambosa&lt;/em&gt; versus &lt;em&gt;amanita_verna&lt;/em&gt;) drop out when &lt;em&gt;verna&lt;/em&gt; is removed from the 66-class head:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Gate ID&lt;/th&gt;
&lt;th&gt;Ship Gate&lt;/th&gt;
&lt;th&gt;Target&lt;/th&gt;
&lt;th&gt;Measured Result&lt;/th&gt;
&lt;th&gt;Status&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GATE-01&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;confidentToxicAsEdible&lt;/code&gt; (Single Frame)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;== 0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;177&lt;/strong&gt; accepted / 441 raw&lt;/td&gt;
&lt;td&gt;❌ &lt;strong&gt;FAIL&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GATE-02&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Dangerous Look-Alike Pairs Confusion&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;&amp;lt;= 5%&lt;/code&gt; across 42 pairs&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;8 failing pairs&lt;/strong&gt; (max &lt;code&gt;37.22%&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;❌ &lt;strong&gt;FAIL&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GATE-03&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;High-Stakes Min Recall (&lt;code&gt;n &amp;gt;= 8&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;gt;= 85.0%&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;68.67%&lt;/strong&gt; (&lt;em&gt;A. moelleri&lt;/em&gt;, 5 failing)&lt;/td&gt;
&lt;td&gt;❌ &lt;strong&gt;FAIL&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GATE-04&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;tfliteAgreement&lt;/code&gt; (FP16 vs. PyTorch)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;gt;= 99.0%&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;99.40% overall / 99.50% HS&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;✅ &lt;strong&gt;PASS&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GATE-05&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;ood_test&lt;/code&gt; Confident Edible&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;&amp;lt;= 2.0%&lt;/code&gt; point estimate; the 95% Wilson upper bound must stay &lt;code&gt;&amp;lt;= 5%&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;17.62%&lt;/strong&gt; (95% Wilson upper bound: &lt;code&gt;19.26%&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;❌ &lt;strong&gt;FAIL&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GATE-06&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Multi-Photo Safety (&lt;code&gt;test.csv&lt;/code&gt;, &lt;code&gt;N = 1,891&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;0&lt;/code&gt; toxic-as-edible, Acc &lt;code&gt;&amp;gt;= 85%&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;9&lt;/strong&gt; toxic-as-edible, &lt;strong&gt;92.49%&lt;/strong&gt; Acc on &lt;strong&gt;1,424 / 1,891&lt;/strong&gt; accepted (75.30%)&lt;/td&gt;
&lt;td&gt;❌ &lt;strong&gt;FAIL&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There were huge wins in those numbers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Multi-photo fusion:&lt;/strong&gt; On the same 1,891 observations, the first frame alone with single-frame gates accepted 1,513 (80.01%) at 88.17% accuracy with &lt;strong&gt;32&lt;/strong&gt; toxic-as-edible errors (84 ungated). Fusion plus veto accepted 1,424 (75.30%) at &lt;strong&gt;92.49%&lt;/strong&gt; accuracy with &lt;strong&gt;9&lt;/strong&gt; errors. Of the 467 rejected observations, 177 were &lt;code&gt;CONFLICT_DETECTED&lt;/code&gt;, 113 &lt;code&gt;HIGH_STAKES_VETO&lt;/code&gt;, 96 &lt;code&gt;ALL_FRAMES_OOD&lt;/code&gt;, and 81 post-fusion &lt;code&gt;OOD&lt;/code&gt;/&lt;code&gt;UNCLEAR&lt;/code&gt;. For scale, the single-frame audit had &lt;strong&gt;177&lt;/strong&gt; accepted toxic-as-edible frames, but frames and observations aren't directly comparable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strong recall on deadly classics:&lt;/strong&gt; &lt;em&gt;Amanita phalloides&lt;/em&gt; hit &lt;strong&gt;&lt;code&gt;95.53%&lt;/code&gt;&lt;/strong&gt; recall, &lt;em&gt;Galerina marginata&lt;/em&gt; hit &lt;strong&gt;&lt;code&gt;92.73%&lt;/code&gt;&lt;/strong&gt;, and &lt;em&gt;Amanita verna&lt;/em&gt; stayed at &lt;strong&gt;&lt;code&gt;0 / 11&lt;/code&gt;&lt;/strong&gt; edible mistakes on test.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Zero-confusion pairs:&lt;/strong&gt; &lt;em&gt;Macrolepiota procera&lt;/em&gt; (Parasol) vs. &lt;em&gt;Amanita phalloides&lt;/em&gt;, &lt;em&gt;A. virosa&lt;/em&gt;, and &lt;em&gt;Lepiota brunneoincarnata&lt;/em&gt; all scored &lt;strong&gt;&lt;code&gt;0.00%&lt;/code&gt;&lt;/strong&gt; confusion across the entire test set.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Yet the system &lt;strong&gt;honestly failed the autonomous safety Ship Gates&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Near-identical species like &lt;em&gt;Hypholoma capnoides&lt;/em&gt; (edible) vs. &lt;em&gt;H. fasciculare&lt;/em&gt; (toxic) still showed &lt;strong&gt;&lt;code&gt;37.22%&lt;/code&gt;&lt;/strong&gt; confusion on single frames (&lt;code&gt;67 / 180&lt;/code&gt;), because they grow in identical clusters on rotting wood and differ mostly by subtle gill tint and bitter taste.&lt;/li&gt;
&lt;li&gt;A closed-set classifier with post-hoc energy gating still leaked &lt;strong&gt;&lt;code&gt;17.62%&lt;/code&gt;&lt;/strong&gt; (&lt;code&gt;393 / 2,230&lt;/code&gt;) of unseen OOD test images as confident edible predictions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;A note on the test set:&lt;/strong&gt; Part 2 changes were driven by error analysis on this audit, so &lt;code&gt;test.csv&lt;/code&gt; is no longer strictly untouched for v2. I'll report v2 on the same split and clearly flag it as tuned-on; a fresh held-out split is planned for the next round.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The product decision:&lt;/strong&gt; &lt;code&gt;mobilenetv4_medium_r256_fp16.tflite&lt;/code&gt; ships strictly as an &lt;strong&gt;offline taxonomic assistant (&lt;code&gt;channel: "test"&lt;/code&gt;)&lt;/strong&gt;, never with an "Edible!" badge. Edibility belongs in the encyclopedia layer, backed by mandatory look-alike comparison cards, multi-angle capture prompts, and UX warnings triggered by &lt;code&gt;HIGH_STAKES_VETO&lt;/code&gt; and &lt;code&gt;CONFLICT_DETECTED&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. Building an ML Pipeline as a Frontend Dev (with Antigravity)
&lt;/h2&gt;

&lt;p&gt;I'm a frontend developer, not a full-time ML engineer. I knew what architecture, UX guardrails, and safety rules I wanted, but writing every PyTorch training loop, calibration script, and export check by hand would have taken months.&lt;/p&gt;

&lt;p&gt;I orchestrated the whole workflow in &lt;strong&gt;Antigravity&lt;/strong&gt;, treating the ML repo the same way I treat production frontend code: strict specs, automated tests, and zero shortcuts. That discipline paid off early. During initial training, the agent caught a silent &lt;strong&gt;EMA&lt;/strong&gt; (&lt;em&gt;Exponential Moving Average&lt;/em&gt;, a smoothed copy of model weights) bug where normalization stats weren't syncing properly (the EMA copy's BatchNorm running statistics weren't synced from the live model), which had crashed validation accuracy to &lt;code&gt;1.49%&lt;/code&gt;. It also verified that the exported TFLite models only used standard mobile ops and kept &lt;code&gt;test.csv&lt;/code&gt; completely untouched across dozens of threshold sweeps. Every new module stayed backed by &lt;strong&gt;107 passing &lt;code&gt;pytest&lt;/code&gt; unit tests&lt;/strong&gt; and zero &lt;code&gt;ruff&lt;/code&gt; linter warnings before moving forward.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. What's Next (Part 2): The Sneaky Crop Bug We Just Found
&lt;/h2&gt;

&lt;p&gt;Right after wrapping up the audit, digging into the error cases exposed a wild bug in our validation and test preprocessing: &lt;code&gt;get_val_transforms&lt;/code&gt; was running &lt;code&gt;Resize(448) + CenterCrop(256)&lt;/code&gt;. On a typical 4:3 smartphone photo, &lt;strong&gt;the model was only seeing the central ~24.5% of the image&lt;/strong&gt;, routinely slicing off stems, rings, and gills, even though the mobile app feeds the entire frame!&lt;/p&gt;

&lt;p&gt;In Part 2 (&lt;code&gt;v2&lt;/code&gt;), I'll walk through what happened when we:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Fixed validation and test framing to use the full image (&lt;code&gt;none_stretch&lt;/code&gt;) and toned down random training color shifts (&lt;code&gt;ColorJitter&lt;/code&gt;) so subtle gill tints survive.&lt;/li&gt;
&lt;li&gt;Added &lt;strong&gt;Energy-Bounded Outlier Exposure&lt;/strong&gt; (explicitly teaching the model to reject non-mushroom forest photos using a separate CC0 dataset) to attack that &lt;code&gt;17.62%&lt;/code&gt; OOD leakage.&lt;/li&gt;
&lt;li&gt;Trained with an &lt;strong&gt;asymmetric safety loss&lt;/strong&gt; (penalizing toxic-as-edible mistakes harder) and tighter pair-specific veto thresholds (&lt;code&gt;0.10&lt;/code&gt; for direct look-alikes), and ran straight into two more nasty silent bugs along the way.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;em&gt;Have you ever been bitten by INT8 quantization drift breaking a threshold, or had to draw the line between ML accuracy and UX safety guardrails? I'd love to hear how you handled it in the comments!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>pytorch</category>
      <category>tflite</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
