【发布时间】: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格式。因此,如果您的图像有任何其他编解码器格式,则必须对其进行转换。如果您需要帮助,请告诉我并尝试一下 -
我应该更新帖子。这就是我接下来尝试成功的方法。
-
我会把这个作为答案