tensorflow tf.nn.max_pool_with_argmax返回最大池化对应索引值
生活随笔
收集整理的這篇文章主要介紹了
tensorflow tf.nn.max_pool_with_argmax返回最大池化对应索引值
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在看Segnet的時候發(fā)現(xiàn)使用了帶有索引的最大池化(max_pool),在tensorflow的實現(xiàn)中,對應的函數(shù)為tf.nn.max_pool_with_argmax(),其返回值為所取最大值位置的索引,但采用了一種指數(shù)的計算方式進行表示
這里為官方注釋
The indices in `argmax` are flattened, so that a maximum value at position`[b, y, x, c]` becomes flattened index`((b * height + y) * width + x) * channels + c`.可以看到對索引的表示增加了一些運算操作,使得我們無法直接使用這個索引對應的位置
這里自己寫了一個拆解方法的代碼,記錄一下(只考慮了通道影響,沒有考慮batch的影響,可能后續(xù)會加以改進說明)
def unravel_corr(list,shape):b,h,w,c = list.get_shape().as_list()cc = []for i in range(c):a = tf.constant(i,dtype=tf.int64)a = [a]aa = (tf.tile(tf.tile(a,[h])[:,tf.newaxis],[1,w])[:,:,tf.newaxis])cc.append(aa)aa = tf.concat(cc,2)aa = tf.tile(aa[tf.newaxis,:,:,:],[b,1,1,1])_,height,width,_ = shapelist_y = (list-aa)/c%widthlist_x = (list-aa)/c//width%heightreturn [list_x,list_y]整體測試代碼如下
import tensorflow as tf a = tf.constant([[5,8,2,1],[4,3,5,7],[0,7,9,1],[2,3,9,7]]) a = tf.reshape(a,[1,4,4,1]) c=tf.constant([[1,5,7,1],[4,8,5,7],[0,7,9,12],[15,3,9,7]]) c = tf.reshape(c,[1,4,4,1]) a = tf.concat([a,c],3) b,list = tf.nn.max_pool_with_argmax(a,[1,2,2,1],[1]+[2,2]+[1],padding='VALID') def unravel_corr(list,shape):b,h,w,c = list.get_shape().as_list()cc = []for i in range(c):a = tf.constant(i,dtype=tf.int64)a = [a]aa = (tf.tile(tf.tile(a,[h])[:,tf.newaxis],[1,w])[:,:,tf.newaxis])cc.append(aa)aa = tf.concat(cc,2)aa = tf.tile(aa[tf.newaxis,:,:,:],[b,1,1,1])_,height,width,_ = shapelist_y = (list-aa)/c%widthlist_x = (list-aa)/c//width%heightreturn [list_x,list_y] cor = unravel_corr(list,[1,4,4,2])with tf.Session() as sess:print('a:',sess.run(a))print('b:',sess.run(b))print('list:',sess.run(cor))經(jīng)計算,得到的位置索引與手動計算得到的結(jié)果相符
總結(jié)
以上是生活随笔為你收集整理的tensorflow tf.nn.max_pool_with_argmax返回最大池化对应索引值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 相机成像投影计算
- 下一篇: Involution代码