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
- Dataset: Arko007/walnut-rancidity-predictor
- 90,000 sequences Β· 5.4 M rows
- 4 Indian storage scenarios (cold warehouse, hill region, ambient, hot transport)
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
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-reported0.9+
- Best Val Loss on Walnut Storage Timeseries (Indian Conditions)self-reported0.389