<jupyter_start><jupyter_text>Performance comparision on SOTAs<jupyter_code># Get the imagenet data & helper files 
!wget https://cs109b-course-data.s3.amazonaws.com/GEC/GEC.zip

# Unzip all the files 
!unzip -qq GEC.zip
!cp GEC/helper.py helper.py
# Import required libraries
import sys, os, time

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from matplotlib.colors import ListedColormap
colors = ['k', 'g', 'r','b','c']
plt.style.use('seaborn-whitegrid')
import helper
import pickle

from tensorflow.keras import backend as K
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import Input
import timeit<jupyter_output><empty_output><jupyter_text>Loading the data<jupyter_code># Useful dictionary to go from label index to actual label
with open('GEC/idx2name.pkl', 'rb') as handle:
    keras_idx_to_name = pickle.load(handle)
# Loading input image and labels
x_val = np.load("GEC/x_val.npy") # loaded as RGB
y_val = np.load("GEC/y_val.npy")

# One hot encoding the labels
y_val_one_hot = to_categorical(y_val, 1000)
# Print a sample image and set the label as title
plt.title(keras_idx_to_name[y_val[0]],fontsize=24,y=0.88)
plt.imshow(x_val[0].astype('int64'))<jupyter_output><empty_output><jupyter_text>⏸ What is the label for the first image in the validation set? A. Cabbage Butterfly B. Mixing bowl C. Wok D. French horn  Benchmark models<jupyter_code># Helper function to get key stats
# (evaluation speed, top-1 % accuracy, total model parameters)
def model_stats(model,x_val,name):
    #Time for evaluation
    time = timeit.timeit(lambda: model.predict(x_val, verbose=1), number=1)
    # Accuracy
    y_pred = model.predict(x_val)
    top_1 = np.any(np.argsort(y_pred)[:,-1:].T == y_val_one_hot.argmax(axis=1),axis=0).mean()
    # Model size 
    params = model.count_params()
    return (time,top_1,params,name)<jupyter_output><empty_output><jupyter_text>SOTA architecturesFor this exercise, we will consider the following SOTAs:- VGG16- VGG19- InceptionV3- ResNet50- MobileNet<jupyter_code># VGG16 stats
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input

# Preprocess step
# We need to call the data because some preprocess steps
# change the value inplace
x_val = np.load("GEC/x_val.npy") # loaded as RGB
x_val = x_val[:100]
x_val = preprocess_input(x_val)

# Call the VGG16 model
model = VGG16()

# Collect stats 
vgg16stats = model_stats(model,x_val,'VGG16')
# VGG19 stats
from tensorflow.keras.applications.vgg19 import VGG19, preprocess_input

x_val = np.load("GEC/x_val.npy") # loaded as RGB
x_val = x_val[:100]
x_val = preprocess_input(x_val)

# Call the VGG19 model
model = VGG19()
# Collect stats 
vgg19stats = model_stats(model,x_val,'VGG19')
# Inception Stats
from tensorflow.keras.applications.inception_v3 import InceptionV3,preprocess_input

x_val = np.load("GEC/x_val.npy") # loaded as RGB
x_val = x_val[:100]
x_val = preprocess_input(x_val)

# Call the InceptionV3 model
model = InceptionV3()

# Collect stats 
inceptionstats = model_stats(model,x_val,'Inception')
# Resnet50 stats
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input

x_val = np.load("GEC/x_val.npy") # loaded as RGB
x_val = x_val[:100]
x_val = preprocess_input(x_val)

# Call the ResNet50 model
model = ___

# Collect stats 
resnetstats = model_stats(model,x_val,'Resnet50')
# MobileNet stats
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2, preprocess_input
x_val = np.load("GEC/x_val.npy") # loaded as RGB
x_val = x_val[:100]
x_val = preprocess_input(x_val)

# Call the MobielNetV2 model
model = ___

# Collect stats 
mobilestats = model_stats(model,x_val,'MobileNet')<jupyter_output><empty_output><jupyter_text>⏸ Which SOTA architecture from above has the **highest** number of trainable parameters? A. VGG-16 B. VGG-19 C. ResNet50 D. MobileNet<jupyter_code># Use the helper code below 
# to plot the model stats for each SOTA
max_ylim = int(vgg19stats[0])+10
fig, ax  = plt.subplots(figsize=(10,6))
for i,val in enumerate([vgg16stats, vgg19stats, inceptionstats,resnetstats, mobilestats]):
    r = val[2]/10**9 + 0.04
    helper.ellipse(val[0]/max_ylim,val[1],width=r,height=0.44*r,color = colors[i],ax=ax)
    ax.text(val[0]/max_ylim + 0.035, val[1]+r/4+ 0.004, val[3], va='center', ha='center',fontsize=12)
    
ax.set_ylim([0.6,0.85])
ax.set_ylabel('Top-1 accuracy [%]',fontsize=20)
ax.set_xlabel('Time for evaluation [s]',fontsize=20)
ax.set_xticklabels(range(0,60,8));
ax.set_yticklabels(range(50,110,10));
for axis in ['bottom','left']:
  ax.spines[axis].set_linewidth(3)
  ax.spines[axis].set_color('k')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12124463519313304, 'fraction_numerical': 0.03412017167381974, 'mean_word_length': 5.774709302325581, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '257108', 'license': 'MIT', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1767, 'n_words': 409, 'path': 'L17_challenge.ipynb', 'repo_name': 'DavidAssaraf106/2021-CS109B', 'size': '4660'}