{"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":"# Pytorch Efficientnet + ArcFace [training]\n## Introduction\n* This notebook uses Efficient net and ArcFace on pytorch for training a model.\n* to train for 10 epoch I got validation accuracy about 0.25 and validation GAP score about 0.20\n* Inference notebook for submission is another one https://www.kaggle.com/zaccheroni/pytorch-efficientnet-arcface-submission Here!\n* To tell the truth I'm a beginer, so if there are any mistakes, please tell me!","metadata":{}},{"cell_type":"markdown","source":"I used a notebook https://www.kaggle.com/rhtsingh/pytorch-training-inference-efficientnet-baseline by @rhtsingh - as a reference.","metadata":{}},{"cell_type":"code","source":"!mkdir -p /tmp/pip/cache/\n!cp ../input/resources-for-google-landmark-recognition-2020/efficientnet_pytorch-0.6.3-py3-none-any.whl /tmp/pip/cache/\n!pip install --no-index --find-links /tmp/pip/cache/ efficientnet_pytorch","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:44:41.801362Z","iopub.execute_input":"2022-04-17T20:44:41.801689Z","iopub.status.idle":"2022-04-17T20:44:50.109834Z","shell.execute_reply.started":"2022-04-17T20:44:41.801658Z","shell.execute_reply":"2022-04-17T20:44:50.109010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import efficientnet_pytorch","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:44:50.112125Z","iopub.execute_input":"2022-04-17T20:44:50.112514Z","iopub.status.idle":"2022-04-17T20:44:51.302755Z","shell.execute_reply.started":"2022-04-17T20:44:50.112470Z","shell.execute_reply":"2022-04-17T20:44:51.301951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if you need notification\n#!pip install slackweb\n# import slackweb","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:44:51.305187Z","iopub.execute_input":"2022-04-17T20:44:51.305442Z","iopub.status.idle":"2022-04-17T20:44:51.309372Z","shell.execute_reply.started":"2022-04-17T20:44:51.305414Z","shell.execute_reply":"2022-04-17T20:44:51.308368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport gc\ngc.enable()\nimport sys\nimport math\nimport json\nimport time\nimport random\nfrom glob import glob\nfrom datetime import datetime\n\nimport cv2\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport multiprocessing\nfrom sklearn.preprocessing import LabelEncoder\n\nimport torch\nimport torchvision\nfrom torch import Tensor\nfrom torchvision import transforms\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.autograd import Variable\nfrom torch.nn.parameter import Parameter\nfrom torch.optim import lr_scheduler\nfrom torch.utils.data import DataLoader, Dataset\nfrom torch.utils.data.sampler import SequentialSampler\nfrom tqdm import tqdm\n\nimport albumentations as A\n\nimport sklearn\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport csv\nimport pprint\nimport pandas as pd\nimport numpy as np\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.nn import Parameter\nimport math\n\nfrom sklearn.model_selection import train_test_split\nimport torch.optim as optim","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:44:51.310936Z","iopub.execute_input":"2022-04-17T20:44:51.311527Z","iopub.status.idle":"2022-04-17T20:44:53.213942Z","shell.execute_reply.started":"2022-04-17T20:44:51.311478Z","shell.execute_reply":"2022-04-17T20:44:53.212959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from __future__ import print_function, division\n\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\nimport torch.backends.cudnn as cudnn\nimport numpy as np\nimport torchvision\nfrom torchvision import datasets, models, transforms\nimport matplotlib.pyplot as plt\nimport time\nimport os\nimport copy\nimport pandas as pd\nimport requests\nimport io\nimport urllib.parse\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport functools\nfrom PIL import Image  # Image utilities.\nimport tqdm\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom torch.utils.data import Dataset, DataLoader\nimport imageio as io_temp\nfrom skimage.transform import resize\nfrom sklearn.metrics import roc_curve, auc, roc_auc_score, classification_report, confusion_matrix\n\n\ncudnn.benchmark = True\nplt.ion()   # interactive mode","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:44:53.216633Z","iopub.execute_input":"2022-04-17T20:44:53.217041Z","iopub.status.idle":"2022-04-17T20:44:53.320727Z","shell.execute_reply.started":"2022-04-17T20:44:53.216997Z","shell.execute_reply":"2022-04-17T20:44:53.319856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Seed everything to avoid non-determinism.\ndef seed_everything(seed=2020):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    \nseed_everything()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:44:53.322621Z","iopub.execute_input":"2022-04-17T20:44:53.323134Z","iopub.status.idle":"2022-04-17T20:44:53.332145Z","shell.execute_reply.started":"2022-04-17T20:44:53.323089Z","shell.execute_reply":"2022-04-17T20:44:53.331095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# SOURCE_URL = 'https://storage.googleapis.com/dm-turtle-recall/images.tar'\nIMAGE_DIR = '../input/effnetweightanddata/images'\n# TAR_PATH = os.path.join(IMAGE_DIR, os.path.basename(SOURCE_URL))\n# EXPECTED_IMAGE_COUNT = 13891\n\n# %sx mkdir --parents \"{IMAGE_DIR}\"\n# if len(os.listdir(IMAGE_DIR)) != EXPECTED_IMAGE_COUNT:\n#   %sx wget --no-check-certificate -O \"{TAR_PATH}\" \"{SOURCE_URL}\"\n#   %sx tar --extract --file=\"{TAR_PATH}\" --directory=\"{IMAGE_DIR}\"\n#   %sx rm \"{TAR_PATH}\"\n\nprint(f'The total number of images is: {len(os.listdir(IMAGE_DIR))}')","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:44:53.333981Z","iopub.execute_input":"2022-04-17T20:44:53.334620Z","iopub.status.idle":"2022-04-17T20:44:53.683602Z","shell.execute_reply.started":"2022-04-17T20:44:53.334572Z","shell.execute_reply":"2022-04-17T20:44:53.682865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_URL = 'https://storage.googleapis.com/dm-turtle-recall/'\n\n\ndef read_csv_from_web(file_name):\n  url = urllib.parse.urljoin(BASE_URL, file_name)\n  content = requests.get(url).content\n  return pd.read_csv(io.StringIO(content.decode('utf-8')))\n\n# Read in csv files.\n# train = read_csv_from_web('train.csv')\ntest = read_csv_from_web('test.csv')\n# extra = read_csv_from_web('extra_images.csv')\n# sample_submission = read_csv_from_web('sample_submission.csv')\n\n# Convert image_location strings to lowercase.\nfor df in [test]:\n  df.image_location = df.image_location.apply(lambda x: x.lower())\n  assert set(df.image_location.unique()) == set(['left', 'right', 'top'])","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:44:53.685867Z","iopub.execute_input":"2022-04-17T20:44:53.686422Z","iopub.status.idle":"2022-04-17T20:45:13.860635Z","shell.execute_reply.started":"2022-04-17T20:44:53.686380Z","shell.execute_reply":"2022-04-17T20:45:13.858760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"../input/turtletestcsv/test (1).csv\")\ntest","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:22.400229Z","iopub.execute_input":"2022-04-17T20:45:22.400606Z","iopub.status.idle":"2022-04-17T20:45:22.430491Z","shell.execute_reply.started":"2022-04-17T20:45:22.400572Z","shell.execute_reply":"2022-04-17T20:45:22.429792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/label-encoder-testing/train (2).csv\")","metadata":{"execution":{"iopub.status.busy":"2022-04-17T21:02:00.686157Z","iopub.execute_input":"2022-04-17T21:02:00.686570Z","iopub.status.idle":"2022-04-17T21:02:00.703844Z","shell.execute_reply.started":"2022-04-17T21:02:00.686527Z","shell.execute_reply":"2022-04-17T21:02:00.702853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls = pd.unique(train['turtle_id'])\nclasses = {}\nfor i in range(len(ls)):\n    classes[ls[i]] = i\nclass_names = classes\nnum_classes = len(class_names)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T21:02:01.890374Z","iopub.execute_input":"2022-04-17T21:02:01.890823Z","iopub.status.idle":"2022-04-17T21:02:01.897076Z","shell.execute_reply.started":"2022-04-17T21:02:01.890775Z","shell.execute_reply":"2022-04-17T21:02:01.895952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_train, X_test, y_train, y_test = train_test_split(train[['image_id','image_location']], train['turtle_id'], test_size=0.20, random_state=42, stratify=train['turtle_id'])\n# X_test, X_val, y_test, y_val = train_test_split(X_test, y_test, test_size=0.50, random_state=42, stratify=y_test)\n\n# print(X_train.shape, X_test.shape, X_val.shape ,y_train.shape, y_test.shape, y_val.shape)\n\n# train_ds = pd.concat([X_train, y_train], axis=1)\n# train_ds['type'] = \"train\"\n# test_ds = pd.concat([X_test, y_test], axis=1)\n# test_ds['type'] = \"test\"\n# val_ds = pd.concat([X_val, y_val], axis=1)\n# val_ds['type'] = \"val\"","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:23.615562Z","iopub.execute_input":"2022-04-17T20:45:23.616016Z","iopub.status.idle":"2022-04-17T20:45:23.620637Z","shell.execute_reply.started":"2022-04-17T20:45:23.615947Z","shell.execute_reply":"2022-04-17T20:45:23.619505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_sizes = {'train': len(train) + len(extra)}","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:25.431856Z","iopub.execute_input":"2022-04-17T20:45:25.432222Z","iopub.status.idle":"2022-04-17T20:45:25.453096Z","shell.execute_reply.started":"2022-04-17T20:45:25.432190Z","shell.execute_reply":"2022-04-17T20:45:25.452003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = pd.concat([train,extra], axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:24:23.357076Z","iopub.execute_input":"2022-04-17T20:24:23.357349Z","iopub.status.idle":"2022-04-17T20:24:23.376581Z","shell.execute_reply.started":"2022-04-17T20:24:23.357322Z","shell.execute_reply":"2022-04-17T20:24:23.374966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = test","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:34.738819Z","iopub.execute_input":"2022-04-17T20:45:34.739397Z","iopub.status.idle":"2022-04-17T20:45:34.748282Z","shell.execute_reply.started":"2022-04-17T20:45:34.739352Z","shell.execute_reply":"2022-04-17T20:45:34.747206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['image_id'] = IMAGE_DIR + \"/\" + dataset['image_id'].astype(str) + \".JPG\"","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:35.219912Z","iopub.execute_input":"2022-04-17T20:45:35.220387Z","iopub.status.idle":"2022-04-17T20:45:35.243416Z","shell.execute_reply.started":"2022-04-17T20:45:35.220342Z","shell.execute_reply":"2022-04-17T20:45:35.242369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = dataset[dataset['turtle_id'].isin(train['turtle_id'])]\ndataset = dataset.reset_index()\ndataset.pop('index')\ndataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:35.595067Z","iopub.execute_input":"2022-04-17T20:45:35.595500Z","iopub.status.idle":"2022-04-17T20:45:35.660237Z","shell.execute_reply.started":"2022-04-17T20:45:35.595457Z","shell.execute_reply":"2022-04-17T20:45:35.656786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dataset['turtle_id'] = dataset['turtle_id'].map(classes)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:38.817951Z","iopub.execute_input":"2022-04-17T20:45:38.818348Z","iopub.status.idle":"2022-04-17T20:45:38.822331Z","shell.execute_reply.started":"2022-04-17T20:45:38.818311Z","shell.execute_reply":"2022-04-17T20:45:38.821415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TurtleDataset(Dataset):\n    \"\"\"Turtle dataset.\"\"\"\n\n    def __init__(self, csv_file, transform=None):\n        \"\"\"\n        Args:\n            csv_file (string): Path to the csv file with annotations.\n            root_dir (string): Directory with all the images.\n            transform (callable, optional): Optional transform to be applied\n                on a sample.\n        \"\"\"\n        self.csv = csv_file\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.csv)\n\n    def __getitem__(self, idx):\n        if torch.is_tensor(idx):\n            idx = idx.tolist()\n\n        img_name = self.csv.iloc[idx, 0]\n        image = io_temp.imread(img_name)\n        details = self.csv.iloc[idx, 1:]\n        details = np.array([details])\n        details = details[:1]\n        sample = {'image': image, 'image_orientation': details[0][0]}#, 'turtle_id': details[0][1]}\n\n        if self.transform:\n            sample = self.transform(sample)\n\n        return [sample,img_name]","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:39.354771Z","iopub.execute_input":"2022-04-17T20:45:39.355110Z","iopub.status.idle":"2022-04-17T20:45:39.364348Z","shell.execute_reply.started":"2022-04-17T20:45:39.355079Z","shell.execute_reply":"2022-04-17T20:45:39.363385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Rescale(object):\n    \"\"\"Rescale the image in a sample to a given size.\n\n    Args:\n        output_size (tuple or int): Desired output size. If tuple, output is\n            matched to output_size. If int, smaller of image edges is matched\n            to output_size keeping aspect ratio the same.\n    \"\"\"\n\n    def __init__(self, output_size = 224):\n        assert isinstance(output_size, (int, tuple))\n        self.output_size = output_size\n\n    def __call__(self, sample):\n        image, turtle_id = sample['image'], sample['turtle_id'] # , sample['image_orientation']\n        \n        # ADD CODE EITHER HERE\n\n        h, w = image.shape[:2]\n        crop_size = min(w, h)\n        crop = image[(h - crop_size) // 2 : (h + crop_size) // 2, (w - crop_size) // 2 : (w + crop_size) // 2]\n        img = resize(crop, (self.output_size, self.output_size))\n\n        # OR ADD CODE HERE\n        \n        return [img.transpose((2,0,1)).astype(np.double),turtle_id]","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:39.857577Z","iopub.execute_input":"2022-04-17T20:45:39.857904Z","iopub.status.idle":"2022-04-17T20:45:39.867008Z","shell.execute_reply.started":"2022-04-17T20:45:39.857872Z","shell.execute_reply":"2022-04-17T20:45:39.866027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Rescale_test(object):\n    \"\"\"Rescale the image in a sample to a given size.\n\n    Args:\n        output_size (tuple or int): Desired output size. If tuple, output is\n            matched to output_size. If int, smaller of image edges is matched\n            to output_size keeping aspect ratio the same.\n    \"\"\"\n\n    def __init__(self, output_size = 224):\n        assert isinstance(output_size, (int, tuple))\n        self.output_size = output_size\n\n    def __call__(self, sample):\n        image = sample['image']\n        \n        # ADD CODE EITHER HERE\n\n        h, w = image.shape[:2]\n        crop_size = min(w, h)\n        crop = image[(h - crop_size) // 2 : (h + crop_size) // 2, (w - crop_size) // 2 : (w + crop_size) // 2]\n        img = resize(crop, (self.output_size, self.output_size))\n\n        # OR ADD CODE HERE\n        \n        return [img.transpose((2,0,1)).astype(np.double)]","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:41.322174Z","iopub.execute_input":"2022-04-17T20:45:41.322537Z","iopub.status.idle":"2022-04-17T20:45:41.331273Z","shell.execute_reply.started":"2022-04-17T20:45:41.322506Z","shell.execute_reply":"2022-04-17T20:45:41.330047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:41.775337Z","iopub.execute_input":"2022-04-17T20:45:41.775756Z","iopub.status.idle":"2022-04-17T20:45:41.781055Z","shell.execute_reply.started":"2022-04-17T20:45:41.775669Z","shell.execute_reply":"2022-04-17T20:45:41.780091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IN_KERNEL = os.environ.get('KAGGLE_WORKING_DIR') is not None\nMIN_SAMPLES_PER_CLASS = 30\nBATCH_SIZE = 8\nNUM_WORKERS = multiprocessing.cpu_count()\nMAX_STEPS_PER_EPOCH = 15000\nNUM_EPOCHS = 20\nLOG_FREQ = 400\nNUM_TOP_PREDICTS = 20","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:42.195221Z","iopub.execute_input":"2022-04-17T20:45:42.195565Z","iopub.status.idle":"2022-04-17T20:45:42.201122Z","shell.execute_reply.started":"2022-04-17T20:45:42.195533Z","shell.execute_reply":"2022-04-17T20:45:42.199945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train = pd.read_csv('../input/landmark-recognition-2020/train.csv')\n# test = pd.read_csv('../input/landmark-recognition-2020/sample_submission.csv')\n# train_dir = '../input/landmark-recognition-2020/train/'\n# test_dir = '../input/landmark-recognition-2020/test/'","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:42.555884Z","iopub.execute_input":"2022-04-17T20:45:42.556239Z","iopub.status.idle":"2022-04-17T20:45:42.560751Z","shell.execute_reply.started":"2022-04-17T20:45:42.556208Z","shell.execute_reply":"2022-04-17T20:45:42.559704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train, val = train_test_split(train, test_size=0.02)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:42.961810Z","iopub.execute_input":"2022-04-17T20:45:42.962206Z","iopub.status.idle":"2022-04-17T20:45:42.966450Z","shell.execute_reply.started":"2022-04-17T20:45:42.962171Z","shell.execute_reply":"2022-04-17T20:45:42.965258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class ImageDataset(torch.utils.data.Dataset):\n#     def __init__(self, dataframe: pd.DataFrame, image_dir:str, mode: str):\n#         self.df = dataframe\n#         self.mode = mode\n#         self.image_dir = image_dir\n        \n#         transforms_list = []\n#         if self.mode == 'train':\n#             # Increase image size from (64,64) to higher resolution,\n#             # Make sure to change in RandomResizedCrop as well.\n#             transforms_list = [\n#                 transforms.Resize((64,64)),\n#                 transforms.RandomHorizontalFlip(),\n#                 transforms.RandomChoice([\n#                     transforms.RandomResizedCrop(64),\n#                     transforms.ColorJitter(0.2, 0.2, 0.2, 0.2),\n#                     transforms.RandomAffine(degrees=15, translate=(0.2, 0.2),\n#                                             scale=(0.8, 1.2), shear=15,\n#                                             resample=Image.BILINEAR)\n#                 ]),\n#                 transforms.ToTensor(),\n#                 transforms.Normalize(mean=[0.485, 0.456, 0.406],\n#                                       std=[0.229, 0.224, 0.225]),\n#             ]\n#         else:\n#             transforms_list.extend([\n#                 # Keep this resize same as train\n#                 transforms.Resize((64,64)),\n#                 transforms.ToTensor(),\n#                 transforms.Normalize(mean=[0.485, 0.456, 0.406],\n#                                       std=[0.229, 0.224, 0.225]),\n#             ])\n#         self.transforms = transforms.Compose(transforms_list)\n\n#     def __getitem__(self, index: int):\n#         image_id = self.df.iloc[index].id\n#         image_path = f\"{self.image_dir}/{image_id[0]}/{image_id[1]}/{image_id[2]}/{image_id}.jpg\"\n#         image = Image.open(image_path)\n#         image = self.transforms(image)\n\n#         if self.mode == 'test':\n#             return {'image':image}\n#         else:\n#             return {'image':image, \n#                     'target':self.df.iloc[index].landmark_id}\n\n#     def __len__(self) -> int:\n#         return self.df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:43.252228Z","iopub.execute_input":"2022-04-17T20:45:43.252541Z","iopub.status.idle":"2022-04-17T20:45:43.260339Z","shell.execute_reply.started":"2022-04-17T20:45:43.252511Z","shell.execute_reply":"2022-04-17T20:45:43.259330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data(train):\n    counts = train.turtle_id.value_counts()\n    print('train_df', train.shape)\n\n    label_encoder = LabelEncoder()\n    label_encoder.fit(train.turtle_id.values)\n    print('found classes', len(label_encoder.classes_))\n\n    train.turtle_id = label_encoder.transform(train.turtle_id)\n\n    train_dataset = TurtleDataset(train, transform=transforms.Compose([Rescale(256)]))\n\n    train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE,\n                              shuffle=True, num_workers=4, drop_last=True)\n        \n    return train_loader, label_encoder, len(label_encoder.classes_)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:45.534452Z","iopub.execute_input":"2022-04-17T20:45:45.536268Z","iopub.status.idle":"2022-04-17T20:45:45.559570Z","shell.execute_reply.started":"2022-04-17T20:45:45.536217Z","shell.execute_reply":"2022-04-17T20:45:45.554512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def adam(parameters, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0):\n    if isinstance(betas, str):\n        betas = eval(betas)\n    return optim.Adam(parameters,\n                      lr=lr,\n                      betas=betas,\n                      eps=eps,\n                      weight_decay=weight_decay)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:50.287273Z","iopub.execute_input":"2022-04-17T20:45:50.287614Z","iopub.status.idle":"2022-04-17T20:45:50.293411Z","shell.execute_reply.started":"2022-04-17T20:45:50.287581Z","shell.execute_reply":"2022-04-17T20:45:50.292561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AverageMeter:\n    ''' Computes and stores the average and current value '''\n    def __init__(self) -> None:\n        self.reset()\n\n    def reset(self) -> None:\n        self.val = 0.0\n        self.avg = 0.0\n        self.sum = 0.0\n        self.count = 0\n\n    def update(self, val: float, n: int = 1) -> None:\n        self.val = val\n        self.sum += val * n\n        self.count += n\n        self.avg = self.sum / self.count","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:50.671324Z","iopub.execute_input":"2022-04-17T20:45:50.671635Z","iopub.status.idle":"2022-04-17T20:45:50.679214Z","shell.execute_reply.started":"2022-04-17T20:45:50.671606Z","shell.execute_reply":"2022-04-17T20:45:50.678018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def GAP(predicts: torch.Tensor, confs: torch.Tensor, targets: torch.Tensor) -> float:\n    ''' Simplified GAP@1 metric: only one prediction per sample is supported '''\n    assert len(predicts.shape) == 1\n    assert len(confs.shape) == 1\n    assert len(targets.shape) == 1\n    assert predicts.shape == confs.shape and confs.shape == targets.shape\n\n    _, indices = torch.sort(confs, descending=True)\n\n    confs = confs.cpu().numpy()\n    predicts = predicts[indices].cpu().numpy()\n    targets = targets[indices].cpu().numpy()\n\n    res, true_pos = 0.0, 0\n\n    for i, (c, p, t) in enumerate(zip(confs, predicts, targets)):\n        rel = int(p == t)\n        true_pos += rel\n\n        res += true_pos / (i + 1) * rel\n\n    res /= targets.shape[0] # FIXME: incorrect, not all test images depict landmarks\n    return res","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:51.090167Z","iopub.execute_input":"2022-04-17T20:45:51.090494Z","iopub.status.idle":"2022-04-17T20:45:51.101295Z","shell.execute_reply.started":"2022-04-17T20:45:51.090462Z","shell.execute_reply":"2022-04-17T20:45:51.099833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ArcMarginProduct(nn.Module):\n    def __init__(self, in_features, out_features, s=30.0, m=0.50, easy_margin=False):\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.weight = 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, train, label=False):\n        # --------------------------- cos(theta) & phi(theta) ---------------------------\n        cosine = F.linear(F.normalize(input), F.normalize(self.weight))\n        sine = torch.sqrt((1.0 - torch.pow(cosine, 2)).clamp(0, 1))\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        if train:\n            one_hot = torch.zeros(cosine.size(), device='cuda')\n            one_hot.scatter_(1, label.cuda().view(-1, 1).long(), 1)\n            output = (one_hot * phi) + ((1.0 - one_hot) * cosine)  # you can use torch.where if your torch.__version__ is 0.4\n        else:\n            output = cosine\n        output *= self.s\n\n        return output","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:52.782483Z","iopub.execute_input":"2022-04-17T20:45:52.782899Z","iopub.status.idle":"2022-04-17T20:45:52.799190Z","shell.execute_reply.started":"2022-04-17T20:45:52.782854Z","shell.execute_reply":"2022-04-17T20:45:52.798259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist()[0]) + ', ' + 'eps=' + str(self.eps) + ')'","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:53.197040Z","iopub.execute_input":"2022-04-17T20:45:53.197385Z","iopub.status.idle":"2022-04-17T20:45:53.207292Z","shell.execute_reply.started":"2022-04-17T20:45:53.197354Z","shell.execute_reply":"2022-04-17T20:45:53.206373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class EfficientNetEncoderHead(nn.Module):\n    def __init__(self, depth, num_classes):\n        super(EfficientNetEncoderHead, self).__init__()\n        self.depth = depth\n        self.base = efficientnet_pytorch.EfficientNet.from_pretrained(f'efficientnet-b{self.depth}')\n        self.gem = GeM()\n        self.output_filter = self.base._fc.in_features\n        self.fc = nn.Linear(self.output_filter, 1000)\n        self.arcface = ArcMarginProduct(1000, num_classes)\n    def forward(self, x, label):\n        x = self.base.extract_features(x)\n        x = self.gem(x).squeeze()\n        x = self.fc(x)\n        if self.training:\n            x = self.arcface(x, self.training, label)\n        else:\n            x = self.arcface(x, self.training)\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:53.587026Z","iopub.execute_input":"2022-04-17T20:45:53.587355Z","iopub.status.idle":"2022-04-17T20:45:53.597570Z","shell.execute_reply.started":"2022-04-17T20:45:53.587325Z","shell.execute_reply":"2022-04-17T20:45:53.595906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## validation\nThis 'val_step' caliculate a validation accuracy and GAP score","metadata":{}},{"cell_type":"code","source":"def val_step(val_loader,\n        model,\n        criterion,\n        label_encoder,\n        all_val_count):\n    \n    val_losses = AverageMeter()\n    val_gap_score = AverageMeter()\n    val_acc = AverageMeter()\n    model.eval()\n    acc_count = 0\n    first = True\n    end = time.time()\n    for i, j in val_loader:\n        input_ = i\n        target = j\n        batch_size, _, _, _ = input_.shape\n        \n        output = model(input_.float().cuda(), target.cuda())\n        confs, predicts = torch.max(output.detach(), dim=1)\n        \n        if first:\n            all_confs = confs\n            all_predicts = predicts\n            all_targets = target\n            first = False\n        else:\n            all_confs = torch.cat([all_confs, confs])\n            all_predicts = torch.cat([all_predicts, predicts])\n            all_targets = torch.cat([all_targets, target])\n\n    val_gap_score = GAP(all_predicts, all_confs, all_targets)\n    val_gap_score = val_gap_score * len(all_confs) / all_val_count\n    \n    for i, (c, p, t) in enumerate(zip(all_confs, all_predicts, all_targets)):\n        if p == t:\n            acc_count += 1\n                \n    acc = float(acc_count) / all_val_count\n    val_time = time.time() - end\n    return acc, val_gap_score, val_time","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:55.359154Z","iopub.execute_input":"2022-04-17T20:45:55.359505Z","iopub.status.idle":"2022-04-17T20:45:55.374803Z","shell.execute_reply.started":"2022-04-17T20:45:55.359468Z","shell.execute_reply":"2022-04-17T20:45:55.373812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training","metadata":{}},{"cell_type":"code","source":"def train_step(train_loader, \n          model, \n          criterion, \n          optimizer,\n          epoch, \n          lr_scheduler):\n    print(f'epoch {epoch}')\n    batch_time = AverageMeter()\n    losses = AverageMeter()\n    avg_score = AverageMeter()\n\n    model.train()\n    num_steps = min(len(train_loader), MAX_STEPS_PER_EPOCH)\n\n    print(f'total batches: {num_steps}')\n\n    end = time.time()\n    lr = None\n\n    for i, j in train_loader:\n        input_ = i\n        target =j\n        batch_size, _, _, _ = input_.shape\n        \n        output = model(input_.float().cuda(), target.cuda())\n        loss = criterion(output, target.cuda())\n        confs, predicts = torch.max(output.detach(), dim=1)\n        avg_score.update(GAP(predicts, confs, target))\n        losses.update(loss.data.item(), input_.size(0))\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        lr_scheduler.step()\n        lr = optimizer.param_groups[0]['lr']\n        \n        batch_time.update(time.time() - end)\n        end = time.time()\n\n        \n    acc, val_gap, val_time = val_step(train_loader, model, criterion, label_encoder, len(dataset))\n    print('validation with training data '+str(val_time))\n    print(f'time {batch_time.val:.3f} ({batch_time.avg:.3f})\\t'\n            f'loss {losses.val:.4f} ({losses.avg:.4f})\\t'\n            f'GAP {avg_score.val:.4f} ({avg_score.avg:.4f})\\t'\n            f'val_acc {acc}\\t'\n            f'val_GAP {val_gap:.4f}\\t'\n                 )\n            #slack = slackweb.Slack(url=\"~~~~~~\")\n            #slack.notify(text= f'{epoch} [{i}/{num_steps}]\\t'\n                    #f'time {batch_time.val:.3f} ({batch_time.avg:.3f})\\t'\n                    #f'loss {losses.val:.4f} ({losses.avg:.4f})\\t'\n                    #f'GAP {avg_score.val:.4f} ({avg_score.avg:.4f})\\t'\n                    #f'val_acc {acc}\\t'\n                    #f'val_GAP {val_gap:.4f}\\t')\n              \n\n    print(f' * average GAP on train {avg_score.avg:.4f}')\n    print(f' time {batch_time.sum:.4f}')\n    return avg_score.avg, losses.avg","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:57.207922Z","iopub.execute_input":"2022-04-17T20:45:57.208332Z","iopub.status.idle":"2022-04-17T20:45:57.222249Z","shell.execute_reply.started":"2022-04-17T20:45:57.208300Z","shell.execute_reply":"2022-04-17T20:45:57.221353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def inference(data_loader, model):\n    model.eval()\n\n    activation = nn.Softmax(dim=1)\n    all_predicts, all_confs, all_targets = [], [], []\n\n    with torch.no_grad():\n        for i, data in enumerate(tqdm(data_loader, disable=IN_KERNEL)):\n            if data_loader.dataset.mode != 'test':\n                input_, target = data['image'], data['target']\n            else:\n                input_, target = data['image'], None\n\n            output = model(input_.cuda())\n            output = activation(output)\n\n            confs, predicts = torch.topk(output, NUM_TOP_PREDICTS)\n            all_confs.append(confs)\n            all_predicts.append(predicts)\n\n            if target is not None:\n                all_targets.append(target)\n\n    predicts = torch.cat(all_predicts)\n    confs = torch.cat(all_confs)\n    targets = torch.cat(all_targets) if len(all_targets) else None\n\n    return predicts, confs, targets","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:45:59.184220Z","iopub.execute_input":"2022-04-17T20:45:59.184533Z","iopub.status.idle":"2022-04-17T20:45:59.196898Z","shell.execute_reply.started":"2022-04-17T20:45:59.184504Z","shell.execute_reply":"2022-04-17T20:45:59.196067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Before starting training, check if there is a trained model. If I have one, I will load it also with optimizer and scheduler","metadata":{}},{"cell_type":"markdown","source":"I made \"selected_classes.csv\". This file shows which classes are selected, in another word, this model is classifying into these classes.","metadata":{}},{"cell_type":"code","source":"if __name__ == '__main__':\n    modelname = 'the_model'\n    input_dir = '../input/'\n    \n    global_start_time = time.time()\n    train_loader, label_encoder, num_classes = load_data(dataset)\n\n    all_classes = label_encoder.classes_\n    all_classes = list(all_classes)\n    selected_classes = train.turtle_id\n    with open('selected_classes.csv', 'w') as f:\n        writer = csv.writer(f)\n        writer.writerow(all_classes)\n    \n    model = EfficientNetEncoderHead(depth=7, num_classes=num_classes)\n    model.cuda()\n\n    criterion = nn.CrossEntropyLoss()\n\n    optimizer = adam(model.parameters(), lr=1e-3, betas=(0.9,0.999), eps=1e-3, weight_decay=1e-4)\n    scheduler = lr_scheduler.CosineAnnealingLR(optimizer, T_max=len(train_loader)*NUM_EPOCHS, eta_min=1e-6)\n\n    s = False\n    # if there is 'learning.txt', read it and start training from the epoch which is written in that file.\n    if os.path.exists(input_dir + 'learning.txt'):\n        with open(input_dir + 'learning.txt') as f:\n            s = f.read()\n            \n    # opttimizer saving dir\n    opt_shc_path = 'optimizer_and_scheduler'\n        \n    \n    if s:\n        model.load_state_dict(torch.load(input_dir + 'the_model'+s+'.pth'))\n        start_epoch = int(s) + 1\n        checkpoint = torch.load(input_dir + 'optimizer_and_scheduler')\n        optimizer.load_state_dict(checkpoint['optimizer'])\n        scheduler.load_state_dict(checkpoint['scheduler'])\n        print('optimizer and scheduler are loaded')\n        \n        pre_history = pd.read_csv(input_dir + 'the_model_history.csv')\n    else:\n        pre_history = pd.DataFrame(columns=['epoch', 'GAP', 'loss'])\n        start_epoch = 1\n\n        \n    for epoch in range(start_epoch, NUM_EPOCHS + 1):\n        print('-' * 50)\n        score, loss = train_step(train_loader, model, criterion, optimizer, epoch, scheduler)\n        pre_history = pre_history.append({'GAP':score,'epoch':epoch,'loss':loss}, ignore_index=True)\n        \n        model_path = 'the_model'+str(epoch)+'.pth'\n        state = {\n            'optimizer': optimizer.state_dict(),\n            'scheduler': scheduler.state_dict()\n        }\n        torch.save(model.state_dict(), model_path)\n        torch.save(state, opt_shc_path)\n        \n        with open('learning.txt', mode='w') as f:\n            f.write(str(epoch))\n            \n#         acc, val_gap, _ = val_step(val_loader, model, criterion, label_encoder, all_val_count)\n        \n        pre_history.to_csv('the_model_history.csv')\n            \n        # if you want to know about learning on slack\n        #slack = slackweb.Slack(url=\"~~~\")\n        #slack.notify(text= f'{epoch:.4f} epoch finished\\t'\n        #            f'val_acc {acc}\\t'\n        #            f'val_GAP {val_gap:.4f}\\t')\n        \n    #slack.notify(text= 'all learning finished')","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-04-17T20:24:02.272462Z","iopub.status.idle":"2022-04-17T20:24:02.273414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data_test(train):\n    print('train_df', train.shape)\n\n    train_dataset = TurtleDataset(train, transform=transforms.Compose([Rescale_test(256)]))\n\n    train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE,\n                              shuffle=True, num_workers=4, drop_last=True)\n        \n    return train_loader\n\n\ntest_loader = load_data_test(test)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:46:03.752105Z","iopub.execute_input":"2022-04-17T20:46:03.752444Z","iopub.status.idle":"2022-04-17T20:46:03.759518Z","shell.execute_reply.started":"2022-04-17T20:46:03.752411Z","shell.execute_reply":"2022-04-17T20:46:03.758494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_step(val_loader,\n        model):\n    top = []\n    name = []\n    model.eval()\n    for i,k in val_loader:\n        name.append(k)\n        output = model(i[0].float().cuda())\n        confs, predicts = torch.max(output.detach(), dim=1)\n        for j in output.detach():\n            top.append(j.topk(5).indices.detach().cpu().numpy())\n            \n    return name,top","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:46:06.520745Z","iopub.execute_input":"2022-04-17T20:46:06.521079Z","iopub.status.idle":"2022-04-17T20:46:06.528674Z","shell.execute_reply.started":"2022-04-17T20:46:06.521046Z","shell.execute_reply":"2022-04-17T20:46:06.527740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id,top_k = test_step(test_loader, model)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T21:15:51.386213Z","iopub.execute_input":"2022-04-17T21:15:51.386799Z","iopub.status.idle":"2022-04-17T21:16:53.106620Z","shell.execute_reply.started":"2022-04-17T21:15:51.386758Z","shell.execute_reply":"2022-04-17T21:16:53.105734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class EfficientNetEncoderHead(nn.Module):\n    def __init__(self, depth, num_classes):\n        super(EfficientNetEncoderHead, self).__init__()\n        self.depth = depth\n        self.base = efficientnet_pytorch.EfficientNet.from_name(\"efficientnet-b7\")\n        self.gem = GeM()\n        self.output_filter = self.base._fc.in_features\n        self.fc = nn.Linear(self.output_filter, 1000)\n        self.arcface = ArcMarginProduct(1000, num_classes)\n    def forward(self, x):\n        x = self.base.extract_features(x)\n        x = self.gem(x).squeeze()\n        x = self.fc(x)\n        x = self.arcface(x, self.training)\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:46:10.302943Z","iopub.execute_input":"2022-04-17T20:46:10.303353Z","iopub.status.idle":"2022-04-17T20:46:10.312686Z","shell.execute_reply.started":"2022-04-17T20:46:10.303321Z","shell.execute_reply":"2022-04-17T20:46:10.311320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = EfficientNetEncoderHead(7,100)\nmodel.load_state_dict(torch.load(\"../input/label-encoder-testing/the_model8.pth\"))\nmodel.cuda()\nprint('done')","metadata":{"execution":{"iopub.status.busy":"2022-04-17T21:15:48.019262Z","iopub.execute_input":"2022-04-17T21:15:48.019634Z","iopub.status.idle":"2022-04-17T21:15:51.384416Z","shell.execute_reply.started":"2022-04-17T21:15:48.019603Z","shell.execute_reply":"2022-04-17T21:15:51.383476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = [j for i in image_id for j in i]","metadata":{"execution":{"iopub.status.busy":"2022-04-17T21:16:53.111026Z","iopub.execute_input":"2022-04-17T21:16:53.111315Z","iopub.status.idle":"2022-04-17T21:16:53.115378Z","shell.execute_reply.started":"2022-04-17T21:16:53.111286Z","shell.execute_reply":"2022-04-17T21:16:53.114522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({\"image_id\":image_id, \"top_5\":top_k})\ndf['prediction1'] = None\ndf['prediction2'] = None\ndf['prediction3'] = None\ndf['prediction4'] = None\ndf['prediction5'] = None\nfor i in range(len(df)):\n    df['prediction1'][i] = df['top_5'][i][0]\n    df['prediction2'][i] = df['top_5'][i][1]\n    df['prediction3'][i] = df['top_5'][i][2]\n    df['prediction4'][i] = df['top_5'][i][3]\n    df['prediction5'][i] = df['top_5'][i][4]\n","metadata":{"execution":{"iopub.status.busy":"2022-04-17T21:16:53.117526Z","iopub.execute_input":"2022-04-17T21:16:53.118247Z","iopub.status.idle":"2022-04-17T21:16:53.276699Z","shell.execute_reply.started":"2022-04-17T21:16:53.118200Z","shell.execute_reply":"2022-04-17T21:16:53.275970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(df)):\n    df['image_id'][i] = df['image_id'][i].split('/')[-1].split('.')[0]","metadata":{"execution":{"iopub.status.busy":"2022-04-17T21:16:53.278038Z","iopub.execute_input":"2022-04-17T21:16:53.278360Z","iopub.status.idle":"2022-04-17T21:16:53.325180Z","shell.execute_reply.started":"2022-04-17T21:16:53.278326Z","shell.execute_reply":"2022-04-17T21:16:53.324410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.pop('top_5')\ndf","metadata":{"execution":{"iopub.status.busy":"2022-04-17T21:16:53.326467Z","iopub.execute_input":"2022-04-17T21:16:53.326876Z","iopub.status.idle":"2022-04-17T21:16:53.346128Z","shell.execute_reply.started":"2022-04-17T21:16:53.326838Z","shell.execute_reply":"2022-04-17T21:16:53.344958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['prediction5'] = \"new_turtle\"\ndf.to_csv(\"prediction_new_effnet_lableencoder_8.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T21:16:53.676888Z","iopub.execute_input":"2022-04-17T21:16:53.677397Z","iopub.status.idle":"2022-04-17T21:16:53.686876Z","shell.execute_reply.started":"2022-04-17T21:16:53.677356Z","shell.execute_reply":"2022-04-17T21:16:53.685957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dc = {'t_id_VP2NW7aV': 0,\n 't_id_qZ0iZYsC': 1,\n 't_id_3b65X5Lw': 2,\n 't_id_YjXYTCGC': 3,\n 't_id_d6aYXtor': 4,\n 't_id_ksTLswDT': 5,\n 't_id_hRzOoJ2t': 6,\n 't_id_utw0thCe': 7,\n 't_id_k1rScFLB': 8,\n 't_id_n2FBHk6d': 9,\n 't_id_ZfvZBX4Q': 10,\n 't_id_G5eoqwD8': 11,\n 't_id_FBsGDJhU': 12,\n 't_id_Ts5LyVQz': 13,\n 't_id_NW7wn8TC': 14,\n 't_id_JI6ba2Yx': 15,\n 't_id_ifWwxWF4': 16,\n 't_id_uIlC9Gfo': 17,\n 't_id_dVQ4x3wz': 18,\n 't_id_3K93fQBS': 19,\n 't_id_IlO9BOKc': 20,\n 't_id_DPYQnZyv': 21,\n 't_id_ROFhVsy2': 22,\n 't_id_BI99coHt': 23,\n 't_id_GrxmyS59': 24,\n 't_id_AOWArhGb': 25,\n 't_id_4XiPKIk7': 26,\n 't_id_mpuNp8mf': 27,\n 't_id_stWei2Uq': 28,\n 't_id_15bo4NKD': 29,\n 't_id_QqeoI5F3': 30,\n 't_id_Kf73l69A': 31,\n 't_id_Kc1tXDbJ': 32,\n 't_id_2Yn71r7R': 33,\n 't_id_iZQiE7wb': 34,\n 't_id_m2JvEcsg': 35,\n 't_id_a4VYrmyA': 36,\n 't_id_UVQa4BMz': 37,\n 't_id_tjWepji1': 38,\n 't_id_BXWccqAn': 39,\n 't_id_1KIezxkh': 40,\n 't_id_e9i3Lbq4': 41,\n 't_id_bYageLYA': 42,\n 't_id_8b8sprYe': 43,\n 't_id_2QmcRkNj': 44,\n 't_id_9GFmcOd5': 45,\n 't_id_smNwfXAT': 46,\n 't_id_hibDzPAP': 47,\n 't_id_D3kHUEgp': 48,\n 't_id_B7LaSiac': 49,\n 't_id_fjHGjp1w': 50,\n 't_id_gJaKYxBQ': 51,\n 't_id_72SiiZCp': 52,\n 't_id_IP1t15lD': 53,\n 't_id_uJXT7dGu': 54,\n 't_id_7gFFZy7i': 55,\n 't_id_87CLFCvE': 56,\n 't_id_J5dngbNA': 57,\n 't_id_HcnnlRda': 58,\n 't_id_WDCMGvI4': 59,\n 't_id_g9Fz8PH7': 60,\n 't_id_SwQZGIpa': 61,\n 't_id_p77GDtzg': 62,\n 't_id_D0gA44av': 63,\n 't_id_OqU1NWEA': 64,\n 't_id_MwnEYfqe': 65,\n 't_id_HxxqrdTx': 66,\n 't_id_IP3xtKuX': 67,\n 't_id_4ZfTUmwL': 68,\n 't_id_DbmclTcj': 69,\n 't_id_AMnriNb5': 70,\n 't_id_fxTQ5vHC': 71,\n 't_id_J24awAHQ': 72,\n 't_id_0g31STvR': 73,\n 't_id_Imm5pnNf': 74,\n 't_id_WXXcPTSW': 75,\n 't_id_VFb44eFm': 76,\n 't_id_uMOOrQu7': 77,\n 't_id_3fKmnkBS': 78,\n 't_id_mXD9Bjsb': 79,\n 't_id_gGQbn7FA': 80,\n 't_id_G5JLzvai': 81,\n 't_id_EEbWq5Pj': 82,\n 't_id_GOIvCduN': 83,\n 't_id_pCO59rOk': 84,\n 't_id_iD9ikw6Y': 85,\n 't_id_dc6pjonY': 86,\n 't_id_uVgs53Cz': 87,\n 't_id_C0wevyOl': 88,\n 't_id_KysHzQQK': 89,\n 't_id_2E8o5Jtl': 90,\n 't_id_ip3jsrYo': 91,\n 't_id_niEvmwXu': 92,\n 't_id_Lhp87PBX': 93,\n 't_id_Dv4O8bOM': 94,\n 't_id_dhdJMT1K': 95,\n 't_id_X3bKBWYW': 96,\n 't_id_0DPPpRUz': 97,\n 't_id_gz3whk0q': 98,\n 't_id_D7NA71la': 99}","metadata":{"execution":{"iopub.status.busy":"2022-04-17T20:53:46.550881Z","iopub.execute_input":"2022-04-17T20:53:46.551243Z","iopub.status.idle":"2022-04-17T20:53:46.569228Z","shell.execute_reply.started":"2022-04-17T20:53:46.551211Z","shell.execute_reply":"2022-04-17T20:53:46.567986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data_label(train):\n    counts = train.turtle_id.value_counts()\n    print('train_df', train.shape)\n\n    label_encoder = LabelEncoder()\n    label_encoder.fit(train.turtle_id.values)\n    return label_encoder\nlabel_encoder = load_data_label(train)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T21:03:11.861129Z","iopub.execute_input":"2022-04-17T21:03:11.861452Z","iopub.status.idle":"2022-04-17T21:03:11.869735Z","shell.execute_reply.started":"2022-04-17T21:03:11.861423Z","shell.execute_reply":"2022-04-17T21:03:11.868755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# label_encoder.inverse_transform()\nfor i in range(len(df)):\n    df['prediction1'][i] = label_encoder.inverse_transform([df['prediction1'][i]])[0]\n    df['prediction2'][i] = label_encoder.inverse_transform([df['prediction2'][i]])[0]\n    df['prediction3'][i] = label_encoder.inverse_transform([df['prediction3'][i]])[0]\n    df['prediction4'][i] = label_encoder.inverse_transform([df['prediction4'][i]])[0]\n    df['prediction5'][i] = label_encoder.inverse_transform([df['prediction5'][i]])[0]","metadata":{"execution":{"iopub.status.busy":"2022-04-17T21:16:53.347802Z","iopub.execute_input":"2022-04-17T21:16:53.348569Z","iopub.status.idle":"2022-04-17T21:16:53.674617Z","shell.execute_reply.started":"2022-04-17T21:16:53.348523Z","shell.execute_reply":"2022-04-17T21:16:53.673804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inv_map = {v: k for k, v in class_names.items()}","metadata":{"execution":{"iopub.status.busy":"2022-04-17T21:03:19.182906Z","iopub.execute_input":"2022-04-17T21:03:19.183241Z","iopub.status.idle":"2022-04-17T21:03:19.187371Z","shell.execute_reply.started":"2022-04-17T21:03:19.183210Z","shell.execute_reply":"2022-04-17T21:03:19.186494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['prediction1'] = df['prediction1'].map(inv_map)\ndf['prediction2'] = df['prediction2'].map(inv_map)\ndf['prediction3'] = df['prediction3'].map(inv_map)\ndf['prediction4'] = df['prediction4'].map(inv_map)\ndf['prediction5'] = df['prediction5'].map(inv_map)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T21:03:20.527096Z","iopub.execute_input":"2022-04-17T21:03:20.527419Z","iopub.status.idle":"2022-04-17T21:03:20.542360Z","shell.execute_reply.started":"2022-04-17T21:03:20.527388Z","shell.execute_reply":"2022-04-17T21:03:20.541489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-04-17T21:12:30.359907Z","iopub.execute_input":"2022-04-17T21:12:30.360412Z","iopub.status.idle":"2022-04-17T21:12:30.380643Z","shell.execute_reply.started":"2022-04-17T21:12:30.360368Z","shell.execute_reply":"2022-04-17T21:12:30.379705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}