[01] AI · COMPUTER VISION · GENERATIVE MODELING

Fruit & Rotten Fruit Classification

Latent Space Representation Learning & Generative Spoilage Modeling with Variational Autoencoders

A deep learning computer vision case study investigating how continuous biological degradation across fresh and rotten fruits can be modeled in stochastic latent space, comparing baseline and regularized VAE architectures with β-warmup scheduling and Inception-v3 Fréchet Inception Distance (FID) evaluation.

ROLEML / Computer Vision Researcher
RESPONSIBILITIESVAE Architecture · β-Warmup Tuning · FID Benchmark
TECH STACKPyTorch · Torchvision · Inception-v3 · SciPy · CUDA
FOCUSLatent Representation Learning & Generative Spoilage
LATENT MANIFOLD & GEN-FID HUD
CUDA · PyTorch
INPUT TENSOR100 × 100 × 3 RGBEnsureRGB Normalized
LATENT BOTTLENECK128-D [z ~ N(μ, σ²)]Stochastic Manifold
MEAN GEN-FID441.63 ↓ (Mod)Inception-v3 Metric
CLUSTER INSPECT:
LATENT DIMENSION Z₁ (PIGMENTATION & COLOR) →LATENT DIMENSION Z₂ (SURFACE TEXTURE) →FRESH CLUSTER (μ₁)ROTTEN CLUSTER (μ₂)
01 / PROJECT OVERVIEW

Modeling continuous biological decay beyond hard discrete classes.

In industrial food sorting and automated agricultural inspection, fruit spoilage does not occur as an instantaneous binary switch. It is an evolving physical and visual continuum—marked by gradual desiccation, enzymatic browning, fungal mold colonies, and structural collapse. This research investigates whether Variational Autoencoders (VAEs) can learn a smooth, topologically organized latent manifold representing this physical progression.

5,219+TOTAL IMAGES AUDITED

Full dataset verification across train, validation, and test splits with zero corrupt files.

6FRUIT CLASSES

Fresh & rotten apples, bananas, and oranges covering spherical, oblong, and textured produce.

128LATENT BOTTLENECK DIMENSIONS

Continuous Gaussian latent space z ~ N(μ, σ²) balancing reconstruction clarity and smooth clustering.

441.63MEAN GEN-FID SCORE

Quantitative generative distribution metric evaluated across all classes with Inception-v3.

02 / WHAT I BUILT

An end-to-end computer vision pipeline from raw pixels to latent evaluation.

Every stage of this case study was engineered from scratch in PyTorch—spanning automated image normalization, dual neural network architectures, custom loss balancing, and distribution benchmarking.

01DATA CLEANING

Dataset Preprocessing & Hygiene

Conducted rigorous automated checks across 5,219+ samples to verify zero corrupted files and resolved severe resolution disparities from up to 8,256×6,000 px.

02TRANSFORM PIPELINE

Color Harmonization (EnsureRGB)

Engineered a custom EnsureRGB transform stripping RGBA alpha channels and converting all images into uniform 3-channel 100×100 RGB tensors.

03CONV ARCHITECTURE

Baseline VAE Construction

Built a 3-Conv2D + 2-MaxPool encoder projecting high-dimensional pixels down to a 128-dimensional stochastic latent bottleneck z = μ + σ ⊙ ε.

04REGULARIZATION

Enhanced VAE (VAE_Mod_Fixed)

Added BatchNorm2d to stabilize encoder activations, lightweight dropout (p=0.05) before latent heads, and kept decoder un-normalized for crisp pixel output.

05KL ANNEALING

Dynamic β-Warmup Scheduling

Formulated a linear β-warmup schedule (β = 0.02 → 0.30) to prevent KL divergence explosion during early epochs and ensure robust reconstruction.

06EVALUATION METRIC

Multi-Class Inception-v3 GEN-FID

Benchmarked generative fidelity across all 6 fruit classes using real test samples vs. prior-generated samples through Inception-v3 feature embeddings.

03 / DATASET ANALYSIS & HYGIENE

Resolving extreme resolution disparities and inconsistent color channels.

Before training any generative model, an in-depth exploratory audit of the dataset was conducted. Three critical data anomalies were discovered and systematically resolved to guarantee numerical stability.

2,778TRAIN SAMPLESBalanced ~463 imgs / class · Random Flip + Rotation (±10°)
2,353VALIDATION SAMPLESBalanced ~392 imgs / class · Pure evaluation (no augmentation)
88TEST SAMPLES13–15 imgs / class · Real-world resolution benchmark
PROBLEM 01

Extreme Resolution Outliers

While training images averaged ~350 × 350 px, test set images contained extreme outliers ranging from 183 px up to 8,256 × 6,000 pixels. Unchecked, this would cause severe GPU out-of-memory crashes and batch dimension mismatches.

SOLUTION: Uniform high-quality bicubic downsampling to standardized 100 × 100 px across all pipelines.
PROBLEM 02

Inconsistent Color Modes (RGB vs. RGBA)

Train and validation sets contained arbitrary mixtures of standard 3-channel RGB images and 4-channel RGBA images (containing transparent alpha channels).

SOLUTION: Custom EnsureRGB transform class integrated into the torchvision pipeline to safely drop alpha channels.
PROBLEM 03

Data Leakage Prevention

Aggressive augmentation on test/validation sets can artificially corrupt perceptual distributions and yield invalid FID evaluation scores.

SOLUTION: Confined horizontal flipping and ±10° rotation strictly to the training split, keeping validation/test raw and uncorrupted.
04 / MODEL ARCHITECTURES

Variational Autoencoder design: Baseline vs. Regularized Enhanced VAE.

To investigate generative stability and representation quality, two distinct VAE architectures were implemented in PyTorch and trained under identical batch parameters (100×100 input, Adam optimizer, lr=1e-3, 15 epochs).

ENHANCED DESIGN

VAE_Mod_Fixed: Stabilized Feature Manifolds

Built to solve the blurriness and gradient instability observed in baseline VAEs by introducing batch normalization on the encoder, subtle dropout, and a pure un-normalized decoder.

INPUT
100 × 100 × 3Normalized [-1, 1]
ENCODER
3× Conv2D + BatchNorm32 → 64 → 128 Channels + 2× MaxPool
REGULARIZATION
Dropout (p=0.05)Pre-Latent Flatten (80,000 dim)
BOTTLENECK
z ~ N(μ, σ²) [128-D]Reparameterization Trick
DECODER
Pure ConvTranspose2DNo BatchNorm · Tanh Output
1. BatchNorm2d on Encoder:

Normalizes layer activations across varied fruit lighting conditions, ensuring smooth gradient propagation and accelerated convergence.

2. Lightweight Dropout (p=0.05):

Placed immediately after flattening before fc_mu and fc_logvar. Purposefully kept low to prevent posterior collapse while curtailing overfitting.

3. Pure Decoder (No BatchNorm):

Excluded batch normalization from the transposed convolutions to preserve natural continuous pixel gradient dynamics and prevent generative artifacts.

05 / TRAINING DYNAMICS & LOSS PROFILES

Preventing KL explosion through dynamic β-warmup scheduling.

Standard VAE training frequently suffers from KL divergence explosion: when the latent regularizer dominates early epochs, the encoder is penalized before it has learned to reconstruct meaningful spatial features. To solve this, a linear β-warmup annealing schedule was formulated.

LOSS FORMULATIONComposite ELBO Optimization with Annealing
L_total(x) = MSE(x, x_recon) + β_t · D_KL( q_φ(z|x) || p(z) )
where β_t = min(0.30, 0.02 × epoch), starting at β₁ = 0.02 and increasing by +0.02 per epoch

By initializing β at 0.02, the network first prioritizes pixel-level structural reconstruction (minimizing MSE). As features solidify, β gradually scales up to enforce a smooth standard Gaussian prior N(0, I) over the 128 latent dimensions without destroying reconstructed detail.

ENHANCED VAE (VAE_Mod_Fixed)
Modified VAE Total Loss
Total Loss Curve (Best Val: 0.2623)Smooth monotonic decline with β-warmup
Modified VAE Recon vs KL
Val Recon (MSE) vs. Val KLControlled KL descent to 0.30
BASELINE VAE (VAE_Base)
Baseline VAE Total Loss
Baseline Loss Curve (Best Val: 0.2732)Sharp drop at epoch 2, leveling off at epoch 15
Baseline VAE Recon vs KL
Baseline Recon & KL DecompositionVal Recon ~0.2312 · Val KL ~0.0420
06 / QUALITATIVE RECONSTRUCTION

Visualizing original inputs vs. decoded stochastic reconstructions.

Qualitative inspection demonstrates how well each model's 128-dimensional latent vector retains critical diagnostic features—such as circular fruit contour, peel luminance, and necrotic mold discoloration.

ENHANCED VAE (VAE_Mod_Fixed)BEST VAL LOSS: 0.2623
Modified VAE Reconstructions
Analysis:

Reconstructions capture distinctive fruit colorations (vibrant citrus orange, banana curve, apple tones) while recovering dark rotting blemishes with reduced edge smearing.

BASELINE VAE (VAE_Base)BEST VAL LOSS: 0.2732
Baseline VAE Reconstructions
Analysis:

Baseline VAE successfully maps overall object geometry, but shows characteristic pixel averaging blur, particularly across dark rotting lesions and peel stem textures.

07 / GENERATIVE EVALUATION (FID)

Measuring feature distribution divergence using Fréchet Inception Distance.

While reconstruction loss (MSE) measures per-pixel Euclidean distance, it fails to quantify perceptual realism and generative manifold coverage. To evaluate true generative capability, Fréchet Inception Distance (GEN-FID) was computed using a pretrained Inception-v3 network comparing real test distributions against samples generated from prior z ~ N(0, I).

MATHEMATICAL METRIC
FID = ||μ_r - μ_g||² + Tr( Σ_r + Σ_g - 2(Σ_r Σ_g)^(1/2) )
where (μ_r, Σ_r) are the mean and covariance of deep Inception-v3 features extracted from real fruit test images, and (μ_g, Σ_g) are from model-generated images. Lower FID indicates greater distributional realism.
CLASS NAMECATEGORYVAE_BASE (FID ↓)VAE_MOD_FIXED (FID ↓)DELTA (Δ)QUALITATIVE FINDING
freshapplesFresh452.150414.090-38.06Significant boost in color purity and surface contour sharpness
freshbananaFresh531.550533.310+1.76Comparable baseline performance on elongated curved geometries
freshorangesFresh419.960474.890+54.93Variance driven by small N=15 test set with heavy specular highlights
rottenapplesRotten418.490410.160-8.33Crisper separation of necrotic brown spots and fungal perimeter
rottenbananaRotten449.840419.260-30.58Substantial improvement in dark peel discoloration texture recovery
rottenorangesRotten396.690398.100+1.41Lowest absolute FID overall, strong mold spore reconstruction
MEAN GEN-FID OVERALL444.780441.634 ↓-3.15Overall generative improvement confirmed across 6 fruit categories
SIGNIFICANT GAINS

Substantial Improvement on Apples & Bananas

The enhanced model achieved impressive FID reductions on freshapples (-38.06), rottenbanana (-30.58), and rottenapples (-8.33). Encoder batch normalization helped the network retain high-frequency textural variance in peel discoloration.

SCIENTIFIC OBSERVATION

Small Sample Variance on Test Set (N=15)

In freshoranges, FID increased from 419.96 to 474.89. Because the test split contained only 13–15 images per class, Inception covariance estimates possess higher statistical variance. Real-world evaluation would benefit from larger sample sizes.

08 / MY ROLE & RESPONSIBILITIES

Independent machine learning research, architecture, and benchmarking.

I spearheaded this computer vision investigation from initial dataset hygiene to model design, mathematical regularization, and distributional evaluation.

01 / RESEARCH CONTRIBUTION

Dataset Sanitation & Pipeline Engineering

Conducted exploratory data analysis across 5,219+ images. Identified and resolved extreme resolution outliers (183px up to 8,256px) and engineered the EnsureRGB custom transform to guarantee consistent 3-channel input.

02 / RESEARCH CONTRIBUTION

PyTorch Model Architecture Design

Architected both the baseline and modified convolutional autoencoders. Carefully balanced encoder depth (3 Conv2D + 2 MaxPool) and configured transposed convolutions to preserve output dimensions at exactly 100×100×3.

03 / RESEARCH CONTRIBUTION

Stochastic Bottleneck & Regularization

Formulated the Gaussian reparameterization trick in PyTorch, integrated BatchNorm2d on the encoder, and calibrated minimal dropout (p=0.05) to eliminate posterior collapse.

04 / RESEARCH CONTRIBUTION

Dynamic β-Warmup KL Annealing

Devised the mathematical warmup schedule β_t = min(0.30, 0.02 × t), preventing KL divergence explosion and stabilizing training to achieve a best validation loss of 0.2623.

05 / RESEARCH CONTRIBUTION

Inception-v3 FID Quantitative Benchmarking

Constructed an end-to-end FID evaluation script extracting 2,048-dimensional feature representations using pretrained Inception-v3 to benchmark generative realism across all 6 fruit classes.

06 / RESEARCH CONTRIBUTION

Experimental Analysis & Documentation

Authored exhaustive comparative analyses linking loss convergence, reconstruction quality, and generative metrics to support industrial automated sorting applications.

09 / TECHNOLOGIES & TOOLS

Technologies supporting computer vision research.

PyTorch (Core Deep Learning)Torchvision (Transforms & Models)Inception-v3 (Pretrained Weights)Variational Autoencoders (VAE)Fréchet Inception Distance (FID)SciPy (Matrix Sqrt & Covariance)NumPy (Array Operations)Matplotlib (Diagnostic Plotting)PIL / Pillow (Image Processing)CUDA / GPU Acceleration
10 / KEY TAKEAWAYS & REFLECTIONS

Technical reflections on generative computer vision.

Key engineering principles and theoretical insights gained from modeling complex biological degradation.

Continuous Latent Manifolds vs. Hard Labels

In biological quality control, treating freshness as a binary classification misses vital intermediate deterioration states. Continuous latent variables enable agricultural systems to grade fruit ripeness along a smooth continuum.

The Delicate Equilibrium of VAE Loss

VAEs are notorious for posterior collapse or blurry reconstructions. Introducing a dynamic β-warmup schedule was pivotal: allowing the autoencoder to first anchor visual features before imposing strict prior regularization.

Perceptual Metrics Over Pixel MSE

Reconstruction loss alone is deceptive: a blurry image can have lower MSE than a sharp image with slightly shifted texture. Using Inception-v3 FID provided an objective, distribution-level measure of true generative fidelity.

Explore the Fruit Classification study repository

View the complete PyTorch implementation, EnsureRGB preprocessing pipelines, baseline vs. enhanced VAE models, and Inception-v3 FID benchmarking notebooks on GitHub.