-
Notifications
You must be signed in to change notification settings - Fork 15
/
coco_explorer.py
208 lines (179 loc) · 9.95 KB
/
coco_explorer.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import argparse
import os
import re
import pandas as pd
import numpy as np
import plotly.express as px
import streamlit as st
from pycocotools.coco import COCO
from cocoinspector import CoCoInspector
@st.cache(allow_output_mutation=True)
def get_inspector(coco_train, coco_predictions, images_path, eval_type, iou_min, iou_max):
coco = COCO(coco_train)
coco_dt = coco.loadRes(coco_predictions)
inspector = CoCoInspector(coco, coco_dt, base_path=images_path,
iou_type=eval_type, iou_min=iou_min, iou_max=iou_max)
inspector.evaluate()
inspector.calculate_stats()
return inspector
def app(args):
st.set_page_config(layout='wide')
st.title('COCO Explorer')
ioumin = st.sidebar.slider("Minimum IoU", min_value=0.0, max_value=1.0, value=args.iou_min)
ioumax = st.sidebar.slider("Maximum IoU", min_value=0.0, max_value=1.0, value=args.iou_max)
topbox = st.sidebar.selectbox("Choose what to do ", ['inspect predictions visually',
'inspect image statistics',
'inspect annotations',
'CoCo scores'
])
inspector = get_inspector(args.coco_train, args.coco_predictions, args.images_path,
args.eval_type, ioumin, ioumax)
if topbox == 'inspect predictions visually':
st.sidebar.subheader('Inspect predictions')
vis_options = {'true positives': 'tp',
'ground truth': 'gt',
'false negatives': 'fn',
'false positives': 'fp',
}
st.sidebar.text("""
What to show on image
TP - results matching GT (orange)
FP - results not matching GT (teal)
FN - GT not matching results (red)
GT - all ground truth (green)
""")
ms = st.sidebar.multiselect("",
list(vis_options.keys()),
default=list(vis_options.keys())
)
st.sidebar.subheader('Visual settings')
size = st.sidebar.slider('plot resolution', min_value=1, max_value=50, value=15)
score = st.sidebar.slider('score threshold', min_value=0.0, max_value=1.0, value=0.5)
draw_pred_mask = st.sidebar.checkbox("Draw predictions masks (red)")
draw_gt_mask = st.sidebar.checkbox("Draw ground truth masks (green)")
adjust_labels = st.sidebar.checkbox("Optimize label placement")
r = st.sidebar.radio('Inspect by', options=['image_id', 'category', 'precision'])
if r == 'image_id':
path = st.text_input('select image by path or filter by regular expression:',)
image_ids = inspector.image_ids
if path:
r = inspector._path2imageid(path)
if r < 0:
r = 0
try:
pattern = re.compile(path)
image_ids = inspector.get_images_for_file_name(pattern.match)
except Exception:
image_ids = []
if not image_ids:
st.error('No such image file_name')
image_ids = inspector.image_ids
else:
r = image_ids.index(r)
else:
r = 0
if len(image_ids) > 1:
r = st.slider('slider trough all images', value=r, min_value=0, max_value=len(image_ids)-1)
path = inspector._imageid2path(image_ids[r])
st.text(path)
print(path)
f, fn = inspector.visualize_image(image_ids[r],
draw_gt_mask=draw_gt_mask,
draw_pred_mask=draw_pred_mask,
adjust_labels=adjust_labels,
score_threshold=score,
fontsize=size,
show_only=[vis_options[o] for o in ms],
figsize=(size, size))
st.pyplot(f[0])
imscores = inspector.image_scores_agg
if image_ids[r] in imscores.index:
st.dataframe(imscores.loc[image_ids[r]])
if r == 'category':
category = st.sidebar.selectbox(label='select by category',
options=[c['name'] for c in inspector.categories])
exclusive = st.sidebar.checkbox(label='Show only this category')
print(category)
if category:
image_ids = inspector.get_images_with_category(category)
image_ids = np.random.permutation(image_ids)
for img in image_ids[:10]:
print(img)
f, fn = inspector.visualize_image(img,
draw_gt_mask=draw_gt_mask,
draw_pred_mask=draw_pred_mask,
adjust_labels=adjust_labels,
only_categories=[category] if exclusive else [],
score_threshold=score,
show_only=[vis_options[o] for o in ms],
fontsize=size,
figsize=(size, size))
st.pyplot(f[0])
if len(image_ids) > 10:
st.button('Sample 10 more images')
if r == 'precision':
prec_min = st.slider(label='Minimum precision', min_value=0.0, max_value=1.0, value=0.0)
prec_max = st.slider(label='Maximum precision', min_value=0.0, max_value=1.0, value=0.3)
agg = inspector.image_scores_agg
agg = agg[agg.precision.between(prec_min, prec_max)]
image_ids = np.random.permutation(agg.index)
for img in image_ids[:10]:
print(img)
f, fn = inspector.visualize_image(img,
draw_gt_mask=draw_gt_mask,
draw_pred_mask=draw_pred_mask,
adjust_labels=adjust_labels,
score_threshold=score,
show_only=[vis_options[o] for o in ms],
fontsize=size,
figsize=(size, size))
st.pyplot(f[0])
if len(image_ids) > 10:
st.button('Sample 10 more images')
elif topbox == 'inspect image statistics':
st.plotly_chart(px.histogram(inspector.images_df, x='aspect_ratio', title='aspect ratio distribiution',
hover_name=inspector.images_df.file_name))
st.plotly_chart(px.histogram(inspector.images_df, x='width', title='image width distribiution'))
st.plotly_chart(px.histogram(inspector.images_df, x='height', title="image height distribiution"))
elif topbox == 'inspect annotations':
df = pd.DataFrame(inspector.annot_df.category_name.value_counts().reset_index())
dfarea = pd.DataFrame(
inspector.annot_df.groupby('category_name')['area'].mean().sort_values(ascending=False)).reset_index()
# annot_aspect = pd.DataFrame(inspector.annot_df.groupby('category_name')['ann_ar'].mean().sort_values(ascending=False)).reset_index()
df.columns = ['category_name', 'category_count']
st.plotly_chart(
px.bar(df, x='category_name', y='category_count', title='annotation count per class'))
st.plotly_chart(
px.bar(dfarea, x='category_name', y='area', title='avg object size(area) per class'))
st.plotly_chart(px.histogram(inspector.annot_df, x='ann_ar', title="Bounding box aspect ratio distribiution"))
elif topbox == 'CoCo scores':
st.subheader("Shows per class mAP scores as calculated by pycocotools")
st.sidebar.header('Inspect predictions')
df = inspector.ap_per_class()
st.dataframe(df)
st.subheader("Average mAP by class")
st.dataframe(df.mean(axis=1))
x = df.mean(axis=1).sort_values(ascending=False).reset_index()
x.columns = ['category', 'AP']
# print("ok")
st.plotly_chart(px.bar(x, y='AP', x='category'))
st.subheader("Original CoCoeval output")
st.text(body=inspector.cocoeval_scores)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--coco_train", type=str, required=True, metavar="PATH/TO/COCO.json",
help="COCO dataset to inspect")
parser.add_argument("--coco_predictions", type=str, required=True, metavar="PATH/TO/COCO.json",
help="COCO annotations to compare to")
parser.add_argument("--images_path", type=str, default=os.getcwd(), metavar="PATH/TO/IMAGES/",
help="Directory path to prepend to file_name paths in COCO")
parser.add_argument("--eval_type", type=str, default="bbox", choices={"bbox", "segm", "keypoints"},
help="Mode of comparison (where to look for a 'match')")
parser.add_argument("--iou_min", type=float, default=0.5,
help="Initial minimum IoU (overlap) (what constitutes a 'match')")
parser.add_argument("--iou_max", type=float, default=0.95,
help="Initial maximum IoU (overlap) (what constitutes a 'match')")
args = parser.parse_args()
if args.images_path[-1] != '/':
args.images_path += '/'
app(args)