{"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":"code","source":"from multiprocessing import Pool, cpu_count\n\nimport numpy as np\nimport tensorflow as tf\nfrom PIL import Image\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = 100\n\nf = \"/kaggle/input/plant-pathology-2021-fgvc8/test_images/85f8cb619c66b863.jpg\"\n\ndef parse_image(_):\n    img = tf.io.read_file(f)\n    img = tf.image.decode_jpeg(img, channels=3)\n    img = tf.image.resize(\n        img,\n        (224, 224),\n        method=tf.image.ResizeMethod.BICUBIC,\n    ).numpy()\n\ndef _resize_tf(_):\n    x = Image.open(f)\n    x = tf.image.resize(\n        np.array(x), \n        (224, 224),\n        method=tf.image.ResizeMethod.BICUBIC\n    ).numpy()\n\n\ndef _resize_PIL(_):\n    x = Image.open(f)\n    x = x.resize((224, 224), Image.BICUBIC)\n    x = np.array(x)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! lscpu | grep 'CPU(s):'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with Pool(cpu_count()) as pool:\n    X = list(\n        tqdm(\n            pool.imap(\n                parse_image,\n                range(N),\n            ),\n            total=N,\n            smoothing=0,\n        )\n    )","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(N), smoothing=0):\n    _resize_tf(i)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(N), smoothing=0):\n    _resize_PIL(i)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(N), smoothing=0):\n    parse_image(i)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}