{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":46105,"databundleVersionId":5087314,"sourceType":"competition"},{"sourceId":5108729,"sourceType":"datasetVersion","datasetId":2966876}],"dockerImageVersionId":30458,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nimport warnings \nwarnings.filterwarnings('ignore')\n%config InlineBackend.figure_format ='retina'\nsns.set(style='ticks')\n\nplt.rc('figure', figsize=(6, 3.7), dpi=100) # figure size\nplt.rc('axes', labelpad=20, facecolor=\"#ffffff\", # properties of axes\n       linewidth=0.4, grid=True, labelsize=10) \nplt.rc('xtick.major', width=0.2) # major ticks where the labels are shown\nplt.rc('ytick.major', width=0.2) # minor ticks where the labels are not shown\nplt.rc('grid', color='#EEEEEE', linewidth=0.25)\nplt.rc('font', family='Arial', weight='400', size=10)\nplt.rc('text', color='#282828')\nplt.rc('xtick', labelsize=10)\nplt.rc('ytick', labelsize=10)\nplt.rc('savefig', pad_inches=0.3, dpi=300)","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TRAIN_FILE = '/kaggle/input/asl-signs/train.csv'","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:04.384991Z","iopub.execute_input":"2023-04-30T20:13:04.385968Z","iopub.status.idle":"2023-04-30T20:13:04.393259Z","shell.execute_reply.started":"2023-04-30T20:13:04.385922Z","shell.execute_reply":"2023-04-30T20:13:04.392107Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img = pd.read_parquet('/kaggle/input/asl-signs/train_landmark_files/16069/100015657.parquet')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-30T20:13:04.394809Z","iopub.execute_input":"2023-04-30T20:13:04.397529Z","iopub.status.idle":"2023-04-30T20:13:04.614727Z","shell.execute_reply.started":"2023-04-30T20:13:04.397477Z","shell.execute_reply":"2023-04-30T20:13:04.613572Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:04.617642Z","iopub.execute_input":"2023-04-30T20:13:04.618027Z","iopub.status.idle":"2023-04-30T20:13:04.626048Z","shell.execute_reply.started":"2023-04-30T20:13:04.617985Z","shell.execute_reply":"2023-04-30T20:13:04.624842Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch.nn as nn","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:04.628901Z","iopub.execute_input":"2023-04-30T20:13:04.629621Z","iopub.status.idle":"2023-04-30T20:13:08.962639Z","shell.execute_reply.started":"2023-04-30T20:13:04.629584Z","shell.execute_reply":"2023-04-30T20:13:08.961624Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class datagen(nn.Module):\n    def __init__(self):\n        super(datagen, self).__init__()\n        pass\n    def forward(self, x):\n    #n = len(x['frame'].value_counts())\n    #x = torch.tensor(x.iloc[:, 4:].to_numpy())\n    #x = x.contiguous().view(n, 543, -1)\n        face_x = x[:,:468,:].contiguous().view(-1, 468*3)\n        lefth_x = x[:,468:489,:].contiguous().view(-1, 21*3)\n        pose_x = x[:,489:522,:].contiguous().view(-1, 33*3)\n        righth_x = x[:,522:,:].contiguous().view(-1, 21*3)\n\n        lefth_x = lefth_x[~torch.any(torch.isnan(lefth_x), dim=1),:]\n        righth_x = righth_x[~torch.any(torch.isnan(righth_x), dim=1),:]\n\n        x1m = torch.mean(face_x, 0)\n        x2m = torch.mean(lefth_x, 0)\n        x3m = torch.mean(pose_x, 0)\n        x4m = torch.mean(righth_x, 0)\n\n        x1s = torch.std(face_x, 0)\n        x2s = torch.std(lefth_x, 0)\n        x3s = torch.std(pose_x, 0)\n        x4s = torch.std(righth_x, 0)\n\n        xfeat = torch.cat([x1m,x2m,x3m,x4m, x1s,x2s,x3s,x4s], axis=0)\n        xfeat = torch.where(torch.isnan(xfeat), torch.tensor(0.0, dtype=torch.float32), xfeat)\n\n        return xfeat\n    \n","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:08.964143Z","iopub.execute_input":"2023-04-30T20:13:08.964740Z","iopub.status.idle":"2023-04-30T20:13:08.976622Z","shell.execute_reply.started":"2023-04-30T20:13:08.964703Z","shell.execute_reply":"2023-04-30T20:13:08.975616Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"d_gen = datagen()","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:08.978605Z","iopub.execute_input":"2023-04-30T20:13:08.979207Z","iopub.status.idle":"2023-04-30T20:13:08.992825Z","shell.execute_reply.started":"2023-04-30T20:13:08.979171Z","shell.execute_reply":"2023-04-30T20:13:08.991811Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/asl-signs/train.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:08.995372Z","iopub.execute_input":"2023-04-30T20:13:08.996191Z","iopub.status.idle":"2023-04-30T20:13:09.213359Z","shell.execute_reply.started":"2023-04-30T20:13:08.996154Z","shell.execute_reply":"2023-04-30T20:13:09.212434Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.values","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:09.214778Z","iopub.execute_input":"2023-04-30T20:13:09.215606Z","iopub.status.idle":"2023-04-30T20:13:09.237905Z","shell.execute_reply.started":"2023-04-30T20:13:09.215567Z","shell.execute_reply":"2023-04-30T20:13:09.236880Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import json\n\nwith open('/kaggle/input/asl-signs/sign_to_prediction_index_map.json', 'r') as f:\n    data = json.load(f)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:09.242795Z","iopub.execute_input":"2023-04-30T20:13:09.243677Z","iopub.status.idle":"2023-04-30T20:13:09.255628Z","shell.execute_reply.started":"2023-04-30T20:13:09.243638Z","shell.execute_reply":"2023-04-30T20:13:09.254431Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tqdm import tqdm\nimport os\nimport torch","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:09.257107Z","iopub.execute_input":"2023-04-30T20:13:09.257621Z","iopub.status.idle":"2023-04-30T20:13:09.263703Z","shell.execute_reply.started":"2023-04-30T20:13:09.257580Z","shell.execute_reply":"2023-04-30T20:13:09.262455Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ROWS_PER_FRAME = 543\ndef load_relevant_data_subset(pq_path):\n    data_columns = ['x', 'y', 'z']\n    data = pd.read_parquet(pq_path, columns=data_columns)\n    n_frames = int(len(data) / 543)\n    data = data.values.reshape(n_frames, 543, len(data_columns))\n    return data.astype(np.float32)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:09.265121Z","iopub.execute_input":"2023-04-30T20:13:09.265957Z","iopub.status.idle":"2023-04-30T20:13:09.274283Z","shell.execute_reply.started":"2023-04-30T20:13:09.265920Z","shell.execute_reply":"2023-04-30T20:13:09.272305Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import Dataset\nfrom torch.utils.data import random_split\nfrom torch.utils.data import DataLoader\n","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:09.275649Z","iopub.execute_input":"2023-04-30T20:13:09.277403Z","iopub.status.idle":"2023-04-30T20:13:09.283449Z","shell.execute_reply.started":"2023-04-30T20:13:09.277354Z","shell.execute_reply":"2023-04-30T20:13:09.282327Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tr = pd.DataFrame(np.load('/kaggle/input/isrl-basic-data-mean/feature_data.npy'))\nout = pd.DataFrame(np.load('/kaggle/input/isrl-basic-data-mean/feature_labels.npy'))","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:09.284795Z","iopub.execute_input":"2023-04-30T20:13:09.285854Z","iopub.status.idle":"2023-04-30T20:13:31.886500Z","shell.execute_reply.started":"2023-04-30T20:13:09.285817Z","shell.execute_reply":"2023-04-30T20:13:31.885253Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class data(Dataset):\n    def __init__(self, df, label):\n        self.df = df\n        self.label = label\n        \n    def __getitem__(self, index):\n        inp = torch.tensor(self.df.values[index]).float()    \n        target = torch.tensor(self.label.values[index]).long()\n        return inp, target\n    \n    def __len__(self):\n        return len(self.df)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:31.888005Z","iopub.execute_input":"2023-04-30T20:13:31.888401Z","iopub.status.idle":"2023-04-30T20:13:31.895380Z","shell.execute_reply.started":"2023-04-30T20:13:31.888358Z","shell.execute_reply":"2023-04-30T20:13:31.894314Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tr_d = data(tr, out)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:31.897601Z","iopub.execute_input":"2023-04-30T20:13:31.898294Z","iopub.status.idle":"2023-04-30T20:13:31.914331Z","shell.execute_reply.started":"2023-04-30T20:13:31.898257Z","shell.execute_reply":"2023-04-30T20:13:31.913021Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tr_d[0]","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:31.916161Z","iopub.execute_input":"2023-04-30T20:13:31.917226Z","iopub.status.idle":"2023-04-30T20:13:32.093340Z","shell.execute_reply.started":"2023-04-30T20:13:31.917189Z","shell.execute_reply":"2023-04-30T20:13:32.092274Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tr_ds, val_ds = random_split(tr_d, [0.80, 0.20])\ntr_dl = DataLoader(tr_ds, batch_size=256, shuffle=True, num_workers=8, pin_memory=True)\nval_dl = DataLoader(val_ds, batch_size=512, num_workers=8, pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:32.094850Z","iopub.execute_input":"2023-04-30T20:13:32.095491Z","iopub.status.idle":"2023-04-30T20:13:32.113081Z","shell.execute_reply.started":"2023-04-30T20:13:32.095450Z","shell.execute_reply":"2023-04-30T20:13:32.112161Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_default_device():\n    if torch.cuda.is_available():\n        return torch.device('cuda')\n    else:\n        return torch.device('cpu')\n    \ndef to_device(data, device):\n    if isinstance(data, (list,tuple)):\n        return [to_device(x, device) for x in data]\n    return data.to(device, non_blocking=True)\n\nclass DeviceDataLoader():\n    def __init__(self, dl, device):\n        self.dl = dl\n        self.device = device\n        \n    def __iter__(self):\n        \"\"\"Yield a batch of data after moving it to device\"\"\"\n        for b in self.dl: \n            yield to_device(b, self.device)\n\n    def __len__(self):\n        \"\"\"Number of batches\"\"\"\n        return len(self.dl)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:32.116377Z","iopub.execute_input":"2023-04-30T20:13:32.116674Z","iopub.status.idle":"2023-04-30T20:13:32.124729Z","shell.execute_reply.started":"2023-04-30T20:13:32.116648Z","shell.execute_reply":"2023-04-30T20:13:32.123669Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = get_default_device()\ndevice","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:32.126036Z","iopub.execute_input":"2023-04-30T20:13:32.127041Z","iopub.status.idle":"2023-04-30T20:13:32.227875Z","shell.execute_reply.started":"2023-04-30T20:13:32.127003Z","shell.execute_reply":"2023-04-30T20:13:32.226700Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tr_dl = DeviceDataLoader(tr_dl, device)\nval_dl = DeviceDataLoader(val_dl, device)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:32.229807Z","iopub.execute_input":"2023-04-30T20:13:32.230686Z","iopub.status.idle":"2023-04-30T20:13:32.239362Z","shell.execute_reply.started":"2023-04-30T20:13:32.230552Z","shell.execute_reply":"2023-04-30T20:13:32.238595Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch.nn as nn","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:32.240882Z","iopub.execute_input":"2023-04-30T20:13:32.241868Z","iopub.status.idle":"2023-04-30T20:13:32.250126Z","shell.execute_reply.started":"2023-04-30T20:13:32.241830Z","shell.execute_reply":"2023-04-30T20:13:32.249197Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch.nn.functional as F\n","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:32.251884Z","iopub.execute_input":"2023-04-30T20:13:32.252647Z","iopub.status.idle":"2023-04-30T20:13:32.261378Z","shell.execute_reply.started":"2023-04-30T20:13:32.252607Z","shell.execute_reply":"2023-04-30T20:13:32.260450Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def accuracy(outputs, labels):\n    _, preds = torch.max(outputs, dim=1)\n    return torch.tensor(torch.sum(preds == labels).item() / len(preds))","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:32.263358Z","iopub.execute_input":"2023-04-30T20:13:32.264077Z","iopub.status.idle":"2023-04-30T20:13:32.273374Z","shell.execute_reply.started":"2023-04-30T20:13:32.264041Z","shell.execute_reply":"2023-04-30T20:13:32.272273Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class lstm_model(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.linear1 = nn.Linear(3258, 1024)\n        self.linear2 = nn.Linear(1024, 512)\n        self.linear3= nn.Linear(512, 250 )\n        self.dropout = nn.Dropout(0.2)\n        self.los = nn.CrossEntropyLoss()\n        \n    def forward(self, x):\n        out= self.linear1(x)\n        out = self.dropout(out)\n        out = self.linear2(out)\n        out = self.dropout(out)\n        out = self.linear3(out)\n        return out\n\n    def training_step(self, batch):\n        inputs, targets = batch\n        outputs = self(inputs)\n        loss = self.los(outputs, targets.squeeze())\n        return loss\n\n    def validation_step(self, batch):\n        inputs, targets = batch\n        outputs = self(inputs)\n        loss = self.los(outputs, targets.squeeze())\n        #acc = accuracy(out, idx)           \n        return {'val_loss': loss.detach()}#, 'val_acc': acc.detach()}\n\n    def validation_epoch_end(self, outputs):\n        batch_losses = [x['val_loss'] for x in outputs]\n        epoch_loss = torch.stack(batch_losses).mean()   # Combine losses\n        #batch_accs = [x['val_acc'] for x in outputs]\n       # epoch_acc = torch.stack(batch_accs).mean()      # Combine accuracies\n        return {'val_loss': epoch_loss.item()}#, 'val_acc': epoch_acc.item()}\n\n    def epoch_end(self, epoch, result):\n       print(\"Epoch [{}], last_lr: {:.5f}, train_loss: {:.4f}, val_loss: {:.4f}\".format(\n            epoch, result['lrs'][-1], result['train_loss'], result['val_loss']))","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:32.276359Z","iopub.execute_input":"2023-04-30T20:13:32.277163Z","iopub.status.idle":"2023-04-30T20:13:32.288697Z","shell.execute_reply.started":"2023-04-30T20:13:32.277126Z","shell.execute_reply":"2023-04-30T20:13:32.287652Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = to_device(lstm_model(), device)\nmodel","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:32.291464Z","iopub.execute_input":"2023-04-30T20:13:32.291826Z","iopub.status.idle":"2023-04-30T20:13:36.420638Z","shell.execute_reply.started":"2023-04-30T20:13:32.291790Z","shell.execute_reply":"2023-04-30T20:13:36.419394Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"@torch.no_grad()\ndef evaluate(model, val_loader):\n    model.eval()\n    outputs = [model.validation_step(batch) for batch in val_loader]\n    return model.validation_epoch_end(outputs)\n\ndef get_lr(optimizer):\n    for param_group in optimizer.param_groups:\n        return param_group['lr']\n\ndef fit_one_cycle(epochs, max_lr, model, train_loader, val_loader, \n                  weight_decay=0, grad_clip=None, opt_func=torch.optim.SGD):\n    torch.cuda.empty_cache()\n    history = []\n    \n    # Set up cutom optimizer with weight decay\n    optimizer = opt_func(model.parameters(), max_lr, weight_decay=weight_decay)\n    # Set up one-cycle learning rate scheduler\n    sched = torch.optim.lr_scheduler.OneCycleLR(optimizer, max_lr, epochs=epochs, \n                                                steps_per_epoch=len(train_loader))\n    \n    for epoch in range(epochs):\n        # Training Phase \n        model.train()\n        train_losses = []\n        lrs = []\n        for batch in train_loader:\n            loss = model.training_step(batch)\n            train_losses.append(loss)\n            loss.backward()\n            \n            # Gradient clipping\n            if grad_clip: \n                nn.utils.clip_grad_value_(model.parameters(), grad_clip)\n            \n            optimizer.step()\n            optimizer.zero_grad()\n            \n            # Record & update learning rate\n            lrs.append(get_lr(optimizer))\n            sched.step()\n        \n        # Validation phase\n        result = evaluate(model, val_loader)\n        result['train_loss'] = torch.stack(train_losses).mean().item()\n        result['lrs'] = lrs\n        model.epoch_end(epoch, result)\n        history.append(result)\n    return history","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:36.422232Z","iopub.execute_input":"2023-04-30T20:13:36.422879Z","iopub.status.idle":"2023-04-30T20:13:36.435107Z","shell.execute_reply.started":"2023-04-30T20:13:36.422841Z","shell.execute_reply":"2023-04-30T20:13:36.434067Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = [evaluate(model, val_dl)]\nhistory","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:36.441856Z","iopub.execute_input":"2023-04-30T20:13:36.442146Z","iopub.status.idle":"2023-04-30T20:13:41.250165Z","shell.execute_reply.started":"2023-04-30T20:13:36.442119Z","shell.execute_reply":"2023-04-30T20:13:41.249012Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"epochs = 150\nmax_lr = 0.006\ngrad_clip = 0.1\nweight_decay = 1e-6\nopt_func = torch.optim.Adam","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:41.253781Z","iopub.execute_input":"2023-04-30T20:13:41.254092Z","iopub.status.idle":"2023-04-30T20:13:41.259448Z","shell.execute_reply.started":"2023-04-30T20:13:41.254062Z","shell.execute_reply":"2023-04-30T20:13:41.258436Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\nhistory += fit_one_cycle(epochs, max_lr, model, tr_dl, val_dl, \n                             grad_clip=grad_clip, \n                             weight_decay=weight_decay, \n                             opt_func=opt_func)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:13:41.261094Z","iopub.execute_input":"2023-04-30T20:13:41.262812Z","iopub.status.idle":"2023-04-30T20:32:42.622756Z","shell.execute_reply.started":"2023-04-30T20:13:41.262769Z","shell.execute_reply":"2023-04-30T20:32:42.620757Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_accuracies(history):\n    accuracies = [x['val_loss'] for x in history]\n    plt.plot(accuracies, '-x')\n    plt.xlabel('epoch')\n    plt.ylabel('accuracy')\n    plt.title('Accuracy vs. No. of epochs');","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:32:42.624753Z","iopub.execute_input":"2023-04-30T20:32:42.625613Z","iopub.status.idle":"2023-04-30T20:32:42.631871Z","shell.execute_reply.started":"2023-04-30T20:32:42.625564Z","shell.execute_reply":"2023-04-30T20:32:42.630753Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install onnx-tf\n!pip install tflite-runtime","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:32:43.116061Z","iopub.execute_input":"2023-04-30T20:32:43.116446Z","iopub.status.idle":"2023-04-30T20:33:08.227402Z","shell.execute_reply.started":"2023-04-30T20:32:43.116407Z","shell.execute_reply":"2023-04-30T20:33:08.226105Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_input = torch.rand((50, 543, 3))\nonnx_feat_gen_path = 'feature_gen.onnx'\n\nd_gen.eval()\n\ntorch.onnx.export(\n    d_gen,                  # PyTorch Model\n    sample_input,                    # Input tensor\n    onnx_feat_gen_path,        # Output file (eg. 'output_model.onnx')\n    opset_version=12,       # Operator support version\n    input_names=['input'],   # Input tensor name (arbitary)\n    output_names=['output'], # Output tensor name (arbitary)\n    dynamic_axes={\n        'input' : {0: 'input'}\n    }\n)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:33:08.230914Z","iopub.execute_input":"2023-04-30T20:33:08.231296Z","iopub.status.idle":"2023-04-30T20:33:08.470810Z","shell.execute_reply.started":"2023-04-30T20:33:08.231262Z","shell.execute_reply":"2023-04-30T20:33:08.469729Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_input = torch.rand((1, 3258)).cuda()\nonnx_model_path = 'asl_model.onnx'\n\nmodel.eval()\n\ntorch.onnx.export(\n    model,                  # PyTorch Model\n    sample_input,                    # Input tensor\n    onnx_model_path,        # Output file (eg. 'output_model.onnx')\n    opset_version=12,       # Operator support version\n    input_names=['input'],   # Input tensor name (arbitary)\n    output_names=['output'], # Output tensor name (arbitary)\n    dynamic_axes={\n        'input' : {0: 'input'}\n    }\n)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:33:08.474067Z","iopub.execute_input":"2023-04-30T20:33:08.474957Z","iopub.status.idle":"2023-04-30T20:33:08.635603Z","shell.execute_reply.started":"2023-04-30T20:33:08.474911Z","shell.execute_reply":"2023-04-30T20:33:08.634413Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import onnx\nfrom onnx_tf.backend import prepare\n\n\ntf_feat_gen_path = '/kaggle/working/tf_feat_gen'\nonnx_feat_gen = onnx.load(onnx_feat_gen_path)\ntf_rep = prepare(onnx_feat_gen)\ntf_rep.export_graph(tf_feat_gen_path)\n\n\ntf_model_path = '/kaggle/working/tf_model'\nonnx_model = onnx.load(onnx_model_path)\ntf_rep = prepare(onnx_model)\ntf_rep.export_graph(tf_model_path)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:33:08.637425Z","iopub.execute_input":"2023-04-30T20:33:08.638242Z","iopub.status.idle":"2023-04-30T20:33:39.643398Z","shell.execute_reply.started":"2023-04-30T20:33:08.638186Z","shell.execute_reply":"2023-04-30T20:33:39.642252Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\n\nclass ASLInferModel(tf.Module):\n    def __init__(self):\n        super(ASLInferModel, self).__init__()\n        self.feature_gen = tf.saved_model.load(tf_feat_gen_path)\n        self.model = tf.saved_model.load(tf_model_path)\n        self.feature_gen.trainable = False\n        self.model.trainable = False\n    \n    @tf.function(input_signature=[\n      tf.TensorSpec(shape=[None, 543, 3], dtype=tf.float32, name='inputs')\n    ])\n    def call(self, input):\n        output_tensors = {}\n        features = self.feature_gen(**{'input': input})['output']\n        output_tensors['outputs'] = self.model(**{'input': tf.expand_dims(features, 0)})['output'][0,:]\n        return output_tensors\n    \n    \nmytfmodel = ASLInferModel()\ntf.saved_model.save(mytfmodel, '/kaggle/working/tf_infer_model', signatures={'serving_default': mytfmodel.call})","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:33:39.645694Z","iopub.execute_input":"2023-04-30T20:33:39.646404Z","iopub.status.idle":"2023-04-30T20:33:40.611438Z","shell.execute_reply.started":"2023-04-30T20:33:39.646349Z","shell.execute_reply":"2023-04-30T20:33:40.610340Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tf_infer_model_path = '/kaggle/working/tf_infer_model'\nconverter = tf.lite.TFLiteConverter.from_saved_model(tf_infer_model_path)\ntflite_model = converter.convert()\n\ntflite_model_path = 'model.tflite'\n\n# Save the model\nwith open(tflite_model_path, 'wb') as f:\n    f.write(tflite_model)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:33:40.613107Z","iopub.execute_input":"2023-04-30T20:33:40.613530Z","iopub.status.idle":"2023-04-30T20:33:42.429508Z","shell.execute_reply.started":"2023-04-30T20:33:40.613489Z","shell.execute_reply":"2023-04-30T20:33:42.428243Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ROWS_PER_FRAME = 543  # number of landmarks per frame\npq_path = \"/kaggle/input/asl-signs/train_landmark_files/18796/1001373962.parquet\"\n\n\nimport tflite_runtime.interpreter as tflite\ninterpreter = tflite.Interpreter(tflite_model_path)\ninterpreter.allocate_tensors()\n\nfound_signatures = list(interpreter.get_signature_list().keys())\n\n# if REQUIRED_SIGNATURE not in found_signatures:\n#     raise KernelEvalException('Required input signature not found.')\n\nprediction_fn = interpreter.get_signature_runner(\"serving_default\")\noutput = prediction_fn(inputs=load_relevant_data_subset(pq_path))\nsign = np.argmax(output[\"outputs\"])\n\nprint(sign, output[\"outputs\"].shape)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:33:42.431038Z","iopub.execute_input":"2023-04-30T20:33:42.431748Z","iopub.status.idle":"2023-04-30T20:33:42.477780Z","shell.execute_reply.started":"2023-04-30T20:33:42.431706Z","shell.execute_reply":"2023-04-30T20:33:42.476622Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!zip submission.zip $tflite_model_path","metadata":{"execution":{"iopub.status.busy":"2023-04-30T20:33:42.479217Z","iopub.execute_input":"2023-04-30T20:33:42.479875Z","iopub.status.idle":"2023-04-30T20:33:44.381995Z","shell.execute_reply.started":"2023-04-30T20:33:42.479834Z","shell.execute_reply":"2023-04-30T20:33:44.380718Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}