如何训练具有可变输出大小的对象检测模型?

时间:2021-06-21 13:06:25

标签: image tensorflow keras deep-learning computer-vision

假设我们正在处理以下数据集:https://www.kaggle.com/jutrera/stanford-car-dataset-by-classes-folder

我想创建对象检测模型,输入为不同形状的图像,输出也为可变形状的图像,但输出图像是从相应的输入图像(因此为可变形状)裁剪的汽车。如何使用 Keras 实现这一点。我知道图像分割和自动编码器的过程,但由于输入和输出的大小是可变的,确切的过程似乎很遥远。请帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用 Tensorflow 对象检测 API 并使用输出边界框来裁剪输入图像。请注意,输入图像将被调整大小,但您可以使用输出边界框来裁剪原始图像。

detections = detect(input_tensor)

bounding_boxes = detections['detection_boxes'].numpy()
confidences = detections['detection_scores'].numpy()

for bbox, conf, path in zip(bounding_boxes, confidences, image_path):
    if len(bbox):
        image_orig = load_orig(path) # load original size image
        height, width, channels = image_orig.shape
        y_min, x_min, y_max, x_max = bbox
        y_min_absolute = int(y_min * height)
        x_min_absolute = int(x_min * width)
        y_max_absolute = int(y_max * height)
        x_max_absolute = int(x_max * width)

        cropped_image = tf.image.crop_to_bounding_box(
            image=image_orig,
            offset_height=y_min_absolute,
            offset_width=x_min_absolute,
            target_height=y_max_absolute - y_min_absolute,
            target_width=x_max_absolute - x_min_absolute)

        cropped_images.append(tf.cast(cropped_image, tf.uint8))