Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 10,445 Bytes
796b62e eb461b0 796b62e 5a6b49d 796b62e eb461b0 796b62e eb461b0 796b62e eb461b0 796b62e eb461b0 796b62e eb461b0 796b62e eb461b0 796b62e eb461b0 796b62e eb461b0 796b62e eb461b0 796b62e eb461b0 796b62e eb461b0 796b62e eb461b0 796b62e eb461b0 796b62e |
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
import plotly.express as px
import plotly.graph_objects as go
from utils import DEEPLITE_LIGHT_BLUE_HEX, load_yolobench_data
df, pareto_indices = load_yolobench_data()
METRIC_NAME_MAPPING = {
'[email protected]': 'mAP_0.5',
'[email protected]:0.95': 'mAP_0.5:0.95',
'Precision': 'precision',
'Recall': 'recall',
}
METRIC_KEYS_TO_NAMES = {v: k for k, v in METRIC_NAME_MAPPING.items()}
LATENCY_KEYS = {
'Raspberry Pi 4 Model B (CPU, TFLite, FP32)': 'raspi4_tflite_latency',
'Raspberry Pi 4 Model B (CPU, ONNX Runtime, FP32)': 'pi4_ort_latency',
'Raspberry Pi 5 Model B (CPU, ONNX Runtime, FP32)': 'pi5_ort_latency',
'Jetson Nano (GPU, ONNX Runtime, FP32)': 'nano_gpu_latency',
'Intel® Core™i7-10875H (CPU, OpenVINO, FP32)': 'openvino_latency',
'Khadas VIM3 (NPU, INT16)': 'vim3_latency',
'Orange Pi 5 (NPU, FP16)': 'orange_pi_latency',
'NVIDIA A40 (GPU, TensorRT, FP32)': 'a40_trt_latency',
}
LATENCY_KEYS_TO_NAMES = {v: k for k, v in LATENCY_KEYS.items()}
DATASET_TAGS = {
'PASCAL VOC': 'voc',
'SKU-110K': 'sku',
'WIDERFACE': 'wider',
'COCO': 'coco',
}
DATASET_TAGS_TO_NAMES = {v: k for k, v in DATASET_TAGS.items()}
def get_scatter_plot(
dataset_tag,
metric_tag,
latency_key,
model_family_coloring=True,
add_pareto_frontier=False,
plot_pareto_only=False,
log_axis=False,
):
fig_opts, layout_opts = {
'opacity': 0.5,
'color_discrete_sequence': [DEEPLITE_LIGHT_BLUE_HEX],
}, {}
if model_family_coloring:
fig_opts = {
'color': 'model_family',
'opacity': 0.75,
'color_discrete_sequence': px.colors.qualitative.Plotly,
}
layout_opts = {
'legend': dict(
title='Model family<br>(click to toggle)',
)
}
frontier = None
if plot_pareto_only:
metric_key = f'{metric_tag}_{dataset_tag}'
frontier = pareto_indices[metric_key][latency_key]
fig = px.scatter(
df if frontier is None else df.iloc[frontier, :],
x=latency_key,
y=f'{metric_tag}_{dataset_tag}',
title=f'{METRIC_KEYS_TO_NAMES[metric_tag]}-latency scatter plot',
hover_data={
'model_name': True,
'model_family': False,
latency_key: ':.2f',
f'{metric_tag}_{dataset_tag}': ':.2f',
},
labels={
'model_name': 'Model name',
latency_key: 'Latency',
f'{metric_tag}_{dataset_tag}': METRIC_KEYS_TO_NAMES[metric_tag],
},
template='plotly_white',
**fig_opts,
)
if log_axis:
fig.update_xaxes(type='log')
fig.update_layout(
height=600,
modebar_remove=[
'lasso',
'autoscale',
'zoomin',
'zoomout',
'select2d',
'select',
],
xaxis_title=f'{LATENCY_KEYS_TO_NAMES[latency_key]} latency, ms',
yaxis_title=f"{METRIC_KEYS_TO_NAMES[metric_tag]}",
xaxis=dict(
rangeslider=dict(
visible=True,
bgcolor=DEEPLITE_LIGHT_BLUE_HEX,
thickness=0.02,
),
),
yaxis=dict(
fixedrange=False,
),
hoverlabel=dict(
# bgcolor="white",
font_size=14,
font_family='Source Sans Pro',
),
**layout_opts,
)
if add_pareto_frontier:
fig = pareto_frontier_layer(fig, dataset_tag, metric_tag, latency_key)
fig.update_layout(autosize=True)
return fig
def get_comparison_plot(
dataset_tag,
metric_tag,
latency_keys,
log_axis=False,
remove_marker=False,
):
if len(latency_keys) == 0:
layout_opts = {
"annotations": [
{
"text": "Please select at least 2 hardware targets to compare!",
"showarrow": False,
"font": {"size": 28},
}
]
}
else:
layout_opts = {
'legend': dict(
title='Hardware targets selected<br>(click to toggle)',
)
}
fig = go.Figure(
layout=go.Layout(
title=go.layout.Title(
text=f'{METRIC_KEYS_TO_NAMES[metric_tag]}-latency Pareto Optimal Models'
)
)
)
if remove_marker:
mode = 'lines'
else:
mode = 'lines+markers'
for latency_key in latency_keys:
metric_key = f'{metric_tag}_{dataset_tag}'
frontier = pareto_indices[metric_key][latency_key]
df_pareto = df.iloc[frontier, :]
model_name = df_pareto['model_name']
fig.add_trace(
go.Scatter(
x=df_pareto[latency_key],
y=df_pareto[f'{metric_tag}_{dataset_tag}'],
name=LATENCY_KEYS_TO_NAMES[latency_key],
mode=mode,
customdata=model_name,
hovertemplate='<b>model name:%{customdata}</b><br>latency:%{x:.2f} <br>metric: %{y:.2f} ',
)
)
if log_axis:
fig.update_xaxes(type='log')
x_axis_title = f'Inference latency, ms (BS=1, log scale)'
else:
x_axis_title = f'Inference latency, ms (BS=1)'
fig.update_layout(
height=600,
plot_bgcolor='white',
modebar_remove=[
'lasso',
'autoscale',
'zoomin',
'zoomout',
'select2d',
'select',
],
xaxis_title=x_axis_title,
yaxis_title=f"{METRIC_KEYS_TO_NAMES[metric_tag]}",
xaxis=dict(
rangeslider=dict(
visible=True,
bgcolor=DEEPLITE_LIGHT_BLUE_HEX,
thickness=0.02,
),
),
yaxis=dict(
fixedrange=False,
),
hoverlabel=dict(
# bgcolor="white",
font_size=14,
font_family='Source Sans Pro',
),
**layout_opts,
)
fig.update_layout(autosize=True, template="plotly_white")
return fig
def pareto_frontier_layer(
fig,
dataset_tag,
metric_tag,
latency_key,
):
metric_key = f'{metric_tag}_{dataset_tag}'
frontier = pareto_indices[metric_key][latency_key]
fig.add_trace(
go.Scatter(
x=df.iloc[frontier, :][latency_key],
y=df.iloc[frontier, :][metric_key],
mode='lines+markers',
opacity=0.5,
line=go.scatter.Line(color='grey'),
showlegend=False,
name=metric_key,
marker=dict(symbol=['circle']),
)
)
return fig
def create_yolobench_plots(
dataset_name,
hardware_name,
metric_name,
vis_options,
table_mode,
):
model_family_coloring = 'Model family' in vis_options
add_pareto_frontier = 'Highlight Pareto' in vis_options
plot_pareto_only = 'Show Pareto only' in vis_options
log_axis = 'Log x-axis' in vis_options
fig = get_scatter_plot(
DATASET_TAGS[dataset_name],
METRIC_NAME_MAPPING[metric_name],
LATENCY_KEYS[hardware_name],
model_family_coloring,
add_pareto_frontier,
plot_pareto_only,
log_axis,
)
pareto_table = get_pareto_table(
dataset_name, hardware_name, metric_name, expand_table='Show all' in table_mode
)
return fig, pareto_table
def pareto_frontier_layer(
fig,
dataset_tag,
metric_tag,
latency_key,
):
metric_key = f'{metric_tag}_{dataset_tag}'
frontier = pareto_indices[metric_key][latency_key]
fig.add_trace(
go.Scatter(
x=df.iloc[frontier, :][latency_key],
y=df.iloc[frontier, :][metric_key],
mode='lines',
opacity=0.5,
line=go.scatter.Line(color='grey'),
showlegend=False,
name=metric_key,
)
)
return fig
def create_comparison_plot(dataset_name, hardware_list, metric_name, vis_options):
log_axis = 'Log x-axis' in vis_options
remove_marker = 'Remove datapoint markers' in vis_options
latency_keys = []
for hardware_name in hardware_list:
latency_keys.append(LATENCY_KEYS[hardware_name])
fig = get_comparison_plot(
DATASET_TAGS[dataset_name],
METRIC_NAME_MAPPING[metric_name],
latency_keys,
log_axis,
remove_marker,
)
return fig
def get_pareto_table(
dataset_name,
hardware_name,
metric_name,
expand_table=False,
):
dataset_tag = DATASET_TAGS[dataset_name]
metric_tag = METRIC_NAME_MAPPING[metric_name]
latency_key = LATENCY_KEYS[hardware_name]
metric_key = f'{metric_tag}_{dataset_tag}'
latency_key_final = f'{LATENCY_KEYS_TO_NAMES[latency_key]} latency, ms'
metric_key_final = METRIC_KEYS_TO_NAMES[metric_tag]
frontier = pareto_indices[metric_key][latency_key]
table_df = df.iloc[frontier, :][['model_name', metric_key, latency_key]]
table_df['Input resolution (px)'] = table_df['model_name'].apply(
lambda name: name.split('_')[-1]
)
table_df['Model name'] = table_df['model_name'].apply(
lambda name: name.split('_')[0]
)
table_df[metric_key_final] = table_df[metric_key].apply(lambda val: round(val, 3))
table_df[latency_key_final] = table_df[latency_key].apply(lambda val: round(val, 2))
def make_clickable(url, name):
return f'<a href="{url}">{name}</a>'
if dataset_name == 'COCO':
table_df['Download link'] = table_df['model_name'].apply(
lambda name: f'https://download.deeplite.ai/zoo/models/YOLOBench/{name.split("_")[0]}_640.pt'
)
table_df['Download link'] = table_df.apply(
lambda x: make_clickable(x['Download link'], 'Weights download'), axis=1
)
else:
table_df['Download link'] = table_df['model_name'].apply(
lambda s: 'Coming soon'
)
table_df = table_df[
[
'Model name',
'Input resolution (px)',
metric_key_final,
latency_key_final,
'Download link',
]
].sort_values(by=metric_key_final, ascending=False)
if not expand_table:
table_df = table_df.iloc[:10, :]
table_df = table_df.to_html(
classes='table', escape=False, render_links=True, index=False
)
return table_df
|