CoolFace
Apppublic

pytorch/ResNeXt101

sourceHugging Faceupdated 5y agoView on Hugging Face
0likes
app.py43 linesDownload Raw Back to root
1import torch2import gradio as gr3import torchvision.transforms as transforms4 5device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")6 7resneXt = torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_resneXt')8utils = torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_convnets_processing_utils')9 10resneXt.eval().to(device)11 12def inference(img):13  img_transforms = transforms.Compose(14                [transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor()]15  )16  img = img_transforms(img)17  with torch.no_grad():18    # mean and std are not multiplied by 255 as they are in training script19    # torch dataloader reads data into bytes whereas loading directly20    # through PIL creates a tensor with floats in [0,1] range21    mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1)22    std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1)23    img = img.float()24    img = img.unsqueeze(0).sub_(mean).div_(std)25 26  batch = torch.cat(27    [img]28  ).to(device)29 30  with torch.no_grad():31    output = torch.nn.functional.softmax(resneXt(batch), dim=1)32    33  results = utils.pick_n_best(predictions=output, n=5)34  35  return results36  37title="ResNeXt101"38description="Gradio demo for ResNeXt101, ResNet with bottleneck 3x3 Convolutions substituted by 3x3 Grouped Convolutions, trained with mixed precision using Tensor Cores. To use it, simply upload your image or click on one of the examples below. Read more at the links below"39 40article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1611.05431'>Aggregated Residual Transformations for Deep Neural Networks</a> | <a href='https://github.com/NVIDIA/DeepLearningExamples/tree/master/PyTorch/Classification/ConvNets/resnext101-32x4d'>Github Repo</a></p>"41 42examples=[['food.jpeg']]43gr.Interface(inference,gr.inputs.Image(type="pil"),"text",title=title,description=description,article=article,examples=examples).launch(enable_queue=True)