{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":24761,"databundleVersionId":2001871,"sourceType":"competition"}],"dockerImageVersionId":31041,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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\nimport numpy as np # linear algebra\nimport 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\nimport os\nfor 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","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T15:25:08.434156Z","iopub.execute_input":"2025-06-22T15:25:08.434407Z","iopub.status.idle":"2025-06-22T15:25:14.079536Z","shell.execute_reply.started":"2025-06-22T15:25:08.434381Z","shell.execute_reply":"2025-06-22T15:25:14.078699Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport json\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nfrom sklearn.model_selection import train_test_split\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset, DataLoader\nimport torchvision.transforms as T\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-22T15:35:14.297119Z","iopub.execute_input":"2025-06-22T15:35:14.297855Z","iopub.status.idle":"2025-06-22T15:35:14.689647Z","shell.execute_reply.started":"2025-06-22T15:35:14.297826Z","shell.execute_reply":"2025-06-22T15:35:14.689101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 1. Load JSON File\nwith open('/kaggle/input/deforestation/train.json') as f:\n    train_label = json.load(f)\n\n# Ambil entry pertama\nsample = train_label['0']\nbase_path = '/kaggle/input/deforestation/train/public/'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-22T15:39:44.197289Z","iopub.execute_input":"2025-06-22T15:39:44.197861Z","iopub.status.idle":"2025-06-22T15:39:44.205037Z","shell.execute_reply.started":"2025-06-22T15:39:44.197840Z","shell.execute_reply":"2025-06-22T15:39:44.204358Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 2. Load file .npy (img_t0, img_t1, mask)\nimg_t0 = np.load(os.path.join(base_path, sample['files']['satellite_img_first']))  # (512, 512, 13)\nimg_t1 = np.load(os.path.join(base_path, sample['files']['satellite_img_second'])) # (512, 512, 13)\nmask = np.load(os.path.join(base_path, sample['files']['mask']))                   # (512, 512)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-22T15:41:41.383216Z","iopub.execute_input":"2025-06-22T15:41:41.383518Z","iopub.status.idle":"2025-06-22T15:41:41.399518Z","shell.execute_reply.started":"2025-06-22T15:41:41.383496Z","shell.execute_reply":"2025-06-22T15:41:41.398907Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 3. Normalisasi dan Ambil RGB (B4, B3, B2) → Index 3, 2, 1\ndef normalize(x):\n    x = x.astype(np.float32)\n    x_min = x.min()\n    x_max = x.max()\n    return (x - x_min) / (x_max - x_min + 1e-6)\n\n# Ambil band RGB\nimg_t0_rgb = img_t0[:, :, [3, 2, 1]]\nimg_t1_rgb = img_t1[:, :, [3, 2, 1]]\n\n# Normalisasi ke 0–1\nimg_t0_rgb = normalize(img_t0_rgb)\nimg_t1_rgb = normalize(img_t1_rgb)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-22T15:41:56.373285Z","iopub.execute_input":"2025-06-22T15:41:56.374048Z","iopub.status.idle":"2025-06-22T15:41:56.397024Z","shell.execute_reply.started":"2025-06-22T15:41:56.374024Z","shell.execute_reply":"2025-06-22T15:41:56.396228Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 4. Tampilkan Gambar\nplt.figure(figsize=(18, 6))\n\nplt.subplot(1, 3, 1)\nplt.imshow(img_t0_rgb)\nplt.title(\"🟩 Sebelum Deforestasi (T0)\")\n\nplt.subplot(1, 3, 2)\nplt.imshow(img_t1_rgb)\nplt.title(\"🟥 Setelah Deforestasi (T1)\")\n\nplt.subplot(1, 3, 3)\nplt.imshow(mask, cmap='gray')\nplt.title(\"⬛ Mask Deforestasi\")\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-22T15:42:27.857275Z","iopub.execute_input":"2025-06-22T15:42:27.857994Z","iopub.status.idle":"2025-06-22T15:42:28.798431Z","shell.execute_reply.started":"2025-06-22T15:42:27.857968Z","shell.execute_reply":"2025-06-22T15:42:28.797359Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 5. Hitung Luasan Deforestasi\n\n# Jumlah piksel bernilai 1\ndef_pixels = np.sum(mask == 1)\n\n# Luas 1 piksel Sentinel-2 (10m × 10m) = 100 m²\narea_per_pixel_m2 = 100\n\n# Total luas deforestasi\narea_def_m2 = def_pixels * area_per_pixel_m2\narea_def_ha = area_def_m2 / 10_000  # 1 ha = 10,000 m²\n\nprint(f\"Jumlah piksel deforestasi: {def_pixels}\")\nprint(f\"Luas deforestasi: {area_def_m2:,.0f} m² ({area_def_ha:.2f} hektar)\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-22T15:43:36.759976Z","iopub.execute_input":"2025-06-22T15:43:36.760753Z","iopub.status.idle":"2025-06-22T15:43:36.766601Z","shell.execute_reply.started":"2025-06-22T15:43:36.760728Z","shell.execute_reply":"2025-06-22T15:43:36.765836Z"}},"outputs":[],"execution_count":null}]}