Numpy 数组:函数也会影响原始输入对象

Numpy array: Function affects original input object as well

我正在 TensorFlow 2.0 中使用我个人的图像增强功能。更具体地说,我写了一个 returns 随机缩放图像的函数。它的输入是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,原始输入对象,已经变了!我不想这样,我不明白为什么会这样。

好吧,这一行 batch[i, :,:,:] = image_distortion.numpy() 修改了作为参数传递的数组。

您的困惑可能源于对另一种语言(如 C++)的熟悉,其中作为参数传递的对象被隐式复制。

在 Python 中,发生的事情就是您所说的按引用传递。除非您想要,否则不会复制。因此,并不是 new_batchimage_batch 都被修改了;它们是指向已更改的相同对象的两个名称。

因此,您可能希望在函数开始时执行类似 batch = batch.copy() 的操作。