<jupyter_start><jupyter_text>Determine paths and directories<jupyter_code>path_imagenet_val_dataset = Path("data/") # path/to/data/
dir_images = Path("data/val") # path/to/images/directory
path_labels = Path("data/ILSVRC2012_validation_ground_truth.txt")
path_synset_words = Path("data/synset_words.txt")
path_meta = Path("data/meta.mat")<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code>x_val_paths = glob(str(path_imagenet_val_dataset / "x_val*.npy"))

# Sort filenames in ascending order
x_val_paths.sort(key=lambda f: int(re.sub('\D', '', f)))
y_val = np.load(str(path_imagenet_val_dataset / "y_val.npy"))
y_val_one_hot = to_categorical(y_val, 1000)<jupyter_output><empty_output><jupyter_text>Benchmark models<jupyter_code>def top_k_accuracy(y_true, y_pred, k=1, tf_enabled=True):
    '''
    Calculates top_k accuracy of predictions. Expects both y_true and y_pred to be one-hot encoded.
    numpy implementation is from: https://github.com/chainer/chainer/issues/606
    '''

    if tf_enabled:
        argsorted_y = tf.argsort(y_pred)[:,-k:]
        matches = tf.cast(tf.math.reduce_any(tf.transpose(argsorted_y) == tf.argmax(y_true, axis=1, output_type=tf.int32), axis=0), tf.float32)
        return tf.math.reduce_mean(matches).numpy()
    else:
        argsorted_y = np.argsort(y_pred)[:,-k:]
        return np.any(argsorted_y.T == y_true.argmax(axis=1), axis=0).mean()
K.clear_session()
model = vgg19.VGG19()
y_pred = None
for i, x_val_path in enumerate(x_val_paths):
    
    x_val = np.load(x_val_path).astype('float32') # loaded as RGB
    x_val = vgg19.preprocess_input(x_val) # converted to BGR
    
    y_pred_sharded = model.predict(x_val, verbose=0, use_multiprocessing=True, batch_size=64, callbacks=None)
    
    try:
        y_pred = np.concatenate([y_pred, y_pred_sharded])
    except ValueError:
        y_pred = y_pred_sharded
        
    del x_val
    gc.collect()
    
    completed_percentage = (i + 1) * 100 / len(x_val_paths)
    if completed_percentage % 5 == 0:
        print("{:5.1f}% completed.".format(completed_percentage))<jupyter_output>5.0% completed.
 10.0% completed.
 15.0% completed.
 20.0% completed.
 25.0% completed.
 30.0% completed.
 35.0% completed.
 40.0% completed.
 45.0% completed.
 50.0% completed.
 55.0% completed.
 60.0% completed.
 65.0% completed.
 70.0% completed.
 75.0% completed.
 80.0% completed.
 85.0% completed.
 90.0% completed.
 95.0% completed.
100.0% completed.<jupyter_text>Top-1 AccuracyCompare to 0.713 from Keras documentation<jupyter_code>top_k_accuracy(y_val_one_hot, y_pred, k=1)<jupyter_output><empty_output><jupyter_text>Top-5 AccuracyCompare to 0.900 from Keras documentation<jupyter_code>top_k_accuracy(y_val_one_hot, y_pred, k=5)<jupyter_output><empty_output><jupyter_text>Save predictions<jupyter_code>np.save(str(path_imagenet_val_dataset / "y_pred_VGG19.npy"), y_pred)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1333103685842232, 'fraction_numerical': 0.03892524974164657, 'mean_word_length': 4.7848605577689245, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '468896', 'license': 'MIT', 'n_tokens_mistral': 1208, 'n_tokens_neox': 1124, 'n_words': 183, 'path': '2. Benchmark Keras pretrained models on ImageNet.ipynb', 'repo_name': 'nrasadi/imagenet_validation', 'size': '2903'}