1 # -*- coding: utf-8 -*- 2 """ 3 Created on Wed Nov 7 15:45:53 2018 4 @author: zhen 5 """ 6 7 import numpy as np 8 from sklearn.datasets import load_sample_images 9 import tensorflow as tf10 import matplotlib.pyplot as plt11 12 # 加载数据集13 dataset = np.array(load_sample_images().images, dtype=np.float32)14 batch_size, height, width, channels = dataset.shape15 16 filters_test = np.zeros(shape=(7, 7, channels, 2), dtype=np.float32)17 filters_test[:, 3, :, 0] = 1 # 垂直18 filters_test[3, :, :, 1] = 1 # 水平19 20 x = tf.placeholder(tf.float32, shape=(None, height, width, channels))21 # 卷积22 convolution = tf.nn.conv2d(x, filter=filters_test, strides=[1, 2, 2, 1], padding='SAME')23 # pooling24 max_pool = tf.nn.max_pool(x, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1], padding='VALID')25 26 with tf.Session() as sess:27 convolution_output = sess.run(convolution, feed_dict={x:dataset})28 max_pool_output = sess.run(max_pool, feed_dict={x:dataset})29 30 plt.imshow(convolution_output[0, :, :, 0]) # 绘制特征图31 plt.show()32 plt.imshow(max_pool_output[0].astype(np.uint8)) # 绘制特征图33 plt.show()
结果: