Skip to content

Commit 135df9d

Browse files
prabhat00155NicolasHugdatumbox
authored andcommitted
[fbsync] Explicitly copying array in pil_to_tensor (#4566)
Summary: * mExplicitly copying array in pil_to_tensor * Update torchvision/transforms/functional.py * Adding comments regarding implicit array deep copy in PILToTensor transform * Update torchvision/transforms/transforms.py Reviewed By: NicolasHug Differential Revision: D31505570 fbshipit-source-id: a2ce1c5cf6b70236bc71a79a3d190ddac7d378a4 Co-authored-by: Nicolas Hug <[email protected]> Co-authored-by: Nicolas Hug <[email protected]> Co-authored-by: Nicolas Hug <[email protected]> Co-authored-by: Vasilis Vryniotis <[email protected]>
1 parent 3cd31d7 commit 135df9d

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
@@ -156,6 +156,10 @@ def pil_to_tensor(pic):
156156
157157
See :class:`~torchvision.transforms.PILToTensor` for more details.
158158
159+
.. note::
160+
161+
A deep copy of the underlying array is performed.
162+
159163
Args:
160164
pic (PIL Image): Image to be converted to tensor.
161165
@@ -172,7 +176,7 @@ def pil_to_tensor(pic):
172176
return torch.as_tensor(nppic)
173177

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

torchvision/transforms/transforms.py

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

143143
def __call__(self, pic):
144144
"""
145+
.. note::
146+
147+
A deep copy of the underlying array is performed.
148+
145149
Args:
146150
pic (PIL Image): Image to be converted to tensor.
147151

0 commit comments

Comments
 (0)