{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":16880,"databundleVersionId":858837,"sourceType":"competition"},{"sourceId":924245,"sourceType":"datasetVersion","datasetId":464091}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# ENV Setup","metadata":{}},{"cell_type":"code","source":"IMAGE_SIZE=224\nBATCH_SIZE=32\nEPOCHS=10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:17.564991Z","iopub.execute_input":"2025-07-17T09:23:17.565973Z","iopub.status.idle":"2025-07-17T09:23:17.569292Z","shell.execute_reply.started":"2025-07-17T09:23:17.565940Z","shell.execute_reply":"2025-07-17T09:23:17.568671Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nimport os\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:17.604029Z","iopub.execute_input":"2025-07-17T09:23:17.604279Z","iopub.status.idle":"2025-07-17T09:23:17.609517Z","shell.execute_reply.started":"2025-07-17T09:23:17.604252Z","shell.execute_reply":"2025-07-17T09:23:17.608331Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/deepfake-faces/metadata.csv')\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:17.628765Z","iopub.execute_input":"2025-07-17T09:23:17.628956Z","iopub.status.idle":"2025-07-17T09:23:17.726972Z","shell.execute_reply.started":"2025-07-17T09:23:17.628941Z","shell.execute_reply":"2025-07-17T09:23:17.726165Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def classify_features(df):\n    categorical_features = []\n    non_categorical_features = []\n    discrete_features = []\n    continuous_features = []\n\n    for column in df.columns:\n        if df[column].dtype == 'object':\n            if df[column].nunique() < 10:\n                categorical_features.append(column)\n            else:\n                non_categorical_features.append(column)\n        elif df[column].dtype in ['int64', 'float64']:\n            if df[column].nunique() < 10:\n                discrete_features.append(column)\n            else:\n                continuous_features.append(column)\n\n    return categorical_features, non_categorical_features, discrete_features, continuous_features","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:17.728204Z","iopub.execute_input":"2025-07-17T09:23:17.728452Z","iopub.status.idle":"2025-07-17T09:23:17.734067Z","shell.execute_reply.started":"2025-07-17T09:23:17.728433Z","shell.execute_reply":"2025-07-17T09:23:17.733204Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical, non_categorical, discrete, continuous = classify_features(df)\nprint(\"Categorical Features:\", categorical)\nprint(\"Non-Categorical Features:\", non_categorical)\nprint(\"Discrete Features:\", discrete)\nprint(\"Continuous Features:\", continuous)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:17.734971Z","iopub.execute_input":"2025-07-17T09:23:17.735362Z","iopub.status.idle":"2025-07-17T09:23:17.779612Z","shell.execute_reply.started":"2025-07-17T09:23:17.735330Z","shell.execute_reply":"2025-07-17T09:23:17.778857Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:17.781336Z","iopub.execute_input":"2025-07-17T09:23:17.781581Z","iopub.status.idle":"2025-07-17T09:23:17.800937Z","shell.execute_reply.started":"2025-07-17T09:23:17.781564Z","shell.execute_reply":"2025-07-17T09:23:17.800106Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.fillna(\"Not Available\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:17.801699Z","iopub.execute_input":"2025-07-17T09:23:17.802031Z","iopub.status.idle":"2025-07-17T09:23:17.846948Z","shell.execute_reply.started":"2025-07-17T09:23:17.801991Z","shell.execute_reply":"2025-07-17T09:23:17.846190Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in categorical:\n    print(i,':', df[i].unique())\n    print()\nfor i in categorical:\n    print(df[i].value_counts())\n    print()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:17.847715Z","iopub.execute_input":"2025-07-17T09:23:17.847989Z","iopub.status.idle":"2025-07-17T09:23:17.865462Z","shell.execute_reply.started":"2025-07-17T09:23:17.847967Z","shell.execute_reply":"2025-07-17T09:23:17.864681Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data visualizing","metadata":{}},{"cell_type":"code","source":"for i in categorical:\n    plt.figure(figsize=(15,6))\n    sns.countplot(x = df[i], data = df, palette = 'hls')\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:17.866388Z","iopub.execute_input":"2025-07-17T09:23:17.866707Z","iopub.status.idle":"2025-07-17T09:23:18.086302Z","shell.execute_reply.started":"2025-07-17T09:23:17.866685Z","shell.execute_reply":"2025-07-17T09:23:18.085535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in categorical:\n    plt.figure(figsize=(30,20)) \n    plt.pie(df[i].value_counts(), labels=df[i].value_counts().index, \n            autopct='%1.1f%%', textprops={ 'fontsize': 20,\n                                           'color': 'black',\n                                           'weight': 'bold',\n                                           'family': 'serif' }) \n    hfont = {'fontname':'serif', 'weight': 'bold'}\n    plt.title(i, size=20, **hfont) \n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:18.088410Z","iopub.execute_input":"2025-07-17T09:23:18.088679Z","iopub.status.idle":"2025-07-17T09:23:18.327080Z","shell.execute_reply.started":"2025-07-17T09:23:18.088661Z","shell.execute_reply":"2025-07-17T09:23:18.326337Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data balancing","metadata":{}},{"cell_type":"code","source":"real_df = df[df[\"label\"] == \"REAL\"]\nfake_df = df[df[\"label\"] == \"FAKE\"]\nsample_size = 10000\n\nreal_df = real_df.sample(sample_size, random_state=42)\nfake_df = fake_df.sample(sample_size, random_state=42)\n\nsample_meta = pd.concat([real_df, fake_df])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:18.327927Z","iopub.execute_input":"2025-07-17T09:23:18.328172Z","iopub.status.idle":"2025-07-17T09:23:18.361313Z","shell.execute_reply.started":"2025-07-17T09:23:18.328147Z","shell.execute_reply":"2025-07-17T09:23:18.360502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nTrain_set, Test_set = train_test_split(sample_meta,test_size=0.2,random_state=42,stratify=sample_meta['label'])\nTrain_set, Val_set  = train_test_split(Train_set,test_size=0.3,random_state=42,stratify=Train_set['label'])\nTrain_set.shape,Val_set.shape,Test_set.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:18.362295Z","iopub.execute_input":"2025-07-17T09:23:18.362638Z","iopub.status.idle":"2025-07-17T09:23:18.398198Z","shell.execute_reply.started":"2025-07-17T09:23:18.362620Z","shell.execute_reply":"2025-07-17T09:23:18.397450Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\n\nimage_path = '/kaggle/input/deepfake-faces/faces_224/'\n\nimage_files = os.listdir(image_path)\n\nimage_files.sort()\n\nselected_images = image_files[:9]\n\nplt.figure(figsize=(10, 10))\n\nfor index, image_file in enumerate(selected_images):\n    image = cv2.imread(os.path.join(image_path, image_file))\n\n    plt.subplot(3, 3, index + 1)\n    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n    plt.title(f'Image {index + 1}')\n    plt.axis('off')\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:18.398951Z","iopub.execute_input":"2025-07-17T09:23:18.399377Z","iopub.status.idle":"2025-07-17T09:23:20.377669Z","shell.execute_reply.started":"2025-07-17T09:23:18.399359Z","shell.execute_reply":"2025-07-17T09:23:20.376942Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def retreive_dataset(set_name):\n    images,labels=[],[]\n    for (img, imclass) in zip(set_name['videoname'], set_name['label']):\n        images.append(cv2.imread('../input/deepfake-faces/faces_224/'+img[:-4]+'.jpg'))\n        if(imclass=='FAKE'):\n            labels.append(1)\n        else:\n            labels.append(0)\n    \n    return np.array(images),np.array(labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:20.378366Z","iopub.execute_input":"2025-07-17T09:23:20.378614Z","iopub.status.idle":"2025-07-17T09:23:20.384192Z","shell.execute_reply.started":"2025-07-17T09:23:20.378595Z","shell.execute_reply":"2025-07-17T09:23:20.383521Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train,y_train=retreive_dataset(Train_set)\nX_val,y_val=retreive_dataset(Val_set)\nX_test,y_test=retreive_dataset(Test_set)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:23:20.385023Z","iopub.execute_input":"2025-07-17T09:23:20.385281Z","iopub.status.idle":"2025-07-17T09:25:51.638839Z","shell.execute_reply.started":"2025-07-17T09:23:20.385260Z","shell.execute_reply":"2025-07-17T09:25:51.638244Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Modeling","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom functools import partial\n\ntf.random.set_seed(42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:25:51.639693Z","iopub.execute_input":"2025-07-17T09:25:51.639890Z","iopub.status.idle":"2025-07-17T09:25:51.651369Z","shell.execute_reply.started":"2025-07-17T09:25:51.639874Z","shell.execute_reply":"2025-07-17T09:25:51.650848Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\n\n# Save the best model based on validation loss\ndef get_callbacks(name):\n    checkpoint_cb = ModelCheckpoint(\n        filepath=f'best_model_{name}.h5',          # Use '.keras' if using newer TF versions\n        monitor='val_loss',\n        save_best_only=True,\n        save_weights_only=False,\n        verbose=1\n    )\n\n    # Stop training early if validation loss doesn't improve\n    earlystop_cb = EarlyStopping(\n        monitor='val_loss',\n        patience=3,       # Number of epochs with no improvement\n        restore_best_weights=True,\n        verbose=1\n    )\n\n    # Reduce learning rate when validation loss plateaus\n    reduce_lr_cb = ReduceLROnPlateau(\n        monitor='val_loss',\n        factor=0.5,\n        patience=5,\n        min_lr=1e-6,\n        verbose=1\n    )\n\n    # Combine all callbacks\n    callbacks = [checkpoint_cb, earlystop_cb, reduce_lr_cb]\n\n    return callbacks\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:27:38.200299Z","iopub.execute_input":"2025-07-17T09:27:38.200665Z","iopub.status.idle":"2025-07-17T09:27:38.208929Z","shell.execute_reply.started":"2025-07-17T09:27:38.200642Z","shell.execute_reply":"2025-07-17T09:27:38.208230Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Custom Model","metadata":{}},{"cell_type":"code","source":"DefaultConv2D = partial(layers.Conv2D, kernel_size=3, padding=\"same\",\n                        activation=\"relu\", kernel_initializer=\"he_normal\")\n\n# Model Definition\nmodel = models.Sequential([\n    DefaultConv2D(filters=64, kernel_size=7, input_shape=[IMAGE_SIZE, IMAGE_SIZE, 3]),\n    layers.MaxPooling2D(),\n    layers.BatchNormalization(),\n    DefaultConv2D(filters=128),\n    DefaultConv2D(filters=128),\n    layers.MaxPooling2D(),\n    layers.BatchNormalization(),\n    layers.Flatten(),\n    layers.Dense(units=128, activation=\"relu\",\n                 kernel_initializer=\"he_normal\"),\n    layers.BatchNormalization(),\n    layers.Dropout(0.5),\n    layers.Dense(units=64, activation=\"relu\",\n                 kernel_initializer=\"he_normal\"),\n    layers.BatchNormalization(),\n    layers.Dropout(0.5),\n    layers.Dense(units=1, activation=\"sigmoid\")\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:25:51.679953Z","iopub.execute_input":"2025-07-17T09:25:51.680335Z","iopub.status.idle":"2025-07-17T09:25:51.792496Z","shell.execute_reply.started":"2025-07-17T09:25:51.680316Z","shell.execute_reply":"2025-07-17T09:25:51.791985Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"initial_learning_rate = 0.001\nlr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate, decay_steps=100000, decay_rate=0.96, staircase=True\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:25:51.793086Z","iopub.execute_input":"2025-07-17T09:25:51.793303Z","iopub.status.idle":"2025-07-17T09:25:51.796933Z","shell.execute_reply.started":"2025-07-17T09:25:51.793287Z","shell.execute_reply":"2025-07-17T09:25:51.796248Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(\n    optimizer=tf.keras.optimizers.Adam(learning_rate=lr_schedule),\n    loss=\"binary_crossentropy\",\n    metrics=[\n        \"accuracy\",\n        tf.keras.metrics.Recall(name='recall'),\n        tf.keras.metrics.Precision(name='precision')\n    ]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:25:51.797413Z","iopub.execute_input":"2025-07-17T09:25:51.797652Z","iopub.status.idle":"2025-07-17T09:25:51.825964Z","shell.execute_reply.started":"2025-07-17T09:25:51.797629Z","shell.execute_reply":"2025-07-17T09:25:51.825501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:25:51.828261Z","iopub.execute_input":"2025-07-17T09:25:51.828454Z","iopub.status.idle":"2025-07-17T09:25:51.848050Z","shell.execute_reply.started":"2025-07-17T09:25:51.828439Z","shell.execute_reply":"2025-07-17T09:25:51.847352Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    X_train, \n    y_train,\n    epochs=10,\n    callbacks=get_callbacks('custom'),\n    batch_size=BATCH_SIZE,\n    validation_data=(X_val,y_val),\n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:27:44.440198Z","iopub.execute_input":"2025-07-17T09:27:44.440786Z","iopub.status.idle":"2025-07-17T09:34:28.487833Z","shell.execute_reply.started":"2025-07-17T09:27:44.440763Z","shell.execute_reply":"2025-07-17T09:34:28.487251Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Model Evolution","metadata":{}},{"cell_type":"code","source":"y_pred = model.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:34:28.489257Z","iopub.execute_input":"2025-07-17T09:34:28.489647Z","iopub.status.idle":"2025-07-17T09:34:36.291252Z","shell.execute_reply.started":"2025-07-17T09:34:28.489629Z","shell.execute_reply":"2025-07-17T09:34:36.290696Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, confusion_matrix, classification_report, roc_auc_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:34:36.292425Z","iopub.execute_input":"2025-07-17T09:34:36.292713Z","iopub.status.idle":"2025-07-17T09:34:36.296340Z","shell.execute_reply.started":"2025-07-17T09:34:36.292688Z","shell.execute_reply":"2025-07-17T09:34:36.295834Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_test_pred_binary = (y_pred > 0.5).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:34:36.298010Z","iopub.execute_input":"2025-07-17T09:34:36.298246Z","iopub.status.idle":"2025-07-17T09:34:36.314367Z","shell.execute_reply.started":"2025-07-17T09:34:36.298220Z","shell.execute_reply":"2025-07-17T09:34:36.313884Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_pred = model.predict(X_train)\ny_train_pred_binary = (y_train_pred > 0.5).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:34:36.315040Z","iopub.execute_input":"2025-07-17T09:34:36.315228Z","iopub.status.idle":"2025-07-17T09:34:56.592013Z","shell.execute_reply.started":"2025-07-17T09:34:36.315212Z","shell.execute_reply":"2025-07-17T09:34:56.591277Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, confusion_matrix, classification_report, roc_auc_score\n\ntrain_accuracy = accuracy_score(y_train, y_train_pred_binary)\nprint(f\"Training Accuracy: {train_accuracy * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:34:56.592844Z","iopub.execute_input":"2025-07-17T09:34:56.593038Z","iopub.status.idle":"2025-07-17T09:34:56.598728Z","shell.execute_reply.started":"2025-07-17T09:34:56.593022Z","shell.execute_reply":"2025-07-17T09:34:56.598107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_accuracy = accuracy_score(y_test, y_test_pred_binary)\nprint(f\"Test Accuracy: {test_accuracy * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:34:56.599434Z","iopub.execute_input":"2025-07-17T09:34:56.599716Z","iopub.status.idle":"2025-07-17T09:34:56.616443Z","shell.execute_reply.started":"2025-07-17T09:34:56.599693Z","shell.execute_reply":"2025-07-17T09:34:56.615726Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"f1 = f1_score(y_test, y_test_pred_binary)\nprint(f\"F1 Score: {f1:.4f}\")\n\nprecision = precision_score(y_test, y_test_pred_binary)\nprint(f\"Precison: {precision:.4f}\")\n\nrecall = recall_score(y_test, y_test_pred_binary)\nprint(f\"Recall: {recall:.4f}\")\n\n# Calculate AUC-ROC\nauc_roc = roc_auc_score(y_test, y_test_pred_binary)\nprint(f\"AUC-ROC: {auc_roc:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:34:56.617133Z","iopub.execute_input":"2025-07-17T09:34:56.617470Z","iopub.status.idle":"2025-07-17T09:34:56.645529Z","shell.execute_reply.started":"2025-07-17T09:34:56.617446Z","shell.execute_reply":"2025-07-17T09:34:56.644946Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conf_matrix = confusion_matrix(y_test, y_test_pred_binary)\nprint(\"Confusion Matrix:\")\nprint(conf_matrix)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:34:56.646195Z","iopub.execute_input":"2025-07-17T09:34:56.646418Z","iopub.status.idle":"2025-07-17T09:34:56.656908Z","shell.execute_reply.started":"2025-07-17T09:34:56.646402Z","shell.execute_reply":"2025-07-17T09:34:56.656329Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nsns.heatmap(conf_matrix, annot=True)\nplt.xlabel('Predicted Label')\nplt.ylabel('True Label')\nplt.title('Confusion Matrix of custom model')\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:51:37.137957Z","iopub.execute_input":"2025-07-17T09:51:37.138249Z","iopub.status.idle":"2025-07-17T09:51:37.315485Z","shell.execute_reply.started":"2025-07-17T09:51:37.138221Z","shell.execute_reply":"2025-07-17T09:51:37.314974Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_report = classification_report(y_test, y_test_pred_binary)\nprint(\"Classification Report:\")\nprint(class_report)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:36:01.115121Z","iopub.execute_input":"2025-07-17T09:36:01.115714Z","iopub.status.idle":"2025-07-17T09:36:01.127979Z","shell.execute_reply.started":"2025-07-17T09:36:01.115691Z","shell.execute_reply":"2025-07-17T09:36:01.127237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Unpack training history\nhist = history.history\n\n# Create 2x2 subplot\nplt.figure(figsize=(14, 10))\n\n# Accuracy\nplt.subplot(2, 2, 1)\nplt.plot(hist['accuracy'], label='Train Accuracy')\nplt.plot(hist['val_accuracy'], label='Val Accuracy')\nplt.title('Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\n\n# Loss\nplt.subplot(2, 2, 2)\nplt.plot(hist['loss'], label='Train Loss')\nplt.plot(hist['val_loss'], label='Val Loss')\nplt.title('Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\n\n# Precision\nplt.subplot(2, 2, 3)\nplt.plot(hist['precision'], label='Train Precision')\nplt.plot(hist['val_precision'], label='Val Precision')\nplt.title('Precision')\nplt.xlabel('Epoch')\nplt.ylabel('Precision')\nplt.legend()\n\n# Recall\nplt.subplot(2, 2, 4)\nplt.plot(hist['recall'], label='Train Recall')\nplt.plot(hist['val_recall'], label='Val Recall')\nplt.title('Recall')\nplt.xlabel('Epoch')\nplt.ylabel('Recall')\nplt.legend()\n\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:36:02.154437Z","iopub.execute_input":"2025-07-17T09:36:02.154726Z","iopub.status.idle":"2025-07-17T09:36:02.835390Z","shell.execute_reply.started":"2025-07-17T09:36:02.154705Z","shell.execute_reply":"2025-07-17T09:36:02.834705Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\"custom_model_ep10.keras\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:51:37.496886Z","iopub.execute_input":"2025-07-17T09:51:37.497165Z","iopub.status.idle":"2025-07-17T09:51:39.878044Z","shell.execute_reply.started":"2025-07-17T09:51:37.497148Z","shell.execute_reply":"2025-07-17T09:51:39.877498Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Training With Pre train model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50\n\nbase_model = ResNet50(weights='imagenet', include_top=False, input_shape=(IMAGE_SIZE,IMAGE_SIZE,3))\n\nfor layer in base_model.layers:\n    layer.trainable = False\n    \nmodel_resnet50 = models.Sequential()\nmodel_resnet50.add(base_model)\nmodel_resnet50.add(layers.GlobalAveragePooling2D())\nmodel_resnet50.add(layers.Dense(1, activation='sigmoid'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:41:04.606051Z","iopub.execute_input":"2025-07-17T09:41:04.606322Z","iopub.status.idle":"2025-07-17T09:41:05.833356Z","shell.execute_reply.started":"2025-07-17T09:41:04.606302Z","shell.execute_reply":"2025-07-17T09:41:05.832635Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_resnet50.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:41:05.834423Z","iopub.execute_input":"2025-07-17T09:41:05.834683Z","iopub.status.idle":"2025-07-17T09:41:05.850751Z","shell.execute_reply.started":"2025-07-17T09:41:05.834665Z","shell.execute_reply":"2025-07-17T09:41:05.850070Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras import optimizers\n\nmodel_resnet50.compile(\n    optimizer=\"adam\", \n    loss='binary_crossentropy', \n    metrics=[\n        \"accuracy\",\n        tf.keras.metrics.Recall(name='recall'),\n        tf.keras.metrics.Precision(name='precision')\n    ]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:43:04.914395Z","iopub.execute_input":"2025-07-17T09:43:04.915110Z","iopub.status.idle":"2025-07-17T09:43:04.927561Z","shell.execute_reply.started":"2025-07-17T09:43:04.915084Z","shell.execute_reply":"2025-07-17T09:43:04.926909Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model_resnet50.fit(\n    X_train, y_train,\n    epochs=10,  \n    callbacks=get_callbacks('resnet50'),\n    validation_data=(X_val, y_val),\n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:43:05.594229Z","iopub.execute_input":"2025-07-17T09:43:05.594526Z","iopub.status.idle":"2025-07-17T09:50:45.697432Z","shell.execute_reply.started":"2025-07-17T09:43:05.594505Z","shell.execute_reply":"2025-07-17T09:50:45.696618Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Model Evolution","metadata":{}},{"cell_type":"code","source":"y_pred = model_resnet50.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:50:45.699085Z","iopub.execute_input":"2025-07-17T09:50:45.699735Z","iopub.status.idle":"2025-07-17T09:51:02.060491Z","shell.execute_reply.started":"2025-07-17T09:50:45.699707Z","shell.execute_reply":"2025-07-17T09:51:02.059885Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_test_pred_binary = (y_pred > 0.5).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:51:02.061424Z","iopub.execute_input":"2025-07-17T09:51:02.061695Z","iopub.status.idle":"2025-07-17T09:51:02.065583Z","shell.execute_reply.started":"2025-07-17T09:51:02.061672Z","shell.execute_reply":"2025-07-17T09:51:02.064905Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_pred = model_resnet50.predict(X_train)\ny_train_pred_binary = (y_train_pred > 0.5).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:51:02.067635Z","iopub.execute_input":"2025-07-17T09:51:02.068023Z","iopub.status.idle":"2025-07-17T09:51:36.403825Z","shell.execute_reply.started":"2025-07-17T09:51:02.068006Z","shell.execute_reply":"2025-07-17T09:51:36.403199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_accuracy = accuracy_score(y_train, y_train_pred_binary)\nprint(f\"Training Accuracy: {train_accuracy * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:51:36.404562Z","iopub.execute_input":"2025-07-17T09:51:36.404770Z","iopub.status.idle":"2025-07-17T09:51:36.409742Z","shell.execute_reply.started":"2025-07-17T09:51:36.404754Z","shell.execute_reply":"2025-07-17T09:51:36.409222Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_accuracy = accuracy_score(y_test, y_test_pred_binary)\nprint(f\"Test Accuracy: {test_accuracy * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:51:36.410329Z","iopub.execute_input":"2025-07-17T09:51:36.410546Z","iopub.status.idle":"2025-07-17T09:51:36.429569Z","shell.execute_reply.started":"2025-07-17T09:51:36.410530Z","shell.execute_reply":"2025-07-17T09:51:36.429001Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"f1 = f1_score(y_test, y_test_pred_binary)\nprint(f\"F1 Score: {f1:.4f}\")\n\nprecision = precision_score(y_test, y_test_pred_binary)\nprint(f\"Precison: {precision:.4f}\")\n\nrecall = recall_score(y_test, y_test_pred_binary)\nprint(f\"Recall: {recall:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:51:36.430180Z","iopub.execute_input":"2025-07-17T09:51:36.430399Z","iopub.status.idle":"2025-07-17T09:51:36.457999Z","shell.execute_reply.started":"2025-07-17T09:51:36.430384Z","shell.execute_reply":"2025-07-17T09:51:36.457144Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conf_matrix = confusion_matrix(y_test, y_test_pred_binary)\nprint(\"Confusion Matrix:\")\nprint(conf_matrix)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:51:36.458673Z","iopub.execute_input":"2025-07-17T09:51:36.458973Z","iopub.status.idle":"2025-07-17T09:51:36.471826Z","shell.execute_reply.started":"2025-07-17T09:51:36.458956Z","shell.execute_reply":"2025-07-17T09:51:36.471209Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nsns.heatmap(conf_matrix, annot=True)\nplt.xlabel('Predicted Label')\nplt.ylabel('True Label')\nplt.title('Confusion Matrix of custom model')\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:51:37.316094Z","iopub.execute_input":"2025-07-17T09:51:37.316366Z","iopub.status.idle":"2025-07-17T09:51:37.496257Z","shell.execute_reply.started":"2025-07-17T09:51:37.316341Z","shell.execute_reply":"2025-07-17T09:51:37.495733Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_report = classification_report(y_test, y_test_pred_binary)\nprint(\"Classification Report:\")\nprint(class_report)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:51:36.472398Z","iopub.execute_input":"2025-07-17T09:51:36.472584Z","iopub.status.idle":"2025-07-17T09:51:36.492118Z","shell.execute_reply.started":"2025-07-17T09:51:36.472570Z","shell.execute_reply":"2025-07-17T09:51:36.491526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Unpack training history\nhist = history.history\n\n# Create 2x2 subplot\nplt.figure(figsize=(14, 10))\n\n# Accuracy\nplt.subplot(2, 2, 1)\nplt.plot(hist['accuracy'], label='Train Accuracy')\nplt.plot(hist['val_accuracy'], label='Val Accuracy')\nplt.title('Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\n\n# Loss\nplt.subplot(2, 2, 2)\nplt.plot(hist['loss'], label='Train Loss')\nplt.plot(hist['val_loss'], label='Val Loss')\nplt.title('Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\n\n# Precision\nplt.subplot(2, 2, 3)\nplt.plot(hist['precision'], label='Train Precision')\nplt.plot(hist['val_precision'], label='Val Precision')\nplt.title('Precision')\nplt.xlabel('Epoch')\nplt.ylabel('Precision')\nplt.legend()\n\n# Recall\nplt.subplot(2, 2, 4)\nplt.plot(hist['recall'], label='Train Recall')\nplt.plot(hist['val_recall'], label='Val Recall')\nplt.title('Recall')\nplt.xlabel('Epoch')\nplt.ylabel('Recall')\nplt.legend()\n\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:51:36.493654Z","iopub.execute_input":"2025-07-17T09:51:36.493837Z","iopub.status.idle":"2025-07-17T09:51:37.137231Z","shell.execute_reply.started":"2025-07-17T09:51:36.493822Z","shell.execute_reply":"2025-07-17T09:51:37.136619Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\"resnet50_model_ep10.keras\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-17T09:51:45.626156Z","iopub.execute_input":"2025-07-17T09:51:45.626375Z","iopub.status.idle":"2025-07-17T09:51:49.133065Z","shell.execute_reply.started":"2025-07-17T09:51:45.626357Z","shell.execute_reply":"2025-07-17T09:51:49.132291Z"}},"outputs":[],"execution_count":null}]}