{"cells":[{"metadata":{"_uuid":"45bad06ecdb61562dd7ccbcacb250e50636369f4"},"cell_type":"markdown","source":"Shoutout to [@Vladimir Iglovikov](https://www.kaggle.com/iglovikov) for providing us this amazing image augmentation library! Heres how you can apply it to this competition."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\nfrom PIL import Image\nimport cv2\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"!pip install albumentations","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1ab08cee621cff00601fe389e55db0d1e545c129"},"cell_type":"markdown","source":"### Load a demo image"},{"metadata":{"trusted":true,"_uuid":"0d344134451d8759ed1633b57ab39ad81858dfc2"},"cell_type":"code","source":"red = np.array(Image.open(\"../input/test/00631ec8-bad9-11e8-b2b9-ac1f6b6435d0_red.png\").convert(\"L\"))\ngreen = np.array(Image.open(\"../input/test/00631ec8-bad9-11e8-b2b9-ac1f6b6435d0_green.png\").convert(\"L\"))\nblue = np.array(Image.open(\"../input/test/00631ec8-bad9-11e8-b2b9-ac1f6b6435d0_blue.png\").convert(\"L\"))\nyellow = np.array(Image.open(\"../input/test/00631ec8-bad9-11e8-b2b9-ac1f6b6435d0_yellow.png\").convert(\"L\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d79661f9db99262d17c288bb287391239b89b208"},"cell_type":"code","source":"demo_rgb=Image.fromarray(np.concatenate((np.expand_dims(red,axis=2),np.expand_dims(green,axis=2),np.expand_dims(blue,axis=2)),axis=2))\ndemo_y=Image.fromarray(np.concatenate((np.expand_dims(yellow,axis=2),np.expand_dims(yellow,axis=2),np.expand_dims(blue,axis=2)),axis=2))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2626200496b738c8aeb2306cb862a3a19b0c7dcf"},"cell_type":"markdown","source":"### Let's see how the 4-channel input looks like..."},{"metadata":{"trusted":true,"_uuid":"e3fd850d983fa2e2341150702e5fe1631477c732"},"cell_type":"code","source":"demo_y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8916436f37122ef190528977e9f554f951ddb34d"},"cell_type":"code","source":"demo_rgb","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3e5d9e271a79b3be1511e5e5eec2ef1bbc0c9b01"},"cell_type":"code","source":"from albumentations import (\n    HorizontalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,\n    Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,\n    IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine,\n    IAASharpen, IAAEmboss, RandomContrast, RandomBrightness, Flip, OneOf, Compose, RandomGamma, ElasticTransform, ChannelShuffle,RGBShift, Rotate\n)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c13c51b185d36b9df51bc8e8c7f04611775b343c"},"cell_type":"markdown","source":"### Basic Transformations"},{"metadata":{"trusted":true,"_uuid":"2d11a73de0294fe8f7767ed823a00bd23c10661d"},"cell_type":"code","source":"def augment(aug, image):\n    return aug(image=image)['image']\n\n# Here's how to deal with 4-channel inputs\n\ndef augment_4chan(aug, image):\n    image[:,:,0:3]=aug(image=image[:,:,0:3])['image']\n    image[:,:,3]=aug(image=image[:,:,1:4])['image'][:,:,2]\n    return image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e16762d5669167c6d1e314838e2082ad6a43371"},"cell_type":"code","source":"aug = HorizontalFlip(p=1)\nImage.fromarray(augment(aug,np.array(demo_rgb)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e8558f4c84f00b6ac299447256bfc1e9ec7d2844"},"cell_type":"code","source":"aug = Blur(p=1,blur_limit=3)\nImage.fromarray(augment(aug,np.array(demo_rgb)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f877798485e12ae4acf1b2373008dc90f5f343cf"},"cell_type":"code","source":"aug = ElasticTransform(p=1,border_mode=cv2.BORDER_REFLECT_101,alpha_affine=40)\nImage.fromarray(augment(aug,np.array(demo_rgb)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0add7cb23f227f088483d45a1a610fc87a0ea658"},"cell_type":"code","source":"aug = RandomGamma(p=1)\nImage.fromarray(augment(aug,np.array(demo_rgb)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e21cde93d15a0ef0cf9817a12f4d9b697e2f6a01"},"cell_type":"code","source":"aug = RandomContrast(p=1)\nImage.fromarray(augment(aug,np.array(demo_rgb)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1ee778b098a105e4bf44f04455a3121be26407d0"},"cell_type":"code","source":"aug = RandomBrightness(p=1)\nImage.fromarray(augment(aug,np.array(demo_rgb)))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"394afdd49d6ab7a1547db2c6fa0a9f053ed2a0a8"},"cell_type":"markdown","source":"*I'm not so sure about this one.....*"},{"metadata":{"trusted":true,"_uuid":"dedf4b3b7eaab60ca4b6a545e452b27ee28a5d40"},"cell_type":"code","source":"aug = ChannelShuffle(p=1)\nImage.fromarray(augment(aug,np.array(demo_rgb)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0579d732a275ba6ae8de4c6a0b5a405d952a594"},"cell_type":"code","source":"aug = Rotate(p=1,limit=30)\nImage.fromarray(augment(aug,np.array(demo_rgb)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"56c04073498bea41481b6d58e9f38d8908c73882"},"cell_type":"markdown","source":"### Composition of augmentations: https://github.com/albu/albumentations/blob/a5bbfedfc500e9cac1c3689b3720769e4fa727b3/notebooks/example.ipynb"},{"metadata":{"trusted":true,"_uuid":"d947674272c008bf344e2e28cc6038de60749ff5"},"cell_type":"code","source":"def strong_aug(p=1):\n    return Compose([\n        RandomRotate90(),\n        Flip(),\n        Transpose(),\n        OneOf([\n            IAAAdditiveGaussianNoise(),\n            GaussNoise(),\n        ], p=0.2),\n        OneOf([\n            MotionBlur(p=.2),\n            MedianBlur(blur_limit=3, p=.1),\n            Blur(blur_limit=3, p=.1),\n        ], p=0.2),\n        ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=.2),\n        OneOf([\n            OpticalDistortion(p=0.3),\n            GridDistortion(p=.1),\n            IAAPiecewiseAffine(p=0.3),\n        ], p=0.2),\n        OneOf([\n            CLAHE(clip_limit=2),\n            IAASharpen(),\n            IAAEmboss(),\n            RandomContrast(),\n            RandomBrightness(),\n        ], p=0.3),\n        #HueSaturationValue(p=0.3),\n    ], p=p)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"98b8ec6f253f584d67f39a9b9e39875e3b788d29"},"cell_type":"code","source":"aug = strong_aug(p=1)\nImage.fromarray(augment(aug,np.array(demo_rgb)))","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}