--- license: mit language: - en library_name: pytorch tags: - lstm - attention - time-series-forecasting - food-science - walnut - rancidity - oxidation - shelf-life - india - multi-task-learning pipeline_tag: time-series-forecasting datasets: - Arko007/walnut-rancidity-predictor metrics: - mae - rmse - auc model-index: - name: WalnutRancidityLSTMAttention results: - task: type: time-series-forecasting name: Walnut Rancidity & Shelf-Life Prediction dataset: name: Walnut Storage Timeseries (Indian Conditions) type: Arko007/walnut-rancidity-predictor metrics: - type: auc value: 0.9+ name: Rancidity AUC - type: mae value: 0.389 name: Best Val Loss --- # 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](https://huggingface.co/datasets/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 ```python 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 ```bash pip install -r requirements.txt python train.py # resumes from saved checkpoint automatically ``` ## License MIT