keras模型中间过程特征图可视化(遥感图像)

1、结果图

keras模型中间过程特征图可视化(遥感图像)_第1张图片

keras模型中间过程特征图可视化(遥感图像)_第2张图片
keras模型中间过程特征图可视化(遥感图像)_第3张图片

2、参考代码

from keras import backend as K
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
#from processor import process_image
from keras.models import load_model
from pylab import *
import cv2

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("888888=",feature_map.shape)
 
    feature_map_combination = []
    plt.figure()
 
    num_pic = feature_map.shape[2]
    row, col = get_row_col(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)
        axis('off')
        title('feature_map_{}'.format(i))
 
    plt.savefig('feature_map.jpg')
    plt.show()
 
    # 各个特征图按1:1 叠加
    feature_map_sum = sum(ele for ele in feature_map_combination)
#    feature_map_sum=(feature_map_sum-np.min(feature_map_sum))/(np.max(feature_map_sum)-np.min(feature_map_sum))
    y_predict = np.array(feature_map_sum).astype('float')
    y_predict = np.round(y_predict,0).astype('uint8')
    y_predict *= 255
    y_predict = np.squeeze(y_predict).astype('uint8')
    cv2.imwrite("C:\\Users\\Administrator\\Desktop\\0.tif", y_predict)  
    
    
    plt.imshow(y_predict)
    plt.savefig("y_predict.jpg")

def visualize_feature_map1(img_batch):
    feature_map = np.squeeze(img_batch, axis=0)
    print ("feature_map_shape=",feature_map.shape)
    num_pic = feature_map.shape[2]
    row, col = get_row_col(num_pic)
    for _ in range(num_pic):
            show_img = f1[:, :, :, _]
            show_img.shape = [feature_map.shape[0], feature_map.shape[1]]
            plt.subplot(row, col, _ + 1)
            plt.imshow(show_img, cmap='gray')
            plt.axis('off')
           
    plt.show()
    plt.savefig("4444.jpg")
    
if __name__ == "__main__":
    #replaced by your model name
    model = load_model("C:\\Users\\Administrator\\Desktop\\9696.hdf5") 
    ##第一个 model.layers[0],不修改,表示输入数据;第二个model.layers[you wanted],修改为你需要输出的层数的编号
    layer_1 = K.function([model.layers[0].input], [model.layers[67].output])
        input_image=np.load("C:\\Users\\Administrator\\Desktop\\test_images.npy")    
    f1 = layer_1([input_image])[0]#只修改inpu_image
    #第一层卷积后的特征图展示,输出是(1,149,149,32),(样本个数,特征图尺寸长,特征图尺寸宽,特征图个数)    
    visualize_feature_map(f1)

3、模型H5文件、数据集,见主页GitHub

你可能感兴趣的:(高光谱图像处理,卷积模型可视化)