|
|
|
from doctest import OutputChecker |
|
import sys |
|
import torch |
|
import re |
|
import os |
|
import gradio as gr |
|
import requests |
|
|
|
|
|
|
|
|
|
from sentence_transformers import SentenceTransformer, util |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_sts = SentenceTransformer('roberta-large-nli-stsb-mean-tokens') |
|
|
|
|
|
|
|
|
|
from transformers import GPT2Tokenizer, GPT2LMHeadModel |
|
import numpy as np |
|
import re |
|
|
|
def Sort_Tuple(tup): |
|
|
|
|
|
tup.sort(key = lambda x: x[1]) |
|
return tup[::-1] |
|
|
|
|
|
def softmax(x): |
|
exps = np.exp(x) |
|
return np.divide(exps, np.sum(exps)) |
|
|
|
|
|
def get_sim(x): |
|
x = str(x)[1:-1] |
|
x = str(x)[1:-1] |
|
return x |
|
|
|
|
|
|
|
model = GPT2LMHeadModel.from_pretrained('gpt2', output_hidden_states = True, output_attentions = True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
tokenizer = GPT2Tokenizer.from_pretrained('gpt2') |
|
|
|
|
|
|
|
def cloze_prob(text): |
|
|
|
whole_text_encoding = tokenizer.encode(text) |
|
|
|
text_list = text.split() |
|
stem = ' '.join(text_list[:-1]) |
|
stem_encoding = tokenizer.encode(stem) |
|
|
|
|
|
cw_encoding = whole_text_encoding[len(stem_encoding):] |
|
|
|
|
|
tokens_tensor = torch.tensor([whole_text_encoding]) |
|
|
|
with torch.no_grad(): |
|
outputs = model(tokens_tensor) |
|
predictions = outputs[0] |
|
|
|
logprobs = [] |
|
|
|
start = -1-len(cw_encoding) |
|
for j in range(start,-1,1): |
|
raw_output = [] |
|
for i in predictions[-1][j]: |
|
raw_output.append(i.item()) |
|
|
|
logprobs.append(np.log(softmax(raw_output))) |
|
|
|
|
|
|
|
|
|
|
|
conditional_probs = [] |
|
for cw,prob in zip(cw_encoding,logprobs): |
|
conditional_probs.append(prob[cw]) |
|
|
|
|
|
|
|
return np.exp(np.sum(conditional_probs)) |
|
|
|
|
|
|
|
|
|
|
|
def cos_sim(a, b): |
|
return np.inner(a, b) / (np.linalg.norm(a) * (np.linalg.norm(b))) |
|
|
|
|
|
|
|
def Visual_re_ranker(caption_G, caption_B, caption_VR, visual_context_label, visual_context_prob): |
|
caption_G = caption_G |
|
caption_B = caption_B |
|
caption_VR = caption_VR |
|
visual_context_label= visual_context_label |
|
visual_context_prob = visual_context_prob |
|
caption_emb_G = model_sts.encode(caption_G, convert_to_tensor=True) |
|
caption_emb_B = model_sts.encode(caption_B, convert_to_tensor=True) |
|
caption_emb_VR = model_sts.encode(caption_VR, convert_to_tensor=True) |
|
|
|
visual_context_label_emb = model_sts.encode(visual_context_label, convert_to_tensor=True) |
|
|
|
|
|
sim_1 = cosine_scores = util.pytorch_cos_sim(caption_emb_G, visual_context_label_emb) |
|
sim_1 = sim_1.cpu().numpy() |
|
sim_1 = get_sim(sim_1) |
|
|
|
sim_2 = cosine_scores = util.pytorch_cos_sim(caption_emb_B, visual_context_label_emb) |
|
sim_2 = sim_2.cpu().numpy() |
|
sim_2 = get_sim(sim_2) |
|
|
|
sim_3 = cosine_scores = util.pytorch_cos_sim(caption_emb_VR, visual_context_label_emb) |
|
sim_3 = sim_3.cpu().numpy() |
|
sim_3 = get_sim(sim_3) |
|
|
|
|
|
LM_1 = cloze_prob(caption_G) |
|
LM_2 = cloze_prob(caption_B) |
|
LM_3 = cloze_prob(caption_VR) |
|
|
|
|
|
score_1 = pow(float(LM_1),pow((1-float(sim_1))/(1+ float(sim_1)),1-float(visual_context_prob))) |
|
score_2 = pow(float(LM_2),pow((1-float(sim_2))/(1+ float(sim_2)),1-float(visual_context_prob))) |
|
score_3 = pow(float(LM_3),pow((1-float(sim_3))/(1+ float(sim_3)),1-float(visual_context_prob))) |
|
|
|
|
|
return {"Greedy": float(score_1)/1, "Best-Beam-5": float(score_2)/1, "Visual_re-Ranker": float(score_3)/1 } |
|
|
|
|
|
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
fn=Visual_re_ranker, |
|
|
|
description="Demo for Caption Re-ranker with Visual Semantic Information", |
|
|
|
|
|
inputs=[gr.Textbox(value="baby is eating in front of a birthday cake") , gr.Textbox(value="a baby sitting in front of a cake"), gr.Textbox(value="a baby sitting in front of a birthday cake"), gr.Textbox(value="candle wax light"), gr.Textbox(value="0.958")], |
|
|
|
outputs="label", |
|
) |
|
|
|
demo.launch() |