原文链接: tf 图像随机变换
上一篇: ros turtlesim 控制小乌龟
下一篇: tf.py_func 操作tensor 增强 拓展 tensorflow灵活性
随机剪裁,先放大1.5倍,然后剪裁出原始尺寸
# 随机剪裁
# 先放大1.5倍后在剪裁出原图尺寸
crop = [tf.random_crop(
tf.image.resize_images(
img,
(img.shape[0] * 3 // 2, img.shape[1] * 3 // 2)
),
img.shape)
for i in range(N_IMAGES)]
crop = tf.concat([img] + crop, axis=1)
crop = tf.clip_by_value(crop, 0, 255)
crop = tf.cast(crop, tf.uint8)
随机旋转,需要使用tf对scipy的方法进行包装
# 随机旋转需要使用包装函数
def random_rotate_image_func(image):
# 旋转角度范围
angle = np.random.uniform(low=-360.0, high=360.0)
return misc.imrotate(image, angle, 'bicubic')
# 输入为img,输出类型
rotate = [tf.py_func(random_rotate_image_func, [img], tf.uint8) for i in range(N_IMAGES)]
rotate = tf.concat([tf.cast(img, tf.uint8)] + rotate, axis=1)
# clip函数只支持的类型,所以需要先转换为int32,再转化为uint8
# bfloat16, float16, float32, float64, int32, int64
rotate