{"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":"markdown","source":"<br>\n<h2 style = \"font-size:60px; font-family:Garamond ; font-weight : normal; background-color: #f6f5f5 ; color : #fe346e; text-align: center; border-radius: 100px 100px;\">[Pytorch] ArcFace Starter</h2>\n<br>","metadata":{}},{"cell_type":"markdown","source":"<br>\n\nIt is based on the work of Vlad Vaduva https://www.kaggle.com/vladvdv/pytorch-train-notebook-arcface-gem-pooling\n    \n* v1: initial code for pytorch lightning\n* v2: trying to fix error ...\n* v3: trying to fix error ...\n* v4: trying to fix error ...\n* v5: check 2 epoch training time\n* v6: test half precision(mixed precision) (canceled)\n* v7: test half precision(mixed precision)\n* v5: check 2 epoch mixed precision training time","metadata":{}},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Install Required Libraries</h1></span>","metadata":{}},{"cell_type":"code","source":"!pip install timm\n!pip install --upgrade wandb\n!pip install --upgrade pytorch-lightning","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-26T00:25:45.008425Z","iopub.execute_input":"2022-02-26T00:25:45.009163Z","iopub.status.idle":"2022-02-26T00:26:16.303522Z","shell.execute_reply.started":"2022-02-26T00:25:45.009092Z","shell.execute_reply":"2022-02-26T00:26:16.30268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Import Required Libraries 📚</h1></span>","metadata":{}},{"cell_type":"code","source":"import os\nimport gc\nimport cv2\nimport math\nimport copy\nimport time\nimport random\n\n# For data manipulation\nimport numpy as np\nimport pandas as pd\n\n# Pytorch Imports\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torch.nn.functional as F\nfrom torch.optim import lr_scheduler\nfrom torch.utils.data import Dataset, DataLoader\n\n# Pytorch Lightning Imports\nimport pytorch_lightning as pl\nfrom pytorch_lightning.loggers.wandb import WandbLogger\nfrom pytorch_lightning.callbacks import LearningRateMonitor, ModelCheckpoint\n\n# Utils\nimport joblib\nfrom tqdm import tqdm\nfrom collections import defaultdict\n\n# Sklearn Imports\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import StratifiedKFold\n\n# For Image Models\nimport timm\n\n# Albumentations for augmentations\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\n\n# For colored terminal text\nfrom colorama import Fore, Back, Style\nb_ = Fore.BLUE\nsr_ = Style.RESET_ALL\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n# For descriptive error messages\nos.environ['CUDA_LAUNCH_BLOCKING'] = \"1\"","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:26:16.305923Z","iopub.execute_input":"2022-02-26T00:26:16.30621Z","iopub.status.idle":"2022-02-26T00:26:26.358769Z","shell.execute_reply.started":"2022-02-26T00:26:16.30617Z","shell.execute_reply":"2022-02-26T00:26:26.357946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<img src=\"https://i.imgur.com/gb6B4ig.png\" width=\"400\" alt=\"Weights & Biases\" />\n\n<span style=\"color: #000508; font-family: Segoe UI; font-size: 1.2em; font-weight: 300;\"> Weights & Biases (W&B) is a set of machine learning tools that helps you build better models faster. <strong>Kaggle competitions require fast-paced model development and evaluation</strong>. There are a lot of components: exploring the training data, training different models, combining trained models in different combinations (ensembling), and so on.</span>\n\n> <span style=\"color: #000508; font-family: Segoe UI; font-size: 1.2em; font-weight: 300;\">⏳ Lots of components = Lots of places to go wrong = Lots of time spent debugging</span>\n\n<span style=\"color: #000508; font-family: Segoe UI; font-size: 1.2em; font-weight: 300;\">W&B can be useful for Kaggle competition with it's lightweight and interoperable tools:</span>\n\n* <span style=\"color: #000508; font-family: Segoe UI; font-size: 1.2em; font-weight: 300;\">Quickly track experiments,<br></span>\n* <span style=\"color: #000508; font-family: Segoe UI; font-size: 1.2em; font-weight: 300;\">Version and iterate on datasets, <br></span>\n* <span style=\"color: #000508; font-family: Segoe UI; font-size: 1.2em; font-weight: 300;\">Evaluate model performance,<br></span>\n* <span style=\"color: #000508; font-family: Segoe UI; font-size: 1.2em; font-weight: 300;\">Reproduce models,<br></span>\n* <span style=\"color: #000508; font-family: Segoe UI; font-size: 1.2em; font-weight: 300;\">Visualize results and spot regressions,<br></span>\n* <span style=\"color: #000508; font-family: Segoe UI; font-size: 1.2em; font-weight: 300;\">Share findings with colleagues.</span>\n\n<span style=\"color: #000508; font-family: Segoe UI; font-size: 1.2em; font-weight: 300;\">To learn more about Weights and Biases check out this <strong><a href=\"https://www.kaggle.com/ayuraj/experiment-tracking-with-weights-and-biases\">kernel</a></strong>.</span>","metadata":{}},{"cell_type":"code","source":"import wandb\n\ntry:\n    from kaggle_secrets import UserSecretsClient\n    user_secrets = UserSecretsClient()\n    api_key = user_secrets.get_secret(\"wandb_api\")\n    wandb.login(key=api_key)\n    anony = None\nexcept:\n    anony = \"must\"\n    print('If you want to use your W&B account, go to Add-ons -> Secrets and provide your W&B access token. Use the Label name as wandb_api. \\nGet your W&B access token from here: https://wandb.ai/authorize')","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:26:26.36027Z","iopub.execute_input":"2022-02-26T00:26:26.362518Z","iopub.status.idle":"2022-02-26T00:26:27.666926Z","shell.execute_reply.started":"2022-02-26T00:26:26.362482Z","shell.execute_reply":"2022-02-26T00:26:27.666227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Training Configuration ⚙️</h1></span>","metadata":{}},{"cell_type":"code","source":"CONFIG = {\"seed\": 2022,\n          \"epochs\": 2,\n          \"img_size\": 768,\n          \"model_name\": \"tf_efficientnet_b4\",\n          \"num_classes\": 15587,\n          \"train_batch_size\": 8,\n          \"valid_batch_size\": 8,\n          \"learning_rate\": 0.0001,\n          \"scheduler\": 'OneCycleLR',\n          \"min_lr\": 1e-6,\n          \"T_max\": 500,\n          \"weight_decay\": 1e-6,\n          \"n_fold\": 5,\n          \"fold_to_run\": 0,\n          \"n_accumulate\": 1,\n          \"test_mode\":False, # enable for testing pipeline, changes epochs to 2 and uses just 100 training samples\n          \"half_precision\": True,\n          # ArcFace Hyperparameters\n          \"s\": 30.0, \n          \"m\": 0.30,\n          \"ls_eps\": 0.0,\n          \"easy_margin\": False\n          }","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:26:27.670727Z","iopub.execute_input":"2022-02-26T00:26:27.670943Z","iopub.status.idle":"2022-02-26T00:26:27.676477Z","shell.execute_reply.started":"2022-02-26T00:26:27.670917Z","shell.execute_reply":"2022-02-26T00:26:27.675756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Set Seed for Reproducibility</h1></span>","metadata":{}},{"cell_type":"code","source":"def set_seed(seed=42):\n    '''Sets the seed of the entire notebook so results are the same every time we run.\n    This is for REPRODUCIBILITY.'''\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    # When running on the CuDNN backend, two further options must be set\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    # Set a fixed value for the hash seed\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    \nset_seed(CONFIG['seed'])","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:26:27.677529Z","iopub.execute_input":"2022-02-26T00:26:27.678269Z","iopub.status.idle":"2022-02-26T00:26:27.694396Z","shell.execute_reply.started":"2022-02-26T00:26:27.678232Z","shell.execute_reply":"2022-02-26T00:26:27.693572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_DIR = '../input/happy-whale-and-dolphin'\nTRAIN_DIR = '../input/happy-whale-and-dolphin/train_images'\nTEST_DIR = '../input/happy-whale-and-dolphin/test_images'","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:26:27.695857Z","iopub.execute_input":"2022-02-26T00:26:27.696441Z","iopub.status.idle":"2022-02-26T00:26:27.700747Z","shell.execute_reply.started":"2022-02-26T00:26:27.696356Z","shell.execute_reply":"2022-02-26T00:26:27.699964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_train_file_path(id):\n    return f\"{TRAIN_DIR}/{id}\"","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:26:27.702455Z","iopub.execute_input":"2022-02-26T00:26:27.703175Z","iopub.status.idle":"2022-02-26T00:26:27.70927Z","shell.execute_reply.started":"2022-02-26T00:26:27.703136Z","shell.execute_reply":"2022-02-26T00:26:27.708495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Read the Data 📖</h1>","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(f\"{ROOT_DIR}/train.csv\")\ndf['file_path'] = df['image'].apply(get_train_file_path)\ndf.head()\n\nif CONFIG[\"test_mode\"]==True:\n    df=df[:100]\n    CONFIG[\"epochs\"] = 2\n    CONFIG[\"n_fold\"] = 2\nencoder = LabelEncoder()\ndf['individual_id'] = encoder.fit_transform(df['individual_id'])\n\nwith open(\"le.pkl\", \"wb\") as fp:\n    joblib.dump(encoder, fp)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:26:27.710589Z","iopub.execute_input":"2022-02-26T00:26:27.711041Z","iopub.status.idle":"2022-02-26T00:26:27.830358Z","shell.execute_reply.started":"2022-02-26T00:26:27.711006Z","shell.execute_reply":"2022-02-26T00:26:27.82934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Create Folds</h1></span>","metadata":{}},{"cell_type":"code","source":"skf = StratifiedKFold(n_splits=CONFIG['n_fold'])\n\nfor fold, ( _, val_) in enumerate(skf.split(X=df, y=df.individual_id)):\n      df.loc[val_ , \"kfold\"] = fold","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:26:27.832209Z","iopub.execute_input":"2022-02-26T00:26:27.832602Z","iopub.status.idle":"2022-02-26T00:26:27.843719Z","shell.execute_reply.started":"2022-02-26T00:26:27.832565Z","shell.execute_reply":"2022-02-26T00:26:27.842986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Dataset Class</h1></span>","metadata":{}},{"cell_type":"code","source":"class HappyWhaleDataset(Dataset):\n    def __init__(self, df, transforms=None):\n        self.df = df\n        self.file_names = df['file_path'].values\n        self.labels = df['individual_id'].values\n        self.transforms = transforms\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, index):\n        img_path = self.file_names[index]\n        img = cv2.imread(img_path)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        label = self.labels[index]\n        \n        if self.transforms:\n            img = self.transforms(image=img)[\"image\"]\n            \n        return {\n            'image': img,\n            'label': torch.tensor(label, dtype=torch.long)\n        }","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:26:27.847006Z","iopub.execute_input":"2022-02-26T00:26:27.847276Z","iopub.status.idle":"2022-02-26T00:26:27.854646Z","shell.execute_reply.started":"2022-02-26T00:26:27.84724Z","shell.execute_reply":"2022-02-26T00:26:27.85324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Augmentations</h1></span>","metadata":{}},{"cell_type":"code","source":"data_transforms = {\n    \"train\": A.Compose([\n        A.Resize(CONFIG['img_size'], CONFIG['img_size']),\n        A.HorizontalFlip(p=0.5),\n        A.Normalize(\n                mean=[0.485, 0.456, 0.406], \n                std=[0.229, 0.224, 0.225], \n                max_pixel_value=255.0, \n                p=1.0\n            ),\n        ToTensorV2()], p=1.),\n    \n    \"valid\": A.Compose([\n        A.Resize(CONFIG['img_size'], CONFIG['img_size']),\n        A.Normalize(\n                mean=[0.485, 0.456, 0.406], \n                std=[0.229, 0.224, 0.225], \n                max_pixel_value=255.0, \n                p=1.0\n            ),\n        ToTensorV2()], p=1.)\n}","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:26:27.855791Z","iopub.execute_input":"2022-02-26T00:26:27.85631Z","iopub.status.idle":"2022-02-26T00:26:27.866655Z","shell.execute_reply.started":"2022-02-26T00:26:27.856275Z","shell.execute_reply":"2022-02-26T00:26:27.865955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">GeM Pooling</h1></span>\n\n<span style=\"color: #000508; font-family: Segoe UI; font-size: 1.5em; font-weight: 300;\">Code taken from <a href=\"https://amaarora.github.io/2020/08/30/gempool.html\">GeM Pooling Explained</a></span>\n\n![](https://i.imgur.com/thTgYWG.jpg)","metadata":{}},{"cell_type":"code","source":"class GeM(nn.Module):\n    def __init__(self, p=3, eps=1e-6):\n        super(GeM, self).__init__()\n        self.p = nn.Parameter(torch.ones(1)*p)\n        self.eps = eps\n\n    def forward(self, x):\n        return self.gem(x, p=self.p, eps=self.eps)\n        \n    def gem(self, x, p=3, eps=1e-6):\n        return F.avg_pool2d(x.clamp(min=eps).pow(p), (x.size(-2), x.size(-1))).pow(1./p)\n        \n    def __repr__(self):\n        return self.__class__.__name__ + \\\n                '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist()[0]) + \\\n                ', ' + 'eps=' + str(self.eps) + ')'","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:26:27.869722Z","iopub.execute_input":"2022-02-26T00:26:27.869931Z","iopub.status.idle":"2022-02-26T00:26:27.879801Z","shell.execute_reply.started":"2022-02-26T00:26:27.869907Z","shell.execute_reply":"2022-02-26T00:26:27.878753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">ArcFace</h1></span>\n\n<span style=\"color: #000508; font-family: Segoe UI; font-size: 1.5em; font-weight: 300;\">Code taken from <a href=\"https://github.com/lyakaap/Landmark2019-1st-and-3rd-Place-Solution/blob/master/src/modeling/metric_learning.py\">Landmark2019-1st-and-3rd-Place-Solution</a></span>","metadata":{}},{"cell_type":"code","source":"class ArcMarginProduct(nn.Module):\n    r\"\"\"Implement of large margin arc distance: :\n        Args:\n            in_features: size of each input sample\n            out_features: size of each output sample\n            s: norm of input feature\n            m: margin\n            cos(theta + m)\n        \"\"\"\n    def __init__(self, in_features, out_features, s=30.0, \n                 m=0.50, easy_margin=False, ls_eps=0.0):\n        super(ArcMarginProduct, self).__init__()\n        self.in_features = in_features\n        self.out_features = out_features\n        self.s = s\n        self.m = m\n        self.ls_eps = ls_eps  # label smoothing\n        self.weight = nn.Parameter(torch.FloatTensor(out_features, in_features))\n        nn.init.xavier_uniform_(self.weight)\n\n        self.easy_margin = easy_margin\n        self.cos_m = math.cos(m)\n        self.sin_m = math.sin(m)\n        self.th = math.cos(math.pi - m)\n        self.mm = math.sin(math.pi - m) * m\n\n    def forward(self, input, label, device):\n        # --------------------------- cos(theta) & phi(theta) ---------------------\n        cosine = F.linear(F.normalize(input), F.normalize(self.weight))\n        if(CONFIG['half_precision']==True):\n            cosine = cosine.to(torch.float32)\n        sine = torch.sqrt(1.0 - torch.pow(cosine, 2))\n        phi = cosine * self.cos_m - sine * self.sin_m\n        if self.easy_margin:\n            phi = torch.where(cosine > 0, phi, cosine)\n        else:\n            phi = torch.where(cosine > self.th, phi, cosine - self.mm)\n        # --------------------------- convert label to one-hot ---------------------\n        # one_hot = torch.zeros(cosine.size(), requires_grad=True, device='cuda')\n        one_hot = torch.zeros(cosine.size(), device=device)\n        one_hot.scatter_(1, label.view(-1, 1).long(), 1)\n        if self.ls_eps > 0:\n            one_hot = (1 - self.ls_eps) * one_hot + self.ls_eps / self.out_features\n        # -------------torch.where(out_i = {x_i if condition_i else y_i) ------------\n        output = (one_hot * phi) + ((1.0 - one_hot) * cosine)\n        output *= self.s\n\n        return output","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:26:27.882422Z","iopub.execute_input":"2022-02-26T00:26:27.88278Z","iopub.status.idle":"2022-02-26T00:26:27.898078Z","shell.execute_reply.started":"2022-02-26T00:26:27.882739Z","shell.execute_reply":"2022-02-26T00:26:27.897269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Create Model</h1></span>","metadata":{}},{"cell_type":"code","source":"class HappyWhaleModel(nn.Module):\n    def __init__(self, model_name, pretrained=True):\n        super(HappyWhaleModel, self).__init__()\n        self.model = timm.create_model(model_name, pretrained=pretrained)\n        in_features = self.model.classifier.in_features\n        self.model.classifier = nn.Identity()\n        self.model.global_pool = nn.Identity()\n        self.pooling = GeM()\n        self.drop = nn.Dropout(p=0.2, inplace=False)\n        self.fc = nn.Linear(in_features,512)\n        self.arc = ArcMarginProduct(512, \n                           CONFIG[\"num_classes\"],\n                           s=CONFIG[\"s\"], \n                           m=CONFIG[\"m\"], \n                           easy_margin=CONFIG[\"ls_eps\"], \n                           ls_eps=CONFIG[\"ls_eps\"])\n    def forward(self, images, labels,device):\n        features = self.model(images)\n        pooled_features = self.pooling(features).flatten(1)\n        pooled_drop = self.drop(pooled_features)\n        emb = self.fc(pooled_drop)\n        output = self.arc(emb,labels,device)\n        return output,emb","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:26:27.899535Z","iopub.execute_input":"2022-02-26T00:26:27.900097Z","iopub.status.idle":"2022-02-26T00:26:27.912136Z","shell.execute_reply.started":"2022-02-26T00:26:27.900048Z","shell.execute_reply":"2022-02-26T00:26:27.91124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Create Lightning Model</h1></span>","metadata":{}},{"cell_type":"code","source":"class LitHappyWhaleModel(pl.LightningModule):\n    def __init__(self, model_name, pretrained=True):\n        super().__init__()\n        self.model = HappyWhaleModel(model_name, pretrained=True)\n        self.criterion = nn.CrossEntropyLoss()\n    \n    # copied from https://pytorchlightning.github.io/lightning-tutorials/notebooks/lightning_examples/text-transformers.html\n    def setup(self, stage=None) -> None:\n        if stage != \"fit\":\n            return\n        # Get dataloader by calling it - train_dataloader() is called after setup() by default\n        train_loader = self.train_dataloader()\n\n        # Calculate total steps\n        tb_size = CONFIG['train_batch_size'] * max(1, self.trainer.gpus)\n        ab_size = tb_size * self.trainer.accumulate_grad_batches\n        self.total_steps = int((len(train_loader.dataset) / ab_size) * float(self.trainer.max_epochs))\n\n        \n    def forward(self, images, labels):\n        return self.model(images, labels, self.device)\n    \n    \n    def training_step(self, batch, batch_idx):\n        images = batch['image']\n        labels = batch['label']\n        \n        outputs, emb = self(images, labels)\n        loss = self.criterion(outputs, labels)\n        \n        self.log(\"train/loss\", loss, prog_bar=True)\n        return loss\n    \n    def validation_step(self, batch, batch_idx):\n        images = batch['image']\n        labels = batch['label']\n        \n        outputs, emb = self(images, labels)\n        loss = self.criterion(outputs, labels)\n        \n        return loss\n\n    def validation_epoch_end(self, validation_step_outputs):\n        loss = torch.stack(validation_step_outputs).mean()\n\n        self.log(\"val/loss\", loss, prog_bar=True)\n    \n\n    def configure_optimizers(self):\n        optimizer = optim.Adam(self.model.parameters(), lr=CONFIG['learning_rate'], \n                       weight_decay=CONFIG['weight_decay'])\n\n        def fetch_scheduler(optimizer):\n            if CONFIG['scheduler'] == 'CosineAnnealingLR':\n                scheduler = lr_scheduler.CosineAnnealingLR(optimizer,T_max=CONFIG['T_max'], \n                                                        eta_min=CONFIG['min_lr'])\n            elif CONFIG['scheduler'] == 'CosineAnnealingWarmRestarts':\n                scheduler = lr_scheduler.CosineAnnealingWarmRestarts(optimizer,T_0=CONFIG['T_0'], \n                                                                    eta_min=CONFIG['min_lr'])\n                        \n            elif CONFIG['scheduler'] == 'OneCycleLR':\n                scheduler = lr_scheduler.OneCycleLR(optimizer,max_lr=CONFIG['learning_rate'],total_steps = self.total_steps)\n                \n            elif CONFIG['scheduler'] == None:\n                return None\n                \n            return scheduler\n        \n        scheduler = fetch_scheduler(optimizer)\n\n        return [optimizer], [scheduler]\n    \n    def train_dataloader(self):\n        df_train = df[df.kfold != CONFIG['fold_to_run']].reset_index(drop=True)\n        train_dataset = HappyWhaleDataset(df_train, transforms=data_transforms[\"train\"])\n        return DataLoader(train_dataset, batch_size=CONFIG['train_batch_size'], \n                              num_workers=2, shuffle=True, pin_memory=True, drop_last=True)\n    \n    def val_dataloader(self):\n        df_valid = df[df.kfold == CONFIG['fold_to_run']].reset_index(drop=True)\n        valid_dataset = HappyWhaleDataset(df_valid, transforms=data_transforms[\"valid\"])\n        return DataLoader(valid_dataset, batch_size=CONFIG['valid_batch_size'], \n                              num_workers=2, shuffle=False, pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:26:27.915696Z","iopub.execute_input":"2022-02-26T00:26:27.915912Z","iopub.status.idle":"2022-02-26T00:26:27.935327Z","shell.execute_reply.started":"2022-02-26T00:26:27.915875Z","shell.execute_reply":"2022-02-26T00:26:27.934445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Run Training</h1></span>","metadata":{}},{"cell_type":"markdown","source":"<span style=\"color: #000508; font-family: Segoe UI; font-size: 1.5em; font-weight: 300;\">Start Training</span>","metadata":{}},{"cell_type":"code","source":"checkpoint_callback  = ModelCheckpoint(\n    monitor=\"val/loss\",\n    mode=\"min\",\n    filename='{val/loss:.4f}-{epoch}',\n    save_top_k=5,\n    verbose=True\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:43:16.980291Z","iopub.execute_input":"2022-02-26T00:43:16.980976Z","iopub.status.idle":"2022-02-26T00:43:16.991288Z","shell.execute_reply.started":"2022-02-26T00:43:16.980938Z","shell.execute_reply":"2022-02-26T00:43:16.990492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer = pl.Trainer(\n    gpus=1,\n    logger=WandbLogger(\n        project='happywhale', \n        config=CONFIG,\n        job_type='Train',\n        tags=['arcface', 'gem-pooling', 'effnet-b4', '768'],\n        anonymous='must'\n    ),\n    precision=16 if CONFIG['half_precision'] else 32,\n    max_epochs = CONFIG['epochs'],\n    accumulate_grad_batches = CONFIG['n_accumulate'],\n    callbacks=[\n        LearningRateMonitor(logging_interval='step'),\n        checkpoint_callback\n    ]\n)\nmodel = LitHappyWhaleModel(CONFIG['model_name'])\ntrainer.fit(model)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T00:43:18.786461Z","iopub.execute_input":"2022-02-26T00:43:18.786984Z","iopub.status.idle":"2022-02-26T00:43:54.616297Z","shell.execute_reply.started":"2022-02-26T00:43:18.786947Z","shell.execute_reply":"2022-02-26T00:43:54.615474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![Upvote!](https://img.shields.io/badge/Upvote-If%20you%20like%20my%20work-07b3c8?style=for-the-badge&logo=kaggle)","metadata":{}}]}