{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":102335,"databundleVersionId":12518947,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":12013393,"sourceType":"datasetVersion","datasetId":7555945}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# About this notebook\n\nI've shared how to create NumPy image files in H5 format in this notebook, \n\nhttps://www.kaggle.com/code/chumajin/cmi3-make-2d-tof-numpyimages?scriptVersionId=242837802\n\nand the dataset is also available here. \n\nhttps://www.kaggle.com/datasets/chumajin/cmi3-2d-tof-numpy-images\n\n\nThis notebook explains how to use them.\n","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)\nimport os\nimport polars as pl\nimport h5py\nfrom torch.utils.data import DataLoader, TensorDataset,Dataset\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-05-31T05:49:20.173464Z","iopub.execute_input":"2025-05-31T05:49:20.174091Z","iopub.status.idle":"2025-05-31T05:49:20.177931Z","shell.execute_reply.started":"2025-05-31T05:49:20.174067Z","shell.execute_reply":"2025-05-31T05:49:20.176904Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pl.read_csv(\"/kaggle/input/cmi-detect-behavior-with-sensor-data/train.csv\")\ntrain.head(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T05:47:26.925085Z","iopub.execute_input":"2025-05-31T05:47:26.925569Z","iopub.status.idle":"2025-05-31T05:47:39.195210Z","shell.execute_reply.started":"2025-05-31T05:47:26.925543Z","shell.execute_reply":"2025-05-31T05:47:39.194539Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# basic","metadata":{}},{"cell_type":"code","source":"images = h5py.File(\"/kaggle/input/cmi3-2d-tof-numpy-images/2dimages.h5\",\"r\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T05:48:12.809711Z","iopub.execute_input":"2025-05-31T05:48:12.810014Z","iopub.status.idle":"2025-05-31T05:48:12.822204Z","shell.execute_reply.started":"2025-05-31T05:48:12.809991Z","shell.execute_reply":"2025-05-31T05:48:12.821409Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rowid = \"SEQ_000007_000001\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T05:48:18.601652Z","iopub.execute_input":"2025-05-31T05:48:18.601917Z","iopub.status.idle":"2025-05-31T05:48:18.606233Z","shell.execute_reply.started":"2025-05-31T05:48:18.601898Z","shell.execute_reply":"2025-05-31T05:48:18.605391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images[rowid][:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T05:48:25.964866Z","iopub.execute_input":"2025-05-31T05:48:25.965633Z","iopub.status.idle":"2025-05-31T05:48:26.094639Z","shell.execute_reply.started":"2025-05-31T05:48:25.965606Z","shell.execute_reply":"2025-05-31T05:48:26.093760Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for a in range(5):\n    plt.figure(figsize=(7,5))\n    sns.heatmap(images[rowid][:][a])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T05:49:23.885440Z","iopub.execute_input":"2025-05-31T05:49:23.885772Z","iopub.status.idle":"2025-05-31T05:49:24.849607Z","shell.execute_reply.started":"2025-05-31T05:49:23.885750Z","shell.execute_reply":"2025-05-31T05:49:24.848921Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images.close()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T05:50:13.808924Z","iopub.execute_input":"2025-05-31T05:50:13.809187Z","iopub.status.idle":"2025-05-31T05:50:13.818160Z","shell.execute_reply.started":"2025-05-31T05:50:13.809168Z","shell.execute_reply":"2025-05-31T05:50:13.817470Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 2. pytorch dataset","metadata":{}},{"cell_type":"code","source":"class PytorchDataSet(Dataset):\n    \n    def __init__(self,df,h5path):\n\n        self.df = df\n\n        self.rowid = self.df[\"row_id\"]\n        self.target = self.df[\"gesture\"]\n\n        self.images = h5py.File(h5path,\"r\")\n\n\n    def __len__(self):\n        \n        return len(self.df)\n    \n    def __getitem__(self,idx):\n\n        rowid = self.rowid[idx]\n        image = self.images[rowid][:]\n        \n        return {\"image\":image,\"target\":self.target[idx]}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T05:50:16.255819Z","iopub.execute_input":"2025-05-31T05:50:16.256060Z","iopub.status.idle":"2025-05-31T05:50:16.261305Z","shell.execute_reply.started":"2025-05-31T05:50:16.256045Z","shell.execute_reply":"2025-05-31T05:50:16.260529Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dataset = PytorchDataSet(train,'/kaggle/input/cmi3-2d-tof-numpy-images/2dimages.h5')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T05:50:16.553984Z","iopub.execute_input":"2025-05-31T05:50:16.554863Z","iopub.status.idle":"2025-05-31T05:50:16.569725Z","shell.execute_reply.started":"2025-05-31T05:50:16.554841Z","shell.execute_reply":"2025-05-31T05:50:16.568668Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dataset[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T05:50:22.076330Z","iopub.execute_input":"2025-05-31T05:50:22.076642Z","iopub.status.idle":"2025-05-31T05:50:22.187011Z","shell.execute_reply.started":"2025-05-31T05:50:22.076622Z","shell.execute_reply":"2025-05-31T05:50:22.186089Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for a in range(5):\n    plt.figure(figsize=(7,5))\n    sns.heatmap(train_dataset[500][\"image\"][a])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T05:50:48.169435Z","iopub.execute_input":"2025-05-31T05:50:48.169740Z","iopub.status.idle":"2025-05-31T05:50:49.199966Z","shell.execute_reply.started":"2025-05-31T05:50:48.169723Z","shell.execute_reply":"2025-05-31T05:50:49.199094Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}