{"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":"# Resize Training Set Preserving Aspect ratio","metadata":{}},{"cell_type":"code","source":"import functools\nimport math\nimport multiprocessing\nimport os\nfrom dataclasses import dataclass\nfrom glob import glob\n\nimport cv2\nimport numpy as np\nfrom tqdm.auto import tqdm\n\n\n@dataclass\nclass PreprocessConfig:\n    resize: int = 512\n    pas_value: int = 0\n    interp: int = cv2.INTER_CUBIC\n\n\ndef pad_resize_image_dir(\n        input_path: str, output_path: str, config: PreprocessConfig\n):\n    os.makedirs(output_path, exist_ok=True)\n    fnames = glob(os.path.join(input_path, \"*.*\"))\n    partial_process = functools.partial(\n        process_image, config=config, output_path=output_path\n    )\n\n    n_workers = os.cpu_count()\n    with multiprocessing.Pool(n_workers) as pool:\n        futures = pool.imap_unordered(partial_process, fnames)\n        list(tqdm(futures, total=len(fnames)))\n\n\ndef process_image(\n        fname: str,\n        output_path: str,\n        config: PreprocessConfig,\n):\n    bname = os.path.basename(fname)\n    image = cv2.imread(fname)\n    preprocessed_image = pad_resize_square(image, config)\n    output_image_fname = os.path.join(output_path, bname)\n    cv2.imwrite(output_image_fname, preprocessed_image)\n\n\ndef pad_resize_square(image: np.ndarray, config: PreprocessConfig):\n    square_image = pad_image_square(image=image, pad_value=config.pas_value)\n    resized_image = resize_image(\n        image=square_image,\n        size=config.resize,\n        interp=config.interp\n    )\n    return resized_image\n\n\ndef pad_image_square(image: np.ndarray, pad_value: int):\n    h, w, _ = image.shape\n    output_size = max(h, w)\n    pad_h = (output_size - h) / 2\n    pad_w = (output_size - w) / 2\n    padded = np.pad(\n        image,\n        [\n            (math.floor(pad_h), math.ceil(pad_h)),\n            (math.floor(pad_w), math.ceil(pad_w)),\n            (0, 0),\n        ],\n        mode=\"constant\",\n        constant_values=pad_value,\n    )\n    return padded\n\n\ndef resize_image(image: np.ndarray, size: int, interp: int):\n    resized = cv2.resize(image, dsize=(size, size), interpolation=interp)\n    return resized\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-11T15:34:26.967574Z","iopub.execute_input":"2022-02-11T15:34:26.968305Z","iopub.status.idle":"2022-02-11T15:34:26.984742Z","shell.execute_reply.started":"2022-02-11T15:34:26.968256Z","shell.execute_reply":"2022-02-11T15:34:26.983911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pad_resize_image_dir(\n    input_path='../input/happy-whale-and-dolphin/train_images', \n    output_path='/kaggle/working/training_set/resized_train_images', \n    config=PreprocessConfig()\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-11T15:34:29.796274Z","iopub.execute_input":"2022-02-11T15:34:29.796613Z","iopub.status.idle":"2022-02-11T16:07:03.591435Z","shell.execute_reply.started":"2022-02-11T15:34:29.796578Z","shell.execute_reply":"2022-02-11T16:07:03.590314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! cp ../input/happy-whale-and-dolphin/train.csv /kaggle/working/training_set/","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:18:18.204730Z","iopub.execute_input":"2022-02-11T16:18:18.206617Z","iopub.status.idle":"2022-02-11T16:18:19.094255Z","shell.execute_reply.started":"2022-02-11T16:18:18.206515Z","shell.execute_reply":"2022-02-11T16:18:19.093155Z"},"trusted":true},"execution_count":null,"outputs":[]}]}