{"cells":[{"metadata":{"_uuid":"5d4fbf34-30da-45a6-8bb9-6a616986f470","_cell_guid":"7684d376-f0b9-459a-abbb-62d6ea16e54b","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"819ae6c6-3cfa-46d6-9dd5-e7ef7c2dacfc","_cell_guid":"6e7c2eea-a12a-4446-86e0-9c0ce877eee7","trusted":true},"cell_type":"code","source":"import PIL\nimport cv2\nfrom PIL import ImageOps","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6215d43d-01aa-498a-b279-73711d439bb4","_cell_guid":"24557cde-5e34-48b7-81ce-243320bacab1","trusted":true},"cell_type":"markdown","source":"At first, let's get packages versions, specs and some info on the machine","execution_count":null},{"metadata":{"_uuid":"43457fc8-7c69-4323-971e-110d8736bea6","_cell_guid":"04ea9bd2-bd94-4aed-b6e5-29c2b13b35c2","trusted":true},"cell_type":"code","source":"print(cv2.__version__, cv2.__spec__)\nprint(cv2.getBuildInformation())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"84b09f04-5419-4eff-8b5b-e2fccb080b40","_cell_guid":"c7b19c69-f3e4-4a5a-9cbc-e922086645f6","trusted":true},"cell_type":"code","source":"PIL.__version__, PIL.__spec__","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d14f057f-08dd-4f3c-b856-56155c59221d","_cell_guid":"16c7e6b8-785c-43d2-abb6-f85b15966b5a","trusted":true},"cell_type":"code","source":"!cat /proc/cpuinfo | egrep \"model name\"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"99cb4821-dc27-4848-ae16-dc9f75893488","_cell_guid":"089609b7-402c-46ac-ba4e-53bad87bf54e","trusted":true},"cell_type":"markdown","source":"Data storage info: `ROTA 1` means rotational device","execution_count":null},{"metadata":{"_uuid":"63e2f830-d17b-4fe4-a3b6-43d38b8246bb","_cell_guid":"9ccc7634-ad77-45c5-ab1f-c96ff379c980","trusted":true},"cell_type":"code","source":"!lsblk -o name,rota,type,mountpoint","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d0a4f4e9-86ab-4e98-a0c2-90fe0acdc519","_cell_guid":"554d7c5a-dc10-4f99-b985-c3d31d7c215b","trusted":true},"cell_type":"markdown","source":"Now let's setup the input data","execution_count":null},{"metadata":{"_uuid":"2223d294-6424-4b02-b9b8-fc7eb6748d8c","_cell_guid":"e008aece-4349-4b8a-aec0-1405bd3964d5","trusted":true},"cell_type":"code","source":"import os\nthis_path = '.'\nINPUT_PATH = os.path.abspath(os.path.join(this_path, '..', 'input'))\nTRAIN_DATA = os.path.join(INPUT_PATH,\"manish\")\n\nfrom glob import glob\nfilenames = glob(os.path.join(TRAIN_DATA, \"*.jpg\"))\nlen(filenames)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b65b8d91-d828-47d6-b42c-4412939f2494","_cell_guid":"90f830d8-e49a-4abf-a803-4dbaf7364612","trusted":true},"cell_type":"code","source":"import matplotlib.pylab as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"23b954f3-ba82-4591-ba43-d6e7ad358020","_cell_guid":"637e197c-8f7f-458d-aa00-8883cc0a1c8d","trusted":true},"cell_type":"markdown","source":"## 1 stage: 100 images, load image + blur + flip","execution_count":null},{"metadata":{"_uuid":"a1ae8a5b-2885-4cef-9ead-02a62f4f8030","_cell_guid":"c34c0088-fa6b-495a-b27a-0a28934add15","trusted":true},"cell_type":"code","source":"import numpy as np\nfrom PIL import Image, ImageOps,ImageFilter\n\ndef stage_1_PIL(filename):\n    img_pil = Image.open(filename)\n    img_pil = img_pil.filter(ImageFilter.BoxBlur(1))\n    img_pil = img_pil.transpose(Image.FLIP_LEFT_RIGHT)\n    return np.asarray(img_pil)\n\ndef stage_1_cv2(filename):\n    img = cv2.imread(filename)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = cv2.blur(img, ksize=(3, 3))\n    img = cv2.flip(img, flipCode=1)\n    return img","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8b7e2962-159d-4076-84d2-ac8e3f9279f0","_cell_guid":"a2e32c03-c5dd-400e-9a83-2181451b3aa9","trusted":true},"cell_type":"markdown","source":"Let's compare briefly results of transformations on the first image. Results are not perfectly the same, but it is not important for the benchmark","execution_count":null},{"metadata":{"_uuid":"aff3cc6f-01e2-4f3e-9fff-befedfdcb0bc","_cell_guid":"6c11d9e0-0fe7-4e4d-9275-030fc9e0068f","trusted":true},"cell_type":"code","source":"f = filenames[0]\nr1 = stage_1_PIL(f) \nr2 = stage_1_cv2(f)\n\nplt.figure(figsize=(16, 16))\nplt.subplot(131)\nplt.imshow(r1)\nplt.subplot(132)\nplt.imshow(r2)\nplt.subplot(133)\nplt.imshow(np.abs(r1 - r2))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"497c34f5-980d-455d-abcf-479b548e9469","_cell_guid":"94308133-8b6d-4e29-9ae6-e6b2b022b310","trusted":true},"cell_type":"code","source":"%timeit -n5 -r3 [stage_1_PIL(f) for f in filenames[:100]]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3b66badc-fe33-4615-8d7f-c6ba6441dfe4","_cell_guid":"bd40b80c-16d8-4c08-acac-7a82320569b8","trusted":true},"cell_type":"code","source":"%timeit -n5 -r3 [stage_1_cv2(f) for f in filenames[:100]]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"97572320-f5cc-4d9a-9335-1c2c0a7dde93","_cell_guid":"f93322dc-bbd9-4d24-8cc1-ea903b3d850e","trusted":true},"cell_type":"markdown","source":"## 1b stage: 100 images, blur + flip","execution_count":null},{"metadata":{"_uuid":"b38d1257-d78b-4926-94cb-735fb98e421a","_cell_guid":"72b872fa-767d-4dea-8437-30fec4b1344b","trusted":true},"cell_type":"code","source":"def stage_1b_PIL(img_pil):\n    img_pil = img_pil.filter(ImageFilter.BoxBlur(1))\n    img_pil = img_pil.transpose(Image.FLIP_LEFT_RIGHT)\n    return np.asarray(img_pil)\n\ndef stage_1b_cv2(img):    \n    img = cv2.blur(img, ksize=(3, 3))\n    img = cv2.flip(img, flipCode=1)\n    return img","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f91e5fe6-110f-4e81-b6c5-7923d249faa0","_cell_guid":"c279b00d-7309-4601-82b2-0ba7cdcb87e9","trusted":true},"cell_type":"code","source":"imgs_PIL = [Image.open(filename) for filename in filenames[:100]]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4f161e3e-b4d1-41e4-b412-770d4631f9c4","_cell_guid":"9bf94d26-13b4-40b9-abcc-f963d7598f96","trusted":true},"cell_type":"code","source":"def cv2_open(filename):\n    img = cv2.imread(filename)\n    return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\nimgs_cv2 = [cv2_open(filename) for filename in filenames[:100]]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0bbbe6a3-8fab-45f2-8e8e-35415e25a5c6","_cell_guid":"1920bf4d-db2c-42bc-a5e2-9ee2b80f6839","trusted":true,"scrolled":true},"cell_type":"code","source":"%timeit -n5 -r3 [stage_1b_PIL(img_pil) for img_pil in imgs_PIL]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"86fdd898-4815-439f-ae83-248264453bab","_cell_guid":"b898bbcb-4358-42bc-bba4-9009d64b7be2","trusted":true},"cell_type":"code","source":"%timeit -n5 -r3 [stage_1b_cv2(img) for img in imgs_cv2]\n\n\n\n\n\n\nimport numpy as np\nfrom PIL import Image, ImageOps\n\n\ndef stage_2_PIL(filename):\n    img_pil = Image.open(filename)\n    img_pil = img_pil.resize((512, 512), Image.CUBIC)\n    img_pil = img_pil.transpose(Image.FLIP_LEFT_RIGHT)\n    img_pil = img_pil.transpose(Image.FLIP_TOP_BOTTOM)\n    return np.asarray(img_pil)\n\ndef stage_2_cv2(filename):\n    img = cv2.imread(filename)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)    \n    img = cv2.resize(img, dsize=(512, 512), interpolation=cv2.INTER_CUBIC)\n    img = cv2.flip(img, flipCode=1)\n    img = cv2.flip(img, flipCode=0)\n    return img","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"13394c3c-f05e-4fc1-9732-92a449ae4b12","_cell_guid":"89fca426-5281-4756-983a-50b3b749e173","trusted":true},"cell_type":"markdown","source":"Again let's compare briefly results of transformations on the first image:","execution_count":null},{"metadata":{"_uuid":"aa8d757d-56d4-4e5e-869f-6e0f403ec740","_cell_guid":"0f0e49ed-7362-405e-8c91-eced51f93ba5","trusted":true},"cell_type":"code","source":"f = filenames[0]\nr1 = stage_2_PIL(f) \nr2 = stage_2_cv2(f)\n\nplt.figure(figsize=(16, 16))\nplt.subplot(131)\nplt.imshow(r1)\nplt.subplot(132)\nplt.imshow(r2)\nplt.subplot(133)\nplt.imshow(np.abs(r1 - r2))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"83b2ca1b-2ae7-42ff-be7e-d963f766e9e4","_cell_guid":"8d8919bd-cb05-46ea-96f6-791bded9ceac","trusted":true},"cell_type":"code","source":"%timeit -n5 -r3 [stage_2_PIL(f) for f in filenames[:200]]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"639f63f8-c072-41ce-87cf-5b9b1229ba7a","_cell_guid":"c33c9460-11c3-4273-a1dd-4611dfd48d4f","trusted":true},"cell_type":"code","source":"%timeit -n5 -r3 [stage_2_cv2(f) for f in filenames[:200]]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2fc7ae-09ee-44b8-a009-d7ea168f62a5","_cell_guid":"dabe8b55-fa31-41a3-856d-d24b9a245315","trusted":true},"cell_type":"markdown","source":"","execution_count":null},{"metadata":{"_uuid":"727532e3-54d1-404d-84b9-ee6fb0a6f9be","_cell_guid":"7bb26b0c-c035-4c94-929e-b2df857cadc1","trusted":true},"cell_type":"markdown","source":"","execution_count":null}],"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":4}