Commit 0630a31c authored by eyaler's avatar eyaler Committed by Yuxin Wu

transpose_and_minmaxnormalize (#288)

* transpose_and_minmaxnormalize

transpose_and_minmaxnormalize

transpose_and_minmaxnormalize

transpose_and_minmaxnormalize

* fix linting
parent f0573ed2
...@@ -7,7 +7,7 @@ import numpy as np ...@@ -7,7 +7,7 @@ import numpy as np
import cv2 import cv2
__all__ = ['Hue', 'Brightness', 'Contrast', 'MeanVarianceNormalize', __all__ = ['Hue', 'Brightness', 'Contrast', 'MeanVarianceNormalize',
'GaussianBlur', 'Gamma', 'Clip', 'Saturation', 'Lighting'] 'GaussianBlur', 'Gamma', 'Clip', 'Saturation', 'Lighting', 'MinMaxNormalize']
class Hue(ImageAugmentor): class Hue(ImageAugmentor):
...@@ -98,7 +98,7 @@ class MeanVarianceNormalize(ImageAugmentor): ...@@ -98,7 +98,7 @@ class MeanVarianceNormalize(ImageAugmentor):
Args: Args:
all_channel (bool): if True, normalize all channels together. else separately. all_channel (bool): if True, normalize all channels together. else separately.
""" """
self.all_channel = all_channel self._init(locals())
def _augment(self, img, _): def _augment(self, img, _):
img = img.astype('float32') img = img.astype('float32')
...@@ -232,3 +232,30 @@ class Lighting(ImageAugmentor): ...@@ -232,3 +232,30 @@ class Lighting(ImageAugmentor):
if old_dtype == np.uint8: if old_dtype == np.uint8:
img = np.clip(img, 0, 255) img = np.clip(img, 0, 255)
return img.astype(old_dtype) return img.astype(old_dtype)
class MinMaxNormalize(ImageAugmentor):
"""
Linearly scales the image to the range [min, max].
This augmentor always returns float32 images.
"""
def __init__(self, min=0, max=255, all_channel=True):
"""
Args:
max (float): The new maximum value
min (float): The new minimum value
all_channel (bool): if True, normalize all channels together. else separately.
"""
self._init(locals())
def _augment(self, img, _):
img = img.astype('float32')
if self.all_channel:
minimum = np.min(img)
maximum = np.max(img)
else:
minimum = np.min(img, axis=(0, 1), keepdims=True)
maximum = np.max(img, axis=(0, 1), keepdims=True)
img = (self.max - self.min) * (img - minimum) / (maximum - minimum) + self.min
return img
...@@ -8,7 +8,7 @@ from ...utils.argtools import shape2d ...@@ -8,7 +8,7 @@ from ...utils.argtools import shape2d
import numpy as np import numpy as np
import cv2 import cv2
__all__ = ['Flip', 'Resize', 'RandomResize', 'ResizeShortestEdge'] __all__ = ['Flip', 'Resize', 'RandomResize', 'ResizeShortestEdge', 'Transpose']
class Flip(ImageAugmentor): class Flip(ImageAugmentor):
...@@ -140,3 +140,31 @@ class RandomResize(ImageAugmentor): ...@@ -140,3 +140,31 @@ class RandomResize(ImageAugmentor):
if img.ndim == 3 and ret.ndim == 2: if img.ndim == 3 and ret.ndim == 2:
ret = ret[:, :, np.newaxis] ret = ret[:, :, np.newaxis]
return ret return ret
class Transpose(ImageAugmentor):
"""
Random transpose the image
"""
def __init__(self, prob=0.5):
"""
Args:
prob (float): probability of transpose.
"""
super(Transpose, self).__init__()
self.prob = prob
self._init()
def _get_augment_params(self, img):
return self._rand_range() < self.prob
def _augment(self, img, do):
ret = img
if do:
ret = cv2.transpose(img)
if img.ndim == 3 and ret.ndim == 2:
ret = ret[:, :, np.newaxis]
return ret
def _fprop_coord(self, coord, param):
raise NotImplementedError()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment