Walnut Rancidity Predictor

Production-grade Stacked LSTM + Attention model for predicting walnut rancidity probability and remaining shelf life from storage condition time-series.

Trained on 90,000 synthetic sequences simulating Indian storage environments using Arrhenius-based lipid oxidation kinetics.

Model Architecture

Input (30 days Γ— 8 features)
  β†’ LSTM layer 1  (hidden=64, dropout=0.2)
  β†’ LSTM layer 2  (hidden=64, dropout=0.2)
  β†’ LSTM layer 3  (hidden=64)
  β†’ Attention (soft weighted context)
  β†’ Dropout (0.2)
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚  Head 1: rancidity_probability   β”‚  Linear β†’ ReLU β†’ Linear β†’ Sigmoid
  β”‚  Head 2: shelf_life_remaining    β”‚  Linear β†’ ReLU β†’ Linear
  β”‚  Head 3: decay_curve_value       β”‚  Linear β†’ ReLU β†’ Linear β†’ Sigmoid
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Property Value
Parameters ~85 K
Input features 8
Sequence length 30 days
LSTM layers 3
Hidden size 64
Dropout 0.2
Epochs trained 12 / 20
Best val loss 0.3892

Training Data

Input Features

Feature Unit Description
temperature Β°C Ambient temperature (2–40 Β°C)
humidity % RH Relative humidity (30–85 %)
moisture % Walnut moisture content (3–8 %)
oxygen fraction Oxygen exposure (0.18–0.23)
peroxide_value meq/kg Primary oxidation marker
free_fatty_acids % FFA content
hexanal_level ppm Volatile oxidation byproduct
oxidation_index β€” Composite oxidation score

Outputs

Output Type Description
rancidity_probability [0, 1] Probability walnuts are rancid
shelf_life_remaining_days float Days until PV > 5 meq/kg
decay_curve_value [0, 1] Normalised peroxide value

Rancidity Threshold

Walnuts are rancid when Peroxide Value > 5 meq/kg (FSSAI / Codex standard).

rancidity_probability = sigmoid(PV βˆ’ 5)

Quick Start

from huggingface_hub import hf_hub_download
import torch, joblib, numpy as np

# Download artifacts
model_path  = hf_hub_download("Arko007/walnut-rancidity-predictor", "models/walnut_rancidity_lstm_attention.pt")
scaler_path = hf_hub_download("Arko007/walnut-rancidity-predictor", "models/feature_scaler.pkl")

# Or clone and use the inference helper directly:
# from model.predict import predict_storage_risk

# Build a 30-day sequence: [temp, humidity, moisture, oxygen, PV, FFA, hexanal, ox_index]
seq = np.column_stack([
    np.full(30, 5.0),             # cold storage
    np.full(30, 50.0),
    np.full(30, 4.0),
    np.full(30, 0.20),
    np.linspace(0.5, 1.2, 30),
    np.linspace(0.05, 0.10, 30),
    np.linspace(0.1,  0.3,  30),
    np.linspace(0.2,  0.5,  30),
])

result = predict_storage_risk(seq)
# β†’ {'rancidity_probability': 0.0312, 'shelf_life_remaining_days': 143.7, 'risk_level': 'LOW'}

Risk Classification

risk_level rancidity_probability
LOW < 0.30
MEDIUM 0.30 – 0.70
HIGH > 0.70

Chemistry Model

k(T)   = A Β· exp(βˆ’Ea / (RΒ·T))    A=1.5Γ—10ΒΉΒ², Ea=80 kJ/mol, R=8.314 J/molΒ·K
PV(t)  = PVβ‚€ Β· exp(k Β· t)

Humidity and moisture accelerate oxidation via empirical correction factors.

Files

models/
  walnut_rancidity_lstm_attention.pt  ← PyTorch checkpoint (epoch 12)
  feature_scaler.pkl                  ← StandardScaler for inference
  metrics.json                        ← Training metrics
model/
  predict.py                          ← Inference API
train.py                              ← Training script (resume from checkpoint)
generate_data.py                      ← Synthetic data generator
requirements.txt

Resume Training

pip install -r requirements.txt
python train.py          # resumes from saved checkpoint automatically

License

MIT

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Dataset used to train Arko007/walnut-rancidity-predictor

Space using Arko007/walnut-rancidity-predictor 1

Evaluation results

  • Rancidity AUC on Walnut Storage Timeseries (Indian Conditions)
    self-reported
    0.9+
  • Best Val Loss on Walnut Storage Timeseries (Indian Conditions)
    self-reported
    0.389