{"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":"!pip install -Uqqq fastai","metadata":{"execution":{"iopub.status.busy":"2022-03-22T12:24:47.868569Z","iopub.execute_input":"2022-03-22T12:24:47.869006Z","iopub.status.idle":"2022-03-22T12:25:10.423357Z","shell.execute_reply.started":"2022-03-22T12:24:47.868888Z","shell.execute_reply":"2022-03-22T12:25:10.422364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2022-03-22T12:25:10.426135Z","iopub.execute_input":"2022-03-22T12:25:10.426699Z","iopub.status.idle":"2022-03-22T12:25:12.909677Z","shell.execute_reply.started":"2022-03-22T12:25:10.426640Z","shell.execute_reply":"2022-03-22T12:25:12.908485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resizing an Image with PIL","metadata":{}},{"cell_type":"code","source":"img = Image.open('../input/plant-pathology-2021-fgvc8/train_images/800113bb65efe69e.jpg')\nprint(img.size)\nimg","metadata":{"execution":{"iopub.status.busy":"2022-03-22T12:25:12.911032Z","iopub.execute_input":"2022-03-22T12:25:12.911274Z","iopub.status.idle":"2022-03-22T12:25:16.702609Z","shell.execute_reply.started":"2022-03-22T12:25:12.911243Z","shell.execute_reply":"2022-03-22T12:25:16.700921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size = 340\nimg.thumbnail((size, size))\nprint(img.size)\nimg","metadata":{"execution":{"iopub.status.busy":"2022-03-22T12:25:16.705734Z","iopub.execute_input":"2022-03-22T12:25:16.706288Z","iopub.status.idle":"2022-03-22T12:25:16.787378Z","shell.execute_reply.started":"2022-03-22T12:25:16.706250Z","shell.execute_reply":"2022-03-22T12:25:16.786507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resizing the whole dataset","metadata":{}},{"cell_type":"code","source":"import os\nfrom tqdm.auto import tqdm\nfrom joblib import Parallel, delayed\nimport multiprocessing","metadata":{"execution":{"iopub.status.busy":"2022-03-22T12:25:16.788852Z","iopub.execute_input":"2022-03-22T12:25:16.789281Z","iopub.status.idle":"2022-03-22T12:25:16.793104Z","shell.execute_reply.started":"2022-03-22T12:25:16.789247Z","shell.execute_reply":"2022-03-22T12:25:16.792469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_jobs = multiprocessing.cpu_count()\nn_jobs","metadata":{"execution":{"iopub.status.busy":"2022-03-22T12:25:16.794438Z","iopub.execute_input":"2022-03-22T12:25:16.794863Z","iopub.status.idle":"2022-03-22T12:25:16.806832Z","shell.execute_reply.started":"2022-03-22T12:25:16.794818Z","shell.execute_reply":"2022-03-22T12:25:16.805934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_path = Path('train')\nos.makedirs(out_path, exist_ok = True)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T12:25:16.808004Z","iopub.execute_input":"2022-03-22T12:25:16.808370Z","iopub.status.idle":"2022-03-22T12:25:16.817885Z","shell.execute_reply.started":"2022-03-22T12:25:16.808331Z","shell.execute_reply":"2022-03-22T12:25:16.816660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = Path('../input/plant-pathology-2021-fgvc8/train_images')\ntrain_imgs = get_image_files(train_path)\ntrain_imgs","metadata":{"execution":{"iopub.status.busy":"2022-03-22T12:25:16.819150Z","iopub.execute_input":"2022-03-22T12:25:16.820125Z","iopub.status.idle":"2022-03-22T12:25:43.196573Z","shell.execute_reply.started":"2022-03-22T12:25:16.820062Z","shell.execute_reply":"2022-03-22T12:25:43.195966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_img_file(file, out_path):\n    img = Image.open(file)\n    img.thumbnail((size, size))\n    img.save(out_path/file.name)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T12:26:04.904755Z","iopub.execute_input":"2022-03-22T12:26:04.905024Z","iopub.status.idle":"2022-03-22T12:26:04.911152Z","shell.execute_reply.started":"2022-03-22T12:26:04.904996Z","shell.execute_reply":"2022-03-22T12:26:04.910429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = Parallel(n_jobs=n_jobs)(delayed(resize_img_file)(file, out_path) for file in tqdm(train_imgs))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil","metadata":{"execution":{"iopub.status.busy":"2022-03-22T12:26:12.051293Z","iopub.execute_input":"2022-03-22T12:26:12.052413Z","iopub.status.idle":"2022-03-22T12:26:12.057440Z","shell.execute_reply.started":"2022-03-22T12:26:12.052354Z","shell.execute_reply":"2022-03-22T12:26:12.056338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.make_archive('train','zip',root_dir='./train')","metadata":{"execution":{"iopub.status.busy":"2022-03-22T12:28:29.661809Z","iopub.execute_input":"2022-03-22T12:28:29.662101Z","iopub.status.idle":"2022-03-22T12:28:29.707552Z","shell.execute_reply.started":"2022-03-22T12:28:29.662069Z","shell.execute_reply":"2022-03-22T12:28:29.706496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf train","metadata":{"execution":{"iopub.status.busy":"2022-03-22T12:28:50.742475Z","iopub.execute_input":"2022-03-22T12:28:50.742779Z","iopub.status.idle":"2022-03-22T12:28:51.533353Z","shell.execute_reply.started":"2022-03-22T12:28:50.742748Z","shell.execute_reply":"2022-03-22T12:28:51.532165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}