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.
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.
Full dataset verification across train, validation, and test splits with zero corrupt files.
Fresh & rotten apples, bananas, and oranges covering spherical, oblong, and textured produce.
Continuous Gaussian latent space z ~ N(μ, σ²) balancing reconstruction clarity and smooth clustering.
Quantitative generative distribution metric evaluated across all classes with Inception-v3.
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.
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.
Color Harmonization (EnsureRGB)
Engineered a custom EnsureRGB transform stripping RGBA alpha channels and converting all images into uniform 3-channel 100×100 RGB tensors.
Baseline VAE Construction
Built a 3-Conv2D + 2-MaxPool encoder projecting high-dimensional pixels down to a 128-dimensional stochastic latent bottleneck z = μ + σ ⊙ ε.
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.
Dynamic β-Warmup Scheduling
Formulated a linear β-warmup schedule (β = 0.02 → 0.30) to prevent KL divergence explosion during early epochs and ensure robust reconstruction.
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.
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.
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.
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).
EnsureRGB transform class integrated into the torchvision pipeline to safely drop alpha channels.Data Leakage Prevention
Aggressive augmentation on test/validation sets can artificially corrupt perceptual distributions and yield invalid FID evaluation scores.
Inspected Dataset Samples Across Splits



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).
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.
Normalizes layer activations across varied fruit lighting conditions, ensuring smooth gradient propagation and accelerated convergence.
Placed immediately after flattening before fc_mu and fc_logvar. Purposefully kept low to prevent posterior collapse while curtailing overfitting.
Excluded batch normalization from the transposed convolutions to preserve natural continuous pixel gradient dynamics and prevent generative artifacts.
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.
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.




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.

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

Baseline VAE successfully maps overall object geometry, but shows characteristic pixel averaging blur, particularly across dark rotting lesions and peel stem textures.
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).
| CLASS NAME | CATEGORY | VAE_BASE (FID ↓) | VAE_MOD_FIXED (FID ↓) | DELTA (Δ) | QUALITATIVE FINDING |
|---|---|---|---|---|---|
| freshapples | Fresh | 452.150 | 414.090 | -38.06 | Significant boost in color purity and surface contour sharpness |
| freshbanana | Fresh | 531.550 | 533.310 | +1.76 | Comparable baseline performance on elongated curved geometries |
| freshoranges | Fresh | 419.960 | 474.890 | +54.93 | Variance driven by small N=15 test set with heavy specular highlights |
| rottenapples | Rotten | 418.490 | 410.160 | -8.33 | Crisper separation of necrotic brown spots and fungal perimeter |
| rottenbanana | Rotten | 449.840 | 419.260 | -30.58 | Substantial improvement in dark peel discoloration texture recovery |
| rottenoranges | Rotten | 396.690 | 398.100 | +1.41 | Lowest absolute FID overall, strong mold spore reconstruction |
| MEAN GEN-FID OVERALL | 444.780 | 441.634 ↓ | -3.15 | Overall generative improvement confirmed across 6 fruit categories | |
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.
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.
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.
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.
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.
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.
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.
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.
Experimental Analysis & Documentation
Authored exhaustive comparative analyses linking loss convergence, reconstruction quality, and generative metrics to support industrial automated sorting applications.
Technologies supporting computer vision research.
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.