File size: 4,048 Bytes
c59a911
 
 
 
 
 
 
 
 
 
 
 
 
b36c717
 
 
 
33c60f2
b36c717
c59a911
4eda6d9
b36c717
 
 
 
 
 
c59a911
 
 
 
 
 
 
 
e1d5e6c
c59a911
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fac71f
c59a911
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fac71f
c59a911
f81eeee
 
b4f6b38
f81eeee
c59a911
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import torch
from PIL import Image
from torchvision import datasets, models, transforms
import gradio as gr
import os
import torch.nn as nn

#os.system("wget https://github.com/liuxiaoyuyuyu/vanGogh-and-Other-Artist/blob/main/artist_classes.txt")
#os.system("wget https://github.com/liuxiaoyuyuyu/vanGogh-and-Other-Artist/blob/main/model_weights_mobilenet_v2_valp1trainp2.pth")

#model = torch.hub.load('pytorch/vision:v0.9.0', 'mobilenet_v2', pretrained=False)
#checkpoint = 'https://github.com/liuxiaoyuyuyu/vanGogh-and-Other-Artist/blob/main/model_weights_mobilenet_v2_valp1trainp2.pth'
#model.load_state_dict(torch.hub.load_state_dict_from_url(checkpoint, progress=False))
#model = models.efficientnet_b0()
#num_ftrs = model.classifier[1].in_features
#model.classifier[1] = nn.Linear(num_ftrs, 6)
#device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
#model = model.to(device)
#model.load_state_dict(torch.load('model_weights_EfficientNetB0_final.pth',map_location=device))


model = models.vgg16()
num_ftrs = model.classifier[6].in_features
model.classifier[6] = nn.Linear(num_ftrs, 6)
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
model = model.to(device)
model.load_state_dict(torch.load('VGG16_weights_May28.pth',map_location=device))
#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(260),
        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')
    else:
        model.to('cpu')

    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
    categories = {
        0:"Vincent van Gogh",
        1:"Monet",
        2:"Leonardo da Vinci",
        3:"Rembrandt",
        4:"Pablo Picasso",
        5:"Salvador Dali"
    }
    
    # Show top categories per image
    top5_prob, top5_catid = torch.topk(probabilities, 6)
    result = {}
    for i in range(top5_prob.size(0)):
        result[categories[top5_catid[i].item()]] = top5_prob[i].item()
    return result

inputs = gr.Image(type='pil',label='Insert the image')
outputs = gr.Label(type="confidences",num_top_classes=5,label='Prediction')

title = "Artist Classifier"
description = "The classifier is a demo classifier to predict the painter using fine-tuned VGG16. Transfer learning is adopted that significantly reduces the time/resource cost.  It allows you to identify the creator of a painting among Vincent van Gogh, Claude Monet, Leonardo da Vinci, Rembrandt, Pablo Picasso, and Salvador Dali. Just upload the image to the left blank box and click the Submit button. A list of confidence will be displayed. Following the link below to find a related work that helps to create your own paintings following the style of painters"

article = """
<p style='text-align: left'><a href='https://huggingface.co/spaces/breynolds1247/StarryNight_StyleTransfer'>Style Transfer: Create your own stylish paintings </a></p>
<p style='text-align: left'>The app is based on <a href='https://arxiv.org/abs/1409.1556'>Very Deep Convolutional Networks</a></p>
"""

#examples = [
#            ['dog.jpg']
#]
#gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch()
gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, analytics_enabled=False).launch()