{"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":"# Converting images and masks to tiles","metadata":{}},{"cell_type":"code","source":"!mkdir tiles\n!mkdir tiles/images\n!mkdir tiles/masks","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls /kaggle/working/tiles","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/pandas-256x256-tiles-of-180-images/tiles/train /kaggle/working","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport os\n\n\ndef create_tiles(image, mask, tile_size, images_output_folder, masks_output_folder, image_filename, threshold=0.05):\n    height, width = image.shape[:2]\n\n    for y in range(0, height, tile_size):\n        for x in range(0, width, tile_size):\n            tile_image = image[y:y + tile_size, x:x + tile_size]\n            tile_mask = mask[y:y + tile_size, x:x + tile_size]\n\n            mean_color = np.mean(tile_image, axis=(0, 1))\n            mean_color_value = np.mean(mean_color)\n\n            if mean_color_value < 255 * (1 - threshold):\n                tile_image_path = os.path.join(images_output_folder, f'{image_filename}_tile_{y}_{x}.png')\n                tile_mask_path = os.path.join(masks_output_folder, f'{image_filename}_tile_{y}_{x}.png')\n\n                cv2.imwrite(tile_image_path, tile_image)\n                \n                # Check if the mask is a single-channel image and save it accordingly\n             \n                cv2.imwrite(tile_mask_path, tile_mask[:, :, 0])\n              \ndef process_images(images_folder, masks_folder, tile_size, images_output_folder, masks_output_folder):\n    image_files = os.listdir(images_folder)\n    count = 0\n    for image_file in image_files:\n        if image_file.endswith('.png'):\n            image_path = os.path.join(images_folder, image_file)\n            mask_path = os.path.join(masks_folder, image_file)\n\n            image = cv2.imread(image_path, cv2.IMREAD_COLOR)\n            mask = cv2.imread(mask_path, cv2.IMREAD_COLOR)\n            if image is None or mask is None:\n                \n                print(f\"Warning: Unable to read image or mask for file {image_file}. Skipping...\")\n                continue\n                \n            image_filename = os.path.splitext(image_file)[0]\n            print(image_filename)\n            count+=1\n            print(count)\n\n            create_tiles(image, mask, tile_size, images_output_folder, masks_output_folder, image_filename)\n\n\n# Input parameters\nimages_folder = '/kaggle/input/tiff-to-png/images'\nmasks_folder = '/kaggle/input/tiff-to-png/masks'\ntile_size = 256\nimages_output_folder = '/kaggle/working/tiles/images'\nmasks_output_folder = '/kaggle/working/tiles/masks'\n\n# Process the images\nprocess_images(images_folder, masks_folder, tile_size, images_output_folder, masks_output_folder)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# creating annotation files","metadata":{}},{"cell_type":"code","source":"\nimport os\nimport cv2\nimport numpy as np\nfrom skimage import measure\n\ndef create_annotations(ground_truth_image, output_annotation_path):\n    class_ids = {\n        0: 'black',\n        1: 'gray',\n        2: 'green',\n        3: 'yellow',\n        4: 'orange',\n        5: 'red'\n    }\n\n    gt_img = cv2.imread(ground_truth_image, cv2.IMREAD_GRAYSCALE)\n    height, width = gt_img.shape\n\n    annotation_data = []\n\n    for class_id, color_name in class_ids.items():\n        mask = np.where(gt_img == class_id, 255, 0).astype(np.uint8)\n        contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n\n        for contour in contours:\n            x, y, w, h = cv2.boundingRect(contour)\n            x_center, y_center = (x + w / 2) / width, (y + h / 2) / height\n            w, h = w / width, h / height\n\n            annotation_data.append(f\"{class_id} {x_center} {y_center} {w} {h}\")\n\n    with open(output_annotation_path, 'w') as f:\n        f.write('\\n'.join(annotation_data))\n\nground_truth_dir = '/kaggle/input/tiff-to-png/masks'\nannotation_output_dir = '/kaggle/working/labels'\ncount = 0\nos.makedirs(annotation_output_dir, exist_ok=True)\n\nfor filename in os.listdir(ground_truth_dir):\n    if filename.endswith('.jpg') or filename.endswith('.png'):\n        ground_truth_image = os.path.join(ground_truth_dir, filename)\n        output_annotation_path = os.path.join(annotation_output_dir, os.path.splitext(filename)[0] + '.txt')\n        create_annotations(ground_truth_image, output_annotation_path)\n        count +=1\n        print(count)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport os\nimport cv2\nimport numpy as np\nfrom skimage import measure\n\ndef create_annotations(ground_truth_image, output_annotation_path):\n    class_ids = {\n        2: 'green',\n        4: 'orange',\n        5: 'red'\n    }\n\n    # Create a mapping between the original class IDs and the new class IDs for YOLOv5 training\n    class_id_mapping = {original_id: new_id for new_id, original_id in enumerate(class_ids)}\n\n    gt_img = cv2.imread(ground_truth_image, cv2.IMREAD_GRAYSCALE)\n    height, width = gt_img.shape\n\n    annotation_data = []\n\n    for class_id, color_name in class_ids.items():\n        mask = np.where(gt_img == class_id, 255, 0).astype(np.uint8)\n        contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n\n        for contour in contours:\n            x, y, w, h = cv2.boundingRect(contour)\n            x_center, y_center = (x + w / 2) / width, (y + h / 2) / height\n            w, h = w / width, h / height\n\n            annotation_data.append(f\"{class_id} {x_center} {y_center} {w} {h}\")\n\n    with open(output_annotation_path, 'w') as f:\n        f.write('\\n'.join(annotation_data))\n\n\nground_truth_dir = '/kaggle/input/tiff-to-png/masks'\nannotation_output_dir = '/kaggle/working/labels'\ncount = 0\nos.makedirs(annotation_output_dir, exist_ok=True)\n\nfor filename in os.listdir(ground_truth_dir):\n    if filename.endswith('.jpg') or filename.endswith('.png'):\n        ground_truth_image = os.path.join(ground_truth_dir, filename)\n        output_annotation_path = os.path.join(annotation_output_dir, os.path.splitext(filename)[0] + '.txt')\n        create_annotations(ground_truth_image, output_annotation_path)\n        count +=1\n        print(count)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls /kaggle/working/train","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mv /kaggle/working/labels /kaggle/working/train","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/working/tiles/images | wc -l\n!ls /kaggle/working/tiles/masks | wc -l\n!ls /kaggle/working/labels | wc -l","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls /kaggle/working/val/labels | wc -l","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"mkdir val","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# copying files to val direcory","metadata":{}},{"cell_type":"code","source":"import os\nimport shutil\nimport random\n\n# Set the paths to the images and annotations folders\nimages_path = '/kaggle/working/train/images'\nannotations_path = '/kaggle/working/train/labels'\n\n# Set the path to the val folder\nval_path = '/kaggle/working/val'\n\n# Create the images and labels folders in the val folder\n#os.makedirs(os.path.join(val_path, 'images'))\n#os.makedirs(os.path.join(val_path, 'labels'))\n\n# Get a list of all the image file names\nimage_files = os.listdir(images_path)\n\n# Randomly select 2000 images\nselected_images = random.sample(image_files, 50)\n\n# Move the selected images and their corresponding annotations to the val folder\nfor image_file in selected_images:\n    # Move the image\n    shutil.move(os.path.join(images_path, image_file), os.path.join(val_path, 'images'))\n    \n    # Move the annotation\n    annotation_file = image_file.split('.')[0] + '.txt'  # assuming the annotations have the same name as the images, but with a .txt extension\n    shutil.move(os.path.join(annotations_path, annotation_file), os.path.join(val_path, 'labels'))\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# moving to train and val directories","metadata":{}},{"cell_type":"code","source":"!mv /kaggle/working/tiles/masks /kaggle/working\n!mv /kaggle/working/tiles/images /kaggle/working/train\n!mv /kaggle/working/labels /kaggle/working/train \n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir /kaggle/working/train","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls /kaggle/working","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Testing if the images are properly annoataed","metadata":{}},{"cell_type":"code","source":"import cv2\nimport os\nimport matplotlib.pyplot as plt\n\ndef visualize_annotations(image_path, annotation_path):\n    image = cv2.imread(image_path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # Convert to RGB for matplotlib\n    with open(annotation_path, 'r') as f:\n        lines = f.readlines()\n\n    for line in lines:\n        data = line.strip().split(' ')\n        class_id, x_center, y_center, width, height = map(float, data)\n        x_center, y_center, width, height = x_center * image.shape[1], y_center * image.shape[0], width * image.shape[1], height * image.shape[0]\n        x1, y1, x2, y2 = int(x_center - width / 2), int(y_center - height / 2), int(x_center + width / 2), int(y_center + height / 2)\n        cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)\n        cv2.putText(image, str(int(class_id)), (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)\n\n    plt.imshow(image)\n    plt.show()\n\nimage_path = '/kaggle/working/train/images/0018ae58b01bdadc8e347995b69f99aa.png'\nannotation_path = '/kaggle/working/train/labels/0018ae58b01bdadc8e347995b69f99aa.txt'\n\nvisualize_annotations(image_path, annotation_path)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# YOLO V5","metadata":{}},{"cell_type":"code","source":"!pip install -r \"https://raw.githubusercontent.com/ultralytics/yolov5/master/requirements.txt\"\n!git clone https://github.com/ultralytics/yolov5.git\n","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:40:40.224611Z","iopub.execute_input":"2023-04-16T03:40:40.225291Z","iopub.status.idle":"2023-04-16T03:41:05.294226Z","shell.execute_reply.started":"2023-04-16T03:40:40.225251Z","shell.execute_reply":"2023-04-16T03:41:05.293086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tensorflow tensorflow-transform librosa\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install some_package --no-deps\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install dask-cudf cupy-cuda115 distributed cloud-tpu-client\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install protobuf==3.9.2 pyarrow==6.0.0 soundfile==0.12.1 google-api-python-client==1.8.0\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Creating YAML file***","metadata":{}},{"cell_type":"code","source":"import yaml\nimport os\n\ntrain_dir = '/kaggle/input/pandas-256x256-tiles-of-180-images/tiles/train'\nval_dir = '/kaggle/input/pandas-256x256-tiles-of-180-images/tiles/val'\nsave_dir = '/kaggle/working'\nnum_classes = 3\nclass_names = ['G3', 'G4', 'G5']\nbatch_size = 16\nepochs = 10\n\ndata = {\n    'train': train_dir,\n    'val': val_dir,\n    'nc': num_classes,\n    'names': class_names,\n    'batch_size': batch_size,\n    'epochs': epochs\n}\n\nwith open(os.path.join(save_dir, 'prostate_cancer_data.yaml'), 'w') as f:\n    yaml.dump(data, f)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:41:42.404145Z","iopub.execute_input":"2023-04-16T03:41:42.405272Z","iopub.status.idle":"2023-04-16T03:41:42.435766Z","shell.execute_reply.started":"2023-04-16T03:41:42.405226Z","shell.execute_reply":"2023-04-16T03:41:42.434841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import yaml\n\ncustom_hyp = {\n    \"lr0\": 0.01,\n    \"lrf\": 0.01,\n    \"momentum\": 0.937,\n    \"weight_decay\": 0.0005,\n    \"warmup_epochs\": 2.0,\n    \"warmup_momentum\": 0.8,\n    \"warmup_bias_lr\": 0.1,\n    \"box\": 0.05,\n    \"cls\": 0.5,\n    \"cls_pw\": 1.0,\n    \"obj\": 1.0,\n    \"obj_pw\": 1.0,\n    \"iou_t\": 0.2,\n    \"anchor_t\": 4.0,\n    \"fl_gamma\": 0.0,\n    \"hsv_h\": 0.015,\n    \"hsv_s\": 0.7,\n    \"hsv_v\": 0.4,\n    \"degrees\": 0.0,\n    \"translate\": 0.1,\n    \"scale\": 0.5,\n    \"shear\": 0.0,\n    \"perspective\": 0.0,\n    \"flipud\": 0.0,\n    \"fliplr\": 0.5,\n    \"mosaic\": 1.0,\n    \"mixup\": 0.0,\n    \"copy_paste\": 0.0,\n}\n\nwith open(\"custom_hyp.yaml\", \"w\") as f:\n    yaml.dump(custom_hyp, f)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:13:38.327105Z","iopub.execute_input":"2023-04-16T04:13:38.327891Z","iopub.status.idle":"2023-04-16T04:13:38.33897Z","shell.execute_reply.started":"2023-04-16T04:13:38.327846Z","shell.execute_reply":"2023-04-16T04:13:38.338007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls /kaggle/working/","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Training**","metadata":{}},{"cell_type":"code","source":"!cp -r /kaggle/input/pandas-256x256-tiles-of-180-images/tiles /kaggle/working","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python yolov5/train.py --img 640 --batch 12 --epochs 5 --data /kaggle/working/prostate_cancer_data.yaml --cfg yolov5l.yaml --weights /kaggle/input/yolo-v5/yolov5/runs/train/yolov5l_prostate_cancer2/weights/best.pt --name yolov5l_prostate_cancer --hyp /kaggle/working/custom_hyp.yaml","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:14:07.891178Z","iopub.execute_input":"2023-04-16T04:14:07.892195Z","iopub.status.idle":"2023-04-16T14:18:33.24416Z","shell.execute_reply.started":"2023-04-16T04:14:07.892156Z","shell.execute_reply":"2023-04-16T14:18:33.241829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**validating**","metadata":{}},{"cell_type":"code","source":"import yaml\n\n# Read the content of the hyp.yaml file\nwith open(\"yolov5/hyp.yaml\", \"r\") as file:\n    hyp = yaml.safe_load(file)\n\n# Modify the hyperparameters\nhyp[\"lr0\"] = 0.01\nhyp[\"warmup_epochs\"] = 2\n# ... (modify other hyperparameters as needed)\n\n# Save the modified content back to the hyp.yaml file\nwith open(\"yolov5/hyp.yaml\", \"w\") as file:\n    yaml.safe_dump(hyp, file)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:06:46.552849Z","iopub.execute_input":"2023-04-16T04:06:46.553406Z","iopub.status.idle":"2023-04-16T04:06:46.624384Z","shell.execute_reply.started":"2023-04-16T04:06:46.553362Z","shell.execute_reply":"2023-04-16T04:06:46.622748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls /kaggle/working/yolov5/data/hyps","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:10:32.751311Z","iopub.execute_input":"2023-04-16T04:10:32.751757Z","iopub.status.idle":"2023-04-16T04:10:33.835202Z","shell.execute_reply.started":"2023-04-16T04:10:32.751714Z","shell.execute_reply":"2023-04-16T04:10:33.833751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!find /kaggle/working/yolov5 -name \"hyp.yaml\"\n","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:09:47.388139Z","iopub.execute_input":"2023-04-16T04:09:47.388756Z","iopub.status.idle":"2023-04-16T04:09:48.329583Z","shell.execute_reply.started":"2023-04-16T04:09:47.388713Z","shell.execute_reply":"2023-04-16T04:09:48.328358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"ls /kaggle/working/yolov5/data/hyps","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:09:12.123134Z","iopub.execute_input":"2023-04-16T04:09:12.12405Z","iopub.status.idle":"2023-04-16T04:09:13.110529Z","shell.execute_reply.started":"2023-04-16T04:09:12.123995Z","shell.execute_reply":"2023-04-16T04:09:13.109338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python yolov5/val.py --weights /kaggle/input/yolo-v5/yolov5/runs/train/yolov5l_prostate_cancer2/weights/best.pt --data /kaggle/working/prostate_cancer_data.yaml --img 640 --iou-thres 0.2 --conf-thres 0.2","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:48:30.135071Z","iopub.execute_input":"2023-04-16T03:48:30.13607Z","iopub.status.idle":"2023-04-16T03:51:45.653474Z","shell.execute_reply.started":"2023-04-16T03:48:30.136002Z","shell.execute_reply":"2023-04-16T03:51:45.652221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp yolov5/runs/train/yolov5l_prostate_cancer2/weights/best.pt kaggle/working","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# For checking the masks ","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport numpy as np\n\n%matplotlib inline\nimage_id = 'ffe9bcababc858e04840669e788065a1'\n\noriginal_image = \"/kaggle/working/train/images/0018ae58b01bdadc8e347995b69f99aa.png\"\nlabel_image_semantic = \"/kaggle/input/tiff-to-png/masks/0018ae58b01bdadc8e347995b69f99aa.png\"\n\nfig, axs = plt.subplots(1, 2, figsize=(16, 8), constrained_layout=True)\n\ncmap = matplotlib.colors.ListedColormap(['black', 'gray', 'green', 'yellow', 'orange', 'red'])\n\naxs[0].imshow( Image.open(original_image))\naxs[0].grid(False)\n\nlabel_image_semantic = Image.open(label_image_semantic)\nlabel_image_semantic = np.asarray(label_image_semantic)\naxs[1].imshow(label_image_semantic,cmap=cmap,interpolation='nearest', vmin=0, vmax=5)\naxs[1].grid(False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}