<jupyter_start><jupyter_text>Visualizing convnet filters<jupyter_code>from keras.applications import VGG16
from keras import backend as K
model = VGG16(weights = 'imagenet', include_top=False)
layer_name = 'block3_conv1'
filter_index = 0

layer_output = model.get_layer(layer_name).output
loss = K.mean(layer_output[:, :, : , filter_index])
grads = K.gradients(loss, model.input)[0]
grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)
iterate = K.function([model.input], [loss, grads])

loss_value, grad_value = iterate([np.zeros((1, 150, 150, 3))])
input_img_data = np.random.random((1, 150, 150, 3)) * 20 + 128
step =1
for i in range(40):
    loss_value, grad_value = iterate([input_img_data])
    input_img_data += grad_value * step
def preprocess_image(x):
    x -= x.mean()
    x /= (x.std() + 1e-5)
    x *= 0.1
    
    x+= 0.5
    x = np.clip(x, 0, 1)
    
    x *=255
    x = np.clip(x, 0, 255).astype('uint8')
    return x<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16145833333333334, 'fraction_numerical': 0.052083333333333336, 'mean_word_length': 4.13903743315508, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '304932', 'license': 'MIT', 'n_tokens_mistral': 420, 'n_tokens_neox': 382, 'n_words': 83, 'path': '06. Visualizing What Convnets Learn.ipynb', 'repo_name': 'Arjitg450/Machine-Learning-Case-Studies', 'size': '960'}