{"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":"%cp -R /kaggle/input/darknet-run-environment/darknet /kaggle/working\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-06T10:12:12.975616Z","iopub.execute_input":"2022-01-06T10:12:12.975938Z","iopub.status.idle":"2022-01-06T10:12:22.532604Z","shell.execute_reply.started":"2022-01-06T10:12:12.975854Z","shell.execute_reply":"2022-01-06T10:12:22.531495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cp '/kaggle/input/predconf/yolov4-custom_3000.weights' '/kaggle/working/darknet'\n%cp '/kaggle/input/predconf/run.py' '/kaggle/working/darknet'\n%cp '/kaggle/input/predconf/obj.data' '/kaggle/working/darknet/data'\n%cp '/kaggle/input/predconf/fresh.cfg' '/kaggle/working/darknet/cfg'\n%cp '/kaggle/input/predconf/obj.names' '/kaggle/working/darknet/data'","metadata":{"execution":{"iopub.status.busy":"2022-01-06T10:12:22.536993Z","iopub.execute_input":"2022-01-06T10:12:22.53722Z","iopub.status.idle":"2022-01-06T10:12:29.007474Z","shell.execute_reply.started":"2022-01-06T10:12:22.537192Z","shell.execute_reply":"2022-01-06T10:12:29.006366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/darknet\n\n!cp '../../input/libcuda/libcuda.so' .\n\n!sed -i 's/OPENCV=0/OPENCV=1/g' Makefile\n!sed -i 's/GPU=0/GPU=1/g' Makefile\n!sed -i 's/CUDNN=0/CUDNN=1/g' Makefile\n!sed -i 's/CUDNN_HALF=0/CUDNN_HALF=1/g' Makefile\n!sed -i 's/LIBSO=0/LIBSO=1/' Makefile\n!sed -i \"s/ARCH= -gencode arch=compute_60,code=sm_60/ARCH= ${ARCH_VALUE}/g\" Makefile\n\n!sed -i 's/LDFLAGS+= -L\\/usr\\/local\\/cuda\\/lib64 -lcuda -lcudart -lcublas -lcurand/LDFLAGS+= -L\\/usr\\/local\\/cuda\\/lib64 -lcudart -lcublas -lcurand -L\\/kaggle\\/working\\/darknet -lcuda/' Makefile\n!make &> compile.log","metadata":{"execution":{"iopub.status.busy":"2022-01-06T10:12:29.009112Z","iopub.execute_input":"2022-01-06T10:12:29.009394Z","iopub.status.idle":"2022-01-06T10:14:20.086026Z","shell.execute_reply.started":"2022-01-06T10:12:29.009347Z","shell.execute_reply":"2022-01-06T10:14:20.085048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tail compile.log","metadata":{"execution":{"iopub.status.busy":"2022-01-06T10:14:20.087956Z","iopub.execute_input":"2022-01-06T10:14:20.088239Z","iopub.status.idle":"2022-01-06T10:14:20.752272Z","shell.execute_reply.started":"2022-01-06T10:14:20.088192Z","shell.execute_reply":"2022-01-06T10:14:20.751403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import greatbarrierreef\nenv = greatbarrierreef.make_env()   # initialize the environment\niter_test = env.iter_test()    # an iterator which loops over the test set and sample submission","metadata":{"execution":{"iopub.status.busy":"2022-01-06T10:14:20.7547Z","iopub.execute_input":"2022-01-06T10:14:20.755183Z","iopub.status.idle":"2022-01-06T10:14:20.782864Z","shell.execute_reply.started":"2022-01-06T10:14:20.75514Z","shell.execute_reply":"2022-01-06T10:14:20.782182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %matplotlib inline\n# import matplotlib.pyplot as plt\n# from matplotlib.pyplot import figure\n# import darknet\n# import cv2\n\n# network, class_names, class_colors = darknet.load_network(\n#         'cfg/yolov4-custom.cfg',\n#         'data/obj.data',\n#         '2000.weights',\n#         1\n#     )\n\n# ip_image = cv2.imread('/kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/9674.jpg')\n# darknet_image = darknet.make_image(640, 640, 3)\n# ip_rgb = cv2.cvtColor(ip_image, cv2.COLOR_BGR2RGB)\n# img_resized = cv2.resize(ip_rgb,(640,360) ,cv2.INTER_AREA)\n# img_resized[360:640, 0:640] = (255,255,255)\n# darknet.copy_image_from_bytes(darknet_image, img_resized.tobytes())\n# detections = darknet.detect_image(network, class_names, darknet_image, 0.5)\n# darknet.free_image(darknet_image)\n# image = darknet.draw_boxes(detections, img_resized, class_colors)\n# figure(figsize=(12.8, 12.8))\n# plt.imshow(image)\n# plt.show()\n# preds = []\n# for label, confidence, bbox in detections:\n#     x1, y1, w1, h1 = bbox\n#     if y1 < 360:\n#         xmin, ymin, xmax, ymax = darknet.bbox2points(bbox)\n#         preds.append(f'0.{confidence[0]} {int(xmin*2)} {int(ymin*2)} {int(w1*2)} {int(h1*2)}')\n# prediction_str = ' '.join(preds)\n# print(prediction_str)\n# #sample_prediction_df['annotations'] = prediction_str\n# #env.predict(sample_prediction_df)   # register your predictions","metadata":{"execution":{"iopub.status.busy":"2022-01-06T10:14:20.784255Z","iopub.execute_input":"2022-01-06T10:14:20.784793Z","iopub.status.idle":"2022-01-06T10:14:20.789847Z","shell.execute_reply.started":"2022-01-06T10:14:20.784756Z","shell.execute_reply":"2022-01-06T10:14:20.789096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nimport matplotlib.pyplot as plt\nfrom matplotlib.pyplot import figure\nimport darknet\nimport cv2\n\nnetwork, class_names, class_colors = darknet.load_network(\n        'cfg/fresh.cfg',\n        'data/obj.data',\n        'yolov4-custom_3000.weights',\n        1\n    )\n\n\nfor (pixel_array, sample_prediction_df) in iter_test:\n    ip_image = pixel_array[:,:,::-1]   #cv2.imread('/kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/9674.jpg')\n    darknet_image = darknet.make_image(640, 640, 3)\n    ip_rgb = cv2.cvtColor(ip_image, cv2.COLOR_BGR2RGB)\n    img_resized = cv2.resize(ip_rgb,(640,360) ,cv2.INTER_AREA)\n    img_resized[360:640, 0:640] = (0,0,0)\n    darknet.copy_image_from_bytes(darknet_image, img_resized.tobytes())\n    detections = darknet.detect_image(network, class_names, darknet_image, 0.35)\n    darknet.free_image(darknet_image)\n    preds = []\n    for label, confidence, bbox in detections:\n        x1, y1, w1, h1 = bbox\n        if y1 < 360:    \n            xmin, ymin, xmax, ymax = darknet.bbox2points(bbox)\n            preds.append(f'0.{confidence[0]} {int(xmin*2)} {int(ymin*2)} {int(w1*2)} {int(h1*2)}')\n    prediction_str = ' '.join(preds)\n    print(prediction_str)\n    sample_prediction_df['annotations'] = prediction_str\n    env.predict(sample_prediction_df)   # register your predictions\n#     ifinal = darknet.draw_boxes(detections, img_resized, class_colors)\n#     figure(figsize=(12.8, 12.8))\n#     plt.imshow(ifinal)\n#     plt.show()\n#     cv2.imwrite('/kaggle/working/a.jpg', ifinal)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T10:14:20.791538Z","iopub.execute_input":"2022-01-06T10:14:20.792298Z","iopub.status.idle":"2022-01-06T10:14:34.202514Z","shell.execute_reply.started":"2022-01-06T10:14:20.792256Z","shell.execute_reply":"2022-01-06T10:14:34.201686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nsub_df = pd.read_csv('submission.csv')\nsub_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T10:14:34.203718Z","iopub.execute_input":"2022-01-06T10:14:34.204262Z","iopub.status.idle":"2022-01-06T10:14:34.219604Z","shell.execute_reply.started":"2022-01-06T10:14:34.204225Z","shell.execute_reply":"2022-01-06T10:14:34.218595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp /kaggle/working/darknet/submission.csv /kaggle/working\n!rm -r /kaggle/working/darknet","metadata":{"execution":{"iopub.status.busy":"2022-01-06T10:14:34.221041Z","iopub.execute_input":"2022-01-06T10:14:34.221289Z","iopub.status.idle":"2022-01-06T10:14:35.721256Z","shell.execute_reply.started":"2022-01-06T10:14:34.221255Z","shell.execute_reply":"2022-01-06T10:14:35.720334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import greatbarrierreef\n#env = greatbarrierreef.make_env()# initialize the environment\n#iter_test = env.iter_test()\n#import pandas as pd\n#df = pd.DataFrame()\n#env.predict(df)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T10:14:35.723724Z","iopub.execute_input":"2022-01-06T10:14:35.724322Z","iopub.status.idle":"2022-01-06T10:14:35.728437Z","shell.execute_reply.started":"2022-01-06T10:14:35.724282Z","shell.execute_reply":"2022-01-06T10:14:35.72774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}