{"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":"### Install dependency","metadata":{}},{"cell_type":"code","source":"!pip install onnx_tf","metadata":{"execution":{"iopub.status.busy":"2023-05-25T00:00:47.921029Z","iopub.execute_input":"2023-05-25T00:00:47.922762Z","iopub.status.idle":"2023-05-25T00:01:02.04499Z","shell.execute_reply.started":"2023-05-25T00:00:47.922697Z","shell.execute_reply":"2023-05-25T00:01:02.042545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport sys\nimport json\nimport numpy as np\nimport pandas as pd\nimport torch\nimport torch.nn as nn\nimport onnx\nimport onnx_tf\nimport tensorflow as tf\n\nimport warnings\nwarnings.filterwarnings(action='ignore')","metadata":{"execution":{"iopub.status.busy":"2023-05-25T00:01:02.049784Z","iopub.execute_input":"2023-05-25T00:01:02.051552Z","iopub.status.idle":"2023-05-25T00:01:02.062421Z","shell.execute_reply.started":"2023-05-25T00:01:02.051467Z","shell.execute_reply":"2023-05-25T00:01:02.060136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models_path = \"/kaggle/working/\"\nonnx_model_path = os.path.join(models_path, 'model.onnx')\ntf_model_path = os.path.join(models_path, 'tf_model')\ntflite_model_path = os.path.join(models_path, 'model.tflite')\ninference_args_path = os.path.join(models_path, 'inference_args.json')\n","metadata":{"execution":{"iopub.status.busy":"2023-05-25T00:01:02.065075Z","iopub.execute_input":"2023-05-25T00:01:02.065621Z","iopub.status.idle":"2023-05-25T00:01:02.081718Z","shell.execute_reply.started":"2023-05-25T00:01:02.065552Z","shell.execute_reply":"2023-05-25T00:01:02.079674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Model(nn.Module):\n    def __init__(self, in_dim, out_dim):\n        super(Model, self).__init__()\n        self.linear = nn.Linear(in_dim, out_dim)\n\n    def forward(self, h):\n        h = torch.tensor(h)\n        h[torch.isnan(h)] = 0\n       \n        # print(h.shape)\n        h = self.linear(h)\n        return h\n","metadata":{"execution":{"iopub.status.busy":"2023-05-25T00:01:02.086382Z","iopub.execute_input":"2023-05-25T00:01:02.087679Z","iopub.status.idle":"2023-05-25T00:01:02.101495Z","shell.execute_reply.started":"2023-05-25T00:01:02.087605Z","shell.execute_reply":"2023-05-25T00:01:02.099856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selected_columns = ['x_right_hand_0', 'y_right_hand_0', 'z_right_hand_0',\n                    'x_right_hand_1', 'y_right_hand_1', 'z_right_hand_1',\n                    'x_right_hand_2', 'y_right_hand_2', 'z_right_hand_2',\n                    'x_right_hand_3', 'y_right_hand_3', 'z_right_hand_3',\n                    'x_right_hand_4', 'y_right_hand_4', 'z_right_hand_4',\n                    'x_right_hand_5', 'y_right_hand_5', 'z_right_hand_5',\n                    'x_right_hand_6', 'y_right_hand_6', 'z_right_hand_6',\n                    'x_right_hand_7', 'y_right_hand_7', 'z_right_hand_7',\n                    'x_right_hand_8', 'y_right_hand_8', 'z_right_hand_8',\n                    'x_right_hand_9', 'y_right_hand_9', 'z_right_hand_9',\n                    'x_right_hand_10', 'y_right_hand_10', 'z_right_hand_10',\n                    'x_right_hand_11', 'y_right_hand_11', 'z_right_hand_11',\n                    'x_right_hand_12', 'y_right_hand_12', 'z_right_hand_12',\n                    'x_right_hand_13', 'y_right_hand_13', 'z_right_hand_13',\n                    'x_right_hand_14', 'y_right_hand_14', 'z_right_hand_14',\n                    'x_right_hand_15', 'y_right_hand_15', 'z_right_hand_15',\n                    'x_right_hand_16', 'y_right_hand_16', 'z_right_hand_16',\n                    'x_right_hand_17', 'y_right_hand_17', 'z_right_hand_17',\n                    'x_right_hand_18', 'y_right_hand_18', 'z_right_hand_18',\n                    'x_right_hand_19', 'y_right_hand_19', 'z_right_hand_19',\n                    'x_right_hand_20', 'y_right_hand_20', 'z_right_hand_20',\n                    ]\n\nselected_columns_dict = {\"selected_columns\": selected_columns}\n\nwith open(inference_args_path, \"w\") as f:\n    json.dump(selected_columns_dict, f)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-25T00:01:02.10388Z","iopub.execute_input":"2023-05-25T00:01:02.104499Z","iopub.status.idle":"2023-05-25T00:01:02.123905Z","shell.execute_reply.started":"2023-05-25T00:01:02.104415Z","shell.execute_reply":"2023-05-25T00:01:02.122604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_class = 59\nopset_version = 12\nNUM_FEATURES = len(selected_columns)\n\npq_path = \"/kaggle/input/asl-fingerspelling/train_landmarks/1019715464.parquet\"\n\nsample_input = pd.read_parquet(pq_path, columns=selected_columns)\nsample_input = sample_input.values\nprint(sample_input.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T00:01:02.126139Z","iopub.execute_input":"2023-05-25T00:01:02.127202Z","iopub.status.idle":"2023-05-25T00:01:02.363721Z","shell.execute_reply.started":"2023-05-25T00:01:02.127114Z","shell.execute_reply":"2023-05-25T00:01:02.362227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"converting from torch to onnx...\")\n\nmodel = Model(in_dim = NUM_FEATURES,\n              out_dim = n_class)\n\nsample_input = torch.tensor(sample_input)\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=opset_version, # Operator support version\n    input_names=['inputs'],      # Input tensor name (arbitary)\n    output_names=['outputs'],    # Output tensor name (arbitary)\n    dynamic_axes={\n        'inputs': {0: 'length',\n                   },\n        'outputs': {0: 'length',\n                    },\n\n    }\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T00:01:02.365211Z","iopub.execute_input":"2023-05-25T00:01:02.365667Z","iopub.status.idle":"2023-05-25T00:01:02.545994Z","shell.execute_reply.started":"2023-05-25T00:01:02.365625Z","shell.execute_reply":"2023-05-25T00:01:02.544385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"converting from onnx to tensorflow...\")\nonnx_model = onnx.load(onnx_model_path)\ntf_model = onnx_tf.backend.prepare(onnx_model)\ntf_model.export_graph(tf_model_path)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T00:01:02.549718Z","iopub.execute_input":"2023-05-25T00:01:02.550536Z","iopub.status.idle":"2023-05-25T00:01:02.808035Z","shell.execute_reply.started":"2023-05-25T00:01:02.550477Z","shell.execute_reply":"2023-05-25T00:01:02.806611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"converting from tensorflow to tflite\")\nconverter = tf.lite.TFLiteConverter.from_saved_model(tf_model_path)\nconverter.optimizations = [tf.lite.Optimize.DEFAULT]\nconverter.target_spec.supported_types = [tf.float16]\n# converter.target_spec.supported_types = [tf.float32]\nconverter.experimental_new_converter = True\nconverter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,\n                                       tf.lite.OpsSet.SELECT_TF_OPS]\ntflite_model = converter.convert()\n# Save the model\nwith open(tflite_model_path, 'wb') as f:\n    f.write(tflite_model)\nprint(\"converting finish!!\")\n# %%","metadata":{"execution":{"iopub.status.busy":"2023-05-25T00:01:02.810278Z","iopub.execute_input":"2023-05-25T00:01:02.811703Z","iopub.status.idle":"2023-05-25T00:01:02.961038Z","shell.execute_reply.started":"2023-05-25T00:01:02.811655Z","shell.execute_reply":"2023-05-25T00:01:02.959369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip submission.zip $tflite_model_path $inference_args_path\n","metadata":{"execution":{"iopub.status.busy":"2023-05-25T00:01:02.96462Z","iopub.execute_input":"2023-05-25T00:01:02.96546Z","iopub.status.idle":"2023-05-25T00:01:04.087512Z","shell.execute_reply.started":"2023-05-25T00:01:02.965412Z","shell.execute_reply":"2023-05-25T00:01:04.085547Z"},"trusted":true},"execution_count":null,"outputs":[]}]}