{"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 wwf timm -qqq","metadata":{"execution":{"iopub.status.busy":"2022-02-02T08:47:05.207053Z","iopub.execute_input":"2022-02-02T08:47:05.207706Z","iopub.status.idle":"2022-02-02T08:47:14.725971Z","shell.execute_reply.started":"2022-02-02T08:47:05.207672Z","shell.execute_reply":"2022-02-02T08:47:14.724503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --upgrade fastai","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-02T08:47:14.730294Z","iopub.execute_input":"2022-02-02T08:47:14.730654Z","iopub.status.idle":"2022-02-02T08:47:25.187600Z","shell.execute_reply.started":"2022-02-02T08:47:14.730610Z","shell.execute_reply":"2022-02-02T08:47:25.186472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\nimport glob\nimport random\nimport torch\nimport torchvision\nfrom torchvision import transforms\nimport albumentations as A\nfrom fastai.vision.all import *\nfrom fastai import *\nfrom wwf.vision.timm import *\nimport timm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-02T08:47:25.190611Z","iopub.execute_input":"2022-02-02T08:47:25.191300Z","iopub.status.idle":"2022-02-02T08:47:30.642518Z","shell.execute_reply.started":"2022-02-02T08:47:25.191254Z","shell.execute_reply":"2022-02-02T08:47:30.641273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed=42):\n    os.environ['PYTHONHASHSEED']=str(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)","metadata":{"execution":{"iopub.status.busy":"2022-02-02T08:47:30.645336Z","iopub.execute_input":"2022-02-02T08:47:30.645676Z","iopub.status.idle":"2022-02-02T08:47:30.651390Z","shell.execute_reply.started":"2022-02-02T08:47:30.645625Z","shell.execute_reply":"2022-02-02T08:47:30.650402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set_seed()","metadata":{"execution":{"iopub.status.busy":"2022-02-02T08:47:30.653405Z","iopub.execute_input":"2022-02-02T08:47:30.654184Z","iopub.status.idle":"2022-02-02T08:47:30.666364Z","shell.execute_reply.started":"2022-02-02T08:47:30.654076Z","shell.execute_reply":"2022-02-02T08:47:30.665253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    model = 'resnext101_32x8d' #Taken ViT, Place your model here!\n    train_bs = 16\n    valid_bs = 32\n    image_size = 224\n    tta = 5\n    epochs = 10\n    lr = 1e-3\n    fp16 = True\n    fp32 = True\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2022-02-02T09:01:35.009841Z","iopub.execute_input":"2022-02-02T09:01:35.010176Z","iopub.status.idle":"2022-02-02T09:01:35.016626Z","shell.execute_reply.started":"2022-02-02T09:01:35.010130Z","shell.execute_reply":"2022-02-02T09:01:35.015645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/happy-whale-and-dolphin/train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-02T08:47:57.218632Z","iopub.execute_input":"2022-02-02T08:47:57.219542Z","iopub.status.idle":"2022-02-02T08:47:57.332043Z","shell.execute_reply.started":"2022-02-02T08:47:57.219491Z","shell.execute_reply":"2022-02-02T08:47:57.331041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['species'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-02T08:47:57.378753Z","iopub.execute_input":"2022-02-02T08:47:57.379084Z","iopub.status.idle":"2022-02-02T08:47:57.400957Z","shell.execute_reply.started":"2022-02-02T08:47:57.379053Z","shell.execute_reply":"2022-02-02T08:47:57.399834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DIR = \"../input/happy-whale-and-dolphin/train_images\"\nimage_path = f'{DIR}/00103cbe9d25ce.jpg'","metadata":{"execution":{"iopub.status.busy":"2022-02-02T09:09:38.616344Z","iopub.execute_input":"2022-02-02T09:09:38.616638Z","iopub.status.idle":"2022-02-02T09:09:38.621479Z","shell.execute_reply.started":"2022-02-02T09:09:38.616605Z","shell.execute_reply":"2022-02-02T09:09:38.620410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_img = cv2.imread(image_path)\nplt.imshow(random_img)","metadata":{"execution":{"iopub.status.busy":"2022-02-02T09:09:40.770900Z","iopub.execute_input":"2022-02-02T09:09:40.771945Z","iopub.status.idle":"2022-02-02T09:09:42.356058Z","shell.execute_reply.started":"2022-02-02T09:09:40.771909Z","shell.execute_reply":"2022-02-02T09:09:42.355143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Thanks to https://www.kaggle.com/khoongweihao/insect-augmentation-et-al\nalbumentation_list = [A.RandomSunFlare(p=1), \n                      A.RandomFog(p=1), \n                      A.RandomBrightness(p=1),\n                      A.RandomCrop(p=1,height = 512, width = 512), \n                      A.Rotate(p=1, limit=90),\n                      A.RGBShift(p=1), \n                      A.RandomSnow(p=1),\n                      A.HorizontalFlip(p=1), \n                      A.VerticalFlip(p=1), \n                      A.RandomContrast(limit = 0.5,p = 1),\n                      A.HueSaturationValue(p=1,hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=50),\n                      A.Cutout(p=1),\n                      A.Transpose(p=1), \n                      A.JpegCompression(p=1),\n                      A.CoarseDropout(p=1),\n                      A.IAAAdditiveGaussianNoise(loc=0, scale=(2.5500000000000003, 12.75), per_channel=False, p=1),\n                      A.IAAAffine(scale=1.0, translate_percent=None, translate_px=None, rotate=0.0, shear=0.0, order=1, cval=0, mode='reflect', p=1),\n                      A.IAAAffine(rotate=90., p=1),\n                      A.IAAAffine(rotate=180., p=1)]","metadata":{"execution":{"iopub.status.busy":"2022-02-02T09:09:49.319947Z","iopub.execute_input":"2022-02-02T09:09:49.320329Z","iopub.status.idle":"2022-02-02T09:09:49.331339Z","shell.execute_reply.started":"2022-02-02T09:09:49.320279Z","shell.execute_reply":"2022-02-02T09:09:49.330093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_matrix_list = []\nbboxes_list = []\nfor aug_type in albumentation_list:\n    img = aug_type(image = random_img)['image']\n    img_matrix_list.append(img)\n\nimg_matrix_list.insert(0,random_img)    \n\ntitles_list = [\"Original\",\"RandomSunFlare\",\"RandomFog\",\"RandomBrightness\",\n               \"RandomCrop\",\"Rotate\", \"RGBShift\", \"RandomSnow\",\"HorizontalFlip\", \"VerticalFlip\", \"RandomContrast\",\"HSV\",\n               \"Cutout\",\"Transpose\",\"JpegCompression\",\"CoarseDropout\",\"IAAAdditiveGaussianNoise\",\"IAAAffine\",\"IAAAffineRotate90\",\"IAAAffineRotate180\"]\n\ndef plot_multiple_img(img_matrix_list, title_list, ncols, nrows=5,  main_title=\"\"):\n    fig, myaxes = plt.subplots(figsize=(20, 15), nrows=nrows, ncols=ncols, squeeze=False)\n    fig.suptitle(main_title, fontsize = 30)\n    fig.subplots_adjust(wspace=0.3)\n    fig.subplots_adjust(hspace=0.3)\n    for i, (img, title) in enumerate(zip(img_matrix_list, title_list)):\n        myaxes[i // ncols][i % ncols].imshow(img)\n        myaxes[i // ncols][i % ncols].set_title(title, fontsize=15)\n    plt.show()\n    \nplot_multiple_img(img_matrix_list, titles_list, ncols = 4,main_title=\"Different Types of Augmentations with Albumentations\")","metadata":{"execution":{"iopub.status.busy":"2022-02-02T09:10:28.047144Z","iopub.execute_input":"2022-02-02T09:10:28.047975Z","iopub.status.idle":"2022-02-02T09:10:55.088903Z","shell.execute_reply.started":"2022-02-02T09:10:28.047937Z","shell.execute_reply":"2022-02-02T09:10:55.084302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/happy-whale-and-dolphin')","metadata":{"execution":{"iopub.status.busy":"2022-02-02T08:48:25.546513Z","iopub.execute_input":"2022-02-02T08:48:25.548234Z","iopub.status.idle":"2022-02-02T08:48:25.558390Z","shell.execute_reply.started":"2022-02-02T08:48:25.548181Z","shell.execute_reply":"2022-02-02T08:48:25.557268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_x(x): return str(path/'train_images') + os.path.sep + x['image']\ndef get_y(y): return y['species']","metadata":{"execution":{"iopub.status.busy":"2022-02-02T08:48:25.559999Z","iopub.execute_input":"2022-02-02T08:48:25.560606Z","iopub.status.idle":"2022-02-02T08:48:25.571595Z","shell.execute_reply.started":"2022-02-02T08:48:25.560564Z","shell.execute_reply":"2022-02-02T08:48:25.570589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AlbumentationsTransform(RandTransform):\n    \"A transform handler for multiple `Albumentation` transforms\"\n    split_idx,order=None,2\n    def __init__(self, train_aug, valid_aug): store_attr()\n    \n    def before_call(self, b, split_idx):\n        self.idx = split_idx\n    \n    def encodes(self, img: PILImage):\n        if self.idx == 0:\n            aug_img = self.train_aug(image=np.array(img))['image']\n        else:\n            aug_img = self.valid_aug(image=np.array(img))['image']\n        return PILImage.create(aug_img)","metadata":{"execution":{"iopub.status.busy":"2022-02-02T09:00:41.577810Z","iopub.execute_input":"2022-02-02T09:00:41.578196Z","iopub.status.idle":"2022-02-02T09:00:41.588955Z","shell.execute_reply.started":"2022-02-02T09:00:41.578155Z","shell.execute_reply":"2022-02-02T09:00:41.587675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_train_aug(sz): return A.Compose([\n                A.Transpose(p=0.5),\n                A.ShiftScaleRotate(p=0.5),\n                A.HueSaturationValue(\n                hue_shift_limit=0.2, \n                sat_shift_limit=0.2, \n                val_shift_limit=0.2, \n                p=0.5\n                ),\n                A.RandomSnow(p=1),\n                A.HorizontalFlip(p=1), \n                A.VerticalFlip(p=1), \n                A.RandomBrightnessContrast(\n                brightness_limit=(-0.1,0.1), \n                contrast_limit=(-0.1, 0.1), \n                p=0.5\n                ),\n               A.CoarseDropout(p=0.9),\n               A.Cutout(p=0.5)\n])\n\ndef get_valid_aug(sz): return A.Compose([\n    A.Resize(sz,sz)\n], p=1.)","metadata":{"execution":{"iopub.status.busy":"2022-02-02T09:01:45.155699Z","iopub.execute_input":"2022-02-02T09:01:45.156033Z","iopub.status.idle":"2022-02-02T09:01:45.165438Z","shell.execute_reply.started":"2022-02-02T09:01:45.156001Z","shell.execute_reply":"2022-02-02T09:01:45.164019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_dls(sz):\n    item_tfms = AlbumentationsTransform(get_train_aug(sz), get_valid_aug(sz))\n    WhaleBlock = DataBlock(\n    blocks = (ImageBlock, CategoryBlock),\n    splitter = RandomSplitter(valid_pct=0.2),\n    get_x=get_x,\n    get_y=get_y,\n    item_tfms=item_tfms,\n    batch_tfms=[*aug_transforms(), Normalize.from_stats(*imagenet_stats)]\n     )\n    \n    return WhaleBlock","metadata":{"execution":{"iopub.status.busy":"2022-02-02T09:01:45.629541Z","iopub.execute_input":"2022-02-02T09:01:45.629833Z","iopub.status.idle":"2022-02-02T09:01:45.636359Z","shell.execute_reply.started":"2022-02-02T09:01:45.629803Z","shell.execute_reply":"2022-02-02T09:01:45.635244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"block = get_dls(512)","metadata":{"execution":{"iopub.status.busy":"2022-02-02T09:01:46.824335Z","iopub.execute_input":"2022-02-02T09:01:46.824642Z","iopub.status.idle":"2022-02-02T09:01:46.833105Z","shell.execute_reply.started":"2022-02-02T09:01:46.824610Z","shell.execute_reply":"2022-02-02T09:01:46.832133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = block.dataloaders(train_df, batch_size=CFG.train_bs)\ndls.valid.show_batch(max_n=8, nrows=2)","metadata":{"execution":{"iopub.status.busy":"2022-02-02T09:11:23.506306Z","iopub.execute_input":"2022-02-02T09:11:23.506853Z","iopub.status.idle":"2022-02-02T09:11:27.430967Z","shell.execute_reply.started":"2022-02-02T09:11:23.506806Z","shell.execute_reply":"2022-02-02T09:11:27.429963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Work In Progress :)","metadata":{}}]}