--- license: mit tags: - image-classification - food-recognition - raw-food - pytorch - resnet - se-resnet - computer-vision - model-comparison datasets: - ibrahimdaud/raw-food-recognition metrics: - accuracy --- # Raw Food Recognition Models: ResNet-50 vs SE-ResNet-50 This repository contains both ResNet-50 and SE-ResNet-50 models trained for raw food ingredient recognition using the merged raw food recognition dataset. ## Model Comparison | Model | Parameters | Validation Accuracy | Architecture | |-------|-----------|-------------------|--------------| | ResNet-50 | ~25.6M | 97.84% | Standard residual network | | SE-ResNet-50 | ~26.0M | 95.72% | ResNet-50 with SE attention | ## Dataset Both models were trained on the [ibrahimdaud/raw-food-recognition](https://huggingface.co/datasets/ibrahimdaud/raw-food-recognition) dataset, which contains 90+ raw food categories. ## Usage ### Download Both Models ```python from huggingface_hub import hf_hub_download import torch from models.resnet50 import create_resnet50 from models.se_resnet50 import create_se_resnet50 # Download ResNet-50 checkpoint resnet_path = hf_hub_download( repo_id="ibrahimdaud/raw-food-recognition-models", filename="resnet50_pytorch_model.bin" ) resnet_checkpoint = torch.load(resnet_path, map_location='cpu') # Download SE-ResNet-50 checkpoint se_resnet_path = hf_hub_download( repo_id="ibrahimdaud/raw-food-recognition-models", filename="se_resnet50_pytorch_model.bin" ) se_resnet_checkpoint = torch.load(se_resnet_path, map_location='cpu') ``` ### Load ResNet-50 ```python # Create ResNet-50 model resnet_model = create_resnet50( num_classes=90, pretrained=False ) resnet_model.load_state_dict(resnet_checkpoint['model_state_dict']) resnet_model.eval() ``` ### Load SE-ResNet-50 ```python # Create SE-ResNet-50 model se_resnet_model = create_se_resnet50( num_classes=90, pretrained=False, reduction=16 ) se_resnet_model.load_state_dict(se_resnet_checkpoint['model_state_dict']) se_resnet_model.eval() ``` ### Compare Predictions ```python import torch from PIL import Image import torchvision.transforms as transforms # Preprocess image transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) image = Image.open('path/to/image.jpg').convert('RGB') image_tensor = transform(image).unsqueeze(0) # ResNet-50 prediction with torch.no_grad(): resnet_outputs = resnet_model(image_tensor) resnet_probs = torch.nn.functional.softmax(resnet_outputs[0], dim=0) resnet_pred_id = torch.argmax(resnet_probs).item() resnet_pred_class = resnet_checkpoint['class_names'][resnet_pred_id] resnet_confidence = resnet_probs[resnet_pred_id].item() # SE-ResNet-50 prediction with torch.no_grad(): se_resnet_outputs = se_resnet_model(image_tensor) se_resnet_probs = torch.nn.functional.softmax(se_resnet_outputs[0], dim=0) se_resnet_pred_id = torch.argmax(se_resnet_probs).item() se_resnet_pred_class = se_resnet_checkpoint['class_names'][se_resnet_pred_id] se_resnet_confidence = se_resnet_probs[se_resnet_pred_id].item() # Compare results print("ResNet-50 Prediction:") print(f" Class: {resnet_pred_class}") print(f" Confidence: {resnet_confidence*100:.2f}%") print("\nSE-ResNet-50 Prediction:") print(f" Class: {se_resnet_pred_class}") print(f" Confidence: {se_resnet_confidence*100:.2f}%") ``` ## Model Details ### ResNet-50 - **Architecture**: Standard residual network with bottleneck blocks - **Parameters**: ~25.6M - **Pretrained**: ImageNet weights - **Best Validation Accuracy**: 97.84% ### SE-ResNet-50 - **Architecture**: ResNet-50 with Squeeze-and-Excitation attention blocks - **Parameters**: ~26.0M - **Pretrained**: ImageNet weights (excluding SE blocks) - **SE Reduction Ratio**: 16 - **Best Validation Accuracy**: 95.72% ## Training Details - **Dataset**: ibrahimdaud/raw-food-recognition - **Number of Classes**: 90 - **Image Size**: 224x224 - **Optimizer**: Adam - **Learning Rate**: 0.001 - **Batch Size**: 32 ## Files in Repository - `resnet50_pytorch_model.bin` - ResNet-50 model weights - `se_resnet50_pytorch_model.bin` - SE-ResNet-50 model weights - `resnet50_metadata.json` - ResNet-50 metadata - `se_resnet50_metadata.json` - SE-ResNet-50 metadata - `README.md` - This file ## Citation If you use these models, please cite: ```bibtex @model{raw_food_recognition_models_2024, title={Raw Food Recognition Models: ResNet-50 and SE-ResNet-50}, author={Ibrahim Daud}, year={2024}, publisher={HuggingFace}, url={https://huggingface.co/ibrahimdaud/raw-food-recognition-models} } ``` ## License MIT License