This model classifies images of American food into 10 categories using a custom ResNet-style CNN architecture with residual connections. The hyperparameters were optimized using the ES(1+1) evolutionary strategy.
1import tensorflow as tf
2from PIL import Image
3import numpy as np
4from huggingface_hub import hf_hub_download
5
6# Download model
7model_path = hf_hub_download(
8 repo_id="HAR5HA-YELLELA/american_food_classifier",
9 filename="optimized_model_best.keras"
10)
11
12# Load model
13model = tf.keras.models.load_model(model_path)
14
15# Class names
16classes = [
17 "chicken_wings", "churros", "french_fries", "hamburger", "hot_dog",
18 "ice_cream", "macaroni_and_cheese", "pancakes", "pizza", "waffles"
19]
20
21# Load and preprocess image
22img = Image.open("your_food_image.jpg").resize((224, 224))
23img_array = np.array(img) / 255.0
24img_batch = np.expand_dims(img_array, axis=0)
25
26# Predict
27predictions = model.predict(img_batch)[0]
28predicted_class = classes[np.argmax(predictions)]
29confidence = np.max(predictions)
30
31print(f"Prediction: {predicted_class}")
32print(f"Confidence: {confidence:.2%}")
1import requests
2from io import BytesIO
3
4def predict_from_url(url, model, classes, threshold=0.6):
5 response = requests.get(url)
6 img = Image.open(BytesIO(response.content)).resize((224, 224))
7 img_array = np.array(img) / 255.0
8 img_batch = np.expand_dims(img_array, axis=0)
9
10 predictions = model.predict(img_batch, verbose=0)[0]
11 top_idx = np.argmax(predictions)
12 confidence = predictions[top_idx]
13
14 if confidence >= threshold:
15 return classes[top_idx], confidence
16 else:
17 return "out_of_scope", confidence
18
19# Example
20result, conf = predict_from_url("https://example.com/pizza.jpg", model, classes)
21print(f"{result}: {conf:.2%}")
The model includes confidence-based out-of-scope detection. Images with prediction confidence below 60% are flagged as potentially out-of-scope (not one of the 10 trained food categories).
1threshold = 0.6
2if confidence < threshold:
3 print("⚠️ Image may be out of scope")
1@misc{yellela2025foodclassifier,
2 author = {Yellela, V. Harsha Vardhan},
3 title = {American Food Image Classifier with ES(1+1) Optimization},
4 year = {2025},
5 publisher = {Hugging Face},
6 url = {https://huggingface.co/HAR5HA-YELLELA/american_food_classifier}
7}