使用Tensorflow对特征图进行可视化输出
training_transform和process_image这两个函数目的是resize时,保证原图比例不变的情况下,使用黑边填充
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.models import Model
def training_transform(height, width, output_height, output_width):
height_scale, width_scale = output_height / height, output_width / width
scale = min(height_scale, width_scale)
resize_height, resize_width = round(height * scale), round(width * scale)
pad_top = (output_height - resize_height) // 2
pad_left = (output_width - resize_width) // 2
A = np.float32([[scale, 0.0], [0.0, scale]])
B = np.float32([[pad_left], [pad_top]])
M = np.hstack([A, B])
return M, output_height, output_width
def process_image(img,input_shape):
h, w = img.shape[:2]
M, h_out, w_out = training_transform(h, w, input_shape[0], input_shape[1])
# 填充黑边缩放
letterbox = cv2.warpAffine(img, M, (w_out, h_out))
pimage = np.float32(letterbox) / 255.
pimage = np.expand_dims(pimage, axis=0)
return pimage
# 下面的第一个model是vgg16,第二个model是使用vgg16的前半部分作为主干网络
model= VGG16(include_top=False,weights='imagenet',input_shape=(224,224,3))
model = Model(inputs=vgg16.input,outputs=model.get_layer('block1_conv2').output)
print(model.summary())
img = cv2.imread('shan.jpg')
img = process_image(img,input_shape=(224,224))
out = model.predict(img)
import matplotlib.pyplot as plt
for i in range(64):
plt.matshow(out[0,:,:,i],cmap='viridis')
plt.axis('off')
plt.savefig('features//feature-{}.png'.format(i),bbox_inches='tight')
plt.show()