{"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":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":7029230,"sourceType":"datasetVersion","datasetId":4027203}],"dockerImageVersionId":30615,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\n# import numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"0.25 scale, with other","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport shutil\nimport os\nimport numpy as np\nimport glob\nimport random\nfrom tqdm import tqdm\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\nimport matplotlib.pyplot as plt\nimport PIL\nfrom joblib import Parallel, delayed","metadata":{"execution":{"iopub.status.busy":"2023-12-09T00:44:27.456458Z","iopub.execute_input":"2023-12-09T00:44:27.456798Z","iopub.status.idle":"2023-12-09T00:44:41.046793Z","shell.execute_reply.started":"2023-12-09T00:44:27.456766Z","shell.execute_reply":"2023-12-09T00:44:41.045907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tumor_thr = 0.3\n\n\ntrain_df = pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\n\nimg2lb = dict()\nfor i in range(len(train_df)):\n    img2lb[str(train_df.loc[i, 'image_id'])] = train_df.loc[i, 'label']\n    \n    \nos.mkdir('./train_dir/')\nos.mkdir('./train_dir/HGSC/')\nos.mkdir('./train_dir/LGSC/')\nos.mkdir('./train_dir/EC/')\nos.mkdir('./train_dir/CC/')\nos.mkdir('./train_dir/MC/')\nos.mkdir('./train_dir/Other/')\n\ndef process_image_and_mask(p):\n    img_path = '/kaggle/input/ubc-ocean-tiles-w-masks-2048px-scale-0-25/train_images/' + p + '/'\n    img_mask_path = '/kaggle/input/ubc-ocean-tiles-w-masks-2048px-scale-0-25/train_annotations/' + p + '/'\n    imgs = os.listdir(img_path)\n    label = img2lb[p]\n    os.mkdir('./train_dir/' + label + '/' + p + '/')\n    os.mkdir('./train_dir/' + \"Other\" + '/' + p + '/')\n    \n    for img in imgs:\n        img1 = PIL.Image.open(img_mask_path + img)\n        img_arr = np.array(img1)\n\n        if np.mean(img_arr[..., 0]) > tumor_thr:\n            shutil.copyfile(img_path + img, \"./train_dir/\" + label + \"/\" + p + \"/\" + img)\n        else:\n            shutil.copyfile(img_path + img, \"./train_dir/\" + \"Other\" + \"/\" + p + \"/\" + img)\n            \n            \nimg_paths = os.listdir(\"/kaggle/input/ubc-ocean-tiles-w-masks-2048px-scale-0-25/train_images/\")\n            \n_ = Parallel(n_jobs=os.cpu_count())(\n    delayed(process_image_and_mask)\n    (p) for p in tqdm(img_paths)\n)","metadata":{},"execution_count":null,"outputs":[]}]}