【发布时间】:2017-02-11 00:15:27
【问题描述】:
我正在尝试从图像中随机裁剪。就像为了增强数据在 Caffe 中所做的一样。
我知道tensorflow已经有函数了
img = tf.random_crop(img, [h, w, 3])
label = tf.random_crop(label, [h, w, 1])
但我不确定是否需要对图像和标签进行相同的裁剪。此外,此功能无法自动对一维或二维小于裁剪尺寸 [h,w] 的图像进行 0 填充。
这又是由
img = tf.image.resize_image_with_crop_or_pad(img, h, w)
label = tf.image.resize_image_with_crop_or_pad(label, h, w)
但它只需要中心作物而不是随机作物。
编辑:
这是一些如何完成填充的代码:
# Cropping dimensions (crops of 700 x 800)
crp_h = tf.constant(700)
crp_w = tf.constant(800)
shape = tf.shape(img)
img_h = shape[0]
img_w = shape[1]
img = tf.cond(img_h < crp_h, lambda: tf.image.pad_to_bounding_box(img, 0, 0, crp_h, img_w), lambda: img)
# Update image dimensions
shape = tf.shape(img)
img_h = shape[0]
img = tf.cond(img_w < crp_w, lambda: tf.image.pad_to_bounding_box(img, 0, 0, img_h, crp_w), lambda: img)
# Update image dimensions
shape = tf.shape(img)
img_w = shape[1]
不幸的是,这里不能使用 python if 条件,所以必须使用丑陋的tf.cond(...)。
【问题讨论】:
标签: tensorflow