Download app.py from coderx7/simplenet: direct link, hf CLI and curl.
- Browser
- Download file 3.19 kB
-
https://huggingface.co/spaces/coderx7/simplenet/resolve/main/app.py
- Command line
-
hf download hf://spaces/coderx7/simplenet/app.py
-
curl -L -o app.py https://huggingface.co/spaces/coderx7/simplenet/resolve/main/app.py
3.19 kB
| import os | |
| from PIL import Image | |
| import gradio as gr | |
| import torch | |
| from torchvision import transforms | |
| os.system("wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt") | |
| model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0","simplenetv1_5m_m1", pretrained=True, trust_repo=True) | |
| # or any of these variants | |
| # model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0","simplenetv1_5m_m2", pretrained=True) | |
| # model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0","simplenetv1_9m_m1", pretrained=True) | |
| # model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0","simplenetv1_9m_m2", pretrained=True) | |
| # model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0","simplenetv1_small_m1_05", pretrained=True) | |
| # model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0","simplenetv1_small_m2_05", pretrained=True) | |
| # model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0","simplenetv1_small_m1_075", pretrained=True) | |
| # model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0","simplenetv1_small_m2_075", pretrained=True) | |
| model.eval() | |
| # Download an example image from the pytorch website | |
| torch.hub.download_url_to_file("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg") | |
| def inference(input_image): | |
| preprocess = transforms.Compose([ | |
| transforms.Resize(256), | |
| transforms.CenterCrop(224), | |
| transforms.ToTensor(), | |
| transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), | |
| ]) | |
| input_tensor = preprocess(input_image) | |
| input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model | |
| # move the input and model to GPU for speed if available | |
| if torch.cuda.is_available(): | |
| input_batch = input_batch.to('cuda') | |
| model.to('cuda') | |
| with torch.no_grad(): | |
| output = model(input_batch) | |
| # The output has unnormalized scores. To get probabilities, you can run a softmax on it. | |
| probabilities = torch.nn.functional.softmax(output[0], dim=0) | |
| # Read the categories | |
| with open("imagenet_classes.txt", "r") as f: | |
| categories = [s.strip() for s in f.readlines()] | |
| # Show top categories per image | |
| top5_prob, top5_catid = torch.topk(probabilities, 5) | |
| result = {} | |
| for i in range(top5_prob.size(0)): | |
| result[categories[top5_catid[i]]] = top5_prob[i].item() | |
| return result | |
| inputs = gr.Image(type='pil') | |
| outputs = gr.Label(num_top_classes=5) | |
| title = "SimpleNet" | |
| description = "Gradio demo for SimpleNet network pre-trained on ImageNet. This demo uses the simplenet_5m_m1 variant. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below." | |
| article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1608.06037' target='_blank'>Lets Keep it simple, Using simple architectures to outperform deeper and more complex architectures</a> | <a href='https://github.com/Coderx7/SimpleNet_Pytorch/blob/master/imagenet/simplenet.py' target='_blank'>Github Repo</a></p>" | |
| examples = [['dog.jpg']] | |
| gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch() |