{"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":"markdown","source":"The torch to tflite code is copied from: https://www.kaggle.com/code/chack3/pytorch-simple-model\n\nThe output is copied from: https://www.kaggle.com/code/anokas/static-greedy-baseline-0-157-lb\n\nThis is just a simple model which returns a const prediction.\n\nI hope that the pytorch can be used to train complex models to summit.\n\nAnd I'm not sure if my way works.","metadata":{}},{"cell_type":"code","source":"# install libs\n!pip install onnx_tf","metadata":{"execution":{"iopub.status.busy":"2023-06-11T12:21:56.143054Z","iopub.execute_input":"2023-06-11T12:21:56.143474Z","iopub.status.idle":"2023-06-11T12:22:12.388065Z","shell.execute_reply.started":"2023-06-11T12:21:56.143434Z","shell.execute_reply":"2023-06-11T12:22:12.386138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import libs\nimport 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\n# import warnings\n# warnings.filterwarnings(action='ignore')","metadata":{"execution":{"iopub.status.busy":"2023-06-11T12:22:12.390799Z","iopub.execute_input":"2023-06-11T12:22:12.391241Z","iopub.status.idle":"2023-06-11T12:22:25.175127Z","shell.execute_reply.started":"2023-06-11T12:22:12.391192Z","shell.execute_reply":"2023-06-11T12:22:25.173887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prepare\nFEATURES = json.load(open(\"/kaggle/input/asl-fingerspelling-summit/inference_args.json\"))[\"selected_columns\"]\nNUM_FEATURES = len(FEATURES)\nNUM_CLASS = 59","metadata":{"execution":{"iopub.status.busy":"2023-06-11T12:22:25.176667Z","iopub.execute_input":"2023-06-11T12:22:25.177766Z","iopub.status.idle":"2023-06-11T12:22:25.188804Z","shell.execute_reply.started":"2023-06-11T12:22:25.177722Z","shell.execute_reply":"2023-06-11T12:22:25.187402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define Model\nclass Model(torch.nn.Module):\n    def __init__(self, in_dim, out_dim, const_pred):\n        super(Model, self).__init__()\n        self.linear = torch.nn.Linear(in_dim, out_dim)\n        self.__constant_matrix = torch.tensor(const_pred)\n\n    def forward(self, h):\n        h = torch.tensor(h, dtype=torch.float32)\n        h[torch.isnan(h)] = 0\n        h = self.linear(h)\n        return self.__constant_matrix + h[0].expand(self.__constant_matrix.shape[0], -1)*0.1","metadata":{"execution":{"iopub.status.busy":"2023-06-11T12:22:25.192132Z","iopub.execute_input":"2023-06-11T12:22:25.192554Z","iopub.status.idle":"2023-06-11T12:22:25.20302Z","shell.execute_reply.started":"2023-06-11T12:22:25.192508Z","shell.execute_reply":"2023-06-11T12:22:25.20125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"converting from torch to onnx...\")\nopset_version = 12\npq_path = \"/kaggle/input/asl-fingerspelling/train_landmarks/1019715464.parquet\"\nsample_input = pd.read_parquet(pq_path, columns=FEATURES)\nsample_input = sample_input.values\nprint(sample_input.shape)\nmodel = torch.load(\"/kaggle/input/asl-fingerspelling-summit/model.pth\")\nsample_input = torch.tensor(sample_input)\ntorch.onnx.export(\n    model,                       # PyTorch Model\n    sample_input,                # Input tensor\n    \"/kaggle/working/model.onnx\",             # 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        'outputs': {0: 'length', },\n    }\n)\nprint(\"converting done.\")","metadata":{"execution":{"iopub.status.busy":"2023-06-11T12:22:25.204972Z","iopub.execute_input":"2023-06-11T12:22:25.205356Z","iopub.status.idle":"2023-06-11T12:22:26.523322Z","shell.execute_reply.started":"2023-06-11T12:22:25.205318Z","shell.execute_reply":"2023-06-11T12:22:26.522307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"converting from onnx to tensorflow...\")\nonnx_model = onnx.load(\"/kaggle/working/model.onnx\")\ntf_model = onnx_tf.backend.prepare(onnx_model)\ntf_model.export_graph(\"/kaggle/working/tf_model\")\nprint(\"converting done.\")","metadata":{"execution":{"iopub.status.busy":"2023-06-11T12:22:26.524868Z","iopub.execute_input":"2023-06-11T12:22:26.525517Z","iopub.status.idle":"2023-06-11T12:22:32.683412Z","shell.execute_reply.started":"2023-06-11T12:22:26.525477Z","shell.execute_reply":"2023-06-11T12:22:32.682129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"converting from tensorflow to tflite\")\nconverter = tf.lite.TFLiteConverter.from_saved_model(\"/kaggle/working/tf_model\")\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(\"/kaggle/working/model.tflite\", 'wb') as f:\n    f.write(tflite_model)\nprint(\"converting done.\")","metadata":{"execution":{"iopub.status.busy":"2023-06-11T12:22:32.685524Z","iopub.execute_input":"2023-06-11T12:22:32.686022Z","iopub.status.idle":"2023-06-11T12:22:33.128725Z","shell.execute_reply.started":"2023-06-11T12:22:32.685949Z","shell.execute_reply":"2023-06-11T12:22:33.12744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip submission.zip /kaggle/working/model.tflite /kaggle/input/asl-fingerspelling-summit/inference_args.json","metadata":{"execution":{"iopub.status.busy":"2023-06-11T12:22:33.130687Z","iopub.execute_input":"2023-06-11T12:22:33.131097Z","iopub.status.idle":"2023-06-11T12:22:35.398446Z","shell.execute_reply.started":"2023-06-11T12:22:33.131059Z","shell.execute_reply":"2023-06-11T12:22:35.39681Z"},"trusted":true},"execution_count":null,"outputs":[]}]}