π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Single value support for normalize function (#301)
Mikhail Druzhinindb9de151c938
diff --git a/kornia/color/normalize.py b/kornia/color/normalize.pyindex 618607b55..1049d0f09 100644--- a/kornia/color/normalize.py+++ b/kornia/color/normalize.py@@ -64,17 +64,19 @@ def normalize(raise TypeError("std should be a tensor or float. Got {}".format(type(std)))# Allow broadcast on channel dimension- if mean.shape[0] != 1:+ if mean.shape and mean.shape[0] != 1:if mean.shape[0] != data.shape[-3] and mean.shape[:2] != data.shape[:2]:raise ValueError("mean length and number of channels do not match")# Allow broadcast on channel dimension- if std.shape[0] != 1:+ if std.shape and std.shape[0] != 1:if std.shape[0] != data.shape[-3] and std.shape[:2] != data.shape[:2]:raise ValueError("std length and number of channels do not match")- mean = mean[..., :, None, None].to(data.device)- std = std[..., :, None, None].to(data.device)+ if mean.shape:+ mean = mean[..., :, None, None].to(data.device)+ if std.shape:+ std = std[..., :, None, None].to(data.device)out: torch.Tensor = (data - mean) / stddiff --git a/test/color/test_normalize.py b/test/color/test_normalize.pyindex 1c179ef55..ae074e5d9 100644--- a/test/color/test_normalize.py+++ b/test/color/test_normalize.py@@ -99,3 +99,14 @@ class TestNormalize:data = utils.tensor_to_gradcheck_var(data) # to varassert gradcheck(kornia.color.Normalize(mean, std), (data,), raise_exception=True)++ def test_single_value(self):+ # prepare input data+ mean = torch.tensor(2)+ std = torch.tensor(3)+ data = torch.ones(2, 3, 256, 313).float()++ # expected output+ expected = (data - mean) / std++ assert_allclose(kornia.normalize(data, mean, std), expected)