赞
踩
版本:
(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:
- # coding: utf-8
- from keras.applications.vgg19 import VGG19
- from keras.preprocessing import image
- from keras.applications.vgg19 import preprocess_input
- from keras.models import Model
- import numpy as np
- import matplotlib.pyplot as plt
- from pylab import *
-
- def get_row_col(num_pic):
- squr = num_pic ** 0.5
- row = round(squr)
- col = row + 1 if squr - row > 0 else row
- return row, col
-
- def visualize_feature_map(img_batch,img_prefix,num):
- feature_map = img_batch
- print(feature_map.shape)
-
- feature_map_combination = []
- plt.figure()
-
- num_pic = feature_map.shape[2]
- row, col = get_row_col(num_pic)
- print(num_pic)
- for i in range(0, num_pic):
- feature_map_split = feature_map[:, :, i]
- feature_map_combination.append(feature_map_split)
- plt.subplot(row, col, i + 1)
- plt.imshow(feature_map_split)
- # plt.pause(1)
- axis('off')#关闭坐标轴
-
- plt.savefig('./result/img_prefix{}_{}feature_map.png'.format(img_prefix,num))
- plt.show()
- plt.pause(1)
-
- # 各个特征图按1:1 叠加
- feature_map_sum = sum(ele for ele in feature_map_combination)
- plt.imshow(feature_map_sum)
- plt.pause(1)
- plt.savefig("./result/img_prefix{}_{}feature_map_sum.png".format(img_prefix,num))
-
-
- if __name__ == "__main__":
-
- base_model = VGG19(weights='imagenet', include_top=False)
- for index in range(1,6):
- # model = Model(inputs=base_model.input, outputs=base_model.get_layer('block1_pool').output)
- # model = Model(inputs=base_model.input, outputs=base_model.get_layer('block2_pool').output)
- # model = Model(inputs=base_model.input, outputs=base_model.get_layer('block3_pool').output)
- # model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output)
- model = Model(inputs=base_model.input, outputs=base_model.get_layer('block{}_pool'.format(index)).output)
- img_path = '3.jpg'
- img_pathPrefix=img_path.split(".")[0]
- img = image.load_img(img_path)
- x = image.img_to_array(img)
- x = np.expand_dims(x, axis=0)
- x = preprocess_input(x)
- block_pool_features = model.predict(x)
- print(block_pool_features.shape)
-
- feature = block_pool_features.reshape(block_pool_features.shape[1:])
- visualize_feature_map(feature,img_pathPrefix,index,)
如果觉得文章错,可以扫码关注下下哈,CSDN里可能更新不那么及时,公众号里面会每天更新干货知识。
参考文献:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。