File size: 2,411 Bytes
f903f2f
ec8f130
f903f2f
 
f17b4f0
f903f2f
 
 
 
 
 
 
 
 
ec8f130
 
 
 
f903f2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a34825
c06152d
 
e8b1f46
f903f2f
 
 
 
 
 
 
 
 
e8b1f46
f903f2f
 
 
 
f17b4f0
f903f2f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import torch

# Download an example image from the pytorch website
import urllib
url, filename = ("https://pytorch.org/assets/images/deeplab1.png", "test.png")
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
# sample execution (requires torchvision)
from PIL import Image
from torchvision import transforms
import gradio as gr
import matplotlib.pyplot as plt



model = torch.hub.load('pytorch/vision:v0.9.0', 'fcn_resnet101', pretrained=True)


def inference(input_image):
    preprocess = transforms.Compose([
        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)['out'][0]
    output_predictions = output.argmax(0)
    # create a color pallette, selecting a color for each class
    palette = torch.tensor([2 ** 25 - 1, 2 ** 15 - 1, 2 ** 21 - 1])
    colors = torch.as_tensor([i for i in range(21)])[:, None] * palette
    colors = (colors % 255).numpy().astype("uint8")

    # plot the semantic segmentation predictions of 21 classes in each color
    r = Image.fromarray(output_predictions.byte().cpu().numpy()).resize(input_image.size)
    r.putpalette(colors)
    plt.imshow(r)
    plt.axis('off')
    plt.savefig('out.png',bbox_inches='tight')
    return 'out.png'


title = "FCN-RESNET101"
description = "Gradio demo for FCN-RESNET101, Fully-Convolutional Network model with a ResNet-101 backbone. 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/1605.06211'>Fully Convolutional Networks for Semantic Segmentation</a> | <a href='https://github.com/pytorch/vision/blob/master/torchvision/models/segmentation/fcn.py'>Github Repo</a></p>"

gr.Interface(
    inference, 
    gr.inputs.Image(type="pil", label="Input"), 
    gr.outputs.Image(type="file", label="Output"),
    title=title,
    description=description,
    article=article,
    examples=[
        ["test.png"]
    ]).launch()