{"cells":[{"metadata":{},"cell_type":"markdown","source":"<a id=\"toc\"></a>\n# Table of Contents\n1. [Configure parameters](#configure_parameters)\n1. [Import modules](#import_modules)\n1. [Define helper-functions](#define_helper_functions)\n1. [Update train.csv](#update_train_csv)\n1. [Get a copy of sample_submission.csv](#get_a_copy_of_sample_submission_csv)\n1. [Resize images](#resize_images)\n  1. [Resize train images](#resize_train_images)\n  1. [Resize test images](#resize_test_images)\n1. [Zip new train_images and test_images directories](#zip_new_train_images_and_test_images_directories)"},{"metadata":{},"cell_type":"markdown","source":"<a id=\"configure_parameters\"></a>\n# Configure parameters\n[Back to Table of Contents](#toc)"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"DATASET_DIR = '/kaggle/input/understanding_cloud_organization/'\nORI_SIZE = (1400, 2100) # (height, width)\nNEW_SIZE = (384, 576) # (height, width)\n\nimport cv2\nINTERPOLATION = cv2.INTER_CUBIC","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<a id=\"import_modules\"></a>\n# Import modules\n[Back to Table of Contents](#toc)"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom tqdm import tqdm_notebook","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<a id=\"define_helper_functions\"></a>\n# Define helper-functions\n[Back to Table of Contents](#toc)"},{"metadata":{"trusted":true},"cell_type":"code","source":"def mask2rle(img):\n    \"\"\"\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formatted\n    \"\"\"\n    pixels = img.T.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n\n\ndef rle2mask(mask_rle, shape):\n    \"\"\"\n    mask_rle: run-length as string formatted (start length)\n    shape: (width,height) of array to return\n    Returns numpy array, 1 - mask, 0 - background\n    \"\"\"\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in\n                       (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0] * shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<a id=\"update_train_csv\"></a>\n# Update train.csv\n[Back to Table of Contents](#toc)"},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(os.path.join(DATASET_DIR, 'train.csv'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for idx, row in df.iterrows():\n    encodedpixels = row[1]\n    if encodedpixels is not np.nan:\n        mask = rle2mask(encodedpixels, shape=ORI_SIZE[::-1])\n        mask = cv2.resize(mask, NEW_SIZE[::-1], interpolation=INTERPOLATION)\n\n        rle = mask2rle(mask)\n        df.at[idx, 'EncodedPixels'] = rle","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv('./train.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<a id=\"get_a_copy_of_sample_submission_csv\"></a>\n# Get a copy of sample_submission.csv\n[Back to Table of Contents](#toc)"},{"metadata":{"trusted":true},"cell_type":"code","source":"cp $DATASET_DIR/sample_submission.csv ./","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<a id=\"resize_images\"></a>\n# Resize images\n[Back to Table of Contents](#toc)"},{"metadata":{},"cell_type":"markdown","source":"<a id=\"resize_train_images\"></a>\n## Resize train images\n[Back to Table of Contents](#toc)"},{"metadata":{"trusted":true},"cell_type":"code","source":"!mkdir /kaggle/train_images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images_dir = os.path.join(DATASET_DIR, 'train_images')\nimage_files = os.listdir(train_images_dir)\n\nfor image_file in tqdm_notebook(image_files):\n    img = cv2.imread(os.path.join(train_images_dir, image_file))\n    img = cv2.resize(img, NEW_SIZE[::-1], interpolation=INTERPOLATION)\n\n    dst = os.path.join('/kaggle/train_images', image_file)\n    cv2.imwrite(dst, img)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<a id=\"resize_test_images\"></a>\n## Resize test images\n[Back to Table of Contents](#toc)"},{"metadata":{"trusted":true},"cell_type":"code","source":"!mkdir /kaggle/test_images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_images_dir = os.path.join(DATASET_DIR, 'test_images')\nimage_files = os.listdir(test_images_dir)\n\nfor image_file in tqdm_notebook(image_files):\n    img = cv2.imread(os.path.join(test_images_dir, image_file))\n    img = cv2.resize(img, NEW_SIZE[::-1], interpolation=INTERPOLATION)\n\n    dst = os.path.join('/kaggle/test_images', image_file)\n    cv2.imwrite(dst, img)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<a id=\"zip_new_train_images_and_test_images_directories\"></a>\n# Zip new train_images and test_images directories\n[Back to Table of Contents](#toc)"},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"!apt install zip","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cd /kaggle/train_images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!zip -r -m -1 -q /kaggle/working/train_images.zip *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cd /kaggle/test_images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!zip -r -m -1 -q /kaggle/working/test_images.zip *","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}