【问题标题】:Missing required arguments: input_tensor缺少必需的参数:input_tensor
【发布时间】:2021-01-01 11:23:00
【问题描述】:

我使用 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 上训练和测试的,这是示例之间的关键区别(除了它是示例所具有的不同模型之外)。将 pngs 转换为 jpgs 会改变根错误:

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

没有重新开始 jpg 和培训/测试,我不确定问题是什么。

【问题讨论】:

  • tensorflow2.x 对象检测 api 已训练模型以及如何训练您自己的模型。但默认情况下,训练/评估采用JPEG 格式。因此,如果您的图像有任何其他编解码器格式,则必须对其进行转换。如果您需要帮助,请告诉我并尝试一下
  • 我应该更新帖子。这就是我接下来尝试成功的方法。
  • 我会把这个作为答案

标签: python-3.x tensorflow2.0


【解决方案1】:

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

但默认情况下,训练/评估采用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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 2018-10-28
    • 2018-12-01
    • 2016-06-29
    • 2018-06-20
    • 2020-09-27
    • 1970-01-01
    相关资源
    最近更新 更多