Trusted-AI/adversarial-robustness-toolbox

`PyTorchYolo` and `PyTorchObjectDetector` modify the original numpy array

f4str opened this issue · 1 comments

f4str commented

Describe the bug
The PyTorchYolo and PyTorchObjectDetectorobject detection estimators modify the original numpy array. This occurs because the torch.from_numpy and in-place torch methods are being used which will re-use the memory from the original numpy array.

To Reproduce
Create a PyTorchYolo model with the clip_values set to (0, 255). Create any numpy image and pass it to the model via the predict method. The original numpy array will be modified.

The following snippets show a simplified version of what is happening:

x = np.array([1])
y = torch.from_numpy(x)
y += 1

print(x)  # [2]
print(y)  # [2]
x = np.array([1])
y = torch.from_numpy(x)
y = y + 1

print(x)  # [1]
print(y)  # [2]
x = np.array([1])
y = torch.tensor(x)
y += 1

print(x)  # [1]
print(y)  # [2]

Expected behavior
The original numpy array should not be modified. This can be fixed by either making a copy or not using in-place operations. One unknown point is how much of the inputs should be copied (i.e., only images or both images and labels).

Screenshots
N/A

System information (please complete the following information):

  • OS
  • Python version
  • ART version or commit number
  • TensorFlow / Keras / PyTorch / MXNet version

Hi @f4str Thank you for opening this issue!