{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import glob\n\ntrain_image_paths = list(glob.glob('/kaggle/input/blood-vessel-segmentation/train/*/images/*.tif'))\ntrain_image_paths.sort()\n\ntrain_label_paths = list(glob.glob('/kaggle/input/blood-vessel-segmentation/train/*/labels/*.tif'))\ntrain_label_paths.sort()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-15T17:31:22.789641Z","iopub.execute_input":"2023-12-15T17:31:22.790179Z","iopub.status.idle":"2023-12-15T17:31:25.205115Z","shell.execute_reply.started":"2023-12-15T17:31:22.790133Z","shell.execute_reply":"2023-12-15T17:31:25.203958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\n\nprint(f'{train_image_paths[1000]}')\nimage = cv2.imread(train_image_paths[1000])\nimage_array = np.array(image)\nimage_tf_tensor = tf.convert_to_tensor(image_array, dtype=tf.float32)\nprint(f'Image shape: {image_tf_tensor.shape}')\n\nprint(f'{train_label_paths[1000]}')\nmask = cv2.imread(train_label_paths[1000])\nmask_array = np.array(mask)\nmask_tf_tensor = tf.convert_to_tensor(image_array, dtype=tf.float32)\nprint(f'Mask shape: {mask_tf_tensor.shape}')\n\nfig, axes = plt.subplots(nrows=1, ncols=4, figsize=(10, 5))\naxes[0].imshow(image_tf_tensor/255)\naxes[1].imshow(tf.image.per_image_standardization(image_tf_tensor))\naxes[2].imshow(tf.image.per_image_standardization(image_tf_tensor/255))\naxes[3].imshow(image_tf_tensor*mask)\n\nprint(tf.reduce_max(tf.image.per_image_standardization(image_tf_tensor/255)))","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:01:54.796099Z","iopub.execute_input":"2023-12-15T18:01:54.796659Z","iopub.status.idle":"2023-12-15T18:01:57.067248Z","shell.execute_reply.started":"2023-12-15T18:01:54.796613Z","shell.execute_reply":"2023-12-15T18:01:57.066181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This means we need to figure out the distribution of images with masks?","metadata":{}},{"cell_type":"code","source":"masks = tf.map_fn(lambda x: tf.reduce_sum(tf.keras.utils.load_img(x.numpy())), \n          tf.convert_to_tensor(train_label_paths), dtype=tf.uint8)","metadata":{"execution":{"iopub.status.busy":"2023-12-15T17:36:23.541282Z","iopub.execute_input":"2023-12-15T17:36:23.541841Z","iopub.status.idle":"2023-12-15T17:40:33.214665Z","shell.execute_reply.started":"2023-12-15T17:36:23.541784Z","shell.execute_reply":"2023-12-15T17:40:33.213132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pandas import Series\n\nmasks_data = Series(masks.numpy())","metadata":{"execution":{"iopub.status.busy":"2023-12-15T17:45:06.111251Z","iopub.execute_input":"2023-12-15T17:45:06.111803Z","iopub.status.idle":"2023-12-15T17:45:06.119840Z","shell.execute_reply.started":"2023-12-15T17:45:06.111760Z","shell.execute_reply":"2023-12-15T17:45:06.118036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks_data.describe()","metadata":{"execution":{"iopub.status.busy":"2023-12-15T17:45:21.287158Z","iopub.execute_input":"2023-12-15T17:45:21.287960Z","iopub.status.idle":"2023-12-15T17:45:21.308007Z","shell.execute_reply.started":"2023-12-15T17:45:21.287877Z","shell.execute_reply":"2023-12-15T17:45:21.306662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks_data.value_counts().iloc[0]","metadata":{"execution":{"iopub.status.busy":"2023-12-15T17:50:23.881106Z","iopub.execute_input":"2023-12-15T17:50:23.881958Z","iopub.status.idle":"2023-12-15T17:50:23.892230Z","shell.execute_reply.started":"2023-12-15T17:50:23.881890Z","shell.execute_reply":"2023-12-15T17:50:23.890246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks_data.plot(kind='hist', color='black')","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:03:20.724514Z","iopub.execute_input":"2023-12-15T18:03:20.725100Z","iopub.status.idle":"2023-12-15T18:03:21.039220Z","shell.execute_reply.started":"2023-12-15T18:03:20.725056Z","shell.execute_reply":"2023-12-15T18:03:21.037740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"from pandas import DataFrame\n\nvalid_paths = DataFrame(train_image_paths).query('@masks_data.reset_index(drop=True)>=50').reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:03:52.678071Z","iopub.execute_input":"2023-12-15T18:03:52.678681Z","iopub.status.idle":"2023-12-15T18:03:52.693377Z","shell.execute_reply.started":"2023-12-15T18:03:52.678635Z","shell.execute_reply":"2023-12-15T18:03:52.692040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_paths","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:03:55.105578Z","iopub.execute_input":"2023-12-15T18:03:55.106164Z","iopub.status.idle":"2023-12-15T18:03:55.124037Z","shell.execute_reply.started":"2023-12-15T18:03:55.106115Z","shell.execute_reply":"2023-12-15T18:03:55.122056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_paths.to_csv('valid_paths.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:04:00.603883Z","iopub.execute_input":"2023-12-15T18:04:00.605292Z","iopub.status.idle":"2023-12-15T18:04:00.644900Z","shell.execute_reply.started":"2023-12-15T18:04:00.605240Z","shell.execute_reply":"2023-12-15T18:04:00.643319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}