{"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":"code","source":"!pip install onnxruntime","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-12-25T23:09:37.584295Z","iopub.execute_input":"2022-12-25T23:09:37.584832Z","iopub.status.idle":"2022-12-25T23:09:53.445387Z","shell.execute_reply.started":"2022-12-25T23:09:37.584723Z","shell.execute_reply":"2022-12-25T23:09:53.443983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import onnxruntime\nimport numpy as np\nimport pandas as pd\nfrom tqdm.auto import tqdm\nimport math","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-25T23:09:53.448718Z","iopub.execute_input":"2022-12-25T23:09:53.450523Z","iopub.status.idle":"2022-12-25T23:09:53.621238Z","shell.execute_reply.started":"2022-12-25T23:09:53.450436Z","shell.execute_reply":"2022-12-25T23:09:53.619804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntest_X = test_df.values.astype(np.float32)","metadata":{"execution":{"iopub.status.busy":"2022-12-25T23:09:53.624709Z","iopub.execute_input":"2022-12-25T23:09:53.625368Z","iopub.status.idle":"2022-12-25T23:09:56.174193Z","shell.execute_reply.started":"2022-12-25T23:09:53.625316Z","shell.execute_reply":"2022-12-25T23:09:56.172536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ort_session = onnxruntime.InferenceSession(\"/kaggle/input/pytorch-lightning-onnx-export/baseline.onnx\")\ninput_name = ort_session.get_inputs()[0].name","metadata":{"execution":{"iopub.status.busy":"2022-12-25T23:09:56.177467Z","iopub.execute_input":"2022-12-25T23:09:56.178945Z","iopub.status.idle":"2022-12-25T23:09:56.195879Z","shell.execute_reply.started":"2022-12-25T23:09:56.178873Z","shell.execute_reply":"2022-12-25T23:09:56.194417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = []\nfor x in tqdm(test_X):\n    test_pred.extend(ort_session.run(None, {input_name: np.reshape(x, (-1, 1, 28*28))})[0])\ntest_pred = np.stack(test_pred)\ntest_pred.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-25T23:09:56.197426Z","iopub.execute_input":"2022-12-25T23:09:56.197761Z","iopub.status.idle":"2022-12-25T23:09:59.674558Z","shell.execute_reply.started":"2022-12-25T23:09:56.197731Z","shell.execute_reply":"2022-12-25T23:09:59.673434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Batch mode","metadata":{}},{"cell_type":"code","source":"batch_size = 128\ntest_pred = []\nfor batch in tqdm(np.array_split(test_X, math.ceil(len(test_X)/batch_size), axis=0)):\n    test_pred.extend(ort_session.run(None, {input_name: np.reshape(batch, (-1, 1, 28*28))})[0])\ntest_pred = np.stack(test_pred)\ntest_pred.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-25T23:09:59.676258Z","iopub.execute_input":"2022-12-25T23:09:59.676616Z","iopub.status.idle":"2022-12-25T23:10:02.412082Z","shell.execute_reply.started":"2022-12-25T23:09:59.676583Z","shell.execute_reply":"2022-12-25T23:10:02.410637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = np.argmax(test_pred, -1)\ntest_pred","metadata":{"execution":{"iopub.status.busy":"2022-12-25T23:10:02.413624Z","iopub.execute_input":"2022-12-25T23:10:02.414128Z","iopub.status.idle":"2022-12-25T23:10:02.424907Z","shell.execute_reply.started":"2022-12-25T23:10:02.414087Z","shell.execute_reply":"2022-12-25T23:10:02.423450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n    \"ImageId\": range(1, len(test_pred)+1),\n    \"Label\": test_pred\n})\nsubmission.to_csv(\"submission.csv\", index=False)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-12-25T23:10:02.426609Z","iopub.execute_input":"2022-12-25T23:10:02.429324Z","iopub.status.idle":"2022-12-25T23:10:02.482999Z","shell.execute_reply.started":"2022-12-25T23:10:02.429276Z","shell.execute_reply":"2022-12-25T23:10:02.481858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check if sumbission is still the same","metadata":{}},{"cell_type":"code","source":"submission_torch = pd.read_csv('/kaggle/input/pytorch-lightning-onnx-export/submission.csv')\n(submission_torch['Label'] == submission['Label']).mean()","metadata":{"execution":{"iopub.status.busy":"2022-12-25T23:12:08.956988Z","iopub.execute_input":"2022-12-25T23:12:08.957390Z","iopub.status.idle":"2022-12-25T23:12:08.977069Z","shell.execute_reply.started":"2022-12-25T23:12:08.957354Z","shell.execute_reply":"2022-12-25T23:12:08.976026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}