diff --git a/lib/alpha_util.lua b/lib/alpha_util.lua index ad8266f..c80c06d 100644 --- a/lib/alpha_util.lua +++ b/lib/alpha_util.lua @@ -40,8 +40,7 @@ function alpha_util.make_border(rgb, alpha, offset) collectgarbage() end end - rgb[torch.gt(rgb, 1.0)] = 1.0 - rgb[torch.lt(rgb, 0.0)] = 0.0 + rgb:clamp(0.0, 1.0) return rgb end diff --git a/lib/data_augmentation.lua b/lib/data_augmentation.lua index 486c960..4fe7e81 100644 --- a/lib/data_augmentation.lua +++ b/lib/data_augmentation.lua @@ -26,8 +26,7 @@ function data_augmentation.color_noise(src, p, factor) pca_space[i]:mul(color_scale[i]) end local dest = torch.mm(pca_space:t(), cv:t()):t():contiguous():resizeAs(src) - dest[torch.lt(dest, 0.0)] = 0.0 - dest[torch.gt(dest, 1.0)] = 1.0 + dest:clamp(0.0, 1.0) if conversion then dest = iproc.float2byte(dest) diff --git a/lib/image_loader.lua b/lib/image_loader.lua index 76fec1b..56a994f 100644 --- a/lib/image_loader.lua +++ b/lib/image_loader.lua @@ -22,8 +22,7 @@ function image_loader.encode_png(rgb, options) else rgb = rgb:clone():add(clip_eps8) end - rgb[torch.lt(rgb, 0.0)] = 0.0 - rgb[torch.gt(rgb, 1.0)] = 1.0 + rgb:clamp(0.0, 1.0) rgb = rgb:mul(255):floor():div(255) else if options.inplace then @@ -31,8 +30,7 @@ function image_loader.encode_png(rgb, options) else rgb = rgb:clone():add(clip_eps16) end - rgb[torch.lt(rgb, 0.0)] = 0.0 - rgb[torch.gt(rgb, 1.0)] = 1.0 + rgb:clamp(0.0, 1.0) rgb = rgb:mul(65535):floor():div(65535) end local im diff --git a/lib/iproc.lua b/lib/iproc.lua index 40d5568..fa9e67b 100644 --- a/lib/iproc.lua +++ b/lib/iproc.lua @@ -44,8 +44,7 @@ function iproc.float2byte(src) if src:type() == "torch.FloatTensor" then conversion = true dest = (src + clip_eps8):mul(255.0) - dest[torch.lt(dest, 0.0)] = 0 - dest[torch.gt(dest, 255.0)] = 255.0 + dest:clamp(0, 255.0) dest = dest:byte() end return dest, conversion @@ -119,8 +118,7 @@ function iproc.white_noise(src, std, rgb_weights, gamma) local dest if gamma ~= 0 then dest = src:clone():pow(gamma):add(noise) - dest[torch.lt(dest, 0.0)] = 0.0 - dest[torch.gt(dest, 1.0)] = 1.0 + dest:clamp(0.0, 1.0) dest:pow(1.0 / gamma) else dest = src + noise