TypeError:获取参数None在tf.reduce_max(pred,axis = 1).eval()中具有无效的类型<class'NoneType'>

时间:2019-08-16 14:55:08

标签: python-3.x tensorflow image-processing

在下面的代码中,我试图获取数组中每个图像的最高概率。它给了我以下错误

TypeError: Fetch argument None has invalid type <class 'NoneType'>

程序代码:

categories = #labels
test_img = []
probab=[]
classes=[]
true_labels = np.array(categories)
new_model =  tf.keras.models.load_model("multi25.h5", custom_objects={'KerasLayer': hub.KerasLayer})
def load(filename):
   np_image = Image.open(filename)
   np_image = np.array(np_image).astype('float32')/255
   np_image = transform.resize(np_image, (224, 224, 3))
   np_image = np.expand_dims(np_image, axis=0)
   return np_image

for i in os.listdir(data_root):
        print(i)
        loc= data_root+"//"+i
        test_img.append(load(loc))
        pred= new_model.predict(test_img)
        classes.append(true_labels[np.argmax(pred, axis=-1)])
        with tf.Session() as sess:
            probab=tf.reduce_max(pred,  axis=1).eval()
            sess.run(print(probab))
print(test_img+" "+classes+" "+probab)

以下代码中出现错误,我在其中使用reduce_max函数获取最大概率。

probab=tf.reduce_max(pred,  axis=1).eval()
sess.run(print(probab))

我已经尝试了其他解决方案,其中我将功能更改为reduce_indices,尝试了不运行会话等。Pred变量没有nonetype。我无法很好地理解错误

这是回溯

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Pawandeep/Desktop/Python projects/multimodel_pred.py", line 52, in <module>
    sess.run(print(probab))
  File "C:\Python\lib\site-packages\tensorflow\python\client\session.py", line 950, in run
    run_metadata_ptr)
  File "C:\Python\lib\site-packages\tensorflow\python\client\session.py", line 1158, in _run
    self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
  File "C:\Python\lib\site-packages\tensorflow\python\client\session.py", line 474, in __init__
    self._fetch_mapper = _FetchMapper.for_fetch(fetches)
  File "C:\Python\lib\site-packages\tensorflow\python\client\session.py", line 261, in for_fetch
    type(fetch)))
TypeError: Fetch argument None has invalid type <class 'NoneType'>

1 个答案:

答案 0 :(得分:0)

使用tf.InteractiveSession代替session.run()解决了问题。参考是here