本文主要是介绍Tensorflow2 tf.nn.maxpool2d()进行池化运算及其可视化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.tf.nn.maxpool2d()函数介绍
tf.nn.max_pool2d(input, ksize, strides, padding, data_format='NHWC', name=None)
参数说明:
Args | |
|---|---|
input | A 4-D Tensor of the format specified by data_format. |
ksize | An int or list of ints that has length 1, 2 or 4. The size of the window for each dimension of the input tensor. |
strides | An int or list of ints that has length 1, 2 or 4. The stride of the sliding window for each dimension of the input tensor. |
padding | Either the string "SAME" or "VALID" indicating the type of padding algorithm to use, or a list indicating the explicit paddings at the start and end of each dimension. When explicit padding is used and data_format is "NHWC", this should be in the form [[0, 0], [pad_top,pad_bottom], [pad_left, pad_right], [0, 0]]. When explicit padding used and data_format is "NCHW", this should be in the form [[0, 0], [0, 0],[pad_top, pad_bottom], [pad_left, pad_right]]. When using explicit padding, the size of the paddings cannot be greater than the sliding window size. |
data_format | A string. 'NHWC', 'NCHW' and 'NCHW_VECT_C' are supported. |
name | Optional name for the operation. |
2.使用数据
flower_photos数据集中选一张玫瑰花图片

3.池化运算代码
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import tensorflow as tfdata = Image.open("roses_4483444865_65962cea07_m.jpg") # 返回一个PIL图像对象
plt.imshow(data)
plt.show()x = np.array(data)
x = x / 255
x = x.reshape(1, 240, 180, 3)image_tensor = tf.convert_to_tensor(x)
x_input = tf.cast(image_tensor, tf.float32)print("x_in{}", x_input.shape)kernel_in = np.array([[[-1, 1]], [[1, 1]],])
print(kernel_in.shape)
kernel = tf.constant(kernel_in, dtype=tf.float32)Z1=tf.nn.max_pool2d(x_input, [1,2,2,1], strides=[1, 2, 2, 1], padding='SAME')x_max_pool2d = np.array(Z1)
print(x_max_pool2d.shape)
x_max_pool2d = x_max_pool2d.reshape(120, 90, 3)
plt.imshow(x_max_pool2d)
plt.show()
4.计算结果
这篇关于Tensorflow2 tf.nn.maxpool2d()进行池化运算及其可视化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!