<jupyter_start><jupyter_text>Setup  Imports<jupyter_code>from tqdm import tqdm_notebook<jupyter_output><empty_output><jupyter_text>Custom Modules<jupyter_code>import vaidata, vaikeras

from vaiutils import path_consts, plot_images
from vaikaggle import dataset_consts
from vaivision import dream
from vaikeras import get_predictions
from vaikaggle import plot_prediction_density, plot_confusion_matrix
from vaimodels import VGG16<jupyter_output><empty_output><jupyter_text>Keras Modules<jupyter_code>from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model, Sequential
from keras.backend import set_learning_phase, clear_session
from keras.layers import Conv2D, Dense, BatchNormalization, Dropout, Flatten

import keras.backend as K<jupyter_output><empty_output><jupyter_text>Define useful variables and functions<jupyter_code>for k, v in path_consts('StateFarm'):
    exec(k + '=v')
for k, v in dataset_consts(DIR_DATA, use_sample=False):
    exec(k + '=v')
def pickle_load(filename, default=None, path=DIR_CHECKPOINTS):
    return vaidata.pickle_load(path + '/' + filename + '.p', default)
def pickle_dump(filename, obj, path=DIR_CHECKPOINTS):
    vaidata.pickle_dump(path + '/' + filename + '.p', obj)
def inflate_model(name=None, model=None):
        return vaikeras.inflate_model(DIR_CHECKPOINTS + '/' + name + '.h5', name,
                                      DIR_CHECKPOINTS + '/' + name + '-history.p', model)
def get_images(filenames):
    return vaidata.get_images(filenames, (224, 224, 3), DIR_VALID)<jupyter_output><empty_output><jupyter_text>Get Data<jupyter_code>valid_batches = vaikeras.get_batches(DIR_VALID, ImageDataGenerator(), batch_size=1, shuffle=False)<jupyter_output><empty_output><jupyter_text>Get Model<jupyter_code>K.clear_session()
K.set_learning_phase(False)
model = inflate_model('FullyConvNetBW')
model.summary()<jupyter_output><empty_output><jupyter_text>Dream  Dream all the convolutional layers<jupyter_code>for j, layer in tqdm_notebook(enumerate(model.layers)):
    if type(layer) is Conv2D:
        plot_images([dream(model, filter_idx=(j, i))
                     for i in tqdm_notebook(randint(0, layer.output_shape[-1], 16))])
        plot_images([dream(model, filter_idx=(j, -1), strength=j)])<jupyter_output><empty_output><jupyter_text>Dream all the Dense Layers<jupyter_code>for j, layer in tqdm_notebook(enumerate(model.layers)):
    if type(layer) == Dense:
        print(layer.name)
        plot_images([dream(model, filter_idx=(j, -1), strength=j)])<jupyter_output><empty_output><jupyter_text>Dream the final layer (categories)<jupyter_code>plot_images([dream(model, filter_idx=(-1, i), strength=100).mean(-1)
             for i in tqdm_notebook(range(10))], cmap='seismic')<jupyter_output><empty_output><jupyter_text>Single Image  Follow an Image<jupyter_code>intermediate_model = Model(
    model.input, [layer.output for layer in model.layers if type(layer) == Conv2D])
sample_image = valid_batches.next()[0][randint(0, valid_batches.batch_size)]
sample_activations = intermediate_model.predict(
    np.expand_dims(sample_image, 0))
plot_images([sample_image])
for s in sample_activations:
    plot_images([s[0, :, :, i] for i in randint(0, s.shape[-1], 16)], pixel_range=(s.min(), s.max()))
plot_images([dream(model, sample_image, filter_idx=(-1, i), strength=10) for i in tqdm_notebook(range(10))])<jupyter_output><empty_output><jupyter_text>Saliency Maps<jupyter_code>sample_image = valid_batches.next()[0][randint(0, valid_batches.batch_size)]
input_img = model.input
grads = K.gradients(K.max(model.output, -1), input_img)[0]
saliency_map = K.function([input_img], [grads])

sample_saliency = saliency_map([np.expand_dims(sample_image, 0)])[0][0]
plot_images([sample_image], flags='retain')
plot_images([np.linalg.norm(sample_saliency, axis=-1)], pixel_range=(sample_saliency.min()*0.1, sample_saliency.max()), cmap='seismic', flags='retain')
plt.axes().images[1].set_alpha(0.4)
plt.figure<jupyter_output><empty_output><jupyter_text>Predictions<jupyter_code>val_predictions = model.predict_generator(valid_batches, valid_batches.num_batches, verbose=1)
val_losses = -np.log(val_predictions[range(valid_batches.samples), valid_batches.classes])
def plot_predictions(mode):
    plot_images(get_images(get_predictions(model, valid_batches, val_losses,
                                           val_predictions, mode=mode)))<jupyter_output><empty_output><jupyter_text>Few Random Images<jupyter_code>plot_predictions('random')<jupyter_output><empty_output><jupyter_text>Random Correct Images<jupyter_code>plot_predictions('correct_random')<jupyter_output><empty_output><jupyter_text>Random Incorrect Images<jupyter_code>plot_predictions('incorrect_random')<jupyter_output><empty_output><jupyter_text>Most Confidently Correct<jupyter_code>plot_predictions('correct_confident')<jupyter_output><empty_output><jupyter_text>Most Confidently Incorrect<jupyter_code>plot_predictions('incorrect_confident')<jupyter_output><empty_output><jupyter_text>Least Confidently Correct<jupyter_code>plot_predictions('correct_doubtful')<jupyter_output><empty_output><jupyter_text>Most Confused<jupyter_code>plot_predictions('confused')<jupyter_output><empty_output><jupyter_text>Analytics<jupyter_code>plot_prediction_density(model, valid_batches)
print(plot_confusion_matrix(model, valid_batches))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12370178041543027, 'fraction_numerical': 0.010014836795252226, 'mean_word_length': 8.171768707482993, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '307856', 'license': 'MIT', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1922, 'n_words': 278, 'path': 'Kaggle/StateFarm/Notebooks/ExploreModel.ipynb', 'repo_name': 'svaisakh/aiprojects', 'size': '5392'}