Python 为什么使用tf.image.sample\u扭曲的\u边界框时边界框和剪裁的图片不匹配?

Python 为什么使用tf.image.sample\u扭曲的\u边界框时边界框和剪裁的图片不匹配?,python,python-3.x,tensorflow,Python,Python 3.x,Tensorflow,我想得到一个边界框并根据边界框剪裁图片,所以我使用了tf.image.sample\u-distributed\u-bounding\u-box。但我失败了,我做错了什么? 我的结果看起来像 根据边界框的边界框和剪裁图片不匹配 我的代码: with tf.Session() as sess: boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]]) image_float

我想得到一个边界框并根据边界框剪裁图片,所以我使用了
tf.image.sample\u-distributed\u-bounding\u-box
。但我失败了,我做错了什么? 我的结果看起来像

根据边界框的边界框和剪裁图片不匹配

我的代码:

with tf.Session() as sess:         
    boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])

    image_float = tf.image.convert_image_dtype(img_data, tf.float32) # uint8 -> float

    # resize image
    image_small = tf.image.resize_images(image_float, [180, 267], method=0)

    # Generate a single distorted bounding box.
    begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(
        tf.shape(image_small),
        bounding_boxes=boxes,
        min_object_covered=0.1)

    # Draw the bounding box in an image summary.
    image_with_box = tf.image.draw_bounding_boxes(tf.expand_dims(image_small, 0),
                                                  bbox_for_draw)

    tf.summary.image('images_with_box', image_with_box)

    # Employ the bounding box to distort the image.
    distorted_image = tf.slice(image_small, begin, size)

    plt.figure(figsize = (30, 20))
    plt.subplot(3, 1, 1)
    plt.title("image with a random box")
    plt.imshow(image_with_box[0].eval())

    plt.subplot(3, 1, 2)
    plt.title("destorted image")
    plt.imshow(distorted_image.eval())
plt.show()

这是因为每次调用
.eval()
都会触发一次新的图形运行,从而生成一个新的随机边界框

要获得一致的输出,您需要同时运行运算符,例如

res = sess.run([image_with_box[0], distorted_image])