File size: 1,793 Bytes
cb8043e
 
 
 
 
 
 
 
 
 
 
 
 
ed167eb
cb8043e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import models
import torch
import torchvision.transforms as transforms
import cv2
import numpy as np


# initialize the computation device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#intialize the model
model = models.model(pretrained=False, requires_grad=False).to(device)
# load the model checkpoint
checkpoint = torch.load('model.pth', map_location=device)
# load model weights state_dict
model.load_state_dict(checkpoint['model_state_dict'])
model.eval()

transform = transforms.Compose([
            transforms.ToPILImage(),
            transforms.ToTensor(),
            ])

genres = ['Action', 'Adventure', 'Animation', 'Biography', 'Comedy', 'Crime',
 'Documentary', 'Drama', 'Family', 'Fantasy', 'History', 'Horror', 'Music',
 'Musical', 'Mystery', 'N/A', 'News', 'Reality-TV', 'Romance', 'Sci-Fi', 'Short',
 'Sport', 'Thriller', 'War', 'Western']


def segment(image):
    image = np.asarray(image)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = transform(image)
    image = torch.tensor(image, dtype=torch.float32)
    image = image.to(device)
    image = torch.unsqueeze(image, dim=0)
    # get the predictions by passing the image through the model
    outputs = model(image)
    outputs = torch.sigmoid(outputs)
    outputs = outputs.detach().cpu()

    out_dict = {k: v for k, v in zip(genres, outputs.tolist()[0])}
    return out_dict

iface = gr.Interface(fn=segment, 
                     inputs="image", 
                     outputs="label",
                     title="Poster classification",
                     description="classify the genre of your poster by uploading an image",
                     examples=[["imagenes/tt0084058.jpg"], ["imagenes/tt0084867.jpg"], ["imagenes/tt0085121.jpg"]]).launch()