【发布时间】:2019-09-10 20:47:36
【问题描述】:
我正在 TensorFlow 2.0 中使用我的个人图像增强功能。更具体地说,我编写了一个返回随机缩放图像的函数。它的输入是image_batch,一个多维numpy数组,形状为:
(no. images, height, width, channel)
在我的具体情况下是:
(31, 300, 300, 3)
这是代码:
def random_zoom(batch, zoom=0.6):
'''
Performs random zoom of a batch of images.
It starts by zero padding the images one by one, then randomly selects
a subsample of the padded image of the same size of the original.
The result is a random zoom
'''
# Import from TensorFlow 2.0
from tensorflow.image import resize_with_pad, random_crop
# save original image height and width
height = batch.shape[1]
width = batch.shape[2]
# Iterate over every image in the batch
for i in range(len(batch)):
# zero pad the image, adding 25-percent to each side
image_distortion = resize_with_pad(batch[i, :,:,:], int(height*(1+zoom)), int(width*(1+zoom)))
# take a subset of the image randomly
image_distortion = random_crop(image_distortion, size=[height, width, 3], seed = 1+i*2)
# put the distorted image back in the batch
batch[i, :,:,:] = image_distortion.numpy()
return batch
然后我可以调用该函数:
new_batch = random_zoom(image_batch)
此时,奇怪的事情发生了:图像的new_batch 和我预期的一样,我很满意……但现在原来的输入对象image_batch 也发生了变化!我不想这样,我不明白为什么会这样。
【问题讨论】:
标签: python numpy tensorflow mutable numpy-ndarray