全局最大池化,找到图片每个通道中最大值的位置坐标

#!/usr/bin/env python
# encoding: utf-8
'''
@author: lele Ye
@contact: 1750112338@qq.com
@software: pycharm 2018.2
@file: 04max_position.py
@time: 2019/4/30 16:32
@desc: 找到图片每个通道中最大值的位置坐标,
下面得出一张三通道图像中每个通道的最大值位置分别为[(4, 1), (3, 6), (3, 2)]
'''
import tensorflow as tf
import numpy as np
list_image = [
    [[[92, 18, 57],
      [66, 28, 38],
      [46, 29, 46],
      [25, 23, 90],
      [70, 13, 27],
      [56, 75, 20],
      [47, 21, 31],
      [12, 52, 98]],
    [[45, 18, 57],
     [66, 28, 38],
     [46, 29, 46],
     [25, 23, 90],
     [170, 13, 27],
     [56, 375, 20],
     [147, 21, 31],
     [12, 52, 98]],
    [[65, 18, 57],
     [66, 28, 211],
     [16, 29, 21],
     [25, 23, 90],
     [60, 13, 27],
     [56, 378, 20],
     [47, 201, 31],
     [45, 52, 98]],
    [[233, 34, 75],
     [15, 264, 52],
     [42, 62, 14],
     [31, 39, 59],
     [59, 20, 82],
     [61, 66, 22],
     [100, 34, 37],
     [15, 264, 52]],
    [[42, 62, 14],
     [31, 39, 59],
     [59, 20, 82],
     [61, 66, 22],
     [100, 34, 37],
     [15, 164, 52],
     [42, 62, 14],
     [31, 30, 54]],
    [[59, 20, 82],
     [61, 6, 22],
     [43, 12,90],
     [76,28,10],
     [87,21,199],
     [90,87,122],
     [98,124,211],
     [10,89,20]]
     ]
    ]


array_image = np.squeeze(list_image,axis=0)
print(array_image[:,:,1])
'''
[[ 18  28  29  23  13  75  21  52]
 [ 18  28  29  23  13 375  21  52]
 [ 18  28  29  23  13 378 201  52]
 [ 34 264  62  39  20  66  34 264]
 [ 62  39  20  66  34 164  62  30]
 [ 20   6  12  28  21  87 124  89]]
'''

image = tf.convert_to_tensor(array_image)

image1 = tf.expand_dims(image,axis=0)
# 全局最大池化,要求ksize于输入特征图维度大小相同
global_pool = tf.nn.max_pool(image1,ksize=[1,6,8,1],strides=[1,1,1,1],padding="VALID")

# 对二维张量(矩阵)求最大值的位置坐标
def get_max_position(sess,image):
    sess.run(tf.global_variables_initializer())
    height, width, channels = sess.run(tf.shape(image))
    position_list = []
    for i in range(channels):
        max_index = tf.argmax(tf.reshape(image[:,:,i], [height * width])) # get the max index
        position = sess.run(max_index)
        position_list.append((position // width + 1,position % width + 1))
    return position_list

with tf.Session() as sess:
    position_list = get_max_position(sess, image)
    print(sess.run(global_pool)) # [[[[233 378 211]]]]
    print(position_list) # [(4, 1), (3, 6), (3, 2)]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值