tf.nn.max_pool作什么用?

先来看一下API的说明:

tf.nn.max_pool(value, ksize, strides, padding, data_format='NHWC', name=None)

Performs the max pooling on the input.

Args:
  • value: A 4-D Tensor with shape [batch, height, width, channels] and type tf.float32.
  • ksize: A list of ints that has length >= 4. The size of the window for each dimension of the input tensor.
  • strides: A list of ints that has length >= 4. The stride of the sliding window for each dimension of the input tensor.
  • padding: A string, either 'VALID' or 'SAME'. The padding algorithm. See the comment here
  • data_format: A string. 'NHWC' and 'NCHW' are supported.
  • name: Optional name for the operation.
Returns:

Tensor with type tf.float32. The max pooled output tensor.

参数是四个,和卷积很类似:

第一个参数value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map,依然是[batch, height, width, channels]这样的shape

第二个参数ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1

第三个参数strides:和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride,stride, 1]

第四个参数padding:和卷积类似,可以取'VALID' 或者'SAME'

返回一个Tensor,类型不变,shape仍然是[batch, height, width, channels]这种形式


例子:


#python 3.5.3      
#2017-03-09 蔡军生  http://blog.csdn.net/caimouse    
#

import tensorflow as tf 
import numpy as np
from PIL import Image

fpath = './test2.jpg'
jpg = tf.read_file(fpath)
img_arr = tf.image.decode_jpeg(jpg, channels=3)
img_4d = tf.cast(tf.reshape(img_arr, [1, 936, 764, 3]), tf.float32)
pool = tf.nn.max_pool(img_4d, [1, 2, 2, 1], [1, 2, 2, 1], 'SAME')

with tf.Session() as sess:
    img, pool = sess.run([img_arr, pool])
    print(img.shape)
    print(pool.shape)
    Image.fromarray(np.uint8(pool.reshape(pool.shape[1:4]))).save('./maxpool2.jpg')
输入图片和输出结果:

tf.nn.max_pool作什么用?_第1张图片


池化之后图片:

tf.nn.max_pool作什么用?_第2张图片



1. TensorFlow入门基本教程

http://edu.csdn.net/course/detail/4369

2. C++标准模板库从入门到精通 

http://edu.csdn.net/course/detail/3324

3.跟老菜鸟学C++

http://edu.csdn.net/course/detail/2901

4. 跟老菜鸟学python

http://edu.csdn.net/course/detail/2592

5. 在VC2015里学会使用tinyxml库

http://edu.csdn.net/course/detail/2590

6. 在Windows下SVN的版本管理与实战 

 http://edu.csdn.net/course/detail/2579

7.Visual Studio 2015开发C++程序的基本使用 

http://edu.csdn.net/course/detail/2570

8.在VC2015里使用protobuf协议

http://edu.csdn.net/course/detail/2582

9.在VC2015里学会使用MySQL数据库

http://edu.csdn.net/course/detail/2672



你可能感兴趣的:(深度学习)