{"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":"# # This Python 3 environment comes with many helpful analytics libraries installed\n# # It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# # For example, here's several helpful packages to load\n\n# import numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# # Input data files are available in the read-only \"../input/\" directory\n# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# # You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# # You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import albumentations as A\nfrom albumentations.core.composition import Compose, OneOf\nfrom albumentations.augmentations.transforms import CLAHE, GaussNoise, ISONoise\nfrom albumentations.pytorch import ToTensorV2,ToTensor\nimport matplotlib.pyplot as plt\nfrom torchvision import transforms as T\nfrom PIL import Image\nimport cv2\nimport os\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2021-05-21T02:51:11.866258Z","iopub.execute_input":"2021-05-21T02:51:11.866598Z","iopub.status.idle":"2021-05-21T02:51:15.425946Z","shell.execute_reply.started":"2021-05-21T02:51:11.866571Z","shell.execute_reply":"2021-05-21T02:51:15.424856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from PIL import Image\n# image = cv2.imread('../input/plant-pathology-2021-fgvc8/test_images/85f8cb619c66b863.jpg')\n# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n# imgResize = A.SmallestMaxSize(max_size=448)\n# image = imgResize(image=image)['image']\n# # image.save('./85f8cb619c66b863.jpg')\n# plt.imshow(image)\n# plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !rm -rf ./*\n# from PIL import Image\n# from torchvision import transforms as T\n# image = Image.open('../input/plant-pathology-2021-fgvc8/test_images/85f8cb619c66b863.jpg')\n# print(image.size)\n# image = image.transpose(Image.ROTATE_90)\n# image.save('./85f8cb619c66b863.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-05-21T04:07:41.510415Z","iopub.execute_input":"2021-05-21T04:07:41.510883Z","iopub.status.idle":"2021-05-21T04:07:42.148283Z","shell.execute_reply.started":"2021-05-21T04:07:41.510771Z","shell.execute_reply":"2021-05-21T04:07:42.147297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from matplotlib import pyplot as plt\n# x = Image.open('./85f8cb619c66b863.jpg')\n# plt.imshow(x)\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-21T04:06:28.742585Z","iopub.execute_input":"2021-05-21T04:06:28.742942Z","iopub.status.idle":"2021-05-21T04:06:30.397091Z","shell.execute_reply.started":"2021-05-21T04:06:28.742915Z","shell.execute_reply":"2021-05-21T04:06:30.39395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nfrom torchvision import transforms as T\nfrom tqdm import tqdm\nimport os\nimgResize = T.Resize(size=684,interpolation=3)\ndef resize_images(path_in:str, path_out:str, filenames:list):\n    if not os.path.isdir(path_out): os.mkdir(path_out)\n    for filename in tqdm(filenames):\n        image = Image.open(os.path.join(path_in, filename))\n        if image.size[1]>image.size[0]:\n            print(image.size)\n            print('rot'+str(filenames))\n            image = image.transpose(Image.ROTATE_90)\n        image_resized = imgResize(img=image)\n        save_to = os.path.join(path_out, filename)\n        image_resized.save(save_to,quality=95)","metadata":{"execution":{"iopub.status.busy":"2021-05-21T04:33:15.562966Z","iopub.execute_input":"2021-05-21T04:33:15.56328Z","iopub.status.idle":"2021-05-21T04:33:15.572369Z","shell.execute_reply.started":"2021-05-21T04:33:15.563249Z","shell.execute_reply":"2021-05-21T04:33:15.57148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir = '../input/plant-pathology-2021-fgvc8/train_images'\nout_dir = './resize-plant-684'\nresize_images(root_dir,out_dir,os.listdir(root_dir))","metadata":{"execution":{"iopub.status.busy":"2021-05-21T04:33:16.639276Z","iopub.execute_input":"2021-05-21T04:33:16.639628Z","iopub.status.idle":"2021-05-21T04:33:26.564893Z","shell.execute_reply.started":"2021-05-21T04:33:16.639598Z","shell.execute_reply":"2021-05-21T04:33:26.563876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# imgResize = A.SmallestMaxSize(max_size=224,p=cv2.INTER_CUBIC)\n# def resize_images(path_in:str, path_out:str, filenames:list):\n#     if not os.path.isdir(path_out): os.mkdir(path_out)\n#     for filename in tqdm(filenames):\n#         image = cv2.imread( os.path.join(path_in, filename) )\n#         image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n#         image_resized = imgResize(image=image)['image']\n#         save_to = os.path.join(path_out, filename)\n#         image_resized = cv2.cvtColor(image_resized, cv2.COLOR_RGB2BGR)\n#         if not cv2.imwrite(filename=save_to, img=image_resized):\n#             print(f'Failed to save image: {filename} to dir:{path_out}')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# root_dir = '../input/plant-pathology-2021-fgvc8/train_images'\n# out_dir = './resize-plant-224'\n# resize_images(root_dir,out_dir,os.listdir(root_dir))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for p in caitModel.named_parameters():\n#     print(p)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# caitModel.train()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# caitModel.to('cpu')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# caitModel.head = nn.Linear(192, 10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x = caitModel(torch.rand(1,3,384,384))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# caitModel.training","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  for k in caitModel.state_dict().keys():\n#         print(k)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# len(os.listdir('../input/compete-test/resize-plant-384/'))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import albumentations as A\n# import cv2\n# TRAIN_TRANSFORM = A.Compose([\n#     A.RandomResizedCrop(height=224, width=224,interpolation=cv2.INTER_CUBIC,p=1),\n#     A.HorizontalFlip(p=0.5),\n#     A.VerticalFlip(p=0.5),\n#     A.ShiftScaleRotate(p=0.5),\n#     A.RandomBrightnessContrast(p=0.5),\n# #     HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),\n# #     RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),\n# #     A.ColorJitter(p=0.4),\n# # #    A.CoarseDropout(p=0.25),\n# # #    Cutout(p=0.5),\n#     A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),\n#     ToTensorV2(),\n# ])\n\n# VALID_TRANSFORM = A.Compose([\n#     A.SmallestMaxSize(max_size=224,p=cv2.INTER_CUBIC),\n#     A.CenterCrop(224,224),\n#     A.Normalize(\n#         mean=(0.485, 0.456, 0.406), \n#         std=(0.229, 0.224, 0.225)\n#     ),\n#     ToTensorV2(),\n# ])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# trainer.fit(model=model, datamodule=dm)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !rm -rf logs","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}