{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":7648576,"sourceType":"datasetVersion","datasetId":4458578},{"sourceId":7649871,"sourceType":"datasetVersion","datasetId":4459499},{"sourceId":123164466,"sourceType":"kernelVersion"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport cv2\nimport matplotlib.pyplot as plt\n\n# Specify the path to the dataset\ndataset_path = '/kaggle/input/gloumerli-tiles'\ntrain_path = '/kaggle/input/gloumerli-tiles/train'\ntest_path = '/kaggle/input/gloumerli-tiles/test'\n\n# Specify the filename of the image you want to read\nimage_filename = '/kaggle/input/gloumerli-tiles/train/LIT_NOR_VUHSK_127_PAS_VUHSK_20_Aperio_11_4_flv_2_png_jpg.rf.b02e2a034aed62b7079cfef62ef24aa6.jpg'\n\n# Construct the full path to the image using os.path.join()\nimage_path = os.path.join(train_path, image_filename)\n\n# Read the image using OpenCV\nimg = cv2.imread(image_path)\n\n# Convert BGR to RGB (OpenCV reads images in BGR format)\nimg_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n# Display the image\nplt.imshow(img_rgb)\nplt.axis('off')  # Turn off axis labels\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-02-18T11:57:10.090512Z","iopub.execute_input":"2024-02-18T11:57:10.090871Z","iopub.status.idle":"2024-02-18T11:57:10.242436Z","shell.execute_reply.started":"2024-02-18T11:57:10.090836Z","shell.execute_reply":"2024-02-18T11:57:10.241552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# clone darknet repo\n!git clone https://github.com/AlexeyAB/darknet","metadata":{"execution":{"iopub.status.busy":"2024-02-18T12:58:48.794041Z","iopub.execute_input":"2024-02-18T12:58:48.794415Z","iopub.status.idle":"2024-02-18T12:58:51.184685Z","shell.execute_reply.started":"2024-02-18T12:58:48.794383Z","shell.execute_reply":"2024-02-18T12:58:51.183738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd darknet","metadata":{"execution":{"iopub.status.busy":"2024-02-18T12:58:59.015352Z","iopub.execute_input":"2024-02-18T12:58:59.016238Z","iopub.status.idle":"2024-02-18T12:58:59.022769Z","shell.execute_reply.started":"2024-02-18T12:58:59.016199Z","shell.execute_reply":"2024-02-18T12:58:59.021926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!sed -i \"s/OPENCV=0/OPENCV=1/\" Makefile\n!sed -i \"s/GPU=0/GPU=1/\" Makefile\n!sed -i \"s/CUDNN=0/CUDNN=1/\" Makefile\n!sed -i \"s/CUDNN_HALF=0/CUDNN_HALF=1/\" Makefile\n!sed -i \"s/LIBSO=0/LIBSO=1/\" Makefile\n!sed -i \"s/OPENMP=0/OPENMP=1/\" Makefile","metadata":{"execution":{"iopub.status.busy":"2024-02-18T12:59:02.009234Z","iopub.execute_input":"2024-02-18T12:59:02.009965Z","iopub.status.idle":"2024-02-18T12:59:07.688057Z","shell.execute_reply.started":"2024-02-18T12:59:02.009933Z","shell.execute_reply":"2024-02-18T12:59:07.686979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!apt-get install make","metadata":{"execution":{"iopub.status.busy":"2024-02-18T12:59:09.371035Z","iopub.execute_input":"2024-02-18T12:59:09.371412Z","iopub.status.idle":"2024-02-18T12:59:11.960914Z","shell.execute_reply.started":"2024-02-18T12:59:09.371380Z","shell.execute_reply":"2024-02-18T12:59:11.959804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!make","metadata":{"execution":{"iopub.status.busy":"2024-02-18T12:59:13.499086Z","iopub.execute_input":"2024-02-18T12:59:13.499436Z","iopub.status.idle":"2024-02-18T12:59:14.609616Z","shell.execute_reply.started":"2024-02-18T12:59:13.499409Z","shell.execute_reply":"2024-02-18T12:59:14.608684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights","metadata":{"execution":{"iopub.status.busy":"2024-02-18T12:59:17.156948Z","iopub.execute_input":"2024-02-18T12:59:17.157845Z","iopub.status.idle":"2024-02-18T12:59:19.481635Z","shell.execute_reply.started":"2024-02-18T12:59:17.157809Z","shell.execute_reply":"2024-02-18T12:59:19.480732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %cd ..\n!chmod +x ./darknet","metadata":{"execution":{"iopub.status.busy":"2024-02-18T12:59:33.589259Z","iopub.execute_input":"2024-02-18T12:59:33.589597Z","iopub.status.idle":"2024-02-18T12:59:34.527138Z","shell.execute_reply.started":"2024-02-18T12:59:33.589571Z","shell.execute_reply":"2024-02-18T12:59:34.525859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd darknet","metadata":{"execution":{"iopub.status.busy":"2024-02-18T12:59:38.304987Z","iopub.execute_input":"2024-02-18T12:59:38.305634Z","iopub.status.idle":"2024-02-18T12:59:38.313920Z","shell.execute_reply.started":"2024-02-18T12:59:38.305599Z","shell.execute_reply":"2024-02-18T12:59:38.313027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/texas-Aerial-Robotics//BBOX-Label-Tool.git","metadata":{"execution":{"iopub.status.busy":"2024-02-18T12:59:41.819962Z","iopub.execute_input":"2024-02-18T12:59:41.820632Z","iopub.status.idle":"2024-02-18T12:59:45.013969Z","shell.execute_reply.started":"2024-02-18T12:59:41.820600Z","shell.execute_reply":"2024-02-18T12:59:45.013018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ..","metadata":{"execution":{"iopub.status.busy":"2024-02-18T12:59:54.111964Z","iopub.execute_input":"2024-02-18T12:59:54.112344Z","iopub.status.idle":"2024-02-18T12:59:54.118783Z","shell.execute_reply.started":"2024-02-18T12:59:54.112311Z","shell.execute_reply":"2024-02-18T12:59:54.117870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !rm -r $darknet_path","metadata":{"execution":{"iopub.status.busy":"2024-02-18T12:58:34.856496Z","iopub.execute_input":"2024-02-18T12:58:34.857451Z","iopub.status.idle":"2024-02-18T12:58:35.993319Z","shell.execute_reply.started":"2024-02-18T12:58:34.857416Z","shell.execute_reply":"2024-02-18T12:58:35.992243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\ndarknet_path = '/kaggle/working/darknet'\nsource_path = '/kaggle/input/gloumerli-tiles'\n\n# Specify the paths\ndata_path = os.path.join(darknet_path, 'data')\nlabel_path = os.path.join(data_path, 'labels')\npiford_path = os.path.join(data_path, 'piford')\n\n# Remove everything from the data folder except the label folder\nfor item in os.listdir(data_path):\n    item_path = os.path.join(data_path, item)\n    \n    # Check if it is a directory and not the label folder\n    if os.path.isdir(item_path) and item != 'labels':\n        shutil.rmtree(item_path)\n    elif os.path.isfile(item_path):\n        os.remove(item_path)\n\n# Create the piford folder\nos.makedirs(piford_path, exist_ok=True)\n\n# Copy the train and valid folders to Darknet's data/piford directory\nshutil.copytree(os.path.join(source_path, 'train'), os.path.join(piford_path, 'train'))\nshutil.copytree(os.path.join(source_path, 'valid'), os.path.join(piford_path, 'valid'))\n\n# Verify the remaining contents\nprint(os.listdir(data_path))\nprint(os.listdir(piford_path))","metadata":{"execution":{"iopub.status.busy":"2024-02-18T13:00:26.708115Z","iopub.execute_input":"2024-02-18T13:00:26.708980Z","iopub.status.idle":"2024-02-18T13:00:30.658767Z","shell.execute_reply.started":"2024-02-18T13:00:26.708945Z","shell.execute_reply":"2024-02-18T13:00:30.657713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#process.py\nimport glob\n\ndarknet_dir = \"darknet\"\ndata_dir = os.path.join(darknet_dir, \"data\")\npiford_dir = os.path.join(data_dir, \"piford\")\ntrain_dir = os.path.join(piford_dir, \"train\")\nvalid_dir = os.path.join(piford_dir, \"valid\")\n\n# split_pct = 10\nfile_train = open(os.path.join(data_dir, \"train.txt\"), \"w\")\nfile_val = open(os.path.join(data_dir, \"valid.txt\"), \"w\")\n\n# Loop over the images in the train folder\nfor pathAndFilename in glob.iglob(os.path.join(train_dir, \"*.jpg\")):\n    file_train.write(pathAndFilename + \"\\n\")\n\n# Loop over the images in the valid folder\nfor pathAndFilename in glob.iglob(os.path.join(valid_dir, \"*.jpg\")):\n    file_val.write(pathAndFilename + \"\\n\")\n\nfile_train.close()\nfile_val.close()","metadata":{"execution":{"iopub.status.busy":"2024-02-18T13:24:29.972766Z","iopub.execute_input":"2024-02-18T13:24:29.973394Z","iopub.status.idle":"2024-02-18T13:24:29.993687Z","shell.execute_reply.started":"2024-02-18T13:24:29.973358Z","shell.execute_reply":"2024-02-18T13:24:29.992728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2024-02-18T13:47:32.895854Z","iopub.execute_input":"2024-02-18T13:47:32.896699Z","iopub.status.idle":"2024-02-18T13:47:33.842102Z","shell.execute_reply.started":"2024-02-18T13:47:32.896659Z","shell.execute_reply":"2024-02-18T13:47:33.840946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create piford.names & piford.data inside darknet/data\ndarknet_dir = \"darknet\"\ndata_dir = os.path.join(darknet_dir, \"data\")\n\n# Create piford.names file\nwith open('piford.names', \"w\") as names_file:\n    names_file.write(\"normal\\nsclerosis\\n\")\n\n# Create piford.data file\nwith open('piford.data', \"w\") as data_file:\n    data_file.write(\"classes=2\\n\")\n    data_file.write(\"train=data/train.txt\\n\")\n    data_file.write(\"valid=data/valid.txt\\n\")\n    data_file.write(\"names=data/piford.names\\n\")\n    data_file.write(\"backup=backup/\\n\")","metadata":{"execution":{"iopub.status.busy":"2024-02-18T13:55:08.284561Z","iopub.execute_input":"2024-02-18T13:55:08.285065Z","iopub.status.idle":"2024-02-18T13:55:08.291985Z","shell.execute_reply.started":"2024-02-18T13:55:08.285030Z","shell.execute_reply":"2024-02-18T13:55:08.291019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.conv.137","metadata":{"execution":{"iopub.status.busy":"2024-02-18T13:58:04.579376Z","iopub.execute_input":"2024-02-18T13:58:04.579766Z","iopub.status.idle":"2024-02-18T13:58:06.719781Z","shell.execute_reply.started":"2024-02-18T13:58:04.579734Z","shell.execute_reply":"2024-02-18T13:58:06.718836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\n# Specify the source and destination file paths\nsource_file_path = \"/kaggle/input/yolov4-config/process.py\"\ndestination_file_path = \"/kaggle/working/darknet/cfg/yolov4-custom.cfg\"\n\n# Copy the file to the destination\nshutil.copyfile(source_file_path, destination_file_path)","metadata":{"execution":{"iopub.status.busy":"2024-02-18T15:09:29.751126Z","iopub.execute_input":"2024-02-18T15:09:29.751928Z","iopub.status.idle":"2024-02-18T15:09:29.763107Z","shell.execute_reply.started":"2024-02-18T15:09:29.751891Z","shell.execute_reply":"2024-02-18T15:09:29.762039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !./darknet detect cfg/yolov4.cfg yolov4.weights data/person.jpg\n!./darknet detector train data/piford.data cfg/yolov4-custom.cfg yolov4.conv.137 -dont_show -map","metadata":{"execution":{"iopub.status.busy":"2024-02-18T15:11:39.264329Z","iopub.execute_input":"2024-02-18T15:11:39.265225Z","iopub.status.idle":"2024-02-18T15:11:40.217427Z","shell.execute_reply.started":"2024-02-18T15:11:39.265189Z","shell.execute_reply":"2024-02-18T15:11:40.216231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pwd\n!./darknet detector train /kaggle/input/gloumerli-tiles/train /kaggle/working/darknet/cfg/yolov4-custom.cfg yolov4.conv.137","metadata":{"execution":{"iopub.status.busy":"2024-02-18T15:16:22.441590Z","iopub.execute_input":"2024-02-18T15:16:22.442094Z","iopub.status.idle":"2024-02-18T15:16:23.401064Z","shell.execute_reply.started":"2024-02-18T15:16:22.442048Z","shell.execute_reply":"2024-02-18T15:16:23.399931Z"},"trusted":true},"execution_count":null,"outputs":[]}]}