<jupyter_start><jupyter_text>Edafa on ImageNet dataset This notebook shows an example on how to use Edafa to obtain better results on **classification task**. We use [ImageNet](http://www.image-net.org/) dataset which has **1000 classes**. We use *Keras* and pretrained weights of VGG16. At the end we compare results of the same model with and without augmentations.  Import dependencies<jupyter_code>%load_ext autoreload
%autoreload 2
# add our package directory to the path
import sys
sys.path.append('../../')
sys.path.append('../')
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input,decode_predictions
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output>Using TensorFlow backend.<jupyter_text>Constants<jupyter_code># Filename to use for comparison (4 sample files are given in 'data' folder)
FILE = '000559'

# Input size of the model
IN_SIZE = 224<jupyter_output><empty_output><jupyter_text>Now we build our model (using pretrained weights)<jupyter_code>model = VGG16(weights='imagenet', include_top=True)<jupyter_output><empty_output><jupyter_text>Read and preprocess image<jupyter_code>img_path = '../data/images/%s.jpg'%FILE
img = image.load_img(img_path, target_size=(IN_SIZE, IN_SIZE))
plt.imshow(img)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)<jupyter_output><empty_output><jupyter_text>Exp1: Predict image without augmentation<jupyter_code>preds_without = model.predict(x)<jupyter_output><empty_output><jupyter_text>Exp2: Using same model with Edafa  step 1: import base class `ClassPredictor`<jupyter_code>from edafa import ClassPredictor<jupyter_output><empty_output><jupyter_text>step 2: inherit `ClassPredictor` and implement the main virtual functions: predict_patches()<jupyter_code>class myPredictor(ClassPredictor):
    def __init__(self,vgg16,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.model = vgg16
        
    def predict_patches(self,patches):
        return self.model.predict(patches)<jupyter_output><empty_output><jupyter_text>step 3: make an instance of your class with the correct parameters<jupyter_code>p = myPredictor(model,"../../conf/imagenet.json")<jupyter_output><empty_output><jupyter_text>step 4: call predict_images()<jupyter_code>preds_with = p.predict_images(x)
## Compare results of Exp1 and Exp2
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print('Predicted without augmentation:', decode_predictions(preds_without, top=2)[0])
print('Predicted with augmentation:', decode_predictions(preds_with, top=2)[0])<jupyter_output>Predicted without augmentation: [('n04404412', 'television', 0.1456603), ('n03180011', 'desktop_computer', 0.104833364)]
Predicted with augmentation: [('n03180011', 'desktop_computer', 0.13454285), ('n04404412', 'television', 0.12845169)]
{'fraction_non_alphanumeric': 0.1111485348602223, 'fraction_numerical': 0.037049511620074096, 'mean_word_length': 7.136986301369863, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '120155', 'license': 'MIT', 'n_tokens_mistral': 1055, 'n_tokens_neox': 964, 'n_words': 246, 'path': 'examples/keras/class_imagenet.ipynb', 'repo_name': 'andrewekhalel/tta_predictor', 'size': '2969'}