{"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":"### Training Yolov5 on Custom Data, Part 2 [INFERENCE]","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":false,"execution":{"iopub.status.busy":"2022-01-15T20:29:30.84502Z","iopub.execute_input":"2022-01-15T20:29:30.845972Z","iopub.status.idle":"2022-01-15T20:29:30.986102Z","shell.execute_reply.started":"2022-01-15T20:29:30.845835Z","shell.execute_reply":"2022-01-15T20:29:30.985434Z"}}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport imageio\nimport matplotlib.pyplot as plt\n\n\n\nmodel_name = '../input/training-yolov5-on-competition-data-tutorial/yolov5/runs/train/exp/weights/best.pt'\nyolov5_folder = '../input/training-yolov5-on-competition-data-tutorial/yolov5'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":false,"execution":{"iopub.status.busy":"2022-02-20T12:17:23.445364Z","iopub.execute_input":"2022-02-20T12:17:23.445917Z","iopub.status.idle":"2022-02-20T12:17:23.507486Z","shell.execute_reply.started":"2022-02-20T12:17:23.445829Z","shell.execute_reply":"2022-02-20T12:17:23.506849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Using Pre-Trained YOLOv5 network.","metadata":{}},{"cell_type":"markdown","source":"Arial.tff has to be downloaded manually from a kaggle dataset since competitions dont allow use of the internet. Yolov5 is gonna look for the file in /root/.config/Ultralytics/","metadata":{}},{"cell_type":"code","source":"!mkdir -p /root/.config/Ultralytics\n!cp ../input/yolov5-font/Arial.ttf /root/.config/Ultralytics/","metadata":{"execution":{"iopub.status.busy":"2022-02-20T12:17:23.508977Z","iopub.execute_input":"2022-02-20T12:17:23.509239Z","iopub.status.idle":"2022-02-20T12:17:24.852555Z","shell.execute_reply.started":"2022-02-20T12:17:23.509206Z","shell.execute_reply":"2022-02-20T12:17:24.851601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def handle_pred(pred):\n    pred.print()\n    res = [str(float(conf)) + \" \" + str(int(xmin)) + \" \" + str(int(ymin)) + \" \" + str(int(xmax-xmin)) + \" \" + str(int(ymax-ymin)) for xmin,ymin,xmax,ymax,conf,_ in pred.xyxy[0]]\n    return \" \".join(res)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T12:17:24.854495Z","iopub.execute_input":"2022-02-20T12:17:24.855044Z","iopub.status.idle":"2022-02-20T12:17:24.861573Z","shell.execute_reply.started":"2022-02-20T12:17:24.854985Z","shell.execute_reply":"2022-02-20T12:17:24.860700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import greatbarrierreef\nimport torch\n\n\nenv = greatbarrierreef.make_env()   # initialize the environment\niter_test = env.iter_test()    # an iterator which loops over the test set and sample submission\nprint(model_name)\n\n#Loading model\nmodel = torch.hub.load(yolov5_folder, 'custom', path=model_name, force_reload=True, source='local')  # custom model\n\nfor pixel_array, sample_prediction_df in iter_test:\n    pred = model(pixel_array, size=1280) #Inference\n    res = handle_pred(pred) # Annotation formatting\n    sample_prediction_df['annotations'] = res\n    env.predict(sample_prediction_df)   # register your predictions","metadata":{"execution":{"iopub.status.busy":"2022-02-20T12:17:24.863617Z","iopub.execute_input":"2022-02-20T12:17:24.864146Z","iopub.status.idle":"2022-02-20T12:17:37.213667Z","shell.execute_reply.started":"2022-02-20T12:17:24.864108Z","shell.execute_reply":"2022-02-20T12:17:37.212786Z"},"trusted":true},"execution_count":null,"outputs":[]}]}