缺少必需的参数:input_tensor

Missing required arguments: input_tensor

我已经使用 EfficientNet 训练了一个模型,在训练没有错误之后,我将模型替换到 Tensorflow Model 随附的 object_detection Python 笔记本中。

def run_inference_for_single_image(model, image):
  image = np.asarray(image)
  input_tensor = tf.convert_to_tensor(image)
  input_tensor = input_tensor[tf.newaxis,...]

  model_fn = model.signatures['serving_default']


  output_dict = model_fn(input_tensor)

  num_detections = int(output_dict.pop('num_detections'))
  output_dict = {key:value[0, :num_detections].numpy() 
                 for key,value in output_dict.items()}
  output_dict['num_detections'] = num_detections

  output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)

  return output_dict

def show_inference(model, image_path):
  image_np = np.array(Image.open(image_path))

  output_dict = run_inference_for_single_image(model, image_np)

# image_path here is just a path to a .jpg
for image_path in TEST_IMAGE_PATHS:
  show_inference(detection_model, image_path)

出现了以下错误:

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]

缺少 jpgs 和 training/testing 重新开始,我不确定是什么问题。

Tensorflow 2.X 对象检测 API 有预训练模型,github 存储库有关于如何训练你自己的模型的很好的文档。

但默认情况下,training/evaluation 是 JPEG 格式。因此,如果您的图像有任何其他编解码器格式,则必须转换它们。

以下片段将轻松将您现有的图像转换为 jpeg 编解码器并使用相同的文件名保存 + .jpg

到运行

python convert.py folder_with_images

#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)