Spaces:
Runtime error
Runtime error
File size: 6,313 Bytes
8dce22b |
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.patches as patches
import numpy as np
from PIL import Image
from zipfile import ZipFile
import gradio as gr
class SampleClass:
def __init__(self):
self.test_df = pd.read_json("data/full_pred_test_w_plurals_w_iou.json")
self.val_df = pd.read_json("data/full_pred_val_w_plurals_w_iou.json")
self.zip_file = ZipFile("data/saiapr_tc-12.zip", 'r')
self.filtered_df = None
def __get(self, img_path):
img_obj = self.zip_file.open(img_path)
img = Image.open(img_obj)
# img = np.array(img)
return img
def __loadPredictions(self, split, model):
assert(split in ['test','val'])
assert(model in ['baseline','extended'])
if split == "test":
df = self.test_df
elif split == "val":
df = self.val_df
else:
raise ValueError("File not available yet")
if model == 'baseline':
df = df.rename(columns={'baseline_hit':'hit', 'baseline_pred':'predictions',
'extended_hit':'hit_other', 'extended_pred':'predictions_other',
'baseline_iou':'iou',
'extended_iou':'iou_other'}
)
elif model == 'extended':
df = df.rename(columns={'extended_hit':'hit', 'extended_pred':'predictions',
'baseline_hit':'hit_other', 'baseline_pred':'predictions_other',
'extended_iou':'iou',
'baseline_iou':'iou_other'}
)
return df
def __getSample(self, id):
sample = self.filtered_df[self.filtered_df.sample_idx == id]
sent = sample['sent'].values[0]
pos_tags = sample['pos_tags'].values[0]
plural_tks = sample['plural_tks'].values[0]
cat_intrinsic = sample['intrinsic'].values[0]
cat_spatial = sample['spatial'].values[0]
cat_ordinal = sample['ordinal'].values[0]
cat_relational = sample['relational'].values[0]
cat_plural = sample['plural'].values[0]
categories = [('instrinsic',cat_intrinsic),
('spatial',cat_spatial),
('ordinal',cat_ordinal),
('relational',cat_relational),
('plural',cat_plural)]
hit = sample['hit'].values[0]
hit_o = sample['hit_other'].values[0]
iou = sample['iou'].values[0]
iou_o = sample['iou_other'].values[0]
prediction = {0:' FAIL ',1:' CORRECT '}
bbox_gt = sample['bbox'].values[0]
x1_gt,y1_gt,x2_gt,y2_gt = bbox_gt
# x1_gt,y1_gt,x2_gt,y2_gt = tuple(map(float,bbox_gt[1:-1].split(",")))
bp_bbox = sample['predictions'].values[0]
x1_pred,y1_pred,x2_pred,y2_pred = bp_bbox
# x1_pred,y1_pred,x2_pred,y2_pred = tuple(map(float,bp_bbox[1:-1].split(",")))
bp_o_bbox = sample['predictions_other'].values[0]
x1_pred_o,y1_pred_o,x2_pred_o,y2_pred_o = bp_o_bbox
# x1_pred_o,y1_pred_o,x2_pred_o,y2_pred_o = tuple(map(float,bp_o_bbox[1:-1].split(",")))
# Create Fig with predictions
img_path = "saiapr_tc-12"+sample['file_path'].values[0].split("saiapr_tc-12")[1]
img_seg_path = img_path.replace("images","segmented_images")
fig, ax = plt.subplots(1)
ax.imshow(self.__get(img_path), interpolation='bilinear')
# Create bbox's
rect_gt = patches.Rectangle((x1_gt,y1_gt), (x2_gt-x1_gt),(y2_gt-y1_gt),
linewidth=2, edgecolor='blue', facecolor='None') #fill=True, alpha=.3
rect_pred = patches.Rectangle((x1_pred,y1_pred), (x2_pred-x1_pred),(y2_pred-y1_pred),
linewidth=2, edgecolor='lightgreen', facecolor='none')
rect_pred_o = patches.Rectangle((x1_pred_o,y1_pred_o), (x2_pred_o-x1_pred_o),(y2_pred_o-y1_pred_o),
linewidth=2, edgecolor='red', facecolor='none')
ax.add_patch(rect_gt)
ax.add_patch(rect_pred)
ax.add_patch(rect_pred_o)
ax.axis('off')
info = {'Expresion':sent,
'Idx Sample':str(id),
'IoU': str(round(iou,2)) + "("+prediction[hit]+")",
'IoU other': str(round(iou_o,2)) + "("+prediction[hit_o]+")",
'Pos Tags':str(pos_tags),
'PluralTks ':plural_tks,
'Categories':",".join([c for c,b in categories if b])
}
plt.title(info['Expresion'], fontsize=12)
plt.tight_layout()
plt.close(fig)
fig.canvas.draw()
data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
w, h = fig.canvas.get_width_height()
img = data.reshape((int(h), int(w), -1))
return info, img, self.__get(img_seg_path)
def explorateSamples(self,
username,
predictions,
category,
model,
split,
next_idx_sample):
next_idx_sample = int(next_idx_sample)
hit = {'fail':0,'correct':1}
df = self.__loadPredictions(split, model)
self.filtered_df = df[(df[category] == 1) & (df.hit == hit[predictions])]
all_idx_samples = self.filtered_df.sample_idx.to_list()
parts = np.array_split(list(all_idx_samples), 4)
user_ids = {
'luciana':list(parts[0]),
'mauri':list(parts[1]),
'jorge':list(parts[2]),
'nano':list(parts[3])
}
try:
id_ = user_ids[username].index(next_idx_sample)
except:
id_ = 0
next_idx_sample = user_ids[username][ min(id_+1, len(user_ids[username])-1) ]
progress = {f"{id_}/{len(user_ids[username])-1}":id_/(len(user_ids[username])-1)}
info, img, img_seg = self.__getSample(user_ids[username][id_])
info = "".join([str(k)+":\t"+str(v)+"\n" for k,v in list(info.items())[1:]]).strip()
return (gr.Number.update(value=next_idx_sample),progress,img,info,img_seg)
|