Repository: incubator-singa Updated Branches: refs/heads/master fd06722fe -> 62f5e1853
Add vertical flip function and method in image_tool.py Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/62f5e185 Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/62f5e185 Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/62f5e185 Branch: refs/heads/master Commit: 62f5e185364fb2a6bd52896f6134d503681bf60d Parents: fd06722 Author: Gene <[email protected]> Authored: Sun Jun 4 01:34:27 2017 +0800 Committer: Gene <[email protected]> Committed: Sun Jun 4 01:34:27 2017 +0800 ---------------------------------------------------------------------- python/singa/image_tool.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/62f5e185/python/singa/image_tool.py ---------------------------------------------------------------------- diff --git a/python/singa/image_tool.py b/python/singa/image_tool.py index 622c6f0..bbdd32e 100644 --- a/python/singa/image_tool.py +++ b/python/singa/image_tool.py @@ -183,6 +183,10 @@ def flip(img): new_img = img.transpose(Image.FLIP_LEFT_RIGHT) return new_img +def flip_down(img): + # print 'flip_down' + new_img = img.transpose(Image.FLIP_TOP_BOTTOM) + return new_img def get_list_sample(l, sample_size): return [l[i] for i in sorted(random.sample(xrange(len(l)), sample_size))] @@ -454,6 +458,32 @@ class ImageTool(): else: return new_imgs + def flip_down(self, num_case=1, inplace=True): + '''Randomly flip a img top to bottom. + Args: + num_case: num of cases, must be in {1,2}; if 2, then add the orignal + and flip_down img + inplace: inplace imgs or not (return new_imgs) + ''' + new_imgs = [] + for img in self.imgs: + if num_case == 1: + if random.randint(0, 1): + new_imgs.append(flip_down(img)) + else: + new_imgs.append(img) + elif num_case == 2: + new_imgs.append(flip_down(img)) + new_imgs.append(img) + else: + raise Exception('num_case must be in [0,2]') + + if inplace: + self.imgs = new_imgs + return self + else: + return new_imgs + def color_cast(self, offset=20, inplace=True): '''Add a random value from [-offset, offset] to each channel
