{"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-11-07T14:43:05.300418Z","iopub.execute_input":"2025-11-07T14:43:05.300650Z","iopub.status.idle":"2025-11-07T14:43:05.308460Z","shell.execute_reply.started":"2025-11-07T14:43:05.300627Z","shell.execute_reply":"2025-11-07T14:43:05.307656Z"}},"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-11-07T14:43:05.310226Z","iopub.execute_input":"2025-11-07T14:43:05.310418Z","iopub.status.idle":"2025-11-07T14:43:06.324822Z","shell.execute_reply.started":"2025-11-07T14:43:05.310403Z","shell.execute_reply":"2025-11-07T14:43:06.324089Z"}},"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-11-07T14:43:06.325594Z","iopub.execute_input":"2025-11-07T14:43:06.325947Z","iopub.status.idle":"2025-11-07T14:43:06.519586Z","shell.execute_reply.started":"2025-11-07T14:43:06.325924Z","shell.execute_reply":"2025-11-07T14:43:06.518839Z"}},"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-11-07T14:43:06.520343Z","iopub.execute_input":"2025-11-07T14:43:06.520643Z","iopub.status.idle":"2025-11-07T14:43:06.525799Z","shell.execute_reply.started":"2025-11-07T14:43:06.520616Z","shell.execute_reply":"2025-11-07T14:43:06.524997Z"}},"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-11-07T14:43:06.527950Z","iopub.execute_input":"2025-11-07T14:43:06.528156Z","iopub.status.idle":"2025-11-07T14:43:06.587710Z","shell.execute_reply.started":"2025-11-07T14:43:06.528140Z","shell.execute_reply":"2025-11-07T14:43:06.586950Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-07T14:43:06.588617Z","iopub.execute_input":"2025-11-07T14:43:06.588993Z","iopub.status.idle":"2025-11-07T14:43:06.607441Z","shell.execute_reply.started":"2025-11-07T14:43:06.588968Z","shell.execute_reply":"2025-11-07T14:43:06.606852Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.fillna(\"Not Available\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-07T14:43:06.608361Z","iopub.execute_input":"2025-11-07T14:43:06.608620Z","iopub.status.idle":"2025-11-07T14:43:06.649017Z","shell.execute_reply.started":"2025-11-07T14:43:06.608598Z","shell.execute_reply":"2025-11-07T14:43:06.648423Z"}},"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-11-07T14:43:06.649726Z","iopub.execute_input":"2025-11-07T14:43:06.650061Z","iopub.status.idle":"2025-11-07T14:43:06.664418Z","shell.execute_reply.started":"2025-11-07T14:43:06.649951Z","shell.execute_reply":"2025-11-07T14:43:06.663746Z"}},"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-11-07T14:43:06.665311Z","iopub.execute_input":"2025-11-07T14:43:06.665579Z","iopub.status.idle":"2025-11-07T14:43:06.979803Z","shell.execute_reply.started":"2025-11-07T14:43:06.665559Z","shell.execute_reply":"2025-11-07T14:43:06.978701Z"}},"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-11-07T14:43:06.980473Z","iopub.execute_input":"2025-11-07T14:43:06.980793Z","iopub.status.idle":"2025-11-07T14:43:07.295988Z","shell.execute_reply.started":"2025-11-07T14:43:06.980771Z","shell.execute_reply":"2025-11-07T14:43:07.295150Z"}},"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-11-07T14:43:07.296904Z","iopub.execute_input":"2025-11-07T14:43:07.297216Z","iopub.status.idle":"2025-11-07T14:43:07.334626Z","shell.execute_reply.started":"2025-11-07T14:43:07.297193Z","shell.execute_reply":"2025-11-07T14:43:07.333953Z"}},"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-11-07T14:43:07.335386Z","iopub.execute_input":"2025-11-07T14:43:07.335658Z","iopub.status.idle":"2025-11-07T14:43:07.594147Z","shell.execute_reply.started":"2025-11-07T14:43:07.335632Z","shell.execute_reply":"2025-11-07T14:43:07.593427Z"}},"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-11-07T14:43:07.597030Z","iopub.execute_input":"2025-11-07T14:43:07.597230Z","iopub.status.idle":"2025-11-07T14:43:09.728637Z","shell.execute_reply.started":"2025-11-07T14:43:07.597214Z","shell.execute_reply":"2025-11-07T14:43:09.727871Z"}},"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-11-07T14:43:09.729413Z","iopub.execute_input":"2025-11-07T14:43:09.729721Z","iopub.status.idle":"2025-11-07T14:43:09.735237Z","shell.execute_reply.started":"2025-11-07T14:43:09.729701Z","shell.execute_reply":"2025-11-07T14:43:09.734607Z"}},"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-11-07T14:43:09.736017Z","iopub.execute_input":"2025-11-07T14:43:09.736209Z","iopub.status.idle":"2025-11-07T14:46:52.710584Z","shell.execute_reply.started":"2025-11-07T14:43:09.736194Z","shell.execute_reply":"2025-11-07T14:46:52.709976Z"}},"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-11-07T14:46:52.711325Z","iopub.execute_input":"2025-11-07T14:46:52.711577Z","iopub.status.idle":"2025-11-07T14:47:06.051476Z","shell.execute_reply.started":"2025-11-07T14:46:52.711560Z","shell.execute_reply":"2025-11-07T14:47:06.050738Z"}},"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-11-07T14:47:06.052342Z","iopub.execute_input":"2025-11-07T14:47:06.052923Z","iopub.status.idle":"2025-11-07T14:47:06.072336Z","shell.execute_reply.started":"2025-11-07T14:47:06.052897Z","shell.execute_reply":"2025-11-07T14:47:06.071770Z"},"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-11-07T14:47:06.073139Z","iopub.execute_input":"2025-11-07T14:47:06.073351Z","iopub.status.idle":"2025-11-07T14:47:08.484589Z","shell.execute_reply.started":"2025-11-07T14:47:06.073329Z","shell.execute_reply":"2025-11-07T14:47:08.483832Z"}},"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-11-07T14:47:08.485415Z","iopub.execute_input":"2025-11-07T14:47:08.485633Z","iopub.status.idle":"2025-11-07T14:47:08.489447Z","shell.execute_reply.started":"2025-11-07T14:47:08.485617Z","shell.execute_reply":"2025-11-07T14:47:08.488781Z"}},"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-11-07T14:47:08.490115Z","iopub.execute_input":"2025-11-07T14:47:08.490489Z","iopub.status.idle":"2025-11-07T14:47:08.517860Z","shell.execute_reply.started":"2025-11-07T14:47:08.490473Z","shell.execute_reply":"2025-11-07T14:47:08.517202Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-07T14:47:08.518528Z","iopub.execute_input":"2025-11-07T14:47:08.518769Z","iopub.status.idle":"2025-11-07T14:47:08.538792Z","shell.execute_reply.started":"2025-11-07T14:47:08.518747Z","shell.execute_reply":"2025-11-07T14:47:08.538248Z"}},"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-11-07T14:47:08.539479Z","iopub.execute_input":"2025-11-07T14:47:08.539735Z","iopub.status.idle":"2025-11-07T14:55:23.205104Z","shell.execute_reply.started":"2025-11-07T14:47:08.539714Z","shell.execute_reply":"2025-11-07T14:55:23.204338Z"}},"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-11-07T14:55:23.206181Z","iopub.execute_input":"2025-11-07T14:55:23.206396Z","iopub.status.idle":"2025-11-07T14:55:31.809095Z","shell.execute_reply.started":"2025-11-07T14:55:23.206379Z","shell.execute_reply":"2025-11-07T14:55:31.808364Z"}},"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-11-07T14:55:31.810165Z","iopub.execute_input":"2025-11-07T14:55:31.810371Z","iopub.status.idle":"2025-11-07T14:55:31.814099Z","shell.execute_reply.started":"2025-11-07T14:55:31.810355Z","shell.execute_reply":"2025-11-07T14:55:31.813488Z"}},"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-11-07T14:55:31.814762Z","iopub.execute_input":"2025-11-07T14:55:31.815010Z","iopub.status.idle":"2025-11-07T14:55:31.826225Z","shell.execute_reply.started":"2025-11-07T14:55:31.814994Z","shell.execute_reply":"2025-11-07T14:55:31.825624Z"}},"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-11-07T14:55:31.826963Z","iopub.execute_input":"2025-11-07T14:55:31.827203Z","iopub.status.idle":"2025-11-07T14:55:54.464918Z","shell.execute_reply.started":"2025-11-07T14:55:31.827184Z","shell.execute_reply":"2025-11-07T14:55:54.464303Z"}},"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-11-07T14:55:54.465630Z","iopub.execute_input":"2025-11-07T14:55:54.465857Z","iopub.status.idle":"2025-11-07T14:55:54.472226Z","shell.execute_reply.started":"2025-11-07T14:55:54.465839Z","shell.execute_reply":"2025-11-07T14:55:54.471605Z"}},"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-11-07T14:55:54.472941Z","iopub.execute_input":"2025-11-07T14:55:54.473376Z","iopub.status.idle":"2025-11-07T14:55:54.485832Z","shell.execute_reply.started":"2025-11-07T14:55:54.473355Z","shell.execute_reply":"2025-11-07T14:55:54.485242Z"}},"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-11-07T14:55:54.486534Z","iopub.execute_input":"2025-11-07T14:55:54.486763Z","iopub.status.idle":"2025-11-07T14:55:54.508964Z","shell.execute_reply.started":"2025-11-07T14:55:54.486747Z","shell.execute_reply":"2025-11-07T14:55:54.508249Z"}},"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-11-07T14:55:54.509682Z","iopub.execute_input":"2025-11-07T14:55:54.509928Z","iopub.status.idle":"2025-11-07T14:55:54.516380Z","shell.execute_reply.started":"2025-11-07T14:55:54.509897Z","shell.execute_reply":"2025-11-07T14:55:54.515860Z"}},"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-11-07T14:55:54.517078Z","iopub.execute_input":"2025-11-07T14:55:54.517314Z","iopub.status.idle":"2025-11-07T14:55:54.690695Z","shell.execute_reply.started":"2025-11-07T14:55:54.517294Z","shell.execute_reply":"2025-11-07T14:55:54.689978Z"}},"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-11-07T14:55:54.691397Z","iopub.execute_input":"2025-11-07T14:55:54.691668Z","iopub.status.idle":"2025-11-07T14:55:54.702639Z","shell.execute_reply.started":"2025-11-07T14:55:54.691647Z","shell.execute_reply":"2025-11-07T14:55:54.701962Z"}},"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-11-07T14:55:54.703378Z","iopub.execute_input":"2025-11-07T14:55:54.703660Z","iopub.status.idle":"2025-11-07T14:55:55.383459Z","shell.execute_reply.started":"2025-11-07T14:55:54.703635Z","shell.execute_reply":"2025-11-07T14:55:55.382736Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\"custom_model_ep10.keras\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-07T14:55:55.384206Z","iopub.execute_input":"2025-11-07T14:55:55.384418Z","iopub.status.idle":"2025-11-07T14:55:57.783756Z","shell.execute_reply.started":"2025-11-07T14:55:55.384402Z","shell.execute_reply":"2025-11-07T14:55:57.783127Z"}},"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-11-07T14:55:57.784462Z","iopub.execute_input":"2025-11-07T14:55:57.784702Z","iopub.status.idle":"2025-11-07T14:55:59.745340Z","shell.execute_reply.started":"2025-11-07T14:55:57.784685Z","shell.execute_reply":"2025-11-07T14:55:59.744549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_resnet50.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-07T14:55:59.746226Z","iopub.execute_input":"2025-11-07T14:55:59.746434Z","iopub.status.idle":"2025-11-07T14:55:59.762868Z","shell.execute_reply.started":"2025-11-07T14:55:59.746419Z","shell.execute_reply":"2025-11-07T14:55:59.762112Z"}},"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-11-07T14:55:59.767569Z","iopub.execute_input":"2025-11-07T14:55:59.767770Z","iopub.status.idle":"2025-11-07T14:55:59.781267Z","shell.execute_reply.started":"2025-11-07T14:55:59.767754Z","shell.execute_reply":"2025-11-07T14:55:59.780462Z"}},"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-11-07T14:55:59.782173Z","iopub.execute_input":"2025-11-07T14:55:59.782399Z","iopub.status.idle":"2025-11-07T15:04:15.909950Z","shell.execute_reply.started":"2025-11-07T14:55:59.782384Z","shell.execute_reply":"2025-11-07T15:04:15.909350Z"}},"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-11-07T15:04:15.910963Z","iopub.execute_input":"2025-11-07T15:04:15.911205Z","iopub.status.idle":"2025-11-07T15:04:32.611887Z","shell.execute_reply.started":"2025-11-07T15:04:15.911182Z","shell.execute_reply":"2025-11-07T15:04:32.611343Z"}},"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-11-07T15:04:32.612849Z","iopub.execute_input":"2025-11-07T15:04:32.613051Z","iopub.status.idle":"2025-11-07T15:04:32.616563Z","shell.execute_reply.started":"2025-11-07T15:04:32.613036Z","shell.execute_reply":"2025-11-07T15:04:32.616007Z"}},"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-11-07T15:04:32.617242Z","iopub.execute_input":"2025-11-07T15:04:32.617467Z","iopub.status.idle":"2025-11-07T15:05:09.732780Z","shell.execute_reply.started":"2025-11-07T15:04:32.617451Z","shell.execute_reply":"2025-11-07T15:05:09.732013Z"}},"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-11-07T15:05:09.739363Z","iopub.execute_input":"2025-11-07T15:05:09.739649Z","iopub.status.idle":"2025-11-07T15:05:09.744647Z","shell.execute_reply.started":"2025-11-07T15:05:09.739632Z","shell.execute_reply":"2025-11-07T15:05:09.744062Z"}},"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-11-07T15:05:09.745410Z","iopub.execute_input":"2025-11-07T15:05:09.746017Z","iopub.status.idle":"2025-11-07T15:05:09.757801Z","shell.execute_reply.started":"2025-11-07T15:05:09.745995Z","shell.execute_reply":"2025-11-07T15:05:09.757113Z"}},"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-11-07T15:05:09.758587Z","iopub.execute_input":"2025-11-07T15:05:09.758834Z","iopub.status.idle":"2025-11-07T15:05:09.773665Z","shell.execute_reply.started":"2025-11-07T15:05:09.758814Z","shell.execute_reply":"2025-11-07T15:05:09.772916Z"}},"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-11-07T15:05:09.774468Z","iopub.execute_input":"2025-11-07T15:05:09.774775Z","iopub.status.idle":"2025-11-07T15:05:09.780364Z","shell.execute_reply.started":"2025-11-07T15:05:09.774751Z","shell.execute_reply":"2025-11-07T15:05:09.779521Z"}},"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-11-07T15:05:09.781137Z","iopub.execute_input":"2025-11-07T15:05:09.781347Z","iopub.status.idle":"2025-11-07T15:05:09.961881Z","shell.execute_reply.started":"2025-11-07T15:05:09.781331Z","shell.execute_reply":"2025-11-07T15:05:09.961151Z"}},"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-11-07T15:05:09.962663Z","iopub.execute_input":"2025-11-07T15:05:09.962966Z","iopub.status.idle":"2025-11-07T15:05:09.973939Z","shell.execute_reply.started":"2025-11-07T15:05:09.962948Z","shell.execute_reply":"2025-11-07T15:05:09.973325Z"}},"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-11-07T15:05:09.974607Z","iopub.execute_input":"2025-11-07T15:05:09.974829Z","iopub.status.idle":"2025-11-07T15:05:10.652374Z","shell.execute_reply.started":"2025-11-07T15:05:09.974803Z","shell.execute_reply":"2025-11-07T15:05:10.651633Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\"resnet50_model_ep10.keras\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-07T15:05:10.653174Z","iopub.execute_input":"2025-11-07T15:05:10.653382Z","iopub.status.idle":"2025-11-07T15:05:13.196338Z","shell.execute_reply.started":"2025-11-07T15:05:10.653365Z","shell.execute_reply":"2025-11-07T15:05:13.195771Z"}},"outputs":[],"execution_count":null}]}