Skip to content

Commit 254a216

Browse files
datumboxjdsgomesNicolasHug
authored
Explicitly copying array in pil_to_tensor (#4566) (#4573)
* mExplicitly copying array in pil_to_tensor * Update torchvision/transforms/functional.py Co-authored-by: Nicolas Hug <[email protected]> * Adding comments regarding implicit array deep copy in PILToTensor transform * Update torchvision/transforms/transforms.py Co-authored-by: Nicolas Hug <[email protected]> Co-authored-by: Nicolas Hug <[email protected]> Co-authored-by: Vasilis Vryniotis <[email protected]> Co-authored-by: Joao Gomes <[email protected]> Co-authored-by: Nicolas Hug <[email protected]>
1 parent b13d5ab commit 254a216

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

torchvision/transforms/functional.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ def pil_to_tensor(pic):
158158
159159
See :class:`~torchvision.transforms.PILToTensor` for more details.
160160
161+
.. note::
162+
163+
A deep copy of the underlying array is performed.
164+
161165
Args:
162166
pic (PIL Image): Image to be converted to tensor.
163167
@@ -174,7 +178,7 @@ def pil_to_tensor(pic):
174178
return torch.as_tensor(nppic)
175179

176180
# handle PIL Image
177-
img = torch.as_tensor(np.asarray(pic))
181+
img = torch.as_tensor(np.array(pic, copy=True))
178182
img = img.view(pic.size[1], pic.size[0], len(pic.getbands()))
179183
# put it from HWC to CHW format
180184
img = img.permute((2, 0, 1))

torchvision/transforms/transforms.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ class PILToTensor:
109109

110110
def __call__(self, pic):
111111
"""
112+
.. note::
113+
114+
A deep copy of the underlying array is performed.
115+
112116
Args:
113117
pic (PIL Image): Image to be converted to tensor.
114118

0 commit comments

Comments
 (0)