Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions torchvision/transforms/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ def to_pil_image(pic, mode=None):
return Image.fromarray(npimg, mode=mode)


def normalize(tensor, mean, std):
def normalize(tensor, mean, std, inplace=False):
"""Normalize a tensor image with mean and standard deviation.

.. note::
This transform acts in-place, i.e., it mutates the input tensor.
This transform acts out of place by default, i.e., it does not mutates the input tensor.

See :class:`~torchvision.transforms.Normalize` for more details.

Expand All @@ -200,10 +200,18 @@ def normalize(tensor, mean, std):
if not _is_tensor_image(tensor):
raise TypeError('tensor is not a torch image.')

# This is faster than using broadcasting, don't change without benchmarking
for t, m, s in zip(tensor, mean, std):
t.sub_(m).div_(s)
return tensor
if not inplace:
tensor_clone = tensor.clone()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just do

if not inplace:
    tensor = tensor.clone()
# everything as before

?

# This is faster than using broadcasting, don't change without benchmarking
for t, m, s in zip(tensor_clone, mean, std):
t.sub_(m).div_(s)
return tensor_clone

else:
# This is faster than using broadcasting, don't change without benchmarking
for t, m, s in zip(tensor, mean, std):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this can now be replaced with something simpler and as efficient as before

mean = torch.tensor(mean, dtype=torch.float32)
std = torch.tensor(std, dtype=torch.float32)
return (tensor - mean[:, None, None]) / std[:, None, None]

This is out-of-place, but can be made in-place as well if needed

Just to double check, can you do some benchmarkings and report back if it's indeed now giving the same runtime speed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fmassa i ran some tests and found that this approach was smewhat faster .

t.sub_(m).div_(s)
return tensor


def resize(img, size, interpolation=Image.BILINEAR):
Expand Down
7 changes: 4 additions & 3 deletions torchvision/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,17 @@ class Normalize(object):
``input[channel] = (input[channel] - mean[channel]) / std[channel]``

.. note::
This transform acts in-place, i.e., it mutates the input tensor.
This transform acts out of place, i.e., it does not mutates the input tensor.

Args:
mean (sequence): Sequence of means for each channel.
std (sequence): Sequence of standard deviations for each channel.
"""

def __init__(self, mean, std):
def __init__(self, mean, std, inplace=False):
self.mean = mean
self.std = std
self.inplace = inplace

def __call__(self, tensor):
"""
Expand All @@ -155,7 +156,7 @@ def __call__(self, tensor):
Returns:
Tensor: Normalized Tensor image.
"""
return F.normalize(tensor, self.mean, self.std)
return F.normalize(tensor, self.mean, self.std, self.inplace)

def __repr__(self):
return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, self.std)
Expand Down