当前位置:   article > 正文

keras特征图可视化:visualize_feature_map_keras特征图可视化resnet101

keras特征图可视化resnet101

版本:

(labelme) C:\Users\wym\Desktop\cjr\Centernet-Tensorflow2.0\TF2-CenterNet>

1、pip install keras

2、pip install tensorflow-cpu==2.2.0

img:

feature_map:

 

  1. # coding: utf-8
  2. from keras.applications.vgg19 import VGG19
  3. from keras.preprocessing import image
  4. from keras.applications.vgg19 import preprocess_input
  5. from keras.models import Model
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. from pylab import *
  9. def get_row_col(num_pic):
  10. squr = num_pic ** 0.5
  11. row = round(squr)
  12. col = row + 1 if squr - row > 0 else row
  13. return row, col
  14. def visualize_feature_map(img_batch,img_prefix,num):
  15. feature_map = img_batch
  16. print(feature_map.shape)
  17. feature_map_combination = []
  18. plt.figure()
  19. num_pic = feature_map.shape[2]
  20. row, col = get_row_col(num_pic)
  21. print(num_pic)
  22. for i in range(0, num_pic):
  23. feature_map_split = feature_map[:, :, i]
  24. feature_map_combination.append(feature_map_split)
  25. plt.subplot(row, col, i + 1)
  26. plt.imshow(feature_map_split)
  27. # plt.pause(1)
  28. axis('off')#关闭坐标轴
  29. plt.savefig('./result/img_prefix{}_{}feature_map.png'.format(img_prefix,num))
  30. plt.show()
  31. plt.pause(1)
  32. # 各个特征图按1:1 叠加
  33. feature_map_sum = sum(ele for ele in feature_map_combination)
  34. plt.imshow(feature_map_sum)
  35. plt.pause(1)
  36. plt.savefig("./result/img_prefix{}_{}feature_map_sum.png".format(img_prefix,num))
  37. if __name__ == "__main__":
  38. base_model = VGG19(weights='imagenet', include_top=False)
  39. for index in range(1,6):
  40. # model = Model(inputs=base_model.input, outputs=base_model.get_layer('block1_pool').output)
  41. # model = Model(inputs=base_model.input, outputs=base_model.get_layer('block2_pool').output)
  42. # model = Model(inputs=base_model.input, outputs=base_model.get_layer('block3_pool').output)
  43. # model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output)
  44. model = Model(inputs=base_model.input, outputs=base_model.get_layer('block{}_pool'.format(index)).output)
  45. img_path = '3.jpg'
  46. img_pathPrefix=img_path.split(".")[0]
  47. img = image.load_img(img_path)
  48. x = image.img_to_array(img)
  49. x = np.expand_dims(x, axis=0)
  50. x = preprocess_input(x)
  51. block_pool_features = model.predict(x)
  52. print(block_pool_features.shape)
  53. feature = block_pool_features.reshape(block_pool_features.shape[1:])
  54. visualize_feature_map(feature,img_pathPrefix,index,)

如果觉得文章错,可以扫码关注下下哈,CSDN里可能更新不那么及时,公众号里面会每天更新干货知识。

 

 

参考文献:

卷积神经网络特征图可视化(自定义网络和VGG网络)_-牧野-的博客-CSDN博客_特征图可视化

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/340887
推荐阅读
相关标签
  

闽ICP备14008679号