import gradio as gr import pandas as pd from fuzzywuzzy import fuzz def load_leaderboard(): imagenet_df = pd.read_csv('https://raw.githubusercontent.com/huggingface/pytorch-image-models/main/results/results-imagenet.csv') imagenet_real_df = pd.read_csv('https://raw.githubusercontent.com/huggingface/pytorch-image-models/main/results/results-imagenet-real.csv') imagenetv2_df = pd.read_csv('https://raw.githubusercontent.com/huggingface/pytorch-image-models/main/results/results-imagenetv2-matched-frequency.csv') sketch_df = pd.read_csv('https://raw.githubusercontent.com/huggingface/pytorch-image-models/main/results/results-sketch.csv') imagenet_a_df = pd.read_csv('https://raw.githubusercontent.com/huggingface/pytorch-image-models/main/results/results-imagenet-a.csv') imagenet_r_df = pd.read_csv('https://raw.githubusercontent.com/huggingface/pytorch-image-models/main/results/results-imagenet-r.csv') # columns to remove from each dataframe remove_column_names = ["top1_err", "top5_err", "top1_diff", "top5_diff", "rank_diff"] for remove_column_name in remove_column_names: if remove_column_name in imagenet_df.columns: imagenet_df = imagenet_df.drop(columns=remove_column_name) if remove_column_name in imagenet_real_df.columns: imagenet_real_df = imagenet_real_df.drop(columns=remove_column_name) if remove_column_name in imagenetv2_df.columns: imagenetv2_df = imagenetv2_df.drop(columns=remove_column_name) if remove_column_name in sketch_df.columns: sketch_df = sketch_df.drop(columns=remove_column_name) if remove_column_name in imagenet_a_df.columns: imagenet_a_df = imagenet_a_df.drop(columns=remove_column_name) if remove_column_name in imagenet_r_df.columns: imagenet_r_df = imagenet_r_df.drop(columns=remove_column_name) # Rename top1 and top5 columns to the name of the dataframe+top1/top5 imagenet_df = imagenet_df.rename(columns={"top1": "imagenet_top1", "top5": "imagenet_top5"}) imagenet_real_df = imagenet_real_df.rename(columns={"top1": "imagenet_real_top1", "top5": "imagenet_real_top5"}) imagenetv2_df = imagenetv2_df.rename(columns={"top1": "imagenetv2_top1", "top5": "imagenetv2_top5"}) sketch_df = sketch_df.rename(columns={"top1": "sketch_top1", "top5": "sketch_top5"}) imagenet_a_df = imagenet_a_df.rename(columns={"top1": "imagenet_a_top1", "top5": "imagenet_a_top5"}) imagenet_r_df = imagenet_r_df.rename(columns={"top1": "imagenet_r_top1", "top5": "imagenet_r_top5"}) # Merge all dataframes result = pd.merge(imagenet_df, imagenet_real_df, on=['model', 'param_count', 'img_size', 'crop_pct', 'interpolation'], how='outer') result = pd.merge(result, imagenetv2_df, on=['model', 'param_count', 'img_size', 'crop_pct', 'interpolation'], how='outer') result = pd.merge(result, sketch_df, on=['model', 'param_count', 'img_size', 'crop_pct', 'interpolation'], how='outer') result = pd.merge(result, imagenet_a_df, on=['model', 'param_count', 'img_size', 'crop_pct', 'interpolation'], how='outer') result = pd.merge(result, imagenet_r_df, on=['model', 'param_count', 'img_size', 'crop_pct', 'interpolation'], how='outer') # Average top1 and top5 and add the average column after `model` column result['average_top1'] = result[['imagenet_top1', 'imagenet_real_top1', 'imagenetv2_top1', 'sketch_top1', 'imagenet_a_top1', 'imagenet_r_top1']].mean(axis=1) result['average_top5'] = result[['imagenet_top5', 'imagenet_real_top5', 'imagenetv2_top5', 'sketch_top5', 'imagenet_a_top5', 'imagenet_r_top5']].mean(axis=1) result = result[['model', 'average_top1', 'average_top5', 'param_count', 'img_size', 'crop_pct', 'interpolation', 'imagenet_top1', 'imagenet_top5', 'imagenet_real_top1', 'imagenet_real_top5', 'imagenetv2_top1', 'imagenetv2_top5', 'sketch_top1', 'sketch_top5', 'imagenet_a_top1', 'imagenet_a_top5', 'imagenet_r_top1', 'imagenet_r_top5']] result = result.sort_values(by='average_top1', ascending=False) # Round the values to 3 decimal places result = result.round(3) return result global df df = load_leaderboard() def search_leaderboard(model_name): if not model_name: return df threshold = 95 # You can adjust this value to make the search more or less strict def calculate_similarity(row): similarity = fuzz.partial_ratio(model_name.lower(), row['model'].lower()) return similarity if similarity >= threshold else 0 # Add a new column for similarity scores df['similarity'] = df.apply(calculate_similarity, axis=1) # Filter and sort the dataframe filtered_df = df[df['similarity'] > 0].sort_values('similarity', ascending=False) # Remove the similarity column before returning filtered_df = filtered_df.drop('similarity', axis=1) return filtered_df with gr.Blocks("Timm Leaderboard") as app: gr.HTML("

PyTorch Image Models Leaderboard

") gr.HTML("

This leaderboard is based on the results of the models from the PyTorch Image Models repository.

") with gr.Row(): search_bar = gr.Textbox(lines=1, label="Search Model (You can press Enter to Search)", placeholder="Search for a model", scale=4) search_btn = gr.Button(value="Search", variant="primary", scale=1) leaderboard = gr.Dataframe(df) refresh_button = gr.Button(value="Refresh Leaderboard", variant="primary") refresh_button.click(load_leaderboard, outputs=[leaderboard]) search_btn.click(search_leaderboard, inputs=[search_bar], outputs=[leaderboard]) search_bar.submit(search_leaderboard, inputs=[search_bar], outputs=[leaderboard]) app.launch()