{"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":"<h1><center>Clean & Modular YoloX Training script</center></h1>     \n\n<center><img src = \"https://i.imgur.com/iatgdo5.jpg\" width = \"635\" height = \"235\"/></center>         \n\nThe dataset was built to be compatible with the train (train.py) script that can be found [HERE](https://github.com/Megvii-BaseDetection/YOLOX). To see how the dataset was built you can check [HERE](https://www.kaggle.com/coldfir3/simple-yolox-dataset-generator-coco-json). The inference notebook is still WIP.\n\nThe four main steps for training YoloX:\n1. Generating the dataset in a compatible format (COCO)\n1. Installing YoloX\n1. Creating the config.py file\n1. Training the model\n\nI took inspiration on [this](https://www.kaggle.com/remekkinas/yolox-training-pipeline-cots-dataset-lb-0-507) amazing notebook\n\n<h3 style='background:orange; color:black'><center>Consider upvoting this notebook if you found it helpful.</center></h3>","metadata":{}},{"cell_type":"markdown","source":"## YoloX install","metadata":{}},{"cell_type":"code","source":"%%capture\n\n!git clone https://github.com/Megvii-BaseDetection/YOLOX -q\n\n%cd YOLOX\n!pip install -U pip && pip install -r requirements.txt\n!pip install -v -e . ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-10T19:27:07.164959Z","iopub.execute_input":"2021-12-10T19:27:07.165749Z","iopub.status.idle":"2021-12-10T19:28:04.419813Z","shell.execute_reply.started":"2021-12-10T19:27:07.165644Z","shell.execute_reply":"2021-12-10T19:28:04.418434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n\n!wget https://github.com/Megvii-BaseDetection/storage/releases/download/0.0.1/yolox_s.pth","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:28:04.429177Z","iopub.execute_input":"2021-12-10T19:28:04.430771Z","iopub.status.idle":"2021-12-10T19:28:06.681894Z","shell.execute_reply.started":"2021-12-10T19:28:04.430660Z","shell.execute_reply":"2021-12-10T19:28:06.681014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## COCO api install","metadata":{}},{"cell_type":"code","source":"!pip install -Uqqq 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:28:06.684518Z","iopub.execute_input":"2021-12-10T19:28:06.684973Z","iopub.status.idle":"2021-12-10T19:28:23.083707Z","shell.execute_reply.started":"2021-12-10T19:28:06.684923Z","shell.execute_reply":"2021-12-10T19:28:23.082473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Configuring your model\n\nFor more details on how to configure the hypeparams check [this](https://github.com/Megvii-BaseDetection/YOLOX/blob/main/docs/train_custom_data.md). Those hyperparametes ARE NOT OPTIMAL and have been chosen just to make commiting this notebook quick. Feel free to share good hyperparams on the comments.","metadata":{}},{"cell_type":"code","source":"%%writefile cots_config.py\n\nimport os\n\nfrom yolox.exp import Exp as MyExp\n\nclass Exp(MyExp):\n    def __init__(self):\n        super(Exp, self).__init__()\n        self.depth = 0.33\n        self.width = 0.50\n        self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(\".\")[0]\n        \n        self.data_dir = \"/kaggle/input/cots-yolox-dataset\"\n        self.train_ann = \"/kaggle/input/cots-yolox-dataset/annotations_train.json\"\n        self.val_ann = \"/kaggle/input/cots-yolox-dataset/annotations_valid.json\"\n\n        self.num_classes = 1\n\n        self.max_epoch = 5\n        self.data_num_workers = 2\n        self.eval_interval = 1\n        \n        self.mosaic_prob = 1.0\n        self.mixup_prob = 1.0\n        self.hsv_prob = 1.0\n        self.flip_prob = 0.5\n        self.no_aug_epochs = 2\n        \n        self.input_size = (960, 960)\n        self.mosaic_scale = (0.5, 1.5)\n        self.random_size = (10, 20)\n        self.test_size = (960, 960)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:30:19.610899Z","iopub.execute_input":"2021-12-10T19:30:19.611232Z","iopub.status.idle":"2021-12-10T19:30:19.617595Z","shell.execute_reply.started":"2021-12-10T19:30:19.611198Z","shell.execute_reply":"2021-12-10T19:30:19.616882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training YolovX","metadata":{}},{"cell_type":"code","source":"!python tools/train.py \\\n    -f cots_config.py \\\n    -d 1 \\\n    -b 32 \\\n    --fp16 \\\n    -o \\\n    -c yolox_s.pth","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:30:20.666793Z","iopub.execute_input":"2021-12-10T19:30:20.667430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}