NN Foundations: How Neural Network Works?

Comparing various image model architectures from timm library based on this notebook

Open In Colab

Compatibility Block

Check Platform

Platform & Environment Configuration

Imports

# import plotly.express as px
from fastai.basics import *
def plot_function(f, title, x=None, x_min=-2.1, x_max=2.1, ax=None, kind='line'):
    if x is None: x = torch.linspace(x_min, x_max, 100) 
    df = pd.DataFrame.from_dict({'x':x, 'y':f(x)})
    df.plot(x='x',y='y', title=title, kind=kind, ax=ax)
title='$3x^2+2x+1$'
f = lambda x: 3*x**2+2*x+1
plot_function(f, title)

def quad(a, b, c, x): return a*x**2+b*x+c
def mk_quad(a, b, c): return partial(quad, a, b, c)
def noise(x, scale): return torch.normal(0, scale, size=x.shape)
def add_noise(x, mult, add): return x*(1+noise(x, mult))+noise(x, add)
def noisy_f(x, f=f2, mult=0.15, add=0.15): return add_noise(f(x), mult, add)


torch.manual_seed(42)
x = torch.linspace(-2, 2, 20)
y = f2(x)
y_dash = add_noise(y, 0.15, 1.5)

x[:5], y[:5], y_dash[:5]

f2 = mk_quad(3,2,1)

# plot_function(f2, title)

# pd.Series(torch.randn(100)).plot(kind='hist')
# mult= add = 0.15
# add_noise(f2(x), mult, add)

ax = plt.gca()
plot_function(f2, title=title, x=x, ax=ax, kind='line')
plot_function(noisy_f, title=title, x=x, ax=ax, kind='scatter')

def get_integrated_data(activity_option, result_option):
  df_results = get_results(result_option)
  url_benchmark = f"https://github.com/huggingface/pytorch-image-models/raw/main/results/benchmark-{activity_option}-amp-nhwc-pt112-cu113-rtx3090.csv"
  df_benchmark = pd.read_csv(url_benchmark)
  df_integrated = df_results.merge(df_benchmark, on='model')
  df_integrated['is_tensorflow_model'] = df_integrated.model.str.split('_').str[0] =='tf'
  df_integrated['family'] = df_integrated.model.str.removeprefix("tf_").str.removeprefix("legacy_").str.removeprefix("nf_").str.removeprefix("nf_").str.extract('^([a-z]+?(?:v2|v3)?)(?:\d|_|$)')[0].values
  df_integrated.loc[df_integrated.model.str.contains('in22'), 'family'] = df_integrated.loc[df_integrated.model.str.contains('in22'), 'family'] + "_in22"
  df_integrated.loc[df_integrated.model.str.contains('resnet.*d'), 'family'] = df_integrated.loc[df_integrated.model.str.contains('resnet.*d'), 'family'] + "d"
  return df_integrated[~df_integrated.model.str.endswith('gn')] # Group norm models. Why Jeremy eliminated them from analysis?
# df_integrated= get_integrated_data('train', 'original')
# models = L(df_integrated.model.values.tolist()); models
# families = L(df_integrated.family.values.tolist()); families.unique()
# df_integrated[df_integrated.model.str.endswith('gn')] # Group norm models. Why Jeremy eliminated them from analysis?
# df_integrated[df_integrated.model.str.contains('rs')]
# df_integrated.model.str.lstrip("tf_").str.extract('^([a-z]+?(?:v2|v3)?)(?:\d|_|$0)')[0].values
# # "cspresnet50".removeprefix("legacy_")
# df_integrated= get_integrated_data('train', 'original')
# df_integrated
@st.cache_data
def get_filtered_data(df_integrated, subs, is_fullmatch=False, drop_tf=True):
  if drop_tf: df_integrated = df_integrated[~df_integrated.is_tensorflow_model]
  if not subs: return df_integrated
  elif is_fullmatch: return df_integrated[df_integrated.family.str.fullmatch(subs)]
  else: return df_integrated[df_integrated.model.str.contains(subs)]
NameError: name 'st' is not defined
def get_data(col_option, activity_option, result_option, subs, is_fullmatch=False, drop_tf=True):
  col = "_".join([activity_option, col_option])
  df_integrated = get_integrated_data(activity_option, result_option)
  df_integrated = get_filtered_data(df_integrated, subs, is_fullmatch=is_fullmatch, drop_tf=drop_tf)
  df_integrated['secs'] =1./df_integrated[col]
  return df_integrated
def plot_selection(df, title, col_option, activity_option, w=1000, h=800):
  size_col = "_".join([activity_option, col_option])
  return px.scatter(df, width=w, height=h, size=df[size_col]**2,trendline="ols", trendline_options={'log_x':True},
                    title=title, x="secs",log_x=True, y='top1', log_y=True,
                    color="family", hover_name='model_org',
                    hover_data=[size_col])
result_options = ['original', 'real'] #result = 'real'
activity_options = ['train', 'infer']
col_options = ['samples_per_sec', 'step_time', 'batch_size', 'img_size', 'gmacs', 'macts']
subs = '^re[sg]netd?|beit|convnext|levit|efficient|vit|vgg|swin'
is_fullmatch = False
drop_tf = False
subs = 'levit|resnetd?|regnetx|vgg|convnext.*|efficientnetv2|beit|swin'
is_fullmatch = True
result_option = result_options[0]
activity_option = activity_options[1]
col_option = col_options[0]
size_col_option = col_options[3]
title_dict = dict(zip(activity_options, ['Training', "Inference"]))
df = get_data(col_option, activity_option, result_option, subs, is_fullmatch=is_fullmatch, drop_tf=drop_tf)
fig = plot_selection(df, title_dict[activity_option], size_col_option, activity_option)
from IPython.display import HTML
HTML(fig.to_html())

Streamlit App

st.title("Which Image Model is best?")
col1, col2 = st.columns([1,3])
with col1:
    st.header("Settings")
    result_option = st.selectbox("Please choose dataset", result_options)
    activity_option = st.selectbox("Please choose activity", activity_options)
    subs = st.text_input("Subs", value='levit|resnetd?|regnetx|vgg|convnext.*|efficientnetv2|beit|swin')
    is_fullmatch = st.checkbox("Is fullmatch", value=True)
    drop_tf = st.checkbox("Drop Tensorflow Models", value=False)
    col_option = st.selectbox("Please choose col_option", col_options)
    size_col_option = st.selectbox("Please choose sizing col_option", col_options, index=3)
with col2:
    title_dict = dict(zip(activity_options, ['Training', "Inference"]))
    df = get_data(col_option, activity_option, result_option, subs, is_fullmatch=is_fullmatch, drop_tf=drop_tf)
    fig = plot_selection(df, None, size_col_option, activity_option, h=500, w=1000)
    # Plot!
    st.header(title_dict[activity_option])
    st.plotly_chart(fig, use_container_width=True, height=500)

Huggingface

huggingface_hub[fastai]
streamlit
plotly
pandas
statsmodels
import nbdev; nbdev.export.nb_export("03a_image_archs.ipynb", lib_path=".")
from huggingface_hub import create_repo, HfApi, notebook_login
notebook_login()
space_repo = "rahuketu86/Space-ImageArchs"
create_repo(space_repo, repo_type='space', space_sdk='streamlit', exist_ok=True)
files_to_upload=['requirements.txt', 'app.py']; files_to_upload
api = HfApi()
for fname in files_to_upload:
    api.upload_file(path_or_fileobj=fname, path_in_repo=fname, repo_id=space_repo, repo_type='space')
L(files_to_upload).map(lambda e: Path(e).unlink())