{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "Nm0samcYYItm" }, "outputs": [], "source": [ "# !pip install roboflow -q" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "-IJMSzpIYWQh" }, "outputs": [], "source": [ "import tensorflow as tf\n", "import numpy as np\n", "import pathlib\n", "import matplotlib.pyplot as plt\n", "# from roboflow import Roboflow\n", "import zipfile\n", "import seaborn as sns\n", "from tensorflow.keras import layers, Model\n", "from sklearn.metrics import confusion_matrix\n", "from sklearn.utils import class_weight" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "csjG-tFKYXa7", "outputId": "8f0a4d1b-f806-441a-c389-9fd783f86868" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Mounted at /content/drive\n" ] } ], "source": [ "# 1. Konfigurasi dataset\n", "# -------------------------------------------------------------------\n", "# rf = Roboflow(api_key=\"XJvBFxKWavgQgkG2i7pF\")\n", "# project = rf.workspace(\"jeo\").project(\"calmscope-pvsog\")\n", "# version = project.version(1)\n", "# dataset = version.download(\"yolov12\")\n", "\n", "from google.colab import drive\n", "drive.mount('/content/drive')\n", "\n", "zip_path = '/content/drive/MyDrive/calmscope_black_white/calmscope_black_white.zip'\n", "with zipfile.ZipFile(zip_path,'r') as zip_ref:\n", " zip_ref.extractall('/content/calmscope_black_white')\n", "\n", "IMG_SIZE = (224, 224)\n", "BATCH_SIZE = 32\n", "DATA_DIR = \"calmscope_black_white/calmscope_black_white\" # folder utama dari Roboflow" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "ssQpngZ9YZgK" }, "outputs": [], "source": [ "# 2. Fungsi load file dan label\n", "# Label diambil dari prefix: Negative-..., Positive-...\n", "# -------------------------------------------------------------------\n", "def load_images_yolo_style(directory):\n", " \"\"\"\n", " Membaca gambar dari folder 'images' dan mengambil label dari\n", " prefix nama file (negative / positive).\n", " \"\"\"\n", " data_dir = pathlib.Path(directory)\n", "\n", " image_files = []\n", " for ext in [\"*.jpg\", \"*.jpeg\", \"*.png\"]:\n", " image_files += list(data_dir.glob(ext))\n", "\n", " file_paths = []\n", " labels = []\n", "\n", " for img_path in image_files:\n", " name = img_path.name.lower()\n", "\n", " if name.startswith(\"negative\"):\n", " labels.append(0) # 0 = depressed\n", " elif name.startswith(\"positive\"):\n", " labels.append(1) # 1 = neutral\n", " else:\n", " # file yang tidak punya prefix akan dilewati\n", " print(\"Peringatan, dilewati:\", img_path.name)\n", " continue\n", "\n", " file_paths.append(str(img_path))\n", "\n", " return file_paths, labels" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "SARjeni9YbeC" }, "outputs": [], "source": [ "# 3. Fungsi membuat tf.data.Dataset\n", "# Preprocessing langsung pakai preprocess_input EfficientNet\n", "# -------------------------------------------------------------------\n", "def create_dataset_from_paths(file_paths, labels,\n", " batch_size=BATCH_SIZE,\n", " shuffle=True,\n", " augment=False):\n", "\n", " # fungsi untuk load satu gambar\n", " def load_and_preprocess(path, label):\n", " img = tf.io.read_file(path)\n", " img = tf.image.decode_jpeg(img, channels=3)\n", " img = tf.image.resize(img, IMG_SIZE)\n", "\n", " # cast ke float32\n", " img = tf.cast(img, tf.float32)\n", "\n", " # preprocessing untuk EfficientNet\n", " img = tf.keras.applications.efficientnet.preprocess_input(img)\n", "\n", " return img, label\n", "\n", " dataset = tf.data.Dataset.from_tensor_slices((file_paths, labels))\n", "\n", " if shuffle:\n", " dataset = dataset.shuffle(len(file_paths), seed=42)\n", "\n", " dataset = dataset.map(load_and_preprocess,\n", " num_parallel_calls=tf.data.AUTOTUNE)\n", "\n", " # augmentasi sederhana\n", " if augment:\n", " aug = tf.keras.Sequential([\n", " layers.RandomFlip(\"horizontal\"),\n", " layers.RandomRotation(0.1),\n", " layers.RandomZoom(0.1),\n", " layers.RandomTranslation(0.05, 0.05),\n", " ])\n", " dataset = dataset.map(lambda x, y: (aug(x, training=True), y),\n", " num_parallel_calls=tf.data.AUTOTUNE)\n", "\n", " dataset = dataset.batch(batch_size)\n", " dataset = dataset.prefetch(tf.data.AUTOTUNE)\n", " return dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ZT5ZpzUwYdnm", "outputId": "6d3844d8-7cb1-4a02-d081-1af0c20d01c3" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "--- Memuat dataset ---\n", "Train dataset: 3293 images\n", "Valid dataset: 952 images\n", "Test dataset : 995 images\n", "Train labels: {0, 1}\n", "Valid labels: {0, 1}\n", "Test labels : {0, 1}\n" ] } ], "source": [ "# 4. Memuat dataset train, valid, test\n", "# -------------------------------------------------------------------\n", "print(\"--- Memuat dataset ---\")\n", "\n", "train_paths, train_labels = load_images_yolo_style(f\"{DATA_DIR}/train/images\")\n", "val_paths, val_labels = load_images_yolo_style(f\"{DATA_DIR}/valid/images\")\n", "test_paths, test_labels = load_images_yolo_style(f\"{DATA_DIR}/test/images\")\n", "\n", "print(f\"Train dataset: {len(train_paths)} images\")\n", "print(f\"Valid dataset: {len(val_paths)} images\")\n", "print(f\"Test dataset : {len(test_paths)} images\")\n", "\n", "print(\"Train labels:\", set(train_labels))\n", "print(\"Valid labels:\", set(val_labels))\n", "print(\"Test labels :\", set(test_labels))\n", "\n", "train_ds = create_dataset_from_paths(train_paths, train_labels,\n", " batch_size=BATCH_SIZE,\n", " shuffle=True,\n", " augment=True)\n", "\n", "val_ds = create_dataset_from_paths(val_paths, val_labels,\n", " batch_size=BATCH_SIZE,\n", " shuffle=False,\n", " augment=False)\n", "\n", "test_ds = create_dataset_from_paths(test_paths, test_labels,\n", " batch_size=BATCH_SIZE,\n", " shuffle=False,\n", " augment=False)\n", "\n", "class_names = [\"depressed\", \"neutral\"]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "amvWpJYbYfhQ", "outputId": "2104e4c5-2d8d-4caf-be96-9c6fc3b2a860" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Class weights: {np.int64(0): np.float64(0.9229260089686099), np.int64(1): np.float64(1.0911199469847581)}\n" ] } ], "source": [ "# 5. Hitung class weight untuk mengatasi imbalance\n", "# -------------------------------------------------------------------\n", "unique_classes = np.unique(train_labels)\n", "cw = class_weight.compute_class_weight(\n", " class_weight=\"balanced\",\n", " classes=unique_classes,\n", " y=train_labels\n", ")\n", "class_weight_dict = dict(zip(unique_classes, cw))\n", "print(\"Class weights:\", class_weight_dict)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 425 }, "id": "sGWa68JzYhYd", "outputId": "e1c8200d-fede-4bc8-a1aa-c83a5ba3d80b" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "--- Membangun EfficientNetB0 ---\n", "Downloading data from https://storage.googleapis.com/keras-applications/efficientnetb0_notop.h5\n", "\u001b[1m16705208/16705208\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m2s\u001b[0m 0us/step\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1mModel: \"EfficientNetB0_depression\"\u001b[0m\n" ], "text/html": [ "
Model: \"EfficientNetB0_depression\"\n",
"\n"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n",
"┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\n",
"┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n",
"│ input (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m224\u001b[0m, \u001b[38;5;34m224\u001b[0m, \u001b[38;5;34m3\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ efficientnetb0 (\u001b[38;5;33mFunctional\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m7\u001b[0m, \u001b[38;5;34m7\u001b[0m, \u001b[38;5;34m1280\u001b[0m) │ \u001b[38;5;34m4,049,571\u001b[0m │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ gap (\u001b[38;5;33mGlobalAveragePooling2D\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m1280\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ dropout1 (\u001b[38;5;33mDropout\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m1280\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ fc1 (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m256\u001b[0m) │ \u001b[38;5;34m327,936\u001b[0m │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ dropout2 (\u001b[38;5;33mDropout\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m256\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ output (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m1\u001b[0m) │ \u001b[38;5;34m257\u001b[0m │\n",
"└─────────────────────────────────┴────────────────────────┴───────────────┘\n"
],
"text/html": [
"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n",
"┃ Layer (type) ┃ Output Shape ┃ Param # ┃\n",
"┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n",
"│ input (InputLayer) │ (None, 224, 224, 3) │ 0 │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ efficientnetb0 (Functional) │ (None, 7, 7, 1280) │ 4,049,571 │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ gap (GlobalAveragePooling2D) │ (None, 1280) │ 0 │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ dropout1 (Dropout) │ (None, 1280) │ 0 │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ fc1 (Dense) │ (None, 256) │ 327,936 │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ dropout2 (Dropout) │ (None, 256) │ 0 │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ output (Dense) │ (None, 1) │ 257 │\n",
"└─────────────────────────────────┴────────────────────────┴───────────────┘\n",
"\n"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"\u001b[1m Total params: \u001b[0m\u001b[38;5;34m4,377,764\u001b[0m (16.70 MB)\n"
],
"text/html": [
"Total params: 4,377,764 (16.70 MB)\n", "\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m328,193\u001b[0m (1.25 MB)\n" ], "text/html": [ "
Trainable params: 328,193 (1.25 MB)\n", "\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m4,049,571\u001b[0m (15.45 MB)\n" ], "text/html": [ "
Non-trainable params: 4,049,571 (15.45 MB)\n", "\n" ] }, "metadata": {} } ], "source": [ "# 6. Membangun model EfficientNetB0\n", "# -------------------------------------------------------------------\n", "print(\"\\n--- Membangun EfficientNetB0 ---\")\n", "\n", "base_model = tf.keras.applications.EfficientNetB0(\n", " include_top=False,\n", " weights=\"imagenet\",\n", " input_shape=(IMG_SIZE[0], IMG_SIZE[1], 3) # Changed from 1 to 3\n", ")\n", "\n", "base_model.trainable = False # tahap pertama, freeze\n", "\n", "inputs = tf.keras.Input(shape=(IMG_SIZE[0], IMG_SIZE[1], 3), name=\"input\") # Changed from 1 to 3\n", "\n", "# input sudah di-preprocess di dataset, jadi langsung ke base_model\n", "x = base_model(inputs, training=False)\n", "x = layers.GlobalAveragePooling2D(name=\"gap\")(x)\n", "x = layers.Dropout(0.2, name=\"dropout1\")(x)\n", "x = layers.Dense(256, activation=\"relu\", name=\"fc1\")(x)\n", "x = layers.Dropout(0.2, name=\"dropout2\")(x)\n", "outputs = layers.Dense(1, activation=\"sigmoid\", name=\"output\")(x)\n", "\n", "model = Model(inputs, outputs, name=\"EfficientNetB0_depression\")\n", "\n", "model.compile(\n", " optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),\n", " loss=\"binary_crossentropy\",\n", " metrics=[\"accuracy\"]\n", ")\n", "\n", "model.summary()\n", "\n", "# -------------------------------------------------------------------\n", "# 7. Callback\n", "# -------------------------------------------------------------------\n", "early_stop = tf.keras.callbacks.EarlyStopping(\n", " monitor=\"val_loss\",\n", " patience=5,\n", " restore_best_weights=True,\n", " verbose=1\n", ")\n", "\n", "reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(\n", " monitor=\"val_loss\",\n", " factor=0.2,\n", " patience=3,\n", " verbose=1\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "4M-qdcMwYjFh", "outputId": "a583002e-e223-45ee-fb47-2f0c4f403a8e" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "--- Training tahap 1 (transfer learning) ---\n", "Epoch 1/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m112s\u001b[0m 776ms/step - accuracy: 0.5458 - loss: 0.6877 - val_accuracy: 0.6481 - val_loss: 0.6285 - learning_rate: 1.0000e-04\n", "Epoch 2/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m46s\u001b[0m 444ms/step - accuracy: 0.6579 - loss: 0.6133 - val_accuracy: 0.6733 - val_loss: 0.5903 - learning_rate: 1.0000e-04\n", "Epoch 3/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m82s\u001b[0m 449ms/step - accuracy: 0.6775 - loss: 0.5901 - val_accuracy: 0.6733 - val_loss: 0.5803 - learning_rate: 1.0000e-04\n", "Epoch 4/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m46s\u001b[0m 446ms/step - accuracy: 0.6729 - loss: 0.5801 - val_accuracy: 0.6996 - val_loss: 0.5731 - learning_rate: 1.0000e-04\n", "Epoch 5/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m46s\u001b[0m 448ms/step - accuracy: 0.7073 - loss: 0.5573 - val_accuracy: 0.6922 - val_loss: 0.5694 - learning_rate: 1.0000e-04\n", "Epoch 6/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m48s\u001b[0m 461ms/step - accuracy: 0.7101 - loss: 0.5639 - val_accuracy: 0.7069 - val_loss: 0.5644 - learning_rate: 1.0000e-04\n", "Epoch 7/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m46s\u001b[0m 446ms/step - accuracy: 0.7079 - loss: 0.5413 - val_accuracy: 0.7090 - val_loss: 0.5572 - learning_rate: 1.0000e-04\n", "Epoch 8/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m47s\u001b[0m 452ms/step - accuracy: 0.6922 - loss: 0.5682 - val_accuracy: 0.7017 - val_loss: 0.5610 - learning_rate: 1.0000e-04\n", "Epoch 9/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m46s\u001b[0m 445ms/step - accuracy: 0.7111 - loss: 0.5408 - val_accuracy: 0.6985 - val_loss: 0.5598 - learning_rate: 1.0000e-04\n", "Epoch 10/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 430ms/step - accuracy: 0.7248 - loss: 0.5497\n", "Epoch 10: ReduceLROnPlateau reducing learning rate to 1.9999999494757503e-05.\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m45s\u001b[0m 441ms/step - accuracy: 0.7248 - loss: 0.5496 - val_accuracy: 0.7038 - val_loss: 0.5575 - learning_rate: 1.0000e-04\n", "Epoch 11/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m47s\u001b[0m 459ms/step - accuracy: 0.7039 - loss: 0.5503 - val_accuracy: 0.7069 - val_loss: 0.5571 - learning_rate: 2.0000e-05\n", "Epoch 12/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m46s\u001b[0m 444ms/step - accuracy: 0.7143 - loss: 0.5371 - val_accuracy: 0.7048 - val_loss: 0.5549 - learning_rate: 2.0000e-05\n", "Epoch 13/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m46s\u001b[0m 445ms/step - accuracy: 0.7162 - loss: 0.5389 - val_accuracy: 0.7059 - val_loss: 0.5536 - learning_rate: 2.0000e-05\n", "Epoch 14/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m84s\u001b[0m 459ms/step - accuracy: 0.7445 - loss: 0.5143 - val_accuracy: 0.7017 - val_loss: 0.5530 - learning_rate: 2.0000e-05\n", "Epoch 15/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m46s\u001b[0m 444ms/step - accuracy: 0.7146 - loss: 0.5394 - val_accuracy: 0.7111 - val_loss: 0.5573 - learning_rate: 2.0000e-05\n", "Epoch 16/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m47s\u001b[0m 457ms/step - accuracy: 0.7188 - loss: 0.5315 - val_accuracy: 0.7048 - val_loss: 0.5522 - learning_rate: 2.0000e-05\n", "Epoch 17/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m81s\u001b[0m 445ms/step - accuracy: 0.7351 - loss: 0.5206 - val_accuracy: 0.7111 - val_loss: 0.5535 - learning_rate: 2.0000e-05\n", "Epoch 18/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m82s\u001b[0m 443ms/step - accuracy: 0.7271 - loss: 0.5327 - val_accuracy: 0.7059 - val_loss: 0.5523 - learning_rate: 2.0000e-05\n", "Epoch 19/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 441ms/step - accuracy: 0.7327 - loss: 0.5149\n", "Epoch 19: ReduceLROnPlateau reducing learning rate to 3.999999898951501e-06.\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m83s\u001b[0m 454ms/step - accuracy: 0.7327 - loss: 0.5149 - val_accuracy: 0.7122 - val_loss: 0.5542 - learning_rate: 2.0000e-05\n", "Epoch 20/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m46s\u001b[0m 443ms/step - accuracy: 0.7554 - loss: 0.5063 - val_accuracy: 0.7132 - val_loss: 0.5514 - learning_rate: 4.0000e-06\n", "Epoch 21/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m47s\u001b[0m 459ms/step - accuracy: 0.7209 - loss: 0.5302 - val_accuracy: 0.7101 - val_loss: 0.5507 - learning_rate: 4.0000e-06\n", "Epoch 22/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m46s\u001b[0m 442ms/step - accuracy: 0.7277 - loss: 0.5231 - val_accuracy: 0.7122 - val_loss: 0.5499 - learning_rate: 4.0000e-06\n", "Epoch 23/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m46s\u001b[0m 445ms/step - accuracy: 0.7335 - loss: 0.5188 - val_accuracy: 0.7080 - val_loss: 0.5496 - learning_rate: 4.0000e-06\n", "Epoch 24/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m83s\u001b[0m 456ms/step - accuracy: 0.7289 - loss: 0.5177 - val_accuracy: 0.7111 - val_loss: 0.5504 - learning_rate: 4.0000e-06\n", "Epoch 25/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m46s\u001b[0m 444ms/step - accuracy: 0.7310 - loss: 0.5193 - val_accuracy: 0.7143 - val_loss: 0.5510 - learning_rate: 4.0000e-06\n", "Epoch 26/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 447ms/step - accuracy: 0.7183 - loss: 0.5311\n", "Epoch 26: ReduceLROnPlateau reducing learning rate to 7.999999979801942e-07.\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m47s\u001b[0m 460ms/step - accuracy: 0.7184 - loss: 0.5310 - val_accuracy: 0.7143 - val_loss: 0.5499 - learning_rate: 4.0000e-06\n", "Epoch 27/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m80s\u001b[0m 444ms/step - accuracy: 0.7278 - loss: 0.5191 - val_accuracy: 0.7143 - val_loss: 0.5499 - learning_rate: 8.0000e-07\n", "Epoch 28/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m81s\u001b[0m 439ms/step - accuracy: 0.7228 - loss: 0.5238 - val_accuracy: 0.7153 - val_loss: 0.5498 - learning_rate: 8.0000e-07\n", "Epoch 28: early stopping\n", "Restoring model weights from the end of the best epoch: 23.\n" ] } ], "source": [ "# 8. Training tahap 1 - transfer learning\n", "# -------------------------------------------------------------------\n", "print(\"\\n--- Training tahap 1 (transfer learning) ---\")\n", "\n", "history_1 = model.fit(\n", " train_ds,\n", " validation_data=val_ds,\n", " epochs=50,\n", " callbacks=[early_stop, reduce_lr]\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "k4qUmeeCYkt6", "outputId": "2d99c6bf-7fba-4c41-841a-4d5046ceec82" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "--- Fine tuning EfficientNet ---\n", "Epoch 1/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m124s\u001b[0m 801ms/step - accuracy: 0.5802 - loss: 0.7179 - val_accuracy: 0.6891 - val_loss: 0.5782 - learning_rate: 5.0000e-06\n", "Epoch 2/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m48s\u001b[0m 470ms/step - accuracy: 0.6337 - loss: 0.6552 - val_accuracy: 0.6775 - val_loss: 0.5823 - learning_rate: 5.0000e-06\n", "Epoch 3/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m50s\u001b[0m 481ms/step - accuracy: 0.6444 - loss: 0.6290 - val_accuracy: 0.6775 - val_loss: 0.5861 - learning_rate: 5.0000e-06\n", "Epoch 4/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 457ms/step - accuracy: 0.6516 - loss: 0.6127\n", "Epoch 4: ReduceLROnPlateau reducing learning rate to 9.999999747378752e-07.\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m48s\u001b[0m 470ms/step - accuracy: 0.6517 - loss: 0.6126 - val_accuracy: 0.6859 - val_loss: 0.5806 - learning_rate: 5.0000e-06\n", "Epoch 5/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m83s\u001b[0m 481ms/step - accuracy: 0.6692 - loss: 0.5988 - val_accuracy: 0.6796 - val_loss: 0.5804 - learning_rate: 1.0000e-06\n", "Epoch 6/50\n", "\u001b[1m103/103\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m49s\u001b[0m 471ms/step - accuracy: 0.6720 - loss: 0.5947 - val_accuracy: 0.6849 - val_loss: 0.5800 - learning_rate: 1.0000e-06\n", "Epoch 6: early stopping\n", "Restoring model weights from the end of the best epoch: 1.\n" ] } ], "source": [ "# 9. Fine tuning EfficientNet\n", "# -------------------------------------------------------------------\n", "print(\"\\n--- Fine tuning EfficientNet ---\")\n", "\n", "base_model.trainable = True\n", "\n", "# Bekukan layer awal (lebih umum)\n", "fine_tune_at = 150\n", "for layer in base_model.layers[:fine_tune_at]:\n", " layer.trainable = False\n", "\n", "model.compile(\n", " optimizer=tf.keras.optimizers.Adam(learning_rate=5e-6),\n", " loss=\"binary_crossentropy\",\n", " metrics=[\"accuracy\"]\n", ")\n", "\n", "history_2 = model.fit(\n", " train_ds,\n", " validation_data=val_ds,\n", " epochs=50,\n", " # class_weight=class_weight_dict,\n", " callbacks=[early_stop, reduce_lr]\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "ktRijw1sYmSN", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "6b35fad2-86f7-4ffd-b92e-78b386687db4" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "--- Evaluasi di test set ---\n", "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m10s\u001b[0m 331ms/step - accuracy: 0.6732 - loss: 0.5686\n", "Test loss: 0.5519\n", "Test accuracy: 0.6894\n" ] } ], "source": [ "# 10. Evaluasi di test set\n", "# -------------------------------------------------------------------\n", "print(\"\\n--- Evaluasi di test set ---\")\n", "test_loss, test_acc = model.evaluate(test_ds)\n", "print(f\"Test loss: {test_loss:.4f}\")\n", "print(f\"Test accuracy: {test_acc:.4f}\")" ] }, { "cell_type": "code", "source": [ "model.save(\"model.h5\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "j9JWPx5b82YI", "outputId": "78a1d83f-f658-4208-971c-abe4ccf508bf" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "WARNING:absl:You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`. \n" ] } ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "vqKor3J7Ynrv", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "b0757963-0e75-494b-bb64-43824e46588e" }, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "