--- license: mit tags: - image-classification - pytorch - cifar100 - resnet - computer-vision datasets: - cifar100 metrics: - accuracy library_name: pytorch --- # ResNet-18 for CIFAR-100 Classification This is a ResNet-18 model trained on the CIFAR-100 dataset for image classification. ## Model Details - **Architecture**: ResNet-18 (Residual Network with 18 layers) - **Parameters**: 11,220,132 - **Dataset**: CIFAR-100 (100 classes, 50,000 training images, 10,000 test images) - **Input Size**: 32x32 RGB images - **Output**: 100 class probabilities ## Training Details ### Training Configuration - **Epochs**: 50 - **Batch Size**: 128 - **Optimizer**: SGD with momentum (0.9) - **Learning Rate**: 0.1 (initial) with Cosine Annealing scheduler - **Weight Decay**: 5e-4 - **Loss Function**: Cross Entropy Loss ### Data Augmentation Training augmentations: - Random Crop (32x32, padding=4) - Random Horizontal Flip - Normalization: mean=(0.5071, 0.4867, 0.4408), std=(0.2675, 0.2565, 0.2761) ## Performance - **Test Accuracy**: 75.84% - **Training Accuracy**: 99.86% (final epoch) - **Validation Accuracy**: 75.68% (final epoch) ## CIFAR-100 Classes The model can classify images into 100 classes: ``` apple, aquarium_fish, baby, bear, beaver, bed, bee, beetle, bicycle, bottle, bowl, boy, bridge, bus, butterfly, camel, can, castle, caterpillar, cattle, chair, chimpanzee, clock, cloud, cockroach, couch, crab, crocodile, cup, dinosaur, dolphin, elephant, flatfish, forest, fox, girl, hamster, house, kangaroo, keyboard, lamp, lawn_mower, leopard, lion, lizard, lobster, man, maple_tree, motorcycle, mountain, mouse, mushroom, oak_tree, orange, orchid, otter, palm_tree, pear, pickup_truck, pine_tree, plain, plate, poppy, porcupine, possum, rabbit, raccoon, ray, road, rocket, rose, sea, seal, shark, shrew, skunk, skyscraper, snail, snake, spider, squirrel, streetcar, sunflower, sweet_pepper, table, tank, telephone, television, tiger, tractor, train, trout, tulip, turtle, wardrobe, whale, willow_tree, wolf, woman, worm ``` ## Usage ### Load the Model ```python import torch import torch.nn as nn # Define the model architecture class BasicBlock(nn.Module): expansion = 1 def __init__(self, in_channels, out_channels, stride=1): super(BasicBlock, self).__init__() self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(out_channels) self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(out_channels) self.shortcut = nn.Sequential() if stride != 1 or in_channels != self.expansion * out_channels: self.shortcut = nn.Sequential( nn.Conv2d(in_channels, self.expansion * out_channels, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(self.expansion * out_channels) ) def forward(self, x): out = torch.relu(self.bn1(self.conv1(x))) out = self.bn2(self.conv2(out)) out += self.shortcut(x) out = torch.relu(out) return out class ResNet(nn.Module): def __init__(self, block, num_blocks, num_classes=100): super(ResNet, self).__init__() self.in_channels = 64 self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(64) self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1) self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2) self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2) self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2) self.linear = nn.Linear(512 * block.expansion, num_classes) def _make_layer(self, block, out_channels, num_blocks, stride): strides = [stride] + [1] * (num_blocks - 1) layers = [] for stride in strides: layers.append(block(self.in_channels, out_channels, stride)) self.in_channels = out_channels * block.expansion return nn.Sequential(*layers) def forward(self, x): out = torch.relu(self.bn1(self.conv1(x))) out = self.layer1(out) out = self.layer2(out) out = self.layer3(out) out = self.layer4(out) out = torch.nn.functional.avg_pool2d(out, 4) out = out.view(out.size(0), -1) out = self.linear(out) return out def ResNet18(): return ResNet(BasicBlock, [2, 2, 2, 2]) # Load model from Hugging Face Hub from huggingface_hub import hf_hub_download # Download the model file model_path = hf_hub_download(repo_id="YOUR_USERNAME/cifar100-resnet18", filename="best_model.pth") # Create model and load weights device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = ResNet18().to(device) model.load_state_dict(torch.load(model_path, map_location=device)) model.eval() ``` ### Make Predictions ```python from PIL import Image import torchvision.transforms as transforms # Define transforms transform = transforms.Compose([ transforms.Resize((32, 32)), transforms.ToTensor(), transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)) ]) # Load and preprocess image image = Image.open('path/to/your/image.jpg') image_tensor = transform(image).unsqueeze(0).to(device) # Make prediction with torch.no_grad(): output = model(image_tensor) probabilities = torch.nn.functional.softmax(output, dim=1) predicted_class = output.argmax(1).item() confidence = probabilities[0][predicted_class].item() # CIFAR-100 class names cifar100_classes = [ 'apple', 'aquarium_fish', 'baby', 'bear', 'beaver', 'bed', 'bee', 'beetle', 'bicycle', 'bottle', 'bowl', 'boy', 'bridge', 'bus', 'butterfly', 'camel', 'can', 'castle', 'caterpillar', 'cattle', 'chair', 'chimpanzee', 'clock', 'cloud', 'cockroach', 'couch', 'crab', 'crocodile', 'cup', 'dinosaur', 'dolphin', 'elephant', 'flatfish', 'forest', 'fox', 'girl', 'hamster', 'house', 'kangaroo', 'keyboard', 'lamp', 'lawn_mower', 'leopard', 'lion', 'lizard', 'lobster', 'man', 'maple_tree', 'motorcycle', 'mountain', 'mouse', 'mushroom', 'oak_tree', 'orange', 'orchid', 'otter', 'palm_tree', 'pear', 'pickup_truck', 'pine_tree', 'plain', 'plate', 'poppy', 'porcupine', 'possum', 'rabbit', 'raccoon', 'ray', 'road', 'rocket', 'rose', 'sea', 'seal', 'shark', 'shrew', 'skunk', 'skyscraper', 'snail', 'snake', 'spider', 'squirrel', 'streetcar', 'sunflower', 'sweet_pepper', 'table', 'tank', 'telephone', 'television', 'tiger', 'tractor', 'train', 'trout', 'tulip', 'turtle', 'wardrobe', 'whale', 'willow_tree', 'wolf', 'woman', 'worm' ] print(f'Predicted class: {cifar100_classes[predicted_class]}') print(f'Confidence: {confidence*100:.2f}%') ``` ## Files - `best_model.pth`: Model weights with best validation accuracy - `cifar100_resnet18_complete.pth`: Complete checkpoint including optimizer state and training history - `training_history.png`: Training and validation curves - `predictions_visualization.png`: Sample predictions ## Training Code The complete training code is available in the accompanying Jupyter notebook `cifar100_resnet.ipynb`. ## Limitations - The model is trained specifically for 32x32 images - Performance may degrade on images significantly different from CIFAR-100 style - Some classes (like lobster/crab, cup/bottle) are challenging to distinguish ## Citation If you use this model, please cite: ```bibtex @misc{cifar100-resnet18, author = {Your Name}, title = {ResNet-18 for CIFAR-100 Classification}, year = {2025}, publisher = {Hugging Face}, howpublished = {\url{https://huggingface.co/YOUR_USERNAME/cifar100-resnet18}} } ``` ## License MIT License