{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:25:39.568220Z","iopub.execute_input":"2026-08-06T17:25:39.568490Z","iopub.status.idle":"2026-08-06T17:25:41.410909Z","shell.execute_reply.started":"2026-08-06T17:25:39.568456Z","shell.execute_reply":"2026-08-06T17:25:41.409990Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define paths for Kaggle environment\nCSV_PATH = '/kaggle/input/competitions/aptos2019-blindness-detection/train.csv'\nIMG_DIR = '/kaggle/input/competitions/aptos2019-blindness-detection/train_images'\nPROCESSED_DIR = './preprocessed_train_images'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:25:41.412571Z","iopub.execute_input":"2026-08-06T17:25:41.413118Z","iopub.status.idle":"2026-08-06T17:25:41.418632Z","shell.execute_reply.started":"2026-08-06T17:25:41.413078Z","shell.execute_reply":"2026-08-06T17:25:41.417793Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.makedirs(PROCESSED_DIR, exist_ok=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:25:41.420147Z","iopub.execute_input":"2026-08-06T17:25:41.420479Z","iopub.status.idle":"2026-08-06T17:25:41.437266Z","shell.execute_reply.started":"2026-08-06T17:25:41.420451Z","shell.execute_reply":"2026-08-06T17:25:41.436203Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def crop_image_from_gray(img, tol=7):\n    if img.ndim == 2:\n        mask = img > tol\n        return img[np.ix_(mask.any(1), mask.any(0))]\n    elif img.ndim == 3:\n        gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n        mask = gray_img > tol\n        if img[:,:,0][np.ix_(mask.any(1), mask.any(0))].shape[0] == 0: return img \n        img1 = img[:,:,0][np.ix_(mask.any(1), mask.any(0))]\n        img2 = img[:,:,1][np.ix_(mask.any(1), mask.any(0))]\n        img3 = img[:,:,2][np.ix_(mask.any(1), mask.any(0))]\n        img = np.stack([img1, img2, img3], axis=-1)\n    return img","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:25:41.439601Z","iopub.execute_input":"2026-08-06T17:25:41.439984Z","iopub.status.idle":"2026-08-06T17:25:41.454567Z","shell.execute_reply.started":"2026-08-06T17:25:41.439954Z","shell.execute_reply":"2026-08-06T17:25:41.453581Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_ben_color(path, img_size=256, sigmaX=10):\n    image = cv2.imread(path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = crop_image_from_gray(image)\n    image = cv2.resize(image, (img_size, img_size))\n    image = cv2.addWeighted(image, 4, cv2.GaussianBlur(image, (0,0), sigmaX), -4, 128)\n    return image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:25:41.456369Z","iopub.execute_input":"2026-08-06T17:25:41.457138Z","iopub.status.idle":"2026-08-06T17:25:41.471715Z","shell.execute_reply.started":"2026-08-06T17:25:41.457095Z","shell.execute_reply":"2026-08-06T17:25:41.470712Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Starting Ben Graham Pre-processing...\")\ndf_etl = pd.read_csv(CSV_PATH)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:25:41.472868Z","iopub.execute_input":"2026-08-06T17:25:41.473296Z","iopub.status.idle":"2026-08-06T17:25:41.506332Z","shell.execute_reply.started":"2026-08-06T17:25:41.473267Z","shell.execute_reply":"2026-08-06T17:25:41.505580Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for idx, row in tqdm(df_etl.iterrows(), total=len(df_etl), desc=\"Processing Images\"):\n    img_name = row['id_code']\n    img_path = os.path.join(IMG_DIR, f\"{img_name}.png\")\n    \n    if os.path.exists(img_path):\n        processed_img = load_ben_color(img_path, img_size=256)\n        save_path = os.path.join(PROCESSED_DIR, f\"{img_name}.png\")\n        cv2.imwrite(save_path, cv2.cvtColor(processed_img, cv2.COLOR_RGB2BGR))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:25:41.507455Z","iopub.execute_input":"2026-08-06T17:25:41.507917Z","iopub.status.idle":"2026-08-06T17:39:30.300569Z","shell.execute_reply.started":"2026-08-06T17:25:41.507867Z","shell.execute_reply":"2026-08-06T17:39:30.299696Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Copy CSV over for easy loading later\ndf_etl.to_csv(os.path.join(PROCESSED_DIR, 'train.csv'), index=False)\nprint(\"ETL complete! All images saved to ./preprocessed_train_images\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:39:30.301671Z","iopub.execute_input":"2026-08-06T17:39:30.302325Z","iopub.status.idle":"2026-08-06T17:39:30.321022Z","shell.execute_reply.started":"2026-08-06T17:39:30.302297Z","shell.execute_reply":"2026-08-06T17:39:30.320244Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import json\nimport kagglehub","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:39:34.893529Z","iopub.execute_input":"2026-08-06T17:39:34.893906Z","iopub.status.idle":"2026-08-06T17:39:36.022634Z","shell.execute_reply.started":"2026-08-06T17:39:34.893876Z","shell.execute_reply":"2026-08-06T17:39:36.021856Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 1. Trigger the official Kaggle interactive login prompt\nprint(\"Please enter your credentials in the prompt below:\")\nkagglehub.login()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:39:36.023993Z","iopub.execute_input":"2026-08-06T17:39:36.024541Z","iopub.status.idle":"2026-08-06T17:39:36.043215Z","shell.execute_reply.started":"2026-08-06T17:39:36.024511Z","shell.execute_reply":"2026-08-06T17:39:36.042148Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 2. Dynamically locate the saved credentials file\nconfig_paths = [\n    os.path.expanduser('~/.config/kaggle/kaggle.json'),\n    os.path.expanduser('~/.kaggle/kaggle.json')\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:39:48.518565Z","iopub.execute_input":"2026-08-06T17:39:48.518902Z","iopub.status.idle":"2026-08-06T17:39:48.523764Z","shell.execute_reply.started":"2026-08-06T17:39:48.518874Z","shell.execute_reply":"2026-08-06T17:39:48.522795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"KAGGLE_USERNAME = None\nfor path in config_paths:\n    if os.path.exists(path):\n        with open(path, 'r') as f:\n            KAGGLE_USERNAME = json.load(f)['username']\n        break","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:39:50.278444Z","iopub.execute_input":"2026-08-06T17:39:50.279423Z","iopub.status.idle":"2026-08-06T17:39:50.284168Z","shell.execute_reply.started":"2026-08-06T17:39:50.279381Z","shell.execute_reply":"2026-08-06T17:39:50.283364Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Failsafe in case it's stored completely in memory\nif not KAGGLE_USERNAME:\n    KAGGLE_USERNAME = input(\"Could not locate config file. Please manually enter your Kaggle Username: \").strip()\n\nprint(f\"Successfully loaded username: {KAGGLE_USERNAME}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:39:52.738563Z","iopub.execute_input":"2026-08-06T17:39:52.738901Z","iopub.status.idle":"2026-08-06T17:39:58.121548Z","shell.execute_reply.started":"2026-08-06T17:39:52.738871Z","shell.execute_reply":"2026-08-06T17:39:58.120720Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 3. Configuration\nPROCESSED_DIR = './preprocessed_train_images'\nDATASET_NAME = \"aptos-2019-ben-graham-256\" # Use hyphens, no spaces","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:40:02.002499Z","iopub.execute_input":"2026-08-06T17:40:02.002892Z","iopub.status.idle":"2026-08-06T17:40:02.007747Z","shell.execute_reply.started":"2026-08-06T17:40:02.002860Z","shell.execute_reply":"2026-08-06T17:40:02.006877Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 4. Build the Kaggle metadata required for publishing\nmetadata = {\n    \"title\": \"APTOS 2019 Ben Graham 256x256\",\n    \"id\": f\"{KAGGLE_USERNAME}/{DATASET_NAME}\",\n    \"licenses\": [{\"name\": \"CC0-1.0\"}]\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:40:02.399443Z","iopub.execute_input":"2026-08-06T17:40:02.399798Z","iopub.status.idle":"2026-08-06T17:40:02.404300Z","shell.execute_reply.started":"2026-08-06T17:40:02.399751Z","shell.execute_reply":"2026-08-06T17:40:02.403452Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Write metadata file into the directory we are about to upload\nwith open(os.path.join(PROCESSED_DIR, 'dataset-metadata.json'), 'w') as f:\n    json.dump(metadata, f, indent=4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:40:04.738398Z","iopub.execute_input":"2026-08-06T17:40:04.738744Z","iopub.status.idle":"2026-08-06T17:40:04.744263Z","shell.execute_reply.started":"2026-08-06T17:40:04.738716Z","shell.execute_reply":"2026-08-06T17:40:04.743383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install --upgrade kaggle","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T18:03:14.138339Z","iopub.execute_input":"2026-08-06T18:03:14.138706Z","iopub.status.idle":"2026-08-06T18:03:22.194700Z","shell.execute_reply.started":"2026-08-06T18:03:14.138668Z","shell.execute_reply":"2026-08-06T18:03:22.193590Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!zip -r processed_dataset.zip {PROCESSED_DIR}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T18:03:36.925398Z","iopub.execute_input":"2026-08-06T18:03:36.925803Z","iopub.status.idle":"2026-08-06T18:03:54.566230Z","shell.execute_reply.started":"2026-08-06T18:03:36.925743Z","shell.execute_reply":"2026-08-06T18:03:54.565150Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!mv {PROCESSED_DIR}/dataset-metadata.json ./dataset-metadata.json","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T18:04:43.733993Z","iopub.execute_input":"2026-08-06T18:04:43.735529Z","iopub.status.idle":"2026-08-06T18:04:43.884243Z","shell.execute_reply.started":"2026-08-06T18:04:43.735468Z","shell.execute_reply":"2026-08-06T18:04:43.882955Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!kaggle datasets create -p . -q","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T18:05:02.012969Z","iopub.execute_input":"2026-08-06T18:05:02.013347Z","iopub.status.idle":"2026-08-06T18:05:05.825696Z","shell.execute_reply.started":"2026-08-06T18:05:02.013306Z","shell.execute_reply":"2026-08-06T18:05:05.824732Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!kaggle datasets version -p . -m \"Updating preprocessed dataset files\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T18:06:46.076426Z","iopub.execute_input":"2026-08-06T18:06:46.077006Z","iopub.status.idle":"2026-08-06T18:06:49.513675Z","shell.execute_reply.started":"2026-08-06T18:06:46.076953Z","shell.execute_reply":"2026-08-06T18:06:49.512691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 5. Trigger the Kaggle CLI to zip and upload the folder instantly\n!kaggle datasets create -p {PROCESSED_DIR} --dir-mode zip -q","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-06T17:40:05.420441Z","iopub.execute_input":"2026-08-06T17:40:05.421225Z","iopub.status.idle":"2026-08-06T18:01:33.884115Z","shell.execute_reply.started":"2026-08-06T17:40:05.421178Z","shell.execute_reply":"2026-08-06T18:01:33.882964Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}