温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

keras特征图可视化的示例分析

发布时间:2021-08-23 10:31:42 来源:亿速云 阅读:258 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关keras特征图可视化的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

使用的比较简单的一个模型:

def simple_cnn():  input_data = Input(shape=(28, 28, 1))  x = Conv2D(64, kernel_size=3, padding='same', activation='relu', name='conv1')(input_data)  x = MaxPooling2D(pool_size=2, strides=2, name='maxpool1')(x)  x = Conv2D(32, kernel_size=3, padding='same', activation='relu', name='conv2')(x)  x = MaxPooling2D(pool_size=2, strides=2, name='maxpool2')(x)  x = Dropout(0.25)(x)  # 获得最后一层卷积层的输出  # 添加自己的全连接  x = Flatten(name='flatten')(x)  x = Dense(128, activation='relu', name='fc1')(x)  x = Dropout(0.25)(x)  x = Dense(10, activation='softmax', name='fc2')(x)  model = Model(inputs=input_data, outputs=x)

此模型已经训练好了,跑了10个epoch,验证集0.33

keras特征图可视化的示例分析

这里的效果还是很好的,┓( ´∀` )┏

下面在网上搞了张手写数字

keras特征图可视化的示例分析

使用网络进行预测,这里就先给出如何可视化第一层的卷积层的输出吧,哇哈哈

代码:

input_data = Input(shape=(28, 28, 1))  x = Conv2D(64, kernel_size=3, padding='same', activation='relu', name='conv1')(input_data)  x = MaxPooling2D(pool_size=2, strides=2, name='maxpool1')(x)  x = Conv2D(32, kernel_size=3, padding='same', activation='relu', name='conv2')(x)  x = MaxPooling2D(pool_size=2, strides=2, name='maxpool2')(x)  x = Dropout(0.25)(x)  x = Flatten(name='flatten')(x)  x = Dense(128, activation='relu', name='fc1')(x)  x = Dropout(0.25)(x)  x = Dense(10, activation='softmax', name='fc2')(x)  model = Model(inputs=input_data, outputs=x)    model.load_weights('final_model_mnist_2019_1_28.h6')    raw_img = cv2.imread('test.png')  test_img = load_img('test.png', color_mode='grayscale', target_size=(28, 28))  test_img = np.array(test_img)  test_img = np.expand_dims(test_img, axis=0)  test_img = np.expand_dims(test_img, axis=3)    conv1_layer = Model(inputs=input_data, outputs=model.get_layer(index=1).output)    conv1_output = conv1_layer.predict(test_img)    for i in range(64):   show_img = conv1_output[:, :, :, i]   print(show_img.shape)   show_img.shape = [28,28]   cv2.imshow('img', show_img)   cv2.waitKey(0)

核心方法就是通过加载模型后,新建Model,将输出部分换为你想要查看的网络层数即可,当然get_layer()包括了name和index两个参数。最后通过遍历当前卷积层的所有特征映射,将每一个都展示出来。就可以了。

keras特征图可视化的示例分析

关于“keras特征图可视化的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI