{"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":"## For smaller model sizes, loading and pre-processing the parquet files consumes a large part of the training time (every epoch). So, training can be sped up by processing the data before training, saving the tensors, and loading the tensors during training. ","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\n","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.019629,"end_time":"2023-03-16T06:59:44.482581","exception":false,"start_time":"2023-03-16T06:59:44.462952","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-19T09:01:38.086691Z","iopub.execute_input":"2023-03-19T09:01:38.087180Z","iopub.status.idle":"2023-03-19T09:01:38.093086Z","shell.execute_reply.started":"2023-03-19T09:01:38.087138Z","shell.execute_reply":"2023-03-19T09:01:38.092133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT=\"/kaggle/input/asl-signs\"","metadata":{"papermill":{"duration":0.012326,"end_time":"2023-03-16T06:59:55.049718","exception":false,"start_time":"2023-03-16T06:59:55.037392","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-19T09:01:39.230321Z","iopub.execute_input":"2023-03-19T09:01:39.230718Z","iopub.status.idle":"2023-03-19T09:01:39.236331Z","shell.execute_reply.started":"2023-03-19T09:01:39.230682Z","shell.execute_reply":"2023-03-19T09:01:39.235385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"papermill":{"duration":0.061919,"end_time":"2023-03-16T06:59:55.115449","exception":false,"start_time":"2023-03-16T06:59:55.05353","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-19T09:01:40.032894Z","iopub.execute_input":"2023-03-19T09:01:40.033610Z","iopub.status.idle":"2023-03-19T09:01:40.039047Z","shell.execute_reply.started":"2023-03-19T09:01:40.033568Z","shell.execute_reply":"2023-03-19T09:01:40.037774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(\"/kaggle/input/asl-signs/train.csv\")\nCLASSES = {C:i for i,C in enumerate(data.sign.unique())}","metadata":{"papermill":{"duration":0.194437,"end_time":"2023-03-16T06:59:55.313898","exception":false,"start_time":"2023-03-16T06:59:55.119461","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-19T09:01:40.518357Z","iopub.execute_input":"2023-03-19T09:01:40.519254Z","iopub.status.idle":"2023-03-19T09:01:40.673097Z","shell.execute_reply.started":"2023-03-19T09:01:40.519201Z","shell.execute_reply":"2023-03-19T09:01:40.669155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader, Dataset\nimport pytorch_lightning as pl\nimport numpy as np\n","metadata":{"papermill":{"duration":1.872741,"end_time":"2023-03-16T06:59:57.269779","exception":false,"start_time":"2023-03-16T06:59:55.397038","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-19T09:01:40.898586Z","iopub.execute_input":"2023-03-19T09:01:40.899000Z","iopub.status.idle":"2023-03-19T09:01:40.905676Z","shell.execute_reply.started":"2023-03-19T09:01:40.898964Z","shell.execute_reply":"2023-03-19T09:01:40.904329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"papermill":{"duration":0.015705,"end_time":"2023-03-16T06:59:57.460123","exception":false,"start_time":"2023-03-16T06:59:57.444418","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-19T09:01:41.156208Z","iopub.execute_input":"2023-03-19T09:01:41.156635Z","iopub.status.idle":"2023-03-19T09:01:41.163082Z","shell.execute_reply.started":"2023-03-19T09:01:41.156586Z","shell.execute_reply":"2023-03-19T09:01:41.161424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://www.kaggle.com/code/hengck23/lb-0-67-one-pytorch-transformer-solution\n\ndef pre_process(all_info):\n    frames = []\n    for _,info in all_info.groupby(\"frame\"):\n        xyz = torch.from_numpy(info[[\"x\",\"y\",\"z\"]].to_numpy())\n        xyz = xyz - xyz[~torch.isnan(xyz)].mean(0,keepdims=True) #noramlisation to common mean\n        xyz = xyz / xyz[~torch.isnan(xyz)].std(0, keepdims=True)\n\n        LIP = [\n            61, 185, 40, 39, 37, 0, 267, 269, 270, 409,\n            291, 146, 91, 181, 84, 17, 314, 405, 321, 375,\n            78, 191, 80, 81, 82, 13, 312, 311, 310, 415,\n            95, 88, 178, 87, 14, 317, 402, 318, 324, 308,\n        ]\n        #LHAND = np.arange(468, 489).tolist()\n        #RHAND = np.arange(522, 543).tolist()\n\n        lip = xyz[LIP]\n        lhand = xyz[468:489]\n        rhand = xyz[522:543]\n        xyz = torch.cat([ #(none, 82, 3)\n            lip,\n            lhand,\n            rhand,\n        ],0)\n        xyz[torch.isnan(xyz)] = 0\n        frames.append(xyz)\n    return torch.stack(frames)","metadata":{"papermill":{"duration":0.024064,"end_time":"2023-03-16T06:59:57.489934","exception":false,"start_time":"2023-03-16T06:59:57.46587","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-19T09:01:41.385087Z","iopub.execute_input":"2023-03-19T09:01:41.385489Z","iopub.status.idle":"2023-03-19T09:01:41.397475Z","shell.execute_reply.started":"2023-03-19T09:01:41.385454Z","shell.execute_reply":"2023-03-19T09:01:41.396276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomDataset(Dataset):\n    def __init__(self, datainfo,CLASSES,MAX_LEN=80,dims=[\"x\",\"y\",\"z\"]):\n        self.datainfo = datainfo.reset_index(drop=True)\n        self.CLASSES = CLASSES\n        self.MAX_LEN=MAX_LEN\n        self.CORD = len(dims)\n\n    def __len__(self):\n        return len(self.datainfo)\n\n    def __getitem__(self, idx):\n        # convert sequence to tensor and return as input/output pair\n        sample = pre_process(pd.read_parquet(os.path.join(ROOT,self.datainfo.path[idx])))\n        sample = torch.tensor(sample).float()\n        n, d,_ = sample.shape\n        sample = sample.reshape(n,-1)\n        mask = torch.ones(self.MAX_LEN)\n        if n < self.MAX_LEN:\n            padding_size = (self.MAX_LEN - n,d*self.CORD)\n            mask[n:] = 0\n            sample = torch.cat([sample,torch.zeros(padding_size)], 0)\n        elif n > self.MAX_LEN:\n            sample = sample[:self.MAX_LEN, :]\n        label = torch.tensor(self.CLASSES[self.datainfo.sign[idx]]).long()\n        return sample,mask,label","metadata":{"papermill":{"duration":0.023108,"end_time":"2023-03-16T06:59:57.518994","exception":false,"start_time":"2023-03-16T06:59:57.495886","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-19T09:01:41.642821Z","iopub.execute_input":"2023-03-19T09:01:41.643291Z","iopub.status.idle":"2023-03-19T09:01:41.654824Z","shell.execute_reply.started":"2023-03-19T09:01:41.643241Z","shell.execute_reply":"2023-03-19T09:01:41.653460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = CustomDataset(data,CLASSES)\ntrain_loader = DataLoader(train_dataset, batch_size=1, shuffle=False,pin_memory=True,num_workers=6)","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:01:41.932526Z","iopub.execute_input":"2023-03-19T09:01:41.932968Z","iopub.status.idle":"2023-03-19T09:01:41.941615Z","shell.execute_reply.started":"2023-03-19T09:01:41.932925Z","shell.execute_reply":"2023-03-19T09:01:41.940372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:01:42.211615Z","iopub.execute_input":"2023-03-19T09:01:42.212065Z","iopub.status.idle":"2023-03-19T09:01:42.217302Z","shell.execute_reply.started":"2023-03-19T09:01:42.212023Z","shell.execute_reply":"2023-03-19T09:01:42.216051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(\"data/tensors\",exist_ok=True)\nos.makedirs(\"data/masks\",exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:01:42.447877Z","iopub.execute_input":"2023-03-19T09:01:42.448321Z","iopub.status.idle":"2023-03-19T09:01:42.455046Z","shell.execute_reply.started":"2023-03-19T09:01:42.448281Z","shell.execute_reply":"2023-03-19T09:01:42.453612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for (inp,msk,lab),pth in tqdm(zip(train_loader,data[\"path\"]),total=len(data)):\n    torch.save(inp[0],os.path.join(\"data/tensors\",os.path.split(pth)[-1]))\n    torch.save(msk[0],os.path.join(\"data/masks\",os.path.split(pth)[-1]))    ","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:01:42.696456Z","iopub.execute_input":"2023-03-19T09:01:42.696934Z","iopub.status.idle":"2023-03-19T09:01:46.765133Z","shell.execute_reply.started":"2023-03-19T09:01:42.696899Z","shell.execute_reply":"2023-03-19T09:01:46.762944Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r all_data.zip ./data","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:02:18.038867Z","iopub.execute_input":"2023-03-19T09:02:18.039307Z","iopub.status.idle":"2023-03-19T09:02:19.328224Z","shell.execute_reply.started":"2023-03-19T09:02:18.039263Z","shell.execute_reply":"2023-03-19T09:02:19.326657Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf ./data","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:03:12.083145Z","iopub.execute_input":"2023-03-19T09:03:12.083582Z","iopub.status.idle":"2023-03-19T09:03:13.203768Z","shell.execute_reply.started":"2023-03-19T09:03:12.083535Z","shell.execute_reply":"2023-03-19T09:03:13.202268Z"},"trusted":true},"execution_count":null,"outputs":[]}]}