如何正确使用tf.nn.max_pool_with_argmax

时间:2016-09-14 14:37:23

标签: python tensorflow

目前我正在使用tensorflow来创建一个更好地理解机器学习张量流本身。因此,我想想象张量流的方法(尽可能多)。为了可视化max_pool,我加载了一个图像并执行该方法。之后我显示了两个:输入和输出图像。

import tensorflow as tf
import cv2
import numpy as np

import matplotlib.pyplot as plt

image = cv2.imread('lena.png')
image_tensor = tf.expand_dims(tf.Variable(image, dtype=tf.float32), 0)

#output, argmax = tf.nn.max_pool_with_argmax(image_tensor, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool1')
output = tf.nn.max_pool(image_tensor, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool1')

init = tf.initialize_all_variables()
session = tf.Session()
session.run(init)

output = session.run(output)

session.close()

image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
plt.figure()
plt.imshow(image)
plt.show()

output = cv2.cvtColor(output[0], cv2.COLOR_RGB2BGR)
plt.figure()
plt.imshow(255-output)
plt.show() 

一切正常,我得到了这个输出(如预期的那样)

image (input) enter image description here

现在我想测试方法tf.nn.max_pool_with_argmax以获取池操作的argmax。但如果我取消注释该行

output, argmax = tf.nn.max_pool_with_argmax(image_tensor, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool1')

Python与

崩溃
  

tensorflow.python.framework.errors.InvalidArgumentError:没有注册OpKernel以支持Op'MaxPoolWithArgmax'与这些attrs        [[Node:pool1 = MaxPoolWithArgmaxT = DT_FLOAT,Targmax = DT_INT64,ksize = [1,2,2,1],padding =“SAME”,strides = [1,2,2,1]]]

我不知道哪个参数是错误的,因为每个参数都应该是正确的(tensorflow docs)......

有谁知道出了什么问题?

1 个答案:

答案 0 :(得分:7)

the implementation看,似乎tf.nn.max_pool_with_argmax()仅针对GPU实现。如果您正在运行TensorFlow的仅CPU版本,那么您将收到"No OpKernel was registered to support Op 'MaxPoolWithArgmax' with these attrs ..."形式的错误。

(这似乎是一个可以改进文档和错误消息的地方。)