Spaces:
Sleeping
Sleeping
| import torch | |
| import torch.nn as nn | |
| from PIL import Image | |
| import torchvision.transforms as transforms | |
| import gradio as gr | |
| from huggingface_hub import hf_hub_download | |
| 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' | |
| ] | |
| 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]) | |
| print("Loading model...") | |
| model_path = hf_hub_download(repo_id="ivantv/cifar100-resnet18", filename="best_model.pth") | |
| 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() | |
| print("Model loaded!") | |
| transform = transforms.Compose([ | |
| transforms.Resize((32, 32)), | |
| transforms.ToTensor(), | |
| transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)) | |
| ]) | |
| def classify_image(image): | |
| if image is None: | |
| return {} | |
| if not isinstance(image, Image.Image): | |
| image = Image.fromarray(image) | |
| if image.mode != 'RGB': | |
| image = image.convert('RGB') | |
| img_tensor = transform(image).unsqueeze(0).to(device) | |
| with torch.no_grad(): | |
| output = model(img_tensor) | |
| probs = torch.nn.functional.softmax(output, dim=1)[0] | |
| top5_prob, top5_idx = torch.topk(probs, 5) | |
| return {CIFAR100_CLASSES[idx]: prob.item() for prob, idx in zip(top5_prob, top5_idx)} | |
| iface = gr.Interface( | |
| fn=classify_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs=gr.Label(num_top_classes=5), | |
| title="CIFAR-100 Classifier", | |
| description="ResNet-18 model trained on CIFAR-100 (75.84% accuracy)" | |
| ) | |
| iface.launch() | |