{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### Augmentation with albumentations\n\nData by @RDizzl3 https://www.kaggle.com/c/happy-whale-and-dolphin/discussion/304686 ","metadata":{}},{"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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nimport cv2\nimport glob\nimport albumentations as A\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport numpy as np\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-09T14:20:41.706925Z","iopub.execute_input":"2022-02-09T14:20:41.707224Z","iopub.status.idle":"2022-02-09T14:20:41.714312Z","shell.execute_reply.started":"2022-02-09T14:20:41.707172Z","shell.execute_reply":"2022-02-09T14:20:41.713248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf = pd.read_csv(\"../input/happy-whale-and-dolphin/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:41.717375Z","iopub.execute_input":"2022-02-09T14:20:41.717846Z","iopub.status.idle":"2022-02-09T14:20:41.789571Z","shell.execute_reply.started":"2022-02-09T14:20:41.717809Z","shell.execute_reply":"2022-02-09T14:20:41.788721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the train image file names\n# Use the * as a wild card this will tell glob get us all images in this directory\nimage_path = '/kaggle/input/jpeg-happywhale-128x128/train_images-128-128/train_images-128-128/*'\ntrain_filenames = glob.glob(image_path)","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:41.792861Z","iopub.execute_input":"2022-02-09T14:20:41.793357Z","iopub.status.idle":"2022-02-09T14:20:41.984251Z","shell.execute_reply.started":"2022-02-09T14:20:41.793322Z","shell.execute_reply":"2022-02-09T14:20:41.983363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get a single image path\nimage_path = train_filenames[67]\nimg = cv2.imread(image_path)\n\n# Check the shape - should be (128, 128, 3)\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:41.985415Z","iopub.execute_input":"2022-02-09T14:20:41.986178Z","iopub.status.idle":"2022-02-09T14:20:41.996419Z","shell.execute_reply.started":"2022-02-09T14:20:41.986136Z","shell.execute_reply":"2022-02-09T14:20:41.995726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.Compose([\n    A.RandomCrop(width=128, height=128),\n    A.HorizontalFlip(p=0.5),\n    A.RandomBrightnessContrast(p=0.2),\n])","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:41.998908Z","iopub.execute_input":"2022-02-09T14:20:41.999363Z","iopub.status.idle":"2022-02-09T14:20:42.004117Z","shell.execute_reply.started":"2022-02-09T14:20:41.999327Z","shell.execute_reply":"2022-02-09T14:20:42.003342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pillow_image = Image.open(train_filenames[678])\nimage = np.array(pillow_image)","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:42.005695Z","iopub.execute_input":"2022-02-09T14:20:42.005880Z","iopub.status.idle":"2022-02-09T14:20:42.014299Z","shell.execute_reply.started":"2022-02-09T14:20:42.005858Z","shell.execute_reply":"2022-02-09T14:20:42.013604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transformed = transform(image=image)","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:42.015574Z","iopub.execute_input":"2022-02-09T14:20:42.016037Z","iopub.status.idle":"2022-02-09T14:20:42.024283Z","shell.execute_reply.started":"2022-02-09T14:20:42.016001Z","shell.execute_reply":"2022-02-09T14:20:42.023550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.axis('off')\nplt.imshow(transformed['image'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:42.027154Z","iopub.execute_input":"2022-02-09T14:20:42.027871Z","iopub.status.idle":"2022-02-09T14:20:42.166029Z","shell.execute_reply.started":"2022-02-09T14:20:42.027834Z","shell.execute_reply":"2022-02-09T14:20:42.165237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Color Augmentations\n","metadata":{}},{"cell_type":"code","source":"light = A.Compose([\n    A.RandomBrightnessContrast(),    \n    A.RandomGamma(p=1),    \n], p=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:42.167223Z","iopub.execute_input":"2022-02-09T14:20:42.167970Z","iopub.status.idle":"2022-02-09T14:20:42.172899Z","shell.execute_reply.started":"2022-02-09T14:20:42.167928Z","shell.execute_reply":"2022-02-09T14:20:42.172006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.axis('off')\nplt.imshow(light(image=image)['image'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:42.174175Z","iopub.execute_input":"2022-02-09T14:20:42.175110Z","iopub.status.idle":"2022-02-09T14:20:42.309985Z","shell.execute_reply.started":"2022-02-09T14:20:42.175072Z","shell.execute_reply":"2022-02-09T14:20:42.309339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hue = A.Compose([\n    A.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=50, val_shift_limit=50, p=1),\n], p=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:42.311173Z","iopub.execute_input":"2022-02-09T14:20:42.311606Z","iopub.status.idle":"2022-02-09T14:20:42.316550Z","shell.execute_reply.started":"2022-02-09T14:20:42.311567Z","shell.execute_reply":"2022-02-09T14:20:42.315733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.axis('off')\nplt.imshow(hue(image=image)['image'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:42.317788Z","iopub.execute_input":"2022-02-09T14:20:42.318413Z","iopub.status.idle":"2022-02-09T14:20:42.465416Z","shell.execute_reply.started":"2022-02-09T14:20:42.318374Z","shell.execute_reply":"2022-02-09T14:20:42.464724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"strong = A.Compose([\n    A.ChannelShuffle(p=0.2),\n], p=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:42.466675Z","iopub.execute_input":"2022-02-09T14:20:42.467255Z","iopub.status.idle":"2022-02-09T14:20:42.472684Z","shell.execute_reply.started":"2022-02-09T14:20:42.467217Z","shell.execute_reply":"2022-02-09T14:20:42.471899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.axis('off')\nplt.imshow(strong(image=image)['image'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:42.479294Z","iopub.execute_input":"2022-02-09T14:20:42.480111Z","iopub.status.idle":"2022-02-09T14:20:42.675536Z","shell.execute_reply.started":"2022-02-09T14:20:42.480063Z","shell.execute_reply":"2022-02-09T14:20:42.673969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"snow = A.Compose(\n    [A.RandomSnow(brightness_coeff=2.5, snow_point_lower=0.3, snow_point_upper=0.5, p=1)],\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:42.682262Z","iopub.execute_input":"2022-02-09T14:20:42.683584Z","iopub.status.idle":"2022-02-09T14:20:42.688810Z","shell.execute_reply.started":"2022-02-09T14:20:42.683402Z","shell.execute_reply":"2022-02-09T14:20:42.687880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.axis('off')\nplt.imshow(snow(image=image)['image'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:42.691552Z","iopub.execute_input":"2022-02-09T14:20:42.692177Z","iopub.status.idle":"2022-02-09T14:20:42.914414Z","shell.execute_reply.started":"2022-02-09T14:20:42.692136Z","shell.execute_reply":"2022-02-09T14:20:42.913684Z"},"trusted":true},"execution_count":null,"outputs":[]}]}