首页 > 解决方案 > 如何训练具有可变输出大小的对象检测模型?

问题描述

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

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

标签: imagetensorflowkerasdeep-learningcomputer-vision

解决方案


您可以使用 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))

推荐阅读