nanom commited on
Commit
8dce22b
0 Parent(s):

Duplicate from DiViorg/categories_error_analysis

Browse files
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.npy filter=lfs diff=lfs merge=lfs -text
13
+ *.npz filter=lfs diff=lfs merge=lfs -text
14
+ *.onnx filter=lfs diff=lfs merge=lfs -text
15
+ *.ot filter=lfs diff=lfs merge=lfs -text
16
+ *.parquet filter=lfs diff=lfs merge=lfs -text
17
+ *.pickle filter=lfs diff=lfs merge=lfs -text
18
+ *.pkl filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pt filter=lfs diff=lfs merge=lfs -text
21
+ *.pth filter=lfs diff=lfs merge=lfs -text
22
+ *.rar filter=lfs diff=lfs merge=lfs -text
23
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
24
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
25
+ *.tflite filter=lfs diff=lfs merge=lfs -text
26
+ *.tgz filter=lfs diff=lfs merge=lfs -text
27
+ *.wasm filter=lfs diff=lfs merge=lfs -text
28
+ *.xz filter=lfs diff=lfs merge=lfs -text
29
+ *.zip filter=lfs diff=lfs merge=lfs -text
30
+ *.zst filter=lfs diff=lfs merge=lfs -text
31
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
32
+ data/saiapr_tc-12.zip filter=lfs diff=lfs merge=lfs -text
33
+ data/full_pred_val_w_plurals_w_iou.json filter=lfs diff=lfs merge=lfs -text
34
+ data/full_pred_test_w_plurals_w_iou.json filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__/
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Categories Error Analysis
3
+ emoji: 🐛
4
+ colorFrom: blue
5
+ colorTo: gray
6
+ sdk: gradio
7
+ sdk_version: 3.2
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: DiViorg/categories_error_analysis
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from turtle import width
2
+ import gradio as gr
3
+ from utils import SampleClass
4
+
5
+ sample = SampleClass()
6
+
7
+ # --- Interface ---
8
+
9
+ demo = gr.Blocks(
10
+ title="Categories_error_analysis.ipynb",
11
+ css=".container { max-width: 98%; margin: auto;};")
12
+
13
+ with demo:
14
+ gr.Markdown("<h2><center> Categories Error Analysis 🐛</center></h2>")
15
+ with gr.Row():
16
+ with gr.Column():
17
+ with gr.Row():
18
+ category = gr.Dropdown(label="Category",value="relational",choices=["intrinsic","spatial","ordinal","relational","plural"])
19
+ predictions = gr.Dropdown(label='Predictions', value='fail',choices=["fail", "correct"])
20
+ with gr.Row():
21
+ model = gr.Dropdown(label='Model', value='baseline', choices=["baseline", "extended"])
22
+ split = gr.Dropdown(label='Split',value='val',choices=["test","val"])
23
+ with gr.Row():
24
+ username = gr.Dropdown(label="UserName",value="luciana",choices=["luciana",'mauri','jorge','nano'])
25
+ next_idx_sample = gr.Number(label='Next Idx Sample', value=0)
26
+ with gr.Row():
27
+ progress = gr.Label(label='Progress',num_top_classes=10)
28
+ with gr.Row():
29
+ btn_next = gr.Button("Get Next Sample")
30
+
31
+ with gr.Column():
32
+ with gr.Row():
33
+ img = gr.Image(label="Sample", type="numpy")
34
+ with gr.Row():
35
+ info = gr.Text(label="Info")
36
+ img_seg = gr.Image(label="Segmentation", type="numpy")
37
+
38
+ btn_next.click(
39
+ fn=sample.explorateSamples,
40
+ inputs=[username,predictions,category,model,split,next_idx_sample],
41
+ outputs=[next_idx_sample, progress, img, info, img_seg])
42
+
43
+ demo.queue(concurrency_count=10)
44
+ demo.launch(debug=False)
data/full_pred_test_w_plurals_w_iou.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:32d8075d6de0d60702cdfd884b43fe23140c7a10e28ddc865a03bc6cb84669fb
3
+ size 28012574
data/full_pred_val_w_plurals_w_iou.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e285882c1ad5c8ad5d9485bfce5f043b909564c5b68a61cf925c2c548b5a1d24
3
+ size 2581176
data/saiapr_tc-12.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cbeb079b66dd88ba58d15c5c421e983a65347527418228ad55022b7535983b35
3
+ size 2751748544
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ matplotlib
2
+ numpy
3
+ pandas
4
+ pillow
5
+ zipfile36
6
+ zipfile38
utils.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib as mpl
2
+ mpl.use('Agg')
3
+ import matplotlib.pyplot as plt
4
+
5
+ import pandas as pd
6
+ import matplotlib.patches as patches
7
+ import numpy as np
8
+ from PIL import Image
9
+ from zipfile import ZipFile
10
+ import gradio as gr
11
+
12
+ class SampleClass:
13
+ def __init__(self):
14
+ self.test_df = pd.read_json("data/full_pred_test_w_plurals_w_iou.json")
15
+ self.val_df = pd.read_json("data/full_pred_val_w_plurals_w_iou.json")
16
+ self.zip_file = ZipFile("data/saiapr_tc-12.zip", 'r')
17
+ self.filtered_df = None
18
+
19
+ def __get(self, img_path):
20
+ img_obj = self.zip_file.open(img_path)
21
+ img = Image.open(img_obj)
22
+ # img = np.array(img)
23
+ return img
24
+
25
+
26
+ def __loadPredictions(self, split, model):
27
+ assert(split in ['test','val'])
28
+ assert(model in ['baseline','extended'])
29
+
30
+ if split == "test":
31
+ df = self.test_df
32
+ elif split == "val":
33
+ df = self.val_df
34
+ else:
35
+ raise ValueError("File not available yet")
36
+
37
+ if model == 'baseline':
38
+ df = df.rename(columns={'baseline_hit':'hit', 'baseline_pred':'predictions',
39
+ 'extended_hit':'hit_other', 'extended_pred':'predictions_other',
40
+ 'baseline_iou':'iou',
41
+ 'extended_iou':'iou_other'}
42
+ )
43
+
44
+ elif model == 'extended':
45
+ df = df.rename(columns={'extended_hit':'hit', 'extended_pred':'predictions',
46
+ 'baseline_hit':'hit_other', 'baseline_pred':'predictions_other',
47
+ 'extended_iou':'iou',
48
+ 'baseline_iou':'iou_other'}
49
+ )
50
+ return df
51
+
52
+ def __getSample(self, id):
53
+ sample = self.filtered_df[self.filtered_df.sample_idx == id]
54
+
55
+ sent = sample['sent'].values[0]
56
+ pos_tags = sample['pos_tags'].values[0]
57
+ plural_tks = sample['plural_tks'].values[0]
58
+
59
+ cat_intrinsic = sample['intrinsic'].values[0]
60
+ cat_spatial = sample['spatial'].values[0]
61
+ cat_ordinal = sample['ordinal'].values[0]
62
+ cat_relational = sample['relational'].values[0]
63
+ cat_plural = sample['plural'].values[0]
64
+ categories = [('instrinsic',cat_intrinsic),
65
+ ('spatial',cat_spatial),
66
+ ('ordinal',cat_ordinal),
67
+ ('relational',cat_relational),
68
+ ('plural',cat_plural)]
69
+
70
+ hit = sample['hit'].values[0]
71
+ hit_o = sample['hit_other'].values[0]
72
+
73
+ iou = sample['iou'].values[0]
74
+ iou_o = sample['iou_other'].values[0]
75
+
76
+ prediction = {0:' FAIL ',1:' CORRECT '}
77
+
78
+ bbox_gt = sample['bbox'].values[0]
79
+ x1_gt,y1_gt,x2_gt,y2_gt = bbox_gt
80
+ # x1_gt,y1_gt,x2_gt,y2_gt = tuple(map(float,bbox_gt[1:-1].split(",")))
81
+
82
+ bp_bbox = sample['predictions'].values[0]
83
+ x1_pred,y1_pred,x2_pred,y2_pred = bp_bbox
84
+ # x1_pred,y1_pred,x2_pred,y2_pred = tuple(map(float,bp_bbox[1:-1].split(",")))
85
+
86
+ bp_o_bbox = sample['predictions_other'].values[0]
87
+ x1_pred_o,y1_pred_o,x2_pred_o,y2_pred_o = bp_o_bbox
88
+ # x1_pred_o,y1_pred_o,x2_pred_o,y2_pred_o = tuple(map(float,bp_o_bbox[1:-1].split(",")))
89
+
90
+ # Create Fig with predictions
91
+ img_path = "saiapr_tc-12"+sample['file_path'].values[0].split("saiapr_tc-12")[1]
92
+ img_seg_path = img_path.replace("images","segmented_images")
93
+
94
+
95
+ fig, ax = plt.subplots(1)
96
+ ax.imshow(self.__get(img_path), interpolation='bilinear')
97
+
98
+ # Create bbox's
99
+ rect_gt = patches.Rectangle((x1_gt,y1_gt), (x2_gt-x1_gt),(y2_gt-y1_gt),
100
+ linewidth=2, edgecolor='blue', facecolor='None') #fill=True, alpha=.3
101
+
102
+ rect_pred = patches.Rectangle((x1_pred,y1_pred), (x2_pred-x1_pred),(y2_pred-y1_pred),
103
+ linewidth=2, edgecolor='lightgreen', facecolor='none')
104
+
105
+ rect_pred_o = patches.Rectangle((x1_pred_o,y1_pred_o), (x2_pred_o-x1_pred_o),(y2_pred_o-y1_pred_o),
106
+ linewidth=2, edgecolor='red', facecolor='none')
107
+
108
+ ax.add_patch(rect_gt)
109
+ ax.add_patch(rect_pred)
110
+ ax.add_patch(rect_pred_o)
111
+ ax.axis('off')
112
+
113
+ info = {'Expresion':sent,
114
+ 'Idx Sample':str(id),
115
+ 'IoU': str(round(iou,2)) + "("+prediction[hit]+")",
116
+ 'IoU other': str(round(iou_o,2)) + "("+prediction[hit_o]+")",
117
+ 'Pos Tags':str(pos_tags),
118
+ 'PluralTks ':plural_tks,
119
+ 'Categories':",".join([c for c,b in categories if b])
120
+ }
121
+
122
+ plt.title(info['Expresion'], fontsize=12)
123
+ plt.tight_layout()
124
+ plt.close(fig)
125
+
126
+ fig.canvas.draw()
127
+ data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
128
+ w, h = fig.canvas.get_width_height()
129
+ img = data.reshape((int(h), int(w), -1))
130
+
131
+
132
+ return info, img, self.__get(img_seg_path)
133
+
134
+ def explorateSamples(self,
135
+ username,
136
+ predictions,
137
+ category,
138
+ model,
139
+ split,
140
+ next_idx_sample):
141
+
142
+ next_idx_sample = int(next_idx_sample)
143
+ hit = {'fail':0,'correct':1}
144
+ df = self.__loadPredictions(split, model)
145
+ self.filtered_df = df[(df[category] == 1) & (df.hit == hit[predictions])]
146
+
147
+
148
+ all_idx_samples = self.filtered_df.sample_idx.to_list()
149
+ parts = np.array_split(list(all_idx_samples), 4)
150
+ user_ids = {
151
+ 'luciana':list(parts[0]),
152
+ 'mauri':list(parts[1]),
153
+ 'jorge':list(parts[2]),
154
+ 'nano':list(parts[3])
155
+ }
156
+
157
+ try:
158
+ id_ = user_ids[username].index(next_idx_sample)
159
+ except:
160
+ id_ = 0
161
+
162
+ next_idx_sample = user_ids[username][ min(id_+1, len(user_ids[username])-1) ]
163
+ progress = {f"{id_}/{len(user_ids[username])-1}":id_/(len(user_ids[username])-1)}
164
+ info, img, img_seg = self.__getSample(user_ids[username][id_])
165
+ info = "".join([str(k)+":\t"+str(v)+"\n" for k,v in list(info.items())[1:]]).strip()
166
+
167
+ return (gr.Number.update(value=next_idx_sample),progress,img,info,img_seg)
168
+