{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":31703,"databundleVersionId":2871752,"sourceType":"competition"},{"sourceId":2869358,"sourceType":"datasetVersion","datasetId":1757219},{"sourceId":6973853,"sourceType":"datasetVersion","datasetId":4007128}],"dockerImageVersionId":30146,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Train YOLOX on COTS dataset (PART 1 - TRAINING)\n\nThis notebook shows how to train custom object detection model (COTS dataset) on Kaggle. It could be good starting point for build own custom model based on YOLOX detector. Full github repository you can find here - [YOLOX](https://github.com/Megvii-BaseDetection/YOLOX)\n\n<div align = 'center'><img src='https://github.com/Megvii-BaseDetection/YOLOX/raw/main/assets/logo.png'/></div>\n\n**Steps covered in this notebook:**\n* Install YOLOX \n* Prepare COTS dataset for YOLOX object detection training\n* Download Pre-Trained Weights for YOLOX\n* Prepare configuration files\n* YOLOX training\n* Run YOLOX inference on test images\n* Export YOLOX weights for Tensorflow inference (soon)\n\nNow I created notebook for learning and prototyping in YOLOX. Next step is too create better model (play with YOLOX experimentation parameters).","metadata":{"execution":{"iopub.status.busy":"2021-11-29T13:34:31.033138Z","iopub.execute_input":"2021-11-29T13:34:31.033449Z","iopub.status.idle":"2021-11-29T13:34:33.455468Z","shell.execute_reply.started":"2021-11-29T13:34:31.033368Z","shell.execute_reply":"2021-11-29T13:34:33.454141Z"}}},{"cell_type":"code","source":"import torch","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:25:55.997170Z","iopub.execute_input":"2023-11-16T03:25:55.997520Z","iopub.status.idle":"2023-11-16T03:25:56.002455Z","shell.execute_reply.started":"2023-11-16T03:25:55.997485Z","shell.execute_reply":"2023-11-16T03:25:56.001392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# get custom trained checkpoint\nckpt_file = \"/kaggle/input/test111/best_ckpt.pth\"\n# ckpt_file = \"./YOLOX_outputs/cots_config/best_ckpt.pth\"\n# ckpt = torch.load(ckpt_file, map_location=\"cpu\")\nckpt  =torch.jit.load(ckpt_file,map_location=torch.device('cpu'))","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:25:56.004385Z","iopub.execute_input":"2023-11-16T03:25:56.005686Z","iopub.status.idle":"2023-11-16T03:25:56.348664Z","shell.execute_reply.started":"2023-11-16T03:25:56.005558Z","shell.execute_reply":"2023-11-16T03:25:56.347466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef traced_model_convert_onnx():\n    model = torch.jit.load(ckpt_file, map_location='cpu')\n    # model.\n    dummy_input = torch.randn(1, 3, 640, 640)\n\n    torch.onnx.export(\n        model,  # model\n        dummy_input, # 模型输入\n        \"best_ckpt_cu.onnx\", # 转换后模型的存储路径\n        export_params=True, # 是否将模型参数保存至onnx中\n        input_names=[\"input_image\"], # 输入节点名称\n        output_names = [\"model_output\"], # 输出节点名称\n        example_outputs =model(dummy_input),\n        opset_version=11 # onnx的版本\n    )\n","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:25:56.352522Z","iopub.execute_input":"2023-11-16T03:25:56.352870Z","iopub.status.idle":"2023-11-16T03:25:56.360879Z","shell.execute_reply.started":"2023-11-16T03:25:56.352832Z","shell.execute_reply":"2023-11-16T03:25:56.359890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traced_model_convert_onnx()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=torch.randn(1, 3, 640, 640)","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:25:56.362734Z","iopub.execute_input":"2023-11-16T03:25:56.363593Z","iopub.status.idle":"2023-11-16T03:25:56.386080Z","shell.execute_reply.started":"2023-11-16T03:25:56.363533Z","shell.execute_reply":"2023-11-16T03:25:56.384588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pwd","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:25:56.389518Z","iopub.execute_input":"2023-11-16T03:25:56.389919Z","iopub.status.idle":"2023-11-16T03:25:56.397615Z","shell.execute_reply.started":"2023-11-16T03:25:56.389867Z","shell.execute_reply":"2023-11-16T03:25:56.396167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.is_available()","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:25:59.562761Z","iopub.execute_input":"2023-11-16T03:25:59.563187Z","iopub.status.idle":"2023-11-16T03:25:59.572071Z","shell.execute_reply.started":"2023-11-16T03:25:59.563137Z","shell.execute_reply":"2023-11-16T03:25:59.571004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:25:59.574017Z","iopub.execute_input":"2023-11-16T03:25:59.574667Z","iopub.status.idle":"2023-11-16T03:26:00.701861Z","shell.execute_reply.started":"2023-11-16T03:25:59.574609Z","shell.execute_reply":"2023-11-16T03:26:00.700668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip list","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:26:00.705019Z","iopub.execute_input":"2023-11-16T03:26:00.705380Z","iopub.status.idle":"2023-11-16T03:26:03.776748Z","shell.execute_reply.started":"2023-11-16T03:26:00.705343Z","shell.execute_reply":"2023-11-16T03:26:03.775593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ckpt.code","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:26:03.778492Z","iopub.execute_input":"2023-11-16T03:26:03.778908Z","iopub.status.idle":"2023-11-16T03:26:03.794132Z","shell.execute_reply.started":"2023-11-16T03:26:03.778866Z","shell.execute_reply":"2023-11-16T03:26:03.792763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traced_model = torch.jit.trace(ckpt, x, ckpt(x))","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:26:03.797448Z","iopub.execute_input":"2023-11-16T03:26:03.797787Z","iopub.status.idle":"2023-11-16T03:26:04.397745Z","shell.execute_reply.started":"2023-11-16T03:26:03.797750Z","shell.execute_reply":"2023-11-16T03:26:04.395171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traced_model.save('traced_model.pt')","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:26:04.399316Z","iopub.execute_input":"2023-11-16T03:26:04.399609Z","iopub.status.idle":"2023-11-16T03:26:04.560715Z","shell.execute_reply.started":"2023-11-16T03:26:04.399573Z","shell.execute_reply":"2023-11-16T03:26:04.559711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pwd","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:26:04.562613Z","iopub.execute_input":"2023-11-16T03:26:04.562935Z","iopub.status.idle":"2023-11-16T03:26:04.577853Z","shell.execute_reply.started":"2023-11-16T03:26:04.562894Z","shell.execute_reply":"2023-11-16T03:26:04.576494Z"},"trusted":true},"execution_count":null,"outputs":[]}]}