{"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":"# ====================================================\n# Directory settings\n# ====================================================\nimport os\n\nOUTPUT_DIR = './'\nif not os.path.exists(OUTPUT_DIR):\n    os.makedirs(OUTPUT_DIR)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-19T13:24:03.757429Z","iopub.execute_input":"2021-10-19T13:24:03.758383Z","iopub.status.idle":"2021-10-19T13:24:03.770317Z","shell.execute_reply.started":"2021-10-19T13:24:03.758234Z","shell.execute_reply":"2021-10-19T13:24:03.769116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nprint(f\"\\n... ACCELERATOR SETUP STARTING ...\\n\")\n\n# Detect hardware, return appropriate distribution strategy\ntry:\n    # TPU detection. No parameters necessary if TPU_NAME environment variable is set. On Kaggle this is always the case.\n    TPU = tf.distribute.cluster_resolver.TPUClusterResolver()  \nexcept ValueError:\n    TPU = None\n\nif TPU:\n    print(f\"\\n... RUNNING ON TPU - {TPU.master()}...\")\n    tf.config.experimental_connect_to_cluster(TPU)\n    tf.tpu.experimental.initialize_tpu_system(TPU)\n    strategy = tf.distribute.experimental.TPUStrategy(TPU)\nelse:\n    print(f\"\\n... RUNNING ON CPU/GPU ...\")\n    # Yield the default distribution strategy in Tensorflow\n    #   --> Works on CPU and single GPU.\n    strategy = tf.distribute.get_strategy() \n\n# What Is a Replica?\n#    --> A single Cloud TPU device consists of FOUR chips, each of which has TWO TPU cores. \n#    --> Therefore, for efficient utilization of Cloud TPU, a program should make use of each of the EIGHT (4x2) cores. \n#    --> Each replica is essentially a copy of the training graph that is run on each core and \n#        trains a mini-batch containing 1/8th of the overall batch size\nN_REPLICAS = strategy.num_replicas_in_sync\n    \nprint(f\"... # OF REPLICAS: {N_REPLICAS} ...\\n\")\n\nprint(f\"\\n... ACCELERATOR SETUP COMPLTED ...\\n\")","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:24:03.789820Z","iopub.execute_input":"2021-10-19T13:24:03.790322Z","iopub.status.idle":"2021-10-19T13:24:09.156113Z","shell.execute_reply.started":"2021-10-19T13:24:03.790290Z","shell.execute_reply":"2021-10-19T13:24:09.154967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append('../input/timm-pytorch-image-models/pytorch-image-models-master')\nimport timm\n\nfrom functools import partial\n\nimport cv2\nfrom PIL import Image\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.optim import Adam, SGD\nimport torchvision.models as models\nfrom torch.nn.parameter import Parameter\nfrom torch.utils.data import DataLoader, Dataset\nfrom torch.nn.utils.rnn import pad_sequence, pack_padded_sequence\nfrom torch.optim.lr_scheduler import CosineAnnealingWarmRestarts, CosineAnnealingLR, ReduceLROnPlateau\n\nfrom albumentations import(\n    Compose, OneOf, Normalize, Resize, RandomResizedCrop, RandomCrop, HorizontalFlip, VerticalFlip, \n    RandomBrightness, RandomContrast, RandomBrightnessContrast, Rotate, ShiftScaleRotate, Cutout, \n    IAAAdditiveGaussianNoise, Transpose, Blur\n    )\nfrom albumentations.pytorch import ToTensorV2\nfrom albumentations import ImageOnlyTransform\n\nimport timm\n\nimport warnings \nwarnings.filterwarnings('ignore')\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:24:09.157585Z","iopub.execute_input":"2021-10-19T13:24:09.158083Z","iopub.status.idle":"2021-10-19T13:24:13.566960Z","shell.execute_reply.started":"2021-10-19T13:24:09.158048Z","shell.execute_reply":"2021-10-19T13:24:13.565689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport os\nimport gc\nimport re\nimport math\nimport time\nimport random\nimport shutil\nimport pickle\nfrom pathlib import Path\nfrom contextlib import contextmanager\nfrom collections import defaultdict, Counter\n\nimport scipy as sp\nimport numpy as np\nimport pandas as pd\nfrom tqdm.auto import tqdm\n\nimport Levenshtein\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import StratifiedKFold, GroupKFold, KFold\nfrom functools import partial\n\nimport cv2\nfrom PIL import Image\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.optim import Adam, SGD\nimport torchvision.models as models\nfrom torch.nn.parameter import Parameter\nfrom torch.utils.data import DataLoader, Dataset\nfrom torch.nn.utils.rnn import pad_sequence, pack_padded_sequence\nfrom torch.optim.lr_scheduler import CosineAnnealingWarmRestarts, CosineAnnealingLR, ReduceLROnPlateau\n\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nfrom albumentations import ImageOnlyTransform\nimport timm\nimport pytorch_lightning as pl\n# from config import Config\n# from models import *\nimport warnings \nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:25:03.810932Z","iopub.execute_input":"2021-10-19T13:25:03.811454Z","iopub.status.idle":"2021-10-19T13:25:04.606958Z","shell.execute_reply.started":"2021-10-19T13:25:03.811419Z","shell.execute_reply":"2021-10-19T13:25:04.606012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Loading Train data","metadata":{}},{"cell_type":"code","source":"def convert_image_id_2_path(image_id: str) -> str:\n    return \"../input/bms-molecular-translation/train/{}/{}/{}/{}.png\".format(\n        image_id[0], image_id[1], image_id[2], image_id \n    )","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:25:06.130575Z","iopub.execute_input":"2021-10-19T13:25:06.130957Z","iopub.status.idle":"2021-10-19T13:25:06.136455Z","shell.execute_reply.started":"2021-10-19T13:25:06.130892Z","shell.execute_reply":"2021-10-19T13:25:06.135421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_train (path) : \n    train = pd.read_pickle(path)\n    train['file_path'] = train['image_id'].apply(convert_image_id_2_path)\n    print(f'train.shape: {train.shape}')\n    return(train)\n    \n# héthi mté3na ahna fiha kén id/inchi/path .... bch nhabto ta3 lékhr Tokenizer + Train\n# def prepare_train (path) : \n#     TRAIN_LABELS_PATH = \"../input/bms-molecular-translation/train_labels.csv\"\n#     train = pd.read_csv(TRAIN_LABELS_PATH, index_col=0)\n#     train = train.reset_index()\n#     train['file_path'] = train['image_id'].apply(convert_image_id_2_path)\n#     return (train)\n","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:25:07.349516Z","iopub.execute_input":"2021-10-19T13:25:07.349867Z","iopub.status.idle":"2021-10-19T13:25:07.354777Z","shell.execute_reply.started":"2021-10-19T13:25:07.349825Z","shell.execute_reply":"2021-10-19T13:25:07.353962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = prepare_train('../input/inchi-preprocess-2/train2.pkl')","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:25:07.989404Z","iopub.execute_input":"2021-10-19T13:25:07.990038Z","iopub.status.idle":"2021-10-19T13:25:21.767797Z","shell.execute_reply.started":"2021-10-19T13:25:07.990001Z","shell.execute_reply":"2021-10-19T13:25:21.766815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# folds = folds_split(train)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Tokenizer ","metadata":{}},{"cell_type":"code","source":"class Tokenizer(object):\n    \n    def __init__(self):\n        self.stoi = {}\n        self.itos = {}\n\n    def __len__(self):\n        return len(self.stoi)\n    \n    def fit_on_texts(self, texts):\n        vocab = set()\n        for text in texts:\n            vocab.update(text.split(' '))\n        vocab = sorted(vocab)\n        vocab.append('<sos>')\n        vocab.append('<eos>')\n        vocab.append('<pad>')\n        for i, s in enumerate(vocab):\n            self.stoi[s] = i\n        self.itos = {item[1]: item[0] for item in self.stoi.items()}\n        \n    def text_to_sequence(self, text):\n        sequence = []\n        sequence.append(self.stoi['<sos>'])\n        for s in text.split(' '):\n            sequence.append(self.stoi[s])\n        sequence.append(self.stoi['<eos>'])\n        return sequence\n    \n    def texts_to_sequences(self, texts):\n        sequences = []\n        for text in texts:\n            sequence = self.text_to_sequence(text)\n            sequences.append(sequence)\n        return sequences\n\n    def sequence_to_text(self, sequence):\n        return ''.join(list(map(lambda i: self.itos[i], sequence)))\n    def sequences_to_texts(self, sequences):\n        texts = []\n        for sequence in sequences:\n            text = self.sequence_to_text(sequence)\n            texts.append(text)\n        return texts\n    \n    def predict_caption(self, sequence):\n        caption = ''\n        for i in sequence:\n            if i == self.stoi['<eos>'] or i == self.stoi['<pad>']:\n                break\n            caption += self.itos[i]\n        return caption\n    \n    def predict_captions(self, sequences):\n        captions = []\n        for sequence in sequences:\n            caption = self.predict_caption(sequence)\n            captions.append(caption)\n        return captions","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:25:23.330794Z","iopub.execute_input":"2021-10-19T13:25:23.331190Z","iopub.status.idle":"2021-10-19T13:25:23.342771Z","shell.execute_reply.started":"2021-10-19T13:25:23.331149Z","shell.execute_reply":"2021-10-19T13:25:23.341453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Configuration","metadata":{}},{"cell_type":"code","source":"class CFG:\n#     model_name='resnet34' # output_dim = encoder_dim = 512\n#     model_name = 'tf_efficientnet_b0_ns' # output_dim = encoder_dim = 1280\n    model_name = 'tf_efficientnet_b3_ns' # output_dim = encoder_dim = 1536\n\n#     model_name='resnet200d' # output_dim = encoder_dim = 2048\n    train = True\n    size = 300\n    max_len = 275\n    batch_size = 256\n    num_workers = 4\n    debug = False\n    encoder_lr=1e-4\n    decoder_lr=4e-4\n\n    attention_dim = 512\n    embed_dim = 512\n    encoder_dim = 1536\n    decoder_dim = 512\n    dropout = 0.5\n    seed = 42\n    n_fold = 5\n    trn_fold = [0] # [0, 1, 2, 3, 4]\n    weight_decay=1e-6\n    scheduler='CosineAnnealingLR' # ['ReduceLROnPlateau', 'CosineAnnealingLR', 'CosineAnnealingWarmRestarts']\n    T_max=4 # CosineAnnealingLR\n    min_lr=1e-6\n    epochs = 1\n\n\n\n  ","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:25:23.889419Z","iopub.execute_input":"2021-10-19T13:25:23.889743Z","iopub.status.idle":"2021-10-19T13:25:23.895977Z","shell.execute_reply.started":"2021-10-19T13:25:23.889713Z","shell.execute_reply":"2021-10-19T13:25:23.894887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Utils","metadata":{}},{"cell_type":"code","source":"# ====================================================\n# Utils\n# ====================================================\ndef get_score(y_true, y_pred):\n    scores = []\n    for true, pred in zip(y_true, y_pred):\n        score = Levenshtein.distance(true, pred)\n        scores.append(score)\n    avg_score = np.mean(scores)\n    return avg_score\n\n\ndef init_logger(log_file=OUTPUT_DIR+'train.log'):\n    from logging import getLogger, INFO, FileHandler,  Formatter,  StreamHandler\n    logger = getLogger(__name__)\n    logger.setLevel(INFO)\n    handler1 = StreamHandler()\n    handler1.setFormatter(Formatter(\"%(message)s\"))\n    handler2 = FileHandler(filename=log_file)\n    handler2.setFormatter(Formatter(\"%(message)s\"))\n    logger.addHandler(handler1)\n    logger.addHandler(handler2)\n    return logger\n\nLOGGER = init_logger()","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:25:24.849655Z","iopub.execute_input":"2021-10-19T13:25:24.850038Z","iopub.status.idle":"2021-10-19T13:25:24.857949Z","shell.execute_reply.started":"2021-10-19T13:25:24.850002Z","shell.execute_reply":"2021-10-19T13:25:24.856655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AverageMeter(object):\n    \"\"\"Computes and stores the average and current value\"\"\"\n    def __init__(self):\n        self.reset()\n\n    def reset(self):\n        self.val = 0\n        self.avg = 0\n        self.sum = 0\n        self.count = 0\n\n    def update(self, val, n=1):\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":"2021-10-19T13:25:25.349382Z","iopub.execute_input":"2021-10-19T13:25:25.349700Z","iopub.status.idle":"2021-10-19T13:25:25.356488Z","shell.execute_reply.started":"2021-10-19T13:25:25.349670Z","shell.execute_reply":"2021-10-19T13:25:25.355686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def asMinutes(s):\n    m = math.floor(s / 60)\n    s -= m * 60\n    return '%dm %ds' % (m, s)\n\n\ndef timeSince(since, percent):\n    now = time.time()\n    s = now - since\n    es = s / (percent)\n    rs = es - s\n    return '%s (remain %s)' % (asMinutes(s), asMinutes(rs))","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:25:25.757886Z","iopub.execute_input":"2021-10-19T13:25:25.758429Z","iopub.status.idle":"2021-10-19T13:25:25.765384Z","shell.execute_reply.started":"2021-10-19T13:25:25.758377Z","shell.execute_reply":"2021-10-19T13:25:25.764447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ====================================================\n# Utils\n# ====================================================\ndef get_score(y_true, y_pred):\n    scores = []\n    for true, pred in zip(y_true, y_pred):\n        score = Levenshtein.distance(true, pred)\n        scores.append(score)\n    avg_score = np.mean(scores)\n    return avg_score\ndef seed_torch(seed=42):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n\nseed_torch(seed=CFG.seed)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:25:26.129374Z","iopub.execute_input":"2021-10-19T13:25:26.130048Z","iopub.status.idle":"2021-10-19T13:25:26.141208Z","shell.execute_reply.started":"2021-10-19T13:25:26.129998Z","shell.execute_reply":"2021-10-19T13:25:26.140119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Train/Test DataSet\n","metadata":{}},{"cell_type":"code","source":"class TrainDataset(Dataset):\n    def __init__(self, df,tokenizer, transform=None):\n        super().__init__()\n        self.df = df\n        self.tokenizer = tokenizer\n        self.file_paths = df['file_path'].values\n        self.transform = transform\n        self.labels = df['InChI_text'].values\n        self.fix_transform = A.Compose([A.Transpose(p=1), A.VerticalFlip(p=1)])\n    \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        file_path = self.file_paths[idx]\n        image = cv2.imread(file_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n#         h, w, _ = image.shape\n#         if h > w:\n#             image = self.fix_transform(image=image)['image']\n        if self.transform:\n            augmented = self.transform(image=image)\n            image = augmented['image']\n        label = self.labels[idx]\n        label = self.tokenizer.text_to_sequence(label)\n        label_length = len(label)\n        label_length = torch.LongTensor([label_length])\n        return image , torch.LongTensor(label), label_length ","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:26:52.168428Z","iopub.execute_input":"2021-10-19T13:26:52.168763Z","iopub.status.idle":"2021-10-19T13:26:52.178932Z","shell.execute_reply.started":"2021-10-19T13:26:52.168728Z","shell.execute_reply":"2021-10-19T13:26:52.178208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestDataset(Dataset):\n    def __init__(self, df,tokenizer, transform=None):\n        super().__init__()\n        self.df = df\n        self.file_paths = df['file_path'].values\n        self.transform = transform\n        self.fix_transform = A.Compose([A.Transpose(p=1), A.VerticalFlip(p=1)])\n        self.tokenizer = tokenizer\n    \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        file_path = self.file_paths[idx]\n        image = cv2.imread(file_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n        h, w, _ = image.shape\n        if h > w:\n            image = self.fix_transform(image=image)['image']\n        if self.transform:\n            augmented = self.transform(image=image)\n            image = augmented['image']\n        return image","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:26:52.526398Z","iopub.execute_input":"2021-10-19T13:26:52.526745Z","iopub.status.idle":"2021-10-19T13:26:52.533644Z","shell.execute_reply.started":"2021-10-19T13:26:52.526710Z","shell.execute_reply":"2021-10-19T13:26:52.532960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Fonction bch tpadi el batch","metadata":{}},{"cell_type":"code","source":"def bms_collate(batch):\n    imgs, labels, label_lengths = [], [], []\n    for data_point in batch:\n        imgs.append(data_point[0])\n        labels.append(data_point[1])\n        label_lengths.append(data_point[2])\n    labels = pad_sequence(labels, batch_first=True, padding_value=tokenizer.stoi[\"<pad>\"])\n    return torch.stack(imgs), labels, torch.stack(label_lengths)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:26:53.646497Z","iopub.execute_input":"2021-10-19T13:26:53.647121Z","iopub.status.idle":"2021-10-19T13:26:53.652314Z","shell.execute_reply.started":"2021-10-19T13:26:53.647078Z","shell.execute_reply":"2021-10-19T13:26:53.651459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Transform","metadata":{}},{"cell_type":"code","source":"def get_transforms(*, data):\n    \n    if data == 'train':\n        return Compose([\n            Resize(CFG.size, CFG.size),\n            Normalize(\n                mean=[0.485, 0.456, 0.406],\n                std=[0.229, 0.224, 0.225],\n            ),\n            ToTensorV2(),\n        ])\n    \n    elif data == 'valid':\n        return Compose([\n            Resize(CFG.size, CFG.size),\n            Normalize(\n                mean=[0.485, 0.456, 0.406],\n                std=[0.229, 0.224, 0.225],\n            ),\n            ToTensorV2(),\n        ])","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:26:54.746547Z","iopub.execute_input":"2021-10-19T13:26:54.746933Z","iopub.status.idle":"2021-10-19T13:26:54.752978Z","shell.execute_reply.started":"2021-10-19T13:26:54.746878Z","shell.execute_reply":"2021-10-19T13:26:54.751941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Model","metadata":{}},{"cell_type":"markdown","source":"Encoder","metadata":{}},{"cell_type":"code","source":"class Encoder(nn.Module):\n    def __init__(self, model_name='resnet18', pretrained=False):\n        super().__init__()\n        self.cnn = timm.create_model(model_name, pretrained=pretrained)\n        self.model_name = model_name\n        if model_name == 'resnet18' or model_name == 'resnet34' or model_name == 'resnet200d':\n            self.n_features = self.cnn.fc.in_features\n            self.cnn.global_pool = nn.Identity()\n            self.cnn.fc = nn.Identity()\n        \n        if model_name == 'tf_efficientnet_b0_ns' or model_name == 'tf_efficientnet_b3_ns' or model_name == 'tf_efficientnet_b4_ns':\n            self.n_features = self.cnn.classifier.in_features\n            self.cnn.global_pool = nn.Identity()\n            self.cnn.classifier = nn.Identity()\n            \n    def forward(self, x):\n        bs = x.size(0)\n        features = self.cnn(x)\n        features = features.permute(0, 2, 3, 1)\n        return features","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:26:56.505968Z","iopub.execute_input":"2021-10-19T13:26:56.506498Z","iopub.status.idle":"2021-10-19T13:26:56.514422Z","shell.execute_reply.started":"2021-10-19T13:26:56.506446Z","shell.execute_reply":"2021-10-19T13:26:56.513400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Attention","metadata":{}},{"cell_type":"code","source":"class Attention(nn.Module):\n    \"\"\"\n    Attention network for calculate attention value\n    \"\"\"\n    def __init__(self, encoder_dim, decoder_dim, attention_dim):\n        \"\"\"\n        :param encoder_dim: input size of encoder network\n        :param decoder_dim: input size of decoder network\n        :param attention_dim: input size of attention network\n        \"\"\"\n        super(Attention, self).__init__()\n        self.encoder_att = nn.Linear(encoder_dim, attention_dim)  # linear layer to transform encoded image\n        self.decoder_att = nn.Linear(decoder_dim, attention_dim)  # linear layer to transform decoder's output\n        self.full_att = nn.Linear(attention_dim, 1)  # linear layer to calculate values to be softmax-ed\n        self.relu = nn.ReLU()\n        self.softmax = nn.Softmax(dim=1) # softmax layer to calculate weights\n\n    def forward(self, encoder_out, decoder_hidden):\n        att1 = self.encoder_att(encoder_out)  # (batch_size, num_pixels, attention_dim)\n        att2 = self.decoder_att(decoder_hidden)  # (batch_size, attention_dim)\n        att = self.full_att(self.relu(att1 + att2.unsqueeze(1))).squeeze(2)  # (batch_size, num_pixels)\n        alpha = self.softmax(att)  # (batch_size, num_pixels)\n        attention_weighted_encoding = (encoder_out * alpha.unsqueeze(2)).sum(dim=1)  # (batch_size, encoder_dim)\n        return attention_weighted_encoding, alpha\n ","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:26:57.506654Z","iopub.execute_input":"2021-10-19T13:26:57.507009Z","iopub.status.idle":"2021-10-19T13:26:57.514295Z","shell.execute_reply.started":"2021-10-19T13:26:57.506972Z","shell.execute_reply":"2021-10-19T13:26:57.513302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Decoder with Attention","metadata":{}},{"cell_type":"code","source":"class DecoderWithAttention(nn.Module):\n    \"\"\"\n    Decoder network with attention network used for training\n    \"\"\"\n\n    def __init__(self, attention_dim, embed_dim, decoder_dim, vocab_size, device, encoder_dim=512, dropout=0.5):\n        \"\"\"\n        :param attention_dim: input size of attention network\n        :param embed_dim: input size of embedding network\n        :param decoder_dim: input size of decoder network\n        :param vocab_size: total number of characters used in training\n        :param encoder_dim: input size of encoder network\n        :param dropout: dropout rate\n        \"\"\"\n        super(DecoderWithAttention, self).__init__()\n        self.encoder_dim = encoder_dim\n        self.attention_dim = attention_dim\n        self.embed_dim = embed_dim\n        self.decoder_dim = decoder_dim\n        self.vocab_size = vocab_size\n        self.dropout = dropout\n        self.device = device\n        self.attention = Attention(encoder_dim, decoder_dim, attention_dim)  # attention network\n        self.embedding = nn.Embedding(vocab_size, embed_dim)  # embedding layer\n        self.dropout = nn.Dropout(p=self.dropout)\n        self.decode_step = nn.LSTMCell(embed_dim + encoder_dim, decoder_dim, bias=True)  # decoding LSTMCell\n        self.init_h = nn.Linear(encoder_dim, decoder_dim)  # linear layer to find initial hidden state of LSTMCell\n        self.init_c = nn.Linear(encoder_dim, decoder_dim)  # linear layer to find initial cell state of LSTMCell\n        self.f_beta = nn.Linear(decoder_dim, encoder_dim)  # linear layer to create a sigmoid-activated gate\n        self.sigmoid = nn.Sigmoid()\n        self.fc = nn.Linear(decoder_dim, vocab_size)  # linear layer to find scores over vocabulary\n        self.init_weights()  # initialize some layers with the uniform distribution\n        \n    def init_weights(self):\n        self.embedding.weight.data.uniform_(-0.1, 0.1)\n        self.fc.bias.data.fill_(0)\n        self.fc.weight.data.uniform_(-0.1, 0.1)\n\n    def load_pretrained_embeddings(self, embeddings):\n        self.embedding.weight = nn.Parameter(embeddings)\n\n    def fine_tune_embeddings(self, fine_tune=True):\n        for p in self.embedding.parameters():\n            p.requires_grad = fine_tune\n\n    def init_hidden_state(self, encoder_out):\n        mean_encoder_out = encoder_out.mean(dim=1)\n        h = self.init_h(mean_encoder_out)  # (batch_size, decoder_dim)\n        c = self.init_c(mean_encoder_out)\n        return h, c\n    def forward(self, encoder_out, encoded_captions, caption_lengths):\n        \"\"\"\n        :param encoder_out: output of encoder network\n        :param encoded_captions: transformed sequence from character to integer\n        :param caption_lengths: length of transformed sequence\n        \"\"\"\n        batch_size = encoder_out.size(0)\n        encoder_dim = encoder_out.size(-1)\n        vocab_size = self.vocab_size\n        encoder_out = encoder_out.view(batch_size, -1, encoder_dim)  # (batch_size, num_pixels, encoder_dim)\n        num_pixels = encoder_out.size(1)\n        caption_lengths, sort_ind = caption_lengths.squeeze(1).sort(dim=0, descending=True)\n        encoder_out = encoder_out[sort_ind]\n        encoded_captions = encoded_captions[sort_ind]\n        # embedding transformed sequence for vector\n        embeddings = self.embedding(encoded_captions)  # (batch_size, max_caption_length, embed_dim)\n        # initialize hidden state and cell state of LSTM cell\n        h, c = self.init_hidden_state(encoder_out) # (batch_size, decoder_dim)\n        # set decode length by caption length - 1 because of omitting start token\n        decode_lengths = (caption_lengths - 1).tolist()\n        predictions = torch.zeros(batch_size, max(decode_lengths), vocab_size).to(self.device)\n        alphas = torch.zeros(batch_size, max(decode_lengths), num_pixels).to(self.device)\n        # predict sequence\n        for t in range(max(decode_lengths)):\n            batch_size_t = sum([l > t for l in decode_lengths])\n            attention_weighted_encoding, alpha = self.attention(encoder_out[:batch_size_t], h[:batch_size_t])\n            gate = self.sigmoid(self.f_beta(h[:batch_size_t]))  # gating scalar, (batch_size_t, encoder_dim)\n            attention_weighted_encoding = gate * attention_weighted_encoding\n            h, c = self.decode_step(\n                torch.cat([embeddings[:batch_size_t, t, :], attention_weighted_encoding], dim=1),\n                (h[:batch_size_t], c[:batch_size_t]))  # (batch_size_t, decoder_dim)\n            preds = self.fc(self.dropout(h))  # (batch_size_t, vocab_size)\n            predictions[:batch_size_t, t, :] = preds\n            alphas[:batch_size_t, t, :] = alpha\n        return predictions, encoded_captions, decode_lengths, alphas, sort_ind\n    \n    def predict(self, encoder_out, decode_lengths, tokenizer):\n        batch_size = encoder_out.size(0)\n        encoder_dim = encoder_out.size(-1)\n        vocab_size = self.vocab_size\n        encoder_out = encoder_out.view(batch_size, -1, encoder_dim)  # (batch_size, num_pixels, encoder_dim)\n        num_pixels = encoder_out.size(1)\n        # embed start tocken for LSTM input\n        start_tockens = torch.ones(batch_size, dtype=torch.long).to(self.device) * tokenizer.stoi[\"<sos>\"]\n        embeddings = self.embedding(start_tockens)\n        # initialize hidden state and cell state of LSTM cell\n        h, c = self.init_hidden_state(encoder_out)  # (batch_size, decoder_dim)\n        predictions = torch.zeros(batch_size, decode_lengths, vocab_size).to(self.device)\n        # predict sequence\n        for t in range(decode_lengths):\n            attention_weighted_encoding, alpha = self.attention(encoder_out, h)\n            gate = self.sigmoid(self.f_beta(h))  # gatting scalar, (batch_size_t, encoder_dim)\n            attention_weighted_encoding = gate * attention_weighted_encoding\n            h, c = self.decode_step(\n                torch.cat([embeddings, attention_weighted_encoding], dim=1),\n                (h, c))  # (batch_size_t, decoder_dim)\n            preds = self.fc(self.dropout(h))  # (batch_size_t, vocab_size)\n            predictions[:, t, :] = preds\n            if np.argmax(preds.detach().cpu().numpy()) == tokenizer.stoi[\"<eos>\"]:\n                break\n            embeddings = self.embedding(torch.argmax(preds, -1))\n        return predictions","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:26:58.602928Z","iopub.execute_input":"2021-10-19T13:26:58.603263Z","iopub.status.idle":"2021-10-19T13:26:58.626627Z","shell.execute_reply.started":"2021-10-19T13:26:58.603235Z","shell.execute_reply":"2021-10-19T13:26:58.625643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_fn(train_loader, encoder, decoder, criterion, \n             encoder_optimizer, decoder_optimizer, epoch,\n             encoder_scheduler, decoder_scheduler, device):\n    batch_time = AverageMeter()\n    data_time = AverageMeter()\n    losses = AverageMeter()\n    # switch to train mode\n    encoder.train()\n    decoder.train()\n    start = end = time.time()\n    global_step = 0\n    for step, (images, labels, label_lengths) in enumerate(train_loader):\n        # measure data loading time\n        data_time.update(time.time() - end)\n        images = images.to(device)\n        labels = labels.to(device)\n        label_lengths = label_lengths.to(device)\n        batch_size = images.size(0)\n        features = encoder(images)\n        predictions, caps_sorted, decode_lengths, alphas, sort_ind = decoder(features, labels, label_lengths)\n        targets = caps_sorted[:, 1:]\n        predictions = pack_padded_sequence(predictions, decode_lengths, batch_first=True).data\n        targets = pack_padded_sequence(targets, decode_lengths, batch_first=True).data\n        loss = criterion(predictions, targets)\n        # record loss\n        losses.update(loss.item(), batch_size)\n        if CFG.gradient_accumulation_steps > 1:\n            loss = loss / CFG.gradient_accumulation_steps\n        loss.backward()\n        encoder_grad_norm = torch.nn.utils.clip_grad_norm_(encoder.parameters(), CFG.max_grad_norm)\n        decoder_grad_norm = torch.nn.utils.clip_grad_norm_(decoder.parameters(), CFG.max_grad_norm)\n        if (step + 1) % CFG.gradient_accumulation_steps == 0:\n            encoder_optimizer.step()\n            decoder_optimizer.step()\n            encoder_optimizer.zero_grad()\n            decoder_optimizer.zero_grad()\n            global_step += 1\n         # measure elapsed time\n        batch_time.update(time.time() - end)\n        end = time.time()\n        if step % CFG.print_freq == 0 or step == (len(train_loader)-1):\n            print('Epoch: [{0}][{1}/{2}] '\n                  'Data {data_time.val:.3f} ({data_time.avg:.3f}) '\n                  'Elapsed {remain:s} '\n                  'Loss: {loss.val:.4f}({loss.avg:.4f}) '\n                  'Encoder Grad: {encoder_grad_norm:.4f}  '\n                  'Decoder Grad: {decoder_grad_norm:.4f}  '\n                  #'Encoder LR: {encoder_lr:.6f}  '\n                  #'Decoder LR: {decoder_lr:.6f}  '\n                  .format(\n                   epoch+1, step, len(train_loader), batch_time=batch_time,\n                   data_time=data_time, loss=losses,\n                   remain=timeSince(start, float(step+1)/len(train_loader)),\n                   encoder_grad_norm=encoder_grad_norm,\n                   decoder_grad_norm=decoder_grad_norm,\n                   #encoder_lr=encoder_scheduler.get_lr()[0],\n                   #decoder_lr=decoder_scheduler.get_lr()[0],\n                   ))\n    return losses.avg","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:26:59.217621Z","iopub.execute_input":"2021-10-19T13:26:59.217980Z","iopub.status.idle":"2021-10-19T13:26:59.230097Z","shell.execute_reply.started":"2021-10-19T13:26:59.217943Z","shell.execute_reply":"2021-10-19T13:26:59.229216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def valid_fn(valid_loader, encoder, decoder, tokenizer, criterion, device):\n    batch_time = AverageMeter()\n    data_time = AverageMeter()\n    # switch to evaluation mode\n    encoder.eval()\n    decoder.eval()\n    text_preds = []\n    start = end = time.time()\n    for step, (images) in enumerate(valid_loader):\n        # measure data loading time\n        data_time.update(time.time() - end)\n        images = images.to(device)\n        batch_size = images.size(0)\n        with torch.no_grad():\n            features = encoder(images)\n            predictions = decoder.predict(features, CFG.max_len, tokenizer)\n        predicted_sequence = torch.argmax(predictions.detach().cpu(), -1).numpy()\n        _text_preds = tokenizer.predict_captions(predicted_sequence)\n        text_preds.append(_text_preds)\n          # measure elapsed time\n        batch_time.update(time.time() - end)\n        end = time.time()\n        if step % CFG.print_freq == 0 or step == (len(valid_loader)-1):\n            print('EVAL: [{0}/{1}] '\n                  'Data {data_time.val:.3f} ({data_time.avg:.3f}) '\n                  'Elapsed {remain:s} '\n                  .format(\n                   step, len(valid_loader), batch_time=batch_time,\n                   data_time=data_time,\n                   remain=timeSince(start, float(step+1)/len(valid_loader)),\n                   ))\n    text_preds = np.concatenate(text_preds)\n    return text_preds","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:27:00.266321Z","iopub.execute_input":"2021-10-19T13:27:00.266957Z","iopub.status.idle":"2021-10-19T13:27:00.275848Z","shell.execute_reply.started":"2021-10-19T13:27:00.266892Z","shell.execute_reply":"2021-10-19T13:27:00.275174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ====================================================\n# Train loop\n# ====================================================\ndef train_loop(folds, fold):\n\n    LOGGER.info(f\"========== fold: {fold} training ==========\")\n\n    # ====================================================\n    # loader\n    # ====================================================\n    trn_idx = folds[folds['fold'] != fold].index\n    val_idx = folds[folds['fold'] == fold].index\n\n    train_folds = folds.loc[trn_idx].reset_index(drop=True)\n    valid_folds = folds.loc[val_idx].reset_index(drop=True)\n    valid_labels = valid_folds['InChI'].values\n\n    train_dataset = TrainDataset(train_folds, tokenizer, transform=get_transforms(data='train'))\n    valid_dataset = TestDataset(valid_folds, tokenizer , transform=get_transforms(data='valid'))\n\n    train_loader = DataLoader(train_dataset, \n                              batch_size=CFG.batch_size, \n                              shuffle=True, \n                              num_workers=CFG.num_workers, \n                              pin_memory=True,\n                              drop_last=True, \n                              collate_fn=bms_collate)\n    valid_loader = DataLoader(valid_dataset, \n                              batch_size=CFG.batch_size, \n                              shuffle=False, \n                              num_workers=CFG.num_workers,\n                              pin_memory=True, \n                              drop_last=False)\n     # ====================================================\n    # scheduler \n    # ====================================================\n    def get_scheduler(optimizer):\n        if CFG.scheduler=='ReduceLROnPlateau':\n            scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=CFG.factor, patience=CFG.patience, verbose=True, eps=CFG.eps)\n        elif CFG.scheduler=='CosineAnnealingLR':\n            scheduler = CosineAnnealingLR(optimizer, T_max=CFG.T_max, eta_min=CFG.min_lr, last_epoch=-1)\n        elif CFG.scheduler=='CosineAnnealingWarmRestarts':\n            scheduler = CosineAnnealingWarmRestarts(optimizer, T_0=CFG.T_0, T_mult=1, eta_min=CFG.min_lr, last_epoch=-1)\n        return scheduler\n     # ====================================================\n    # model & optimizer\n    # ====================================================\n    encoder = Encoder(CFG.model_name, pretrained=True)\n    encoder.to(device)\n    encoder_optimizer = Adam(encoder.parameters(), lr=CFG.encoder_lr, weight_decay=CFG.weight_decay, amsgrad=False)\n    encoder_scheduler = get_scheduler(encoder_optimizer)\n    \n    decoder = DecoderWithAttention(attention_dim=CFG.attention_dim,\n                                   embed_dim=CFG.embed_dim,\n                                   decoder_dim=CFG.decoder_dim,\n                                   vocab_size=len(tokenizer),\n                                   dropout=CFG.dropout,\n                                   device=device)\n    decoder.to(device)\n    decoder_optimizer = Adam(decoder.parameters(), lr=CFG.decoder_lr, weight_decay=CFG.weight_decay, amsgrad=False)\n    decoder_scheduler = get_scheduler(decoder_optimizer)\n      # ====================================================\n    # loop\n    # ====================================================\n    criterion = nn.CrossEntropyLoss(ignore_index=tokenizer.stoi[\"<pad>\"])\n\n    best_score = np.inf\n    best_loss = np.inf\n    \n    for epoch in range(CFG.epochs):\n        \n        start_time = time.time()\n        \n        # train\n        avg_loss = train_fn(train_loader, encoder, decoder, criterion, \n                            encoder_optimizer, decoder_optimizer, epoch, \n                            encoder_scheduler, decoder_scheduler, device)\n\n        # eval\n        text_preds = valid_fn(valid_loader, encoder, decoder, tokenizer, criterion, device)\n        text_preds = [f\"InChI=1S/{text}\" for text in text_preds]\n        LOGGER.info(f\"labels: {valid_labels[:5]}\")\n        LOGGER.info(f\"preds: {text_preds[:5]}\")\n           # scoring\n        score = get_score(valid_labels, text_preds)\n        \n        if isinstance(encoder_scheduler, ReduceLROnPlateau):\n            encoder_scheduler.step(score)\n        elif isinstance(encoder_scheduler, CosineAnnealingLR):\n            encoder_scheduler.step()\n        elif isinstance(encoder_scheduler, CosineAnnealingWarmRestarts):\n            encoder_scheduler.step()\n            \n        if isinstance(decoder_scheduler, ReduceLROnPlateau):\n            decoder_scheduler.step(score)\n        elif isinstance(decoder_scheduler, CosineAnnealingLR):\n            decoder_scheduler.step()\n        elif isinstance(decoder_scheduler, CosineAnnealingWarmRestarts):\n            decoder_scheduler.step()\n\n        elapsed = time.time() - start_time\n\n        LOGGER.info(f'Epoch {epoch+1} - avg_train_loss: {avg_loss:.4f}  time: {elapsed:.0f}s')\n        LOGGER.info(f'Epoch {epoch+1} - Score: {score:.4f}')\n                \n        if score < best_score:\n            best_score = score\n            LOGGER.info(f'Epoch {epoch+1} - Save Best Score: {best_score:.4f} Model')\n            torch.save({'encoder': encoder.state_dict(), \n                        'encoder_optimizer': encoder_optimizer.state_dict(), \n                        'encoder_scheduler': encoder_scheduler.state_dict(), \n                        'decoder': decoder.state_dict(), \n                        'decoder_optimizer': decoder_optimizer.state_dict(), \n                        'decoder_scheduler': decoder_scheduler.state_dict(), \n                        'text_preds': text_preds,\n                       },\n                        OUTPUT_DIR+f'{CFG.model_name}_fold{fold}_best.pth')\n","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:27:00.816145Z","iopub.execute_input":"2021-10-19T13:27:00.816718Z","iopub.status.idle":"2021-10-19T13:27:00.833240Z","shell.execute_reply.started":"2021-10-19T13:27:00.816671Z","shell.execute_reply":"2021-10-19T13:27:00.832359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def main():\n\n    \"\"\"\n    Prepare: 1.train  2.folds\n    \"\"\"\n\n    if CFG.train:\n        # train\n        oof_df = pd.DataFrame()\n        for fold in range(CFG.n_fold):\n            if fold in CFG.trn_fold:\n                train_loop(folds, fold)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:27:01.313252Z","iopub.execute_input":"2021-10-19T13:27:01.313740Z","iopub.status.idle":"2021-10-19T13:27:01.318361Z","shell.execute_reply.started":"2021-10-19T13:27:01.313707Z","shell.execute_reply":"2021-10-19T13:27:01.317405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenizer = torch.load('../input/inchi-preprocess-2/tokenizer2.pth')\n","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:27:02.045453Z","iopub.execute_input":"2021-10-19T13:27:02.045845Z","iopub.status.idle":"2021-10-19T13:27:02.063997Z","shell.execute_reply.started":"2021-10-19T13:27:02.045796Z","shell.execute_reply":"2021-10-19T13:27:02.063232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if __name__ == '__main__':\n#     main()","metadata":{"execution":{"iopub.status.busy":"2021-10-19T13:36:43.889608Z","iopub.execute_input":"2021-10-19T13:36:43.890975Z","iopub.status.idle":"2021-10-19T13:36:43.898360Z","shell.execute_reply.started":"2021-10-19T13:36:43.890841Z","shell.execute_reply":"2021-10-19T13:36:43.897061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}