Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. from keras.applications.vgg16 import VGG16, preprocess_input
  2. from keras.preprocessing.image import load_img,img_to_array
  3. from keras.models import Model
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6.  
  7. model = VGG16()
  8.  
  9. layer_dict = dict([(layer.name, layer) for layer in model.layers])
  10.  
  11. layer_name = 'block1_conv2'
  12.  
  13. model = Model(inputs=model.inputs, outputs=layer_dict[layer_name].output)
  14.  
  15. # Perpare the image
  16. image = load_img('tiger.jpg', target_size=(224, 224))
  17. image = img_to_array(image)
  18. image = np.expand_dims(image, axis=0)
  19. image = preprocess_input(image)
  20.  
  21. # Apply the model to the image
  22. feature_maps = model.predict(image)
  23.  
  24. square = 8
  25. index = 1
  26. for _ in range(square):
  27. for _ in range(square):
  28.  
  29. ax = plt.subplot(square, square, index)
  30. ax.set_xticks([])
  31. ax.set_yticks([])
  32.  
  33. plt.imshow(feature_maps[0, :, :, index-1], cmap='viridis')
  34. index += 1
  35.  
  36. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement