Python 3.x 缺少必需的参数:输入张量

Python 3.x 缺少必需的参数:输入张量,python-3.x,tensorflow2.0,Python 3.x,Tensorflow2.0,我已经使用EfficientNet训练了一个模型,在训练中没有出现错误后,我将该模型替换到了包含的object_detection Python笔记本中 def为单个图像(模型,图像)运行推理: image=np.asarray(图像) 输入\u张量=tf。将\u转换为\u张量(图像) 输入张量=输入张量[tf.newaxis,…] model_fn=model.signatures['service_default'] 输出dict=模型fn(输入张量) num\u detections=in

我已经使用EfficientNet训练了一个模型,在训练中没有出现错误后,我将该模型替换到了包含的object_detection Python笔记本中

def为单个图像(模型,图像)运行推理:
image=np.asarray(图像)
输入\u张量=tf。将\u转换为\u张量(图像)
输入张量=输入张量[tf.newaxis,…]
model_fn=model.signatures['service_default']
输出dict=模型fn(输入张量)
num\u detections=int(输出dict.pop('num\u detections'))
output_dict={key:value[0,:num_检测].numpy()
对于键,输出中的值\u dict.items()}
输出dict['num\u detections']=num\u detections
output_dict['detection_classes']=output_dict['detection_classes'].astype(np.int64)
返回输出命令
def显示推理(模型、图像路径):
image\u np=np.array(image.open(image\u路径))
输出\u dict=运行\u推断\u用于\u单个图像(模型,图像\u np)
#这里的image_path只是指向.jpg的路径
对于测试图像路径中的图像路径:
显示推理(检测模型、图像路径)
出现以下错误:

TypeError: signature_wrapper(*, input_tensor) missing required arguments: input_tensor

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument:  Incompatible shapes: [1,256,256] vs. [1,1,3]
     [[{{node StatefulPartitionedCall/Preprocessor/sub}}]]
     [[StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/MultiClassNonMaxSuppression/Reshape_11/_112]]
  (1) Invalid argument:  Incompatible shapes: [1,256,256] vs. [1,1,3]
     [[{{node StatefulPartitionedCall/Preprocessor/sub}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_signature_wrapper_73496]
该模型在(黑白)PNG上进行了训练和测试,这是该示例之间的关键区别(此外,该示例具有不同的模型)。将PNG转换为JPG会更改根错误:

Invalid argument:  input must be 4-dimensional[1,256,256]

除了重新开始JPG和培训/测试,我不确定问题出在哪里。

Tensorflow 2.X对象检测API有预先培训过的模型&github存储库有很好的文档说明如何培训您自己的模型

但默认情况下,培训/评估采用
JPEG
格式。因此,如果您的图像有任何其他编解码器格式,则必须对其进行转换

以下截图将轻松地将现有图像转换为jpeg编解码器,并使用相同的文件名+
.jpg

运行

带有图像的python convert.py文件夹

#convert.py
from PIL import Image     
import os
import sys 

path = sys.argv[1] # Source Folder
if path[-1] != '/':
    path = path +'/' 
for file in os.listdir(path):      
        extension = file.split('.')[-1]
        name = file.split('.')[0] + '.jpg'
        fileLoc = path+file
        img = Image.open(fileLoc)
        new = Image.new("RGB", img.size, (255, 255, 255))
        new.paste(img,None) # save the new image with jpg as extension
        new.save(path+name, 'JPEG', quality=100)
        if(extension != 'jpg'): #remove the old image
            os.remove(path+file)

tensorflow2.x
objectdetectionapi提供了经过训练的模型以及如何训练自己的模型。但默认情况下,培训/评估采用
JPEG
格式。因此,如果您的图像有任何其他编解码器格式,则必须对其进行转换。让我知道,如果你需要帮助,并尝试它,我应该更新后。这就是我下一步尝试的成功之处。我将把它作为答案