-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
45 lines (37 loc) · 1.31 KB
/
demo.py
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
import gradio as gr
import torch
from models import *
from evaluation import *
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def main():
print("Device being used: ", device)
# Parâmetros do decoder
hidden_size = 512
emb_size = 512
attention_size = 512
img_emb_size = 14 * 14
num_channels = 512
dropout = 0.4
# Carregando modelos
checkpoint = torch.load("checkpoints/best.pth")
vocab = torch.load("checkpoints/vocab.pth")
encoder = Encoder().to(device)
decoder = Decoder(hidden_size, len(vocab), emb_size, attention_size, img_emb_size, num_channels, dropout).to(device)
encoder.load_state_dict(checkpoint["encoder"])
decoder.load_state_dict(checkpoint["decoder"])
caption_image_interface = gr.Interface(
fn=lambda image: string_from_caption(caption_image(encoder, decoder, image, vocab)[0]),
inputs="image",
outputs="text"
)
visualize_attention_interface = gr.Interface(
fn=lambda image: visualize_attention(encoder, decoder, image, vocab),
inputs="image",
outputs="image"
)
demo = gr.TabbedInterface(
[caption_image_interface, visualize_attention_interface],
["Caption Image", "Visualize Attention"]
).launch(share=False)
if __name__ == "__main__":
main()