# coding: utf-8
import time
import cv2
import numpy as np
from PIL import Image
from yolact import YOLACT
from keras.models import Model
import cv2
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers import Activation
from pylab import *
from utils.utils import (get_classes, get_coco_label_map,
show_config)
from keras import backend as K
from nets.yolact import get_train_model, yolact
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):
feature_map = np.squeeze(img_batch, axis=0)
print(feature_map.shape)
feature_map_combination = []
plt.figure()
# num_pic = feature_map.shape[2]# 标记2
num_pic =10
print('num_pic', num_pic)
row, col = get_row_col(num_pic)
for i in range(0, num_pic):
feature_map_split = feature_map[:, :, i] # 标记3
feature_map_combination.append(feature_map_split)
plt.subplot(row, col, i + 1)
plt.imshow(feature_map_split)
axis('off')
title('feature_map_{}'.format(i))
# plt.savefig('feature_map.png')
plt.show()
# 各个特征图按1:1 叠加
feature_map_sum = sum(ele for ele in feature_map_combination)
plt.imshow(feature_map_sum)
# plt.savefig("feature_map_sum.png")
if __name__ == "__main__":
img = cv2.imread('./img/0.jpg')
input_shape = [544, 544]
img=cv2.resize(img,(544,544))
plt.imshow(img)
classes_path = 'model_data/shape_classes.txt'
class_names, num_classes = get_classes(classes_path)
num_classes = num_classes + 1
model_body = yolact([input_shape[0], input_shape[1], 3], num_classes, train_mode=True)
model_body.summary()
model = Model(inputs=model_body.input, outputs=model_body.get_layer(name="proto_net.proto2.2").output) # 标记0
print('img ', img.shape)
img_batch = np.expand_dims(img, axis=0)
print('img_batch', img_batch.shape)
conv_img = model.predict(img_batch) # conv_img 卷积结果
print('conv_img.shape', conv_img.shape)
visualize_feature_map(conv_img)
通过model.summary可以获得各个层的名字,通过名字进行索引。可以将每一层的特征图进行输出
在此由衷感谢,特此记录,有啥不懂的欢迎讨论(4条消息) keras如何可视化 模型预测时的 中间 特征图,网络模型如何可视化中间层特征图以及如何打印网络结构_EvaJason的博客-CSDN博客