CoolFace
Modelpublic

ShurongSR/pet-food-recall-risk-classifier

sourceHugging Facecc-by-4.0updated 5mo agoView on Hugging Face
0likes
Model Card

Pet Food Recall Risk Classifier

A multi-label text classifier that assigns risk categories to pet food recall and safety-alert records.

Built as an academic assignment for an Information Retrieval course.


Model Description

This model combines a frozen sentence transformer encoder with a trained sklearn classifier head:

Recall text
→ frozen SentenceTransformer (sentence-transformers/all-MiniLM-L6-v2)
→ fixed-size embedding (dim 384)
→ OneVsRestClassifier(LogisticRegression)   ← this repository
→ predicted risk labels

The transformer encoder (sentence-transformers/all-MiniLM-L6-v2) is not fine-tuned and is not included in this repository. It is loaded at inference time via the sentence-transformers library. Only the trained classifier head (classifier.joblib), per-label thresholds (thresholds.json), and label metadata (label_columns.json) are stored here.


Task

Supervised multi-label text classification.

Given a structured text constructed from brand name, product description, and recall reason, the model predicts one or more of three risk categories. A record may carry a single label or multiple labels when several risk types are described.


Input Format

Build the input string using this template before encoding:

Brand: {brand_names}. Product: {product_description}. Recall reason: {recall_reason_description}.

Example:

Brand: Example Brand. Product: Dry dog food. Recall reason: May be contaminated with Salmonella.

Output Labels

LabelDescription
PATHOGEN_CONTAMINATIONBacterial or pathogen-related contamination (Salmonella, Listeria, E. coli, etc.)
CHEMICAL_OR_NUTRITIONAL_RISKChemical, mycotoxin, heavy metal, feed additive, vitamin, or mineral-level risks
PHYSICAL_OR_QUALITY_ISSUEForeign material, labeling, packaging, import, inspection, or process-control issues

Label order used in all arrays: label_columns.json.


Per-Label Prediction Thresholds

Thresholds were tuned on the validation split and are stored in thresholds.json.

LabelThreshold
PATHOGEN_CONTAMINATION0.50
CHEMICAL_OR_NUTRITIONAL_RISK0.50
PHYSICAL_OR_QUALITY_ISSUE0.40

If no label clears its threshold, the label with the highest probability score is returned as a low-confidence fallback.


Usage

python
import json
import joblib
import numpy as np
from sentence_transformers import SentenceTransformer

# Load artifacts
clf        = joblib.load("classifier.joblib")
thresholds = json.load(open("thresholds.json"))["thresholds"]
labels     = json.load(open("label_columns.json"))

# Build input text
text = (
    "Brand: Example Brand. "
    "Product: Dry dog food. "
    "Recall reason: May be contaminated with Salmonella."
)

# Encode with frozen transformer
encoder   = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
embedding = encoder.encode([text])           # shape (1, 384)

# Predict
proba           = clf.predict_proba(embedding)[0]   # shape (3,)
predicted       = [l for i, l in enumerate(labels) if proba[i] >= thresholds[l]]
low_confidence  = False
if not predicted:
    predicted      = [labels[int(np.argmax(proba))]]
    low_confidence = True

print(predicted, low_confidence)

The src/predict.py script in the source repository wraps this pipeline and outputs clean JSON.


Validation Results (model selection)

Four classifiers were trained and compared on the validation split (15 examples). Logistic Regression was selected because it achieved the best validation macro F1 while also supporting predict_proba and per-label threshold tuning.

ModelMicro F1Macro F1Weighted F1Hamming LossSubset Accuracy
Dummy baseline0.5160.2320.3480.3330.467
Logistic Regression0.9410.9330.9500.0440.867
Linear SVC0.9030.8570.8930.0670.800
Random Forest0.9410.9330.9500.0440.867

Final Test Results

The selected model was evaluated once on the held-out test set (15 examples). No training, threshold tuning, or model selection changes were made after inspecting test results.

Overall

MetricValue
Micro F10.875
Macro F10.841
Weighted F10.881
Hamming Loss0.089
Subset Accuracy0.800

Per-Label

LabelPrecisionRecallF1Support
PATHOGEN_CONTAMINATION1.0001.0001.0008
CHEMICAL_OR_NUTRITIONAL_RISK1.0000.7500.8574
PHYSICAL_OR_QUALITY_ISSUE0.6000.7500.6674

The test set contains only 15 rows, so metrics are sensitive to 1–2 examples. Results should be interpreted as assignment-scale evidence, not robust production performance estimates.


Training Data

103 labeled records from official public pet food recall and safety-alert portals (FDA, EU RASFF, UK FSA, Canada CFIA, openFDA). Labels were assigned using transparent rule-based keyword patterns. 14 uncertain records were excluded rather than forced into a label.

SplitRows
Train73
Validation15
Test15

See the companion dataset repository for full split details and source attribution.


Repository Contents

FileDescription
classifier.joblibTrained OneVsRestClassifier(LogisticRegression)
thresholds.jsonPer-label prediction thresholds (validation-tuned)
label_columns.jsonOrdered list of output label names
model_config.jsonTraining provenance and selection metadata
test_metrics.jsonFinal held-out test metrics
per_label_metrics.csvPer-label precision / recall / F1 on test set

Limitations

  1. 1.Small training set — 73 training rows; model may not generalise beyond this vocabulary and source mix.
  2. 2.Small test set — 15 rows; per-label metrics are sensitive to 1–2 examples.
  3. 3.Rule-based labels — not manually annotated by domain experts.
  4. 4.Consolidated taxonomy — three labels combine six more specific risk types.
  5. 5.Source heterogeneity — records from five sources with different terminology.
  6. 6.Frozen encoder — the transformer is not fine-tuned on recall text.
  7. 7.Not a safety authority tool — the model predicts text categories, not product safety.

Educational Disclaimer

This model is for educational purposes only.

It does not provide veterinary advice, legal advice, product safety certification, official recall interpretation, or medical or nutritional recommendations.

Always consult official recall notices and qualified professionals for authoritative information about pet food safety.