{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":16880,"databundleVersionId":858837,"sourceType":"competition"},{"sourceId":7714881,"sourceType":"datasetVersion","datasetId":4505544},{"sourceId":127490,"sourceType":"modelInstanceVersion","modelInstanceId":34433,"modelId":48430},{"sourceId":129696,"sourceType":"modelInstanceVersion","modelInstanceId":69960,"modelId":48430}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!nvidia-smi","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T06:37:55.321432Z","iopub.execute_input":"2025-04-16T06:37:55.321592Z","iopub.status.idle":"2025-04-16T06:37:55.548195Z","shell.execute_reply.started":"2025-04-16T06:37:55.321577Z","shell.execute_reply":"2025-04-16T06:37:55.547544Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python --version\n!pip --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T06:37:55.549609Z","iopub.execute_input":"2025-04-16T06:37:55.549829Z","iopub.status.idle":"2025-04-16T06:37:56.211296Z","shell.execute_reply.started":"2025-04-16T06:37:55.549809Z","shell.execute_reply":"2025-04-16T06:37:56.210524Z"}},"outputs":[],"execution_count":null},{"cell_type":"raw","source":"!pip install -U --upgrade tensorflow\n!pip install keras","metadata":{"_kg_hide-input":false,"_kg_hide-output":true}},{"cell_type":"code","source":"tf.keras.backend.clear_session()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-07T10:09:34.085452Z","iopub.execute_input":"2025-04-07T10:09:34.085738Z","iopub.status.idle":"2025-04-07T10:09:34.267971Z","shell.execute_reply.started":"2025-04-07T10:09:34.085715Z","shell.execute_reply":"2025-04-07T10:09:34.267265Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from memory_profiler import memory_usage\nimport numpy as np\nimport tensorflow as tf\n\n# Load a small model for demo — replace with your own\nmodel = tf.keras.applications.MobileNetV2(weights=None, input_shape=(224, 224, 3), classes=1)\n\ndef run_inference():\n    dummy_input = np.random.rand(100, 224, 224, 3).astype(np.float32)\n    output = model.predict(dummy_input)\n    return output\n\n# Measure memory used during inference\nmem_usage = memory_usage((run_inference, ), interval=0.1, max_usage=True)\nprint(f\"Peak memory usage: {mem_usage} MiB\")\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-04-16T06:28:24.543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\ndf=pd.read_csv(\"/kaggle/working/MemoryProfile.csv\")\nsum=0\nfor i in df.Deit_ft:\n    sum+=i\nsum/100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-17T06:16:14.559595Z","iopub.execute_input":"2025-04-17T06:16:14.560257Z","iopub.status.idle":"2025-04-17T06:16:14.567081Z","shell.execute_reply.started":"2025-04-17T06:16:14.560231Z","shell.execute_reply":"2025-04-17T06:16:14.566512Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import sys\nimport sklearn\n\nimport os\nimport cv2\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\n\n\n\nplt.rc('font', size=14)\nplt.rc('axes', labelsize=14, titlesize=14)\nplt.rc('legend', fontsize=14)\nplt.rc('xtick', labelsize=10)\nplt.rc('ytick', labelsize=10)","metadata":{"execution":{"iopub.status.busy":"2025-04-08T10:18:42.339137Z","iopub.execute_input":"2025-04-08T10:18:42.339495Z","iopub.status.idle":"2025-04-08T10:18:42.344847Z","shell.execute_reply.started":"2025-04-08T10:18:42.339469Z","shell.execute_reply":"2025-04-08T10:18:42.343801Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nprint(tf.__version__)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# deepfake-faces Dataset","metadata":{}},{"cell_type":"markdown","source":"## **Data Visualization**","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport transformers as T\nimport pandas as pd\nprint(tf.__version__)\nprint(T.__version__)","metadata":{"execution":{"iopub.status.busy":"2025-04-08T06:08:28.497903Z","iopub.execute_input":"2025-04-08T06:08:28.498239Z","iopub.status.idle":"2025-04-08T06:08:28.503050Z","shell.execute_reply.started":"2025-04-08T06:08:28.498215Z","shell.execute_reply":"2025-04-08T06:08:28.502200Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n# dfdc faces\ndef get_data():\n    return pd.read_csv(\"/kaggle/input/deepfake-faces/metadata.csv\")\n#     return pd.read_csv('/kaggle/input/shrey-faces-dataset/CustomDataset/meta.csv')\n        \n    \n","metadata":{"execution":{"iopub.status.busy":"2025-04-08T10:15:25.651082Z","iopub.execute_input":"2025-04-08T10:15:25.651465Z","iopub.status.idle":"2025-04-08T10:15:25.655894Z","shell.execute_reply.started":"2025-04-08T10:15:25.651433Z","shell.execute_reply":"2025-04-08T10:15:25.654898Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"meta=get_data()\nmeta","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-08T10:15:27.947106Z","iopub.execute_input":"2025-04-08T10:15:27.947464Z","iopub.status.idle":"2025-04-08T10:15:28.140692Z","shell.execute_reply.started":"2025-04-08T10:15:27.947437Z","shell.execute_reply":"2025-04-08T10:15:28.139564Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"meta.shape #95634\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-08T10:15:35.177122Z","iopub.execute_input":"2025-04-08T10:15:35.177538Z","iopub.status.idle":"2025-04-08T10:15:35.183787Z","shell.execute_reply.started":"2025-04-08T10:15:35.177477Z","shell.execute_reply":"2025-04-08T10:15:35.182872Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(meta[meta.label=='FAKE']),len(meta[meta.label=='REAL'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-08T10:15:43.729050Z","iopub.execute_input":"2025-04-08T10:15:43.729430Z","iopub.status.idle":"2025-04-08T10:15:43.760227Z","shell.execute_reply.started":"2025-04-08T10:15:43.729397Z","shell.execute_reply":"2025-04-08T10:15:43.759283Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"real_df = meta[meta[\"label\"] == \"REAL\"]\nfake_df = meta[meta[\"label\"] == \"FAKE\"]\n# sample_size = 8000\n\nreal_df = real_df.sample(8000, random_state=42)\nfake_df = fake_df.sample(8000, random_state=42)\n\nsample_meta = pd.concat([real_df, fake_df])\nlen(sample_meta)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-08T10:16:11.019356Z","iopub.execute_input":"2025-04-08T10:16:11.019698Z","iopub.status.idle":"2025-04-08T10:16:11.059780Z","shell.execute_reply.started":"2025-04-08T10:16:11.019669Z","shell.execute_reply":"2025-04-08T10:16:11.058579Z"}},"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'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-08T10:16:16.115509Z","iopub.execute_input":"2025-04-08T10:16:16.115836Z","iopub.status.idle":"2025-04-08T10:16:16.300476Z","shell.execute_reply.started":"2025-04-08T10:16:16.115810Z","shell.execute_reply":"2025-04-08T10:16:16.299174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Test_set","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-08T10:16:18.415730Z","iopub.execute_input":"2025-04-08T10:16:18.416052Z","iopub.status.idle":"2025-04-08T10:16:18.427602Z","shell.execute_reply.started":"2025-04-08T10:16:18.416028Z","shell.execute_reply":"2025-04-08T10:16:18.426462Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(15,15))\nfor cur,i in enumerate(Test_set.index):\n    plt.subplot(5,5,cur+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    img = cv2.imread('/kaggle/input/deepfake-faces/faces_224/'+Test_set.loc[i,'videoname'][:-4]+'.jpg')\n#     print(img)\n    img = cv2.resize(img,(224,224))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    plt.imshow(img)\n#     plt.imshow(cv2.imread('../input/deepfake-faces/faces_224/'+Test_set.loc[i,'videoname'][:-4]+'.jpg'))\n#     plt.imshow(cv2.imread('../input/shrey-faces-dataset/CustomDataset/'+Test_set.loc[i,'name'][:-4]+'.jpg'))\n\n    \n    if(Test_set.loc[i,'label']=='FAKE'):\n        plt.xlabel('FAKE Image')\n    else:\n        plt.xlabel('REAL Image')\n        \nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-08T10:21:00.408406Z","iopub.execute_input":"2025-04-08T10:21:00.408734Z","iopub.status.idle":"2025-04-08T10:21:02.533785Z","shell.execute_reply.started":"2025-04-08T10:21:00.408710Z","shell.execute_reply":"2025-04-08T10:21:02.532372Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n","metadata":{}},{"cell_type":"markdown","source":"## **Custom CNN Architecture**","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def retreive_dataset(set_name):\n    images,labels=[],[]\n    def imgResize(img,width,height):\n        imgResize = cv2.resize(img,(width,height))\n        return imgResize\n    \n    for (img, imclass) in zip(set_name['videoname'], set_name['label']):\n        img = cv2.imread('/kaggle/input/deepfake-faces/faces_224/'+img[:-4]+'.jpg')\n#         img = cv2.imread('../input/shrey-faces-dataset/CustomDataset/'+img[:-4]+'.jpg')\n        img = imgResize(img, 224,224) \n        images.append(img)\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-04-08T10:21:49.811133Z","iopub.execute_input":"2025-04-08T10:21:49.811516Z","iopub.status.idle":"2025-04-08T10:21:49.817280Z","shell.execute_reply.started":"2025-04-08T10:21:49.811489Z","shell.execute_reply":"2025-04-08T10:21:49.816392Z"}},"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-04-08T10:21:52.459177Z","iopub.execute_input":"2025-04-08T10:21:52.459527Z","iopub.status.idle":"2025-04-08T10:23:06.624674Z","shell.execute_reply.started":"2025-04-08T10:21:52.459503Z","shell.execute_reply":"2025-04-08T10:23:06.623673Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from functools import partial\nimport tensorflow as tf\n\ntf.random.set_seed(42) \nDefaultConv2D = partial(tf.keras.layers.Conv2D, kernel_size=3, padding=\"same\",\n                        activation=\"relu\", kernel_initializer=\"he_normal\")\n\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Input(shape=[224, 224, 3]),  # Use Input layer with shape\n    DefaultConv2D(filters=64, kernel_size=7),\n    tf.keras.layers.MaxPool2D(),\n    DefaultConv2D(filters=128),\n    DefaultConv2D(filters=128),\n    tf.keras.layers.MaxPool2D(),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(units=128, activation=\"relu\",\n                          kernel_initializer=\"he_normal\"),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(units=64, activation=\"relu\",\n                          kernel_initializer=\"he_normal\"),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(units=1, activation=\"sigmoid\")\n])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# from tensorflow.keras.utils import plot_model\n\n# plot_model(model,show_shapes=True, show_layer_names=True)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(loss=\"binary_crossentropy\", optimizer=\"nadam\",\n              metrics=[\"accuracy\"])\nmodel.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(X_train, y_train, epochs=5,batch_size=64,\n                    validation_data=(X_val, y_val))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extract metrics\nepoch_data = {\n    'Epoch': list(range(1, len(history.history['accuracy']) + 1)),\n    'Train Accuracy': history.history['accuracy'],\n    'Train Loss': history.history['loss'],\n    'Validation Accuracy': history.history['val_accuracy'],\n    'Validation Loss': history.history['val_loss']\n}\n\n# Create DataFrame\ndf = pd.DataFrame(epoch_data)\n\n# Display the table\nprint(df)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Assuming 'model' is your Keras model\nmodel.save('DFDC_CNN.keras')\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load the model\nmodel = keras.models.load_model(\"/kaggle/input/deepfake_models/keras/deepfake_models/8/dfdc_New_models/dfdc_Xception.h5\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-08T10:18:30.657494Z","iopub.execute_input":"2025-04-08T10:18:30.657938Z","iopub.status.idle":"2025-04-08T10:18:30.683938Z","shell.execute_reply.started":"2025-04-08T10:18:30.657906Z","shell.execute_reply":"2025-04-08T10:18:30.682583Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.history\nprint(history.history)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score = model.evaluate(X_test, y_test)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, f1_score, confusion_matrix, roc_curve, auc\nimport matplotlib.pyplot as plt\n\n# Assuming you have predictions (y_pred) from your model\ny_pred = model.predict(X_test)\n\n# Assuming y_pred is a probability score, you may need to convert it to binary predictions\ny_pred_binary = (y_pred > 0.5).astype(int)\n\naccuracy = accuracy_score(y_test, y_pred_binary)\nprint(f'Accuracy: {accuracy}')\n\nf1 = f1_score(y_test, y_pred_binary)\nprint(f'F1 Score: {f1}')\n\n# Generate confusion matrix\nconf_matrix = confusion_matrix(y_test, y_pred_binary)\nprint('Confusion Matrix:')\nprint(conf_matrix)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install seaborn","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Assuming y_test and y_pred_binary are defined\n\n# Generate confusion matrix\nconf_matrix = confusion_matrix(y_test, y_pred_binary)\n\n# Plot confusion matrix as a heatmap\nplt.figure(figsize=(8, 6))\nsns.heatmap(conf_matrix, annot=True, fmt=\"d\", cmap=\"Blues\", cbar=False,\n            xticklabels=['Real', 'Fake'],\n            yticklabels=['Fake', 'Real'])\nplt.title('Confusion Matrix')\nplt.xlabel('Predicted')\nplt.ylabel('Actual')\nplt.show()\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot ROC curve\nfpr, tpr, thresholds = roc_curve(y_test, y_pred)\nroc_auc = auc(fpr, tpr)\n\nplt.figure(figsize=(8, 6))\nplt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (AUC = {roc_auc:.2f})')\nplt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.title('Receiver Operating Characteristic (ROC) Curve')\nplt.legend(loc='lower right')\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot model performance\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs_range = range(1, len(history.epoch) + 1)\n\nplt.figure(figsize=(15,5))\n\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Train Set')\nplt.plot(epochs_range, val_acc, label='Val Set')\nplt.legend(loc=\"best\")\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.title('Model Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Train Set')\nplt.plot(epochs_range, val_loss, label='Val Set')\nplt.legend(loc=\"best\")\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.title('Model Loss')\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":" **Xception**","metadata":{}},{"cell_type":"code","source":"train_set_raw=tf.data.Dataset.from_tensor_slices((X_train,y_train))\nvalid_set_raw=tf.data.Dataset.from_tensor_slices((X_val,y_val))\ntest_set_raw=tf.data.Dataset.from_tensor_slices((X_test,y_test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-08T10:24:18.815484Z","iopub.execute_input":"2025-04-08T10:24:18.815928Z","iopub.status.idle":"2025-04-08T10:24:25.716934Z","shell.execute_reply.started":"2025-04-08T10:24:18.815895Z","shell.execute_reply":"2025-04-08T10:24:25.716152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tf.keras.backend.clear_session()  # extra code – resets layer name counter\n\nbatch_size = 32\npreprocess = tf.keras.applications.xception.preprocess_input\ntrain_set = train_set_raw.map(lambda X, y: (preprocess(tf.cast(X, tf.float32)), y))\ntrain_set = train_set.shuffle(1000, seed=42).batch(batch_size).prefetch(1)\nvalid_set = valid_set_raw.map(lambda X, y: (preprocess(tf.cast(X, tf.float32)), y)).batch(batch_size)\ntest_set = test_set_raw.map(lambda X, y: (preprocess(tf.cast(X, tf.float32)), y)).batch(batch_size)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-08T10:24:36.600705Z","iopub.execute_input":"2025-04-08T10:24:36.601002Z","iopub.status.idle":"2025-04-08T10:24:39.286866Z","shell.execute_reply.started":"2025-04-08T10:24:36.600981Z","shell.execute_reply":"2025-04-08T10:24:39.285755Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# extra code – displays the first 9 images in the first batch of valid_set\n\nplt.figure(figsize=(12, 12))\nfor X_batch, y_batch in test_set.take(1):\n    for index in range(9):\n        plt.subplot(3, 3, index + 1)\n        plt.imshow((X_batch[index] + 1) / 2)  # rescale to 0–1 for imshow()\n        if(y_batch[index]==1):\n            classt='FAKE'\n        else:\n            classt='REAL'\n        plt.title(f\"Class: {classt}\")\n        plt.axis(\"off\")\n\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_augmentation = tf.keras.Sequential([\n    tf.keras.layers.RandomFlip(mode=\"horizontal\", seed=42),\n    tf.keras.layers.RandomRotation(factor=0.05, seed=42),\n    tf.keras.layers.RandomContrast(factor=0.2, seed=42)\n])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# extra code – displays the same first 9 images, after augmentation\n\nplt.figure(figsize=(12, 12))\nfor X_batch, y_batch in test_set.take(1):\n    X_batch_augmented = data_augmentation(X_batch, training=True)\n    for index in range(9):\n        plt.subplot(3, 3, index + 1)\n        # We must rescale the images to the 0-1 range for imshow(), and also\n        # clip the result to that range, because data augmentation may\n        # make some values go out of bounds (e.g., RandomContrast in this case).\n        plt.imshow(np.clip((X_batch_augmented[index] + 1) / 2, 0, 1))\n        if(y_batch[index]==1):\n            classt='FAKE'\n        else:\n            classt='REAL'\n        plt.title(f\"Class: {classt}\")\n        plt.axis(\"off\")\n\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tf.random.set_seed(42)  # extra code – ensures reproducibility\n# base_model = tf.keras.applications.xception.Xception(weights=\"imagenet\",include_top=False)\n# base_model = tf.keras.applications.ResNet50(weights=\"imagenet\", include_top=False)\n\n# base_model = tf.keras.applications.InceptionV3(weights='imagenet', include_top=False)\n\n# base_model = tf.keras.applications.DenseNet121(weights='imagenet', include_top=False)\nbase_model = tf.keras.applications.ConvNeXtBase(weights=\"imagenet\",include_top=False)\n\navg = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)\noutput = tf.keras.layers.Dense(1, activation=\"sigmoid\")(avg)\nmodel = tf.keras.Model(inputs=base_model.input, outputs=output)\n\nfor layer in base_model.layers:\n    layer.trainable = False","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for layer in base_model.layers[-100:]:\n    layer.trainable = True","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(base_model.layers)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n    \noptimizer = tf.keras.optimizers.SGD(learning_rate=0.1, momentum=0.9)\nmodel.compile(loss=\"binary_crossentropy\", optimizer=optimizer,\n              metrics=[\"accuracy\"])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nhistory = model.fit(train_set, validation_data=valid_set, epochs=3)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\"dfdc_Xception.h5\")","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## MACs and FLOPs","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2_as_graph\n\ndef get_flops(model, input_shape):\n    # Convert Keras model to ConcreteFunction\n    concrete_func = tf.function(model).get_concrete_function(tf.TensorSpec(input_shape, tf.float32))\n    \n    # Convert ConcreteFunction to a frozen graph\n    frozen_func, graph_def = convert_variables_to_constants_v2_as_graph(concrete_func)\n    \n    # Create a session to run the frozen graph\n    with tf.compat.v1.Session(graph=tf.Graph()) as sess:\n        tf.import_graph_def(graph_def, name=\"\")\n        graph = tf.compat.v1.get_default_graph()\n        \n        # Calculate FLOPs using TensorFlow profiler\n        run_meta = tf.compat.v1.RunMetadata()\n        opts = tf.compat.v1.profiler.ProfileOptionBuilder.float_operation()\n        flops = tf.compat.v1.profiler.profile(graph=graph, run_meta=run_meta, cmd='op', options=opts)\n        \n    return flops.total_float_ops\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define input shape\ninput_shape = (1, 224, 224,3 )\n# Calculate FLOPs\nflops = get_flops(model, input_shape)\nmacs = flops // 2  # MACs is half the FLOPs for Conv2D\n\nprint(f\"MACs: {macs}\")\nprint(f\"FLOPs: {flops}\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f\"MACs: {macs}\")\nprint(f\"FLOPs: {flops}\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data import","metadata":{}},{"cell_type":"code","source":"import sys\nimport sklearn\n\nimport os\nimport cv2\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n\n\nplt.rc('font', size=14)\nplt.rc('axes', labelsize=14, titlesize=14)\nplt.rc('legend', fontsize=14)\nplt.rc('xtick', labelsize=10)\nplt.rc('ytick', labelsize=10)","metadata":{"execution":{"iopub.status.busy":"2025-04-16T06:51:36.473275Z","iopub.execute_input":"2025-04-16T06:51:36.474041Z","iopub.status.idle":"2025-04-16T06:51:36.478793Z","shell.execute_reply.started":"2025-04-16T06:51:36.474016Z","shell.execute_reply":"2025-04-16T06:51:36.478097Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nprint(tf.__version__)\n# Check if a GPU is available\ngpus = tf.config.list_physical_devices('GPU')\nif gpus:\n    try:\n        # Set memory growth to prevent TensorFlow from allocating all memory at once\n        for gpu in gpus:\n            tf.config.experimental.set_memory_growth(gpu, True)\n        print(\"GPU is available and will be used.\")\n    except RuntimeError as e:\n        # Memory growth must be set before GPUs have been initialized\n        print(f\"Error: {e}\")\nelse:\n    print(\"GPU is not available, running on CPU.\")\n\ntf.test.is_gpu_available()","metadata":{"execution":{"iopub.status.busy":"2025-04-16T06:51:36.856669Z","iopub.execute_input":"2025-04-16T06:51:36.857197Z","iopub.status.idle":"2025-04-16T06:51:36.866846Z","shell.execute_reply.started":"2025-04-16T06:51:36.857178Z","shell.execute_reply":"2025-04-16T06:51:36.866170Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nseed_value = 42\ntf.random.set_seed(seed_value)\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()  # TPU detection\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.TPUStrategy(tpu)\n    print(\"TPU is available and will be used.\")\nexcept ValueError:\n    print(\"TPU is not available, using GPU or CPU.\")","metadata":{"execution":{"iopub.status.busy":"2025-04-16T06:51:39.855669Z","iopub.execute_input":"2025-04-16T06:51:39.856382Z","iopub.status.idle":"2025-04-16T06:51:39.876460Z","shell.execute_reply.started":"2025-04-16T06:51:39.856357Z","shell.execute_reply":"2025-04-16T06:51:39.875709Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nprint(os.getcwd())\n# dfdc faces\ndef get_data():\n    return pd.read_csv('/kaggle/input/deepfake-faces/metadata.csv')\n    # return pd.read_csv('/kaggle/input/ff-face-224/FF++_Faces_224/metadata.csv')\n#     return pd.read_csv('/kaggle/input/celebsv2-faces-224/CelebsV2_Faces_224/metadata.csv')\n    \n        \nmeta = get_data()    \nget_data()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T06:39:04.245541Z","iopub.execute_input":"2025-04-16T06:39:04.246097Z","iopub.status.idle":"2025-04-16T06:39:04.497239Z","shell.execute_reply.started":"2025-04-16T06:39:04.246065Z","shell.execute_reply":"2025-04-16T06:39:04.496458Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# #modification for ff++\n# meta = get_data()\n# Name = [ i.split('/')[1] for i in meta[\"Name\"]]\n# meta[\"Name\"] = Name\n# meta","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len( meta[meta[\"label\"] == \"FAKE\"]),len( meta[meta[\"label\"] == \"REAL\"])","metadata":{"execution":{"iopub.status.busy":"2025-04-16T06:51:55.197031Z","iopub.execute_input":"2025-04-16T06:51:55.197577Z","iopub.status.idle":"2025-04-16T06:51:55.222336Z","shell.execute_reply.started":"2025-04-16T06:51:55.197556Z","shell.execute_reply":"2025-04-16T06:51:55.221777Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nreal_df = meta[meta[\"label\"] == \"REAL\"]\n\nfake_df = meta[meta[\"label\"] == \"FAKE\"]\n# sample_size = 8000\n\nreal_df = real_df.sample(8000, random_state=42)\nfake_df = fake_df.sample(8000, random_state=42)\n\nsample_meta = pd.concat([real_df, fake_df])\n","metadata":{"execution":{"iopub.status.busy":"2025-04-16T06:51:50.221101Z","iopub.execute_input":"2025-04-16T06:51:50.221376Z","iopub.status.idle":"2025-04-16T06:51:50.259546Z","shell.execute_reply.started":"2025-04-16T06:51:50.221357Z","shell.execute_reply":"2025-04-16T06:51:50.259012Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport numpy as np\ndef retreive_dataset(set_name):\n    images,labels=[],[]\n    def imgResize(img,width,height):\n        imgResize = cv2.resize(img,(width,height))\n        return imgResize\n    \n    for (img, imclass) in zip(set_name['videoname'], set_name['label']):\n        img = cv2.imread('/kaggle/input/deepfake-faces/faces_224/'+img[:-4]+'.jpg')\n#         img = cv2.imread('/kaggle/input/celebsv2-faces-224/CelebsV2_Faces_224/CelebsV2_Faces_224/'+img[:-4]+'.jpg')\n#         img = cv2.imread('../input/deepfake-faces/faces_224/'+img[:-4]+'.jpg')\n#         img = cv2.imread('../input/shrey-faces-dataset/CustomDataset/'+img[:-4]+'.jpg')\n#         img = imgResize(img, 224,224) \n        images.append(img)\n        if(imclass=='FAKE'):\n            labels.append(1)\n        else:\n            labels.append(0)\n    print(\"Done\")\n    \n    return np.array(images),np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2025-04-16T06:39:25.039672Z","iopub.execute_input":"2025-04-16T06:39:25.040257Z","iopub.status.idle":"2025-04-16T06:39:25.046414Z","shell.execute_reply.started":"2025-04-16T06:39:25.040223Z","shell.execute_reply":"2025-04-16T06:39:25.045594Z"},"trusted":true},"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'])","metadata":{"execution":{"iopub.status.busy":"2025-04-16T06:39:28.191065Z","iopub.execute_input":"2025-04-16T06:39:28.191623Z","iopub.status.idle":"2025-04-16T06:39:28.368650Z","shell.execute_reply.started":"2025-04-16T06:39:28.191600Z","shell.execute_reply":"2025-04-16T06:39:28.368137Z"},"trusted":true},"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":{"execution":{"iopub.status.busy":"2025-04-16T06:39:28.526685Z","iopub.execute_input":"2025-04-16T06:39:28.527247Z","iopub.status.idle":"2025-04-16T06:40:44.151784Z","shell.execute_reply.started":"2025-04-16T06:39:28.527227Z","shell.execute_reply":"2025-04-16T06:40:44.151004Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ntrain_set_raw=tf.data.Dataset.from_tensor_slices((X_train,y_train))\nvalid_set_raw=tf.data.Dataset.from_tensor_slices((X_val,y_val))\ntest_set_raw=tf.data.Dataset.from_tensor_slices((X_test,y_test))","metadata":{"execution":{"iopub.status.busy":"2025-04-16T06:40:44.152993Z","iopub.execute_input":"2025-04-16T06:40:44.153237Z","iopub.status.idle":"2025-04-16T06:40:50.011517Z","shell.execute_reply.started":"2025-04-16T06:40:44.153220Z","shell.execute_reply":"2025-04-16T06:40:50.010725Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_set_raw","metadata":{"execution":{"iopub.status.busy":"2024-10-16T06:29:16.587911Z","iopub.execute_input":"2024-10-16T06:29:16.588290Z","iopub.status.idle":"2024-10-16T06:29:16.595198Z","shell.execute_reply.started":"2024-10-16T06:29:16.588255Z","shell.execute_reply":"2024-10-16T06:29:16.594195Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## pretrain model","metadata":{}},{"cell_type":"code","source":"tf.keras.backend.clear_session()  # extra code – resets layer name counter\n\nbatch_size = 32\npreprocess = tf.keras.applications.convnext.preprocess_input\ntrain_set = train_set_raw.map(lambda X, y: (preprocess(tf.cast(X, tf.float32)), y))\ntrain_set = train_set.shuffle(1000, seed=42).batch(batch_size).prefetch(1)\nvalid_set = valid_set_raw.map(lambda X, y: (preprocess(tf.cast(X, tf.float32)), y)).batch(batch_size)\ntest_set = test_set_raw.map(lambda X, y: (preprocess(tf.cast(X, tf.float32)), y)).batch(batch_size)","metadata":{"execution":{"iopub.status.busy":"2025-04-16T07:17:44.062964Z","iopub.execute_input":"2025-04-16T07:17:44.063217Z","iopub.status.idle":"2025-04-16T07:17:44.499231Z","shell.execute_reply.started":"2025-04-16T07:17:44.063200Z","shell.execute_reply":"2025-04-16T07:17:44.498678Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nfor X_batch, y_batch in train_set.take(1):\n    for index in range(9):\n        plt.subplot(3, 3, index + 1)\n        img = (X_batch[index].numpy())\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        plt.imshow(img /np.max(img)) # rescale to 0–1 for imshow()\n        if(y_batch[index]==1):\n            classt='FAKE'\n        else:\n            classt='REAL'\n        plt.title(f\"Class: {classt}\")\n        plt.axis(\"off\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-04-10T21:06:21.491195Z","iopub.execute_input":"2025-04-10T21:06:21.491458Z","iopub.status.idle":"2025-04-10T21:06:23.987365Z","shell.execute_reply.started":"2025-04-10T21:06:21.491438Z","shell.execute_reply":"2025-04-10T21:06:23.986439Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tf.random.set_seed(42) \ndata_augmentation = tf.keras.Sequential([\n    tf.keras.layers.RandomFlip(mode=\"horizontal\", seed=42),\n    tf.keras.layers.RandomRotation(factor=0.05, seed=42),\n    tf.keras.layers.RandomContrast(factor=0.2, seed=42)\n])","metadata":{"execution":{"iopub.status.busy":"2024-10-16T06:30:21.613395Z","iopub.execute_input":"2024-10-16T06:30:21.613802Z","iopub.status.idle":"2024-10-16T06:30:21.635712Z","shell.execute_reply.started":"2024-10-16T06:30:21.613764Z","shell.execute_reply":"2024-10-16T06:30:21.634901Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# extra code – displays the same first 9 images, after augmentation\n\nplt.figure(figsize=(12, 12))\nfor X_batch, y_batch in train_set.take(1):\n    X_batch_augmented = data_augmentation(X_batch, training=True)\n    for index in range(9):\n        plt.subplot(3, 3, index + 1)\n        img = (X_batch_augmented[index].numpy())\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        plt.imshow(img /np.max(img)) # rescale to 0–1 for imshow()\n        \n        if(y_batch[index]==1):\n            classt='FAKE'\n        else:\n            classt='REAL'\n        plt.title(f\"Class: {classt}\")\n        plt.axis(\"off\")\n\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tf.random.set_seed(42)  # Ensures reproducibility\n\ntf.keras.backend.clear_session()\n# Ensures reproducibility\n\n\nbase_model = tf.keras.applications.ConvNeXtBase(weights=\"imagenet\", include_top=False)\navg = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)\noutput = tf.keras.layers.Dense(1, activation=\"sigmoid\")(avg)\n\nmodel = tf.keras.Model(inputs=base_model.input, outputs=output)\nfor layer in base_model.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2025-04-16T07:21:06.996729Z","iopub.execute_input":"2025-04-16T07:21:06.997605Z","iopub.status.idle":"2025-04-16T07:21:09.913250Z","shell.execute_reply.started":"2025-04-16T07:21:06.997571Z","shell.execute_reply":"2025-04-16T07:21:09.912644Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\noptimizer = tf.keras.optimizers.Adam(learning_rate=0.0001)\nmodel.compile(loss=\"binary_crossentropy\", optimizer=optimizer,\n              metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2024-10-16T08:05:39.307539Z","iopub.execute_input":"2024-10-16T08:05:39.307863Z","iopub.status.idle":"2024-10-16T08:05:39.321151Z","shell.execute_reply.started":"2024-10-16T08:05:39.307830Z","shell.execute_reply":"2024-10-16T08:05:39.320423Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint, CSVLogger\n\nmodel_name = \"ff_InceptionV3\"\n\ncsv_logger = CSVLogger(model_name+'.csv',append = False)\n\ncallbacks = [csv_logger]","metadata":{"execution":{"iopub.status.busy":"2024-10-16T08:04:53.968514Z","iopub.execute_input":"2024-10-16T08:04:53.969186Z","iopub.status.idle":"2024-10-16T08:04:53.973891Z","shell.execute_reply.started":"2024-10-16T08:04:53.969145Z","shell.execute_reply":"2024-10-16T08:04:53.972883Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nhistory = model.fit(train_set, validation_data=valid_set, epochs=10, callbacks= callbacks)\n","metadata":{"execution":{"iopub.status.busy":"2024-10-16T07:58:17.738187Z","iopub.execute_input":"2024-10-16T07:58:17.739139Z","iopub.status.idle":"2024-10-16T07:59:32.659910Z","shell.execute_reply.started":"2024-10-16T07:58:17.739082Z","shell.execute_reply":"2024-10-16T07:59:32.658971Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport psutil\nimport subprocess\nimport tensorflow as tf\nimport time\n\ndef get_cpu_memory():\n    process = psutil.Process(os.getpid())\n    return process.memory_info().rss  # in bytes\n\ndef get_gpu_memory():\n    result = subprocess.run(\n        ['nvidia-smi', '--query-gpu=memory.used', '--format=csv,nounits,noheader'],\n        stdout=subprocess.PIPE, universal_newlines=True\n    )\n    return int(result.stdout.strip().split('\\n')[0])  # in MB\n\ndef measure_model_load_memory(model_load_func):\n    # Clear session to release any old models\n    tf.keras.backend.clear_session()\n    time.sleep(1)\n\n    cpu_before = get_cpu_memory()\n    gpu_before = get_gpu_memory()\n\n    # --- Load Model ---\n    start = time.time()\n    model = model_load_func()\n    end = time.time()\n\n    cpu_after = get_cpu_memory()\n    gpu_after = get_gpu_memory()\n\n    cpu_used = (cpu_after - cpu_before) / (1024 ** 2)  # MB\n    gpu_used = gpu_after - gpu_before\n\n    print(\"\\n🧠 Model Load Memory Usage\")\n    print(\"------------------------------\")\n    print(f\"CPU Memory Used  : {cpu_used:.2f} MB\")\n    print(f\"GPU Memory Used  : {gpu_used} MB\")\n    print(f\"Model Load Time  : {end - start:.2f} sec\")\n\n    return model  # return the loaded model if needed\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T06:41:07.641939Z","iopub.execute_input":"2025-04-16T06:41:07.642564Z","iopub.status.idle":"2025-04-16T06:41:07.648750Z","shell.execute_reply.started":"2025-04-16T06:41:07.642542Z","shell.execute_reply":"2025-04-16T06:41:07.647994Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"!nividia-smi","metadata":{}},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2025-04-16T06:42:51.604836Z","iopub.execute_input":"2025-04-16T06:42:51.605443Z","iopub.status.idle":"2025-04-16T06:42:54.884410Z","shell.execute_reply.started":"2025-04-16T06:42:51.605419Z","shell.execute_reply":"2025-04-16T06:42:54.883902Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Infernceing","metadata":{}},{"cell_type":"code","source":"!pip install memory_profiler","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T06:42:57.864288Z","iopub.execute_input":"2025-04-16T06:42:57.864823Z","iopub.status.idle":"2025-04-16T06:43:02.368159Z","shell.execute_reply.started":"2025-04-16T06:42:57.864798Z","shell.execute_reply":"2025-04-16T06:43:02.367329Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def profile_model_avg_per_image(model, dataset):\n    import os\n    import psutil\n    import subprocess\n    import threading\n    import time\n    import tensorflow as tf\n    from memory_profiler import memory_usage\n\n    # --- Memory utils ---\n    def get_cpu_memory():\n        process = psutil.Process(os.getpid())\n        return process.memory_info().rss  # in bytes\n\n    def get_gpu_memory():\n        result = subprocess.run(\n            ['nvidia-smi', '--query-gpu=memory.used', '--format=csv,nounits,noheader'],\n            stdout=subprocess.PIPE, universal_newlines=True\n        )\n        return int(result.stdout.strip().split('\\n')[0])\n\n    class GPUMemoryTracker:\n        def __init__(self, interval=0.05):\n            self.interval = interval\n            self.peak_memory = 0\n            self.running = False\n\n        def _get_gpu_memory(self):\n            return get_gpu_memory()\n\n        def _track(self):\n            while self.running:\n                mem = self._get_gpu_memory()\n                if mem > self.peak_memory:\n                    self.peak_memory = mem\n                time.sleep(self.interval)\n\n        def start(self):\n            self.running = True\n            self.thread = threading.Thread(target=self._track)\n            self.thread.start()\n\n        def stop(self):\n            self.running = False\n            self.thread.join()\n\n    # Unbatch to ensure single images are processed\n    dataset = dataset.unbatch().batch(1)\n\n    # Lists to track per-image metrics\n    time_list = []\n    cpu_used_list = []\n    cpu_peak_list = []\n    gpu_used_list = []\n    gpu_peak_list = []\n\n    total_images = 0\n    max_images = 100  # Limit to 100 images\n\n    \n    cpu_before = get_cpu_memory()\n    gpu_before = get_gpu_memory()\n    for image_batch, _ in dataset:\n        if total_images >= max_images:\n            break\n\n        total_images += 1\n\n        # Capture memory before\n        \n\n        tracker = GPUMemoryTracker()\n        tracker.start()\n\n        def inference():\n            model.predict(image_batch, verbose=0)\n\n        start = time.time()\n        cpu_peak = memory_usage((inference,), interval=0.05, max_usage=True)\n        end = time.time()\n        tracker.stop()\n\n        # Capture memory after\n        cpu_after = get_cpu_memory()\n        gpu_after = get_gpu_memory()\n\n        # Metrics\n        cpu_used = (cpu_after - cpu_before) / (1024 ** 2)\n        gpu_used = gpu_after - gpu_before\n        cpu_peak = cpu_peak if isinstance(cpu_peak, float) else max(cpu_peak)\n        time_taken = end - start\n\n        time_list.append(time_taken)\n        cpu_used_list.append(cpu_used)\n        cpu_peak_list.append(cpu_peak)\n        gpu_used_list.append(gpu_used)\n        gpu_peak_list.append(tracker.peak_memory)\n\n    # Final averages\n    avg_time = sum(time_list) / total_images\n    avg_cpu_used = sum(cpu_used_list) / total_images\n    avg_cpu_peak = sum(cpu_peak_list) / total_images\n    avg_gpu_used = sum(gpu_used_list) / total_images\n    avg_gpu_peak = sum(gpu_peak_list) / total_images\n\n    print(\"\\n🧠 Per-Image Average Performance Summary\")\n    print(\"------------------------------------------\")\n    print(f\"Total Images         : {total_images}\")\n    print(f\"Avg Inference Time   : {avg_time:.4f} sec\")\n    print(f\"Avg CPU Memory Used  : {avg_cpu_used:.2f} MB\")\n    print(f\"Avg CPU Peak Memory  : {avg_cpu_peak:.2f} MB\")\n    print(f\"Avg GPU Memory Used  : {avg_gpu_used:.2f} MB\")\n    print(f\"Avg GPU Peak Memory  : {avg_gpu_peak:.2f} MB\")\n\n    return {\n        \"time\": time_list,\n        \"cpu_used\": cpu_used_list,\n        \"cpu_peak\": cpu_peak_list,\n        \"gpu_used\": gpu_used_list,\n        \"gpu_peak\": gpu_peak_list,\n    }\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T06:44:21.171726Z","iopub.execute_input":"2025-04-16T06:44:21.172559Z","iopub.status.idle":"2025-04-16T06:44:21.184961Z","shell.execute_reply.started":"2025-04-16T06:44:21.172528Z","shell.execute_reply":"2025-04-16T06:44:21.184256Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tf.keras.backend.clear_session()  # extra code – resets layer name counter\n# model.save_weights(model_name+'.weights.h5')\nmodel.load_weights(\"/kaggle/input/deepfake_models/keras/deepfake_models/8/dfdc_New_models/dfdc_ConvNeXt.weights.h5\")\n# model = tf.keras.models.load_model(\"/kaggle/input/deepfake_models/keras/deepfake_models/8/dfdc_New_models/dfdc_ResNet50.h5\")\n# model = measure_model_load_memory(lambda: tf.keras.models.load_model(\n#     \"/kaggle/input/deepfake_models/keras/deepfake_models/8/dfdc_New_models/dfdc_ResNet50.h5\"\n# ))\n# #Load model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T07:20:50.439080Z","iopub.execute_input":"2025-04-16T07:20:50.439366Z","iopub.status.idle":"2025-04-16T07:20:53.232920Z","shell.execute_reply.started":"2025-04-16T07:20:50.439348Z","shell.execute_reply":"2025-04-16T07:20:53.232345Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# %%time\n# Run profiling\nprofile = profile_model_avg_per_image(model, test_set)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T07:18:12.642899Z","iopub.execute_input":"2025-04-16T07:18:12.643179Z","iopub.status.idle":"2025-04-16T07:18:57.512916Z","shell.execute_reply.started":"2025-04-16T07:18:12.643160Z","shell.execute_reply":"2025-04-16T07:18:57.511991Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# df = pd.DataFrame()\ndf['ConvNeXt'] = profile[\"cpu_used\"]\ndf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T07:18:57.514449Z","iopub.execute_input":"2025-04-16T07:18:57.514680Z","iopub.status.idle":"2025-04-16T07:18:57.530334Z","shell.execute_reply.started":"2025-04-16T07:18:57.514660Z","shell.execute_reply":"2025-04-16T07:18:57.529772Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tf.keras.backend.clear_session()  # extra code – resets layer name counter\n# model.save_weights(model_name+'.weights.h5')\nmodel.load_weights(\"/kaggle/input/deepfake_models/keras/deepfake_models/8/dfdc_New_models/dfdc_ConvNeXt_ft.weights.h5\")\n# model = tf.keras.models.load_model(\"/kaggle/input/deepfake_models/keras/deepfake_models/8/dfdc_New_models/dfdc_ResNet50_ft.h5\")\n# model = measure_model_load_memory(lambda: tf.keras.models.load_model(\n#     \"/kaggle/input/deepfake_models/keras/deepfake_models/8/dfdc_New_models/dfdc_ResNet50.h5\"\n# ))\n# #Load model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T07:21:45.082576Z","iopub.execute_input":"2025-04-16T07:21:45.082885Z","iopub.status.idle":"2025-04-16T07:21:48.405033Z","shell.execute_reply.started":"2025-04-16T07:21:45.082841Z","shell.execute_reply":"2025-04-16T07:21:48.404482Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"profile = profile_model_avg_per_image(model, test_set)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T07:21:53.106806Z","iopub.execute_input":"2025-04-16T07:21:53.107135Z","iopub.status.idle":"2025-04-16T07:22:37.265280Z","shell.execute_reply.started":"2025-04-16T07:21:53.107114Z","shell.execute_reply":"2025-04-16T07:22:37.264396Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# df = pd.DataFrame()\ndf['ConvNeXt_ft'] = profile[\"cpu_used\"]\ndf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T07:22:37.266788Z","iopub.execute_input":"2025-04-16T07:22:37.267075Z","iopub.status.idle":"2025-04-16T07:22:37.283148Z","shell.execute_reply.started":"2025-04-16T07:22:37.267045Z","shell.execute_reply":"2025-04-16T07:22:37.282307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save to CSV\ndf.to_csv('MemoryProfile.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T07:24:47.922275Z","iopub.execute_input":"2025-04-16T07:24:47.922838Z","iopub.status.idle":"2025-04-16T07:24:47.929107Z","shell.execute_reply.started":"2025-04-16T07:24:47.922807Z","shell.execute_reply":"2025-04-16T07:24:47.928397Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Results(model,test_set)","metadata":{"execution":{"iopub.status.busy":"2024-10-16T08:00:05.137820Z","iopub.execute_input":"2024-10-16T08:00:05.138710Z","iopub.status.idle":"2024-10-16T08:00:34.886277Z","shell.execute_reply.started":"2024-10-16T08:00:05.138667Z","shell.execute_reply":"2024-10-16T08:00:34.885321Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define input shape\ninput_shape = (1, 224, 224,3 )\n# Calculate FLOPs\nflops = get_flops(model, input_shape)\nmacs = flops // 2  # MACs is half the FLOPs for Conv2D\n\nprint(f\"MACs: {macs}\")\nprint(f\"FLOPs: {flops}\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"_kg_hide-input":false,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"##  Fine tuned","metadata":{}},{"cell_type":"code","source":"# model = tf.keras.models.load_model(\"/kaggle/working/dfdc_Xception.h5\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(base_model.layers)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nfor layer in base_model.layers[-150:]:\n    layer.trainable = True","metadata":{"execution":{"iopub.status.busy":"2024-10-16T08:08:43.570945Z","iopub.execute_input":"2024-10-16T08:08:43.571351Z","iopub.status.idle":"2024-10-16T08:08:43.580299Z","shell.execute_reply.started":"2024-10-16T08:08:43.571313Z","shell.execute_reply":"2024-10-16T08:08:43.579388Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n    \noptimizer = tf.keras.optimizers.Adam(learning_rate=0.0001)\nmodel.compile(loss=\"binary_crossentropy\", optimizer=optimizer,\n              metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2024-10-16T08:08:46.310466Z","iopub.execute_input":"2024-10-16T08:08:46.310848Z","iopub.status.idle":"2024-10-16T08:08:46.321954Z","shell.execute_reply.started":"2024-10-16T08:08:46.310812Z","shell.execute_reply":"2024-10-16T08:08:46.320888Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint, CSVLogger\n\n\n\ncsv_logger = CSVLogger(model_name+'_ft.csv',append = False)\n\ncallbacks = [csv_logger]","metadata":{"execution":{"iopub.status.busy":"2024-10-16T08:08:47.346770Z","iopub.execute_input":"2024-10-16T08:08:47.347673Z","iopub.status.idle":"2024-10-16T08:08:47.352048Z","shell.execute_reply.started":"2024-10-16T08:08:47.347635Z","shell.execute_reply":"2024-10-16T08:08:47.351100Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nhistory = model.fit(train_set, validation_data=valid_set, epochs=10, callbacks= callbacks)\n","metadata":{"execution":{"iopub.status.busy":"2024-10-16T08:08:48.558474Z","iopub.execute_input":"2024-10-16T08:08:48.559148Z","iopub.status.idle":"2024-10-16T08:09:42.023755Z","shell.execute_reply.started":"2024-10-16T08:08:48.559109Z","shell.execute_reply":"2024-10-16T08:09:42.022790Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\n# model_name = \"dfdc_Xception_ft\"\n# model.save_weights(model_name+'_ft.weights.h5')\n# model.load_weights(\"/kaggle/working/ff_InceptionV3_ft.weights.h5\")\nmodel.load_weights(\"/kaggle/input/deepfake_models/keras/deepfake_models/8/dfdc_New_models/dfdc_ConvNeXt_ft.weights.h5\")\n\n# model = tf.keras.models.load_model(\"/kaggle/input/deepfake_models/keras/deepfake_models/8/dfdc_New_models/dfdc_ResNet50_ft.h5\")","metadata":{"execution":{"iopub.status.busy":"2025-04-10T21:15:30.547722Z","iopub.execute_input":"2025-04-10T21:15:30.548235Z","iopub.status.idle":"2025-04-10T21:15:34.193899Z","shell.execute_reply.started":"2025-04-10T21:15:30.548211Z","shell.execute_reply":"2025-04-10T21:15:34.193084Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Run profiling\nprofile = profile_model_avg_per_image(model, test_set)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-10T21:15:36.591936Z","iopub.execute_input":"2025-04-10T21:15:36.592443Z","iopub.status.idle":"2025-04-10T21:16:24.691556Z","shell.execute_reply.started":"2025-04-10T21:15:36.592422Z","shell.execute_reply":"2025-04-10T21:16:24.690667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Results(model,test_set)","metadata":{"execution":{"iopub.status.busy":"2024-10-16T08:09:52.358650Z","iopub.execute_input":"2024-10-16T08:09:52.359038Z","iopub.status.idle":"2024-10-16T08:10:08.774262Z","shell.execute_reply.started":"2024-10-16T08:09:52.359001Z","shell.execute_reply":"2024-10-16T08:10:08.773239Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define input shape\ninput_shape = (1, 224, 224,3 )\n# Calculate FLOPs\nflops = get_flops(model, input_shape)\nmacs = flops // 2  # MACs is half the FLOPs for Conv2D\n\nprint(f\"MACs: {macs}\")\nprint(f\"FLOPs: {flops}\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Xception","metadata":{}},{"cell_type":"code","source":"plot_combined_history(\"/kaggle/working/dfdc_DenseNet121.csv\", \"/kaggle/working/dfdc_DenseNet121_ft.csv\",'1','2')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = tf.keras.models.load_model(\"/kaggle/input/deepfake_models/keras/deepfake_models/6/ResNet50_deepfake_image.h5\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Results(model,test_set)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define input shape\ninput_shape = (1, 224, 224,3 )\n# Calculate FLOPs\nflops = get_flops(model, input_shape)\nmacs = flops // 2  # MACs is half the FLOPs for Conv2D\n\nprint(f\"MACs: {macs}\")\nprint(f\"FLOPs: {flops}\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Transformer","metadata":{}},{"cell_type":"code","source":"# !pip install --upgrade pip\n!pip install transformers==4.42.4 tensorflow==2.15.0 ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T21:51:32.093334Z","iopub.execute_input":"2025-04-16T21:51:32.093659Z","iopub.status.idle":"2025-04-16T21:53:20.398546Z","shell.execute_reply.started":"2025-04-16T21:51:32.093635Z","shell.execute_reply":"2025-04-16T21:53:20.392099Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport transformers as T\nprint(tf.__version__) \nprint(T.__version__)","metadata":{"execution":{"iopub.status.busy":"2025-04-16T21:53:20.401073Z","iopub.execute_input":"2025-04-16T21:53:20.401504Z","iopub.status.idle":"2025-04-16T21:53:56.703439Z","shell.execute_reply.started":"2025-04-16T21:53:20.401460Z","shell.execute_reply":"2025-04-16T21:53:56.696963Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import PIL\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nimport cv2 \nimport numpy as np \nfrom transformers import AutoImageProcessor\n","metadata":{"execution":{"iopub.status.busy":"2025-04-16T21:53:56.705716Z","iopub.execute_input":"2025-04-16T21:53:56.706163Z","iopub.status.idle":"2025-04-16T21:54:06.676739Z","shell.execute_reply.started":"2025-04-16T21:53:56.706137Z","shell.execute_reply":"2025-04-16T21:54:06.672455Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nseed_value = 42\ntf.random.set_seed(seed_value)\n\n# Check if a GPU is available\ngpus = tf.config.list_physical_devices('GPU')\nif gpus:\n    try:\n        # Set memory growth to prevent TensorFlow from allocating all memory at once\n        for gpu in gpus:\n            tf.config.experimental.set_memory_growth(gpu, True)\n        print(\"GPU is available and will be used.\")\n    except RuntimeError as e:\n        # Memory growth must be set before GPUs have been initialized\n        print(f\"Error: {e}\")\nelse:\n    print(\"GPU is not available, running on CPU.\")\n\ntf.test.is_gpu_available()","metadata":{"execution":{"iopub.status.busy":"2025-04-16T21:54:06.678561Z","iopub.execute_input":"2025-04-16T21:54:06.679039Z","iopub.status.idle":"2025-04-16T21:54:06.706396Z","shell.execute_reply.started":"2025-04-16T21:54:06.679011Z","shell.execute_reply":"2025-04-16T21:54:06.701753Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nseed_value = 42\ntf.random.set_seed(seed_value)\n\ntry:\n    # TPU detection and connection\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()  # Detect TPU\n    tf.config.experimental_connect_to_cluster(tpu)  # Connect to the TPU\n    tf.tpu.experimental.initialize_tpu_system(tpu)  # Initialize TPU\n    strategy = tf.distribute.TPUStrategy(tpu)  # Set TPU strategy\n    print(\"TPU is available and will be used.\")\nexcept ValueError:\n    print(\"TPU is not available, CPU will be used instead.\")\n","metadata":{"execution":{"iopub.status.busy":"2025-04-16T19:33:15.083249Z","iopub.execute_input":"2025-04-16T19:33:15.083579Z","iopub.status.idle":"2025-04-16T19:33:15.095082Z","shell.execute_reply.started":"2025-04-16T19:33:15.083554Z","shell.execute_reply":"2025-04-16T19:33:15.090108Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_data():\n    return pd.read_csv(\"/kaggle/input/deepfake-faces/metadata.csv\")\n    # return pd.read_csv('/kaggle/input/ff-face-224/FF++_Faces_224/metadata.csv')\n#     return pd.read_csv('/kaggle/input/celebsv2-faces-224/CelebsV2_Faces_224/metadata.csv')\n\n#modification for ff++\nmeta = get_data()\n\n\nprint(meta.shape)\nprint(len(meta[meta.label == 'FAKE']), len(meta[meta.label == 'REAL']))\n\nreal_df = meta[meta[\"label\"] == \"REAL\"]\nfake_df = meta[meta[\"label\"] == \"FAKE\"]","metadata":{"execution":{"iopub.status.busy":"2025-04-16T21:54:06.708787Z","iopub.execute_input":"2025-04-16T21:54:06.709026Z","iopub.status.idle":"2025-04-16T21:54:06.896179Z","shell.execute_reply.started":"2025-04-16T21:54:06.709002Z","shell.execute_reply":"2025-04-16T21:54:06.892020Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# Load data from CSV\ndef get_data():\n    return pd.read_csv('/kaggle/input/deepfake-faces/metadata.csv')\n    # return pd.read_csv('/kaggle/input/ff-face-224/FF++_Faces_224/metadata.csv')\n\n#modification for ff++\nmeta = get_data()\n\n\nprint(meta.shape)\nprint(len(meta[meta.label == 'FAKE']), len(meta[meta.label == 'REAL']))\n\nreal_df = meta[meta[\"label\"] == \"REAL\"]\nfake_df = meta[meta[\"label\"] == \"FAKE\"]\n# sample_size = 8000\n\nreal_df = real_df.sample(8000, random_state=42)\nfake_df = fake_df.sample(8000, random_state=42)\nsample_meta = pd.concat([real_df, fake_df])\n# sample_meta = sample_meta.sample(fra/c=1, random_state=42).reset_index(drop=True)\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'])\nprint(Train_set.shape, Val_set.shape, Test_set.shape)\n\n\n# Function to retrieve dataset\ndef retrieve_dataset(set_name, dir = \"/kaggle/input/deepfake-faces/faces_224/\"):\n    images, labels = [], []\n\n    def imgResize(img, width, height):\n        return cv2.resize(img, (width, height))\n\n    for (img, imclass) in zip(set_name['videoname'], set_name['label']):\n        img = cv2.imread(dir+'/' + img[:-4] + '.jpg')\n        img = imgResize(img, 224, 224)\n#         img = img.astype('uint8')\n        images.append(img)\n        labels.append(1 if imclass == 'FAKE' else 0)\n    \n    return np.array(images), np.array(labels)\n\nX_train, y_train = retrieve_dataset(Train_set)\nX_val, y_val = retrieve_dataset(Val_set)\nX_test, y_test = retrieve_dataset(Test_set)\n\nprint(X_train.shape)\nprint(\"Retrieved successfully\")","metadata":{"execution":{"iopub.status.busy":"2025-04-16T21:54:06.898223Z","iopub.execute_input":"2025-04-16T21:54:06.898759Z","iopub.status.idle":"2025-04-16T21:55:48.601967Z","shell.execute_reply.started":"2025-04-16T21:54:06.898728Z","shell.execute_reply":"2025-04-16T21:55:48.597657Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# tf.keras.backend.clear_session()\ndef preprocessed( images ,image_processor ):\n    Pre_images = []\n    for image in images:\n        \n        pre_img = image_processor(image, return_tensors=\"tf\").pixel_values[0]\n        Pre_images.append(pre_img)\n        \n    print(\"Done\")\n    return tf.convert_to_tensor(Pre_images)\n\nimage_processor = AutoImageProcessor.from_pretrained(\"facebook/deit-base-distilled-patch16-224\")\n# image_processor = AutoImageProcessor.from_pretrained(\"google/vit-base-patch16-224-in21k\")\n# image_processor = AutoImageProcessor.from_pretrained(\"microsoft/swin-tiny-patch4-window7-224\")\n\n\nX_train_preprocessed = preprocessed(X_train,image_processor)\nX_val_preprocessed = preprocessed(X_val,image_processor)\nX_test_preprocessed = preprocessed(X_test,image_processor)\n\n\nprint(\"Pre-Processed succesful\")\n","metadata":{"execution":{"iopub.status.busy":"2025-04-16T21:55:48.604300Z","iopub.execute_input":"2025-04-16T21:55:48.604540Z","iopub.status.idle":"2025-04-16T22:00:05.386885Z","shell.execute_reply.started":"2025-04-16T21:55:48.604516Z","shell.execute_reply":"2025-04-16T22:00:05.382202Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"counts = np.bincount(y_train)#array([ 497, 2800])\ncounts","metadata":{"execution":{"iopub.status.busy":"2024-10-18T10:22:23.599104Z","iopub.execute_input":"2024-10-18T10:22:23.599969Z","iopub.status.idle":"2024-10-18T10:22:23.607006Z","shell.execute_reply.started":"2024-10-18T10:22:23.599933Z","shell.execute_reply":"2024-10-18T10:22:23.606266Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"batch_size = 32\n\n# Convert preprocessed images into datasets suitable for training\ntrain_dataset = tf.data.Dataset.from_tensor_slices((X_train_preprocessed, y_train )).shuffle(buffer_size=1000,seed=42).batch(batch_size).prefetch(1)\nvalid_dataset = tf.data.Dataset.from_tensor_slices((X_val_preprocessed, y_val)).batch(batch_size)\ntest_dataset = tf.data.Dataset.from_tensor_slices((X_test_preprocessed, y_test)).batch(batch_size)","metadata":{"execution":{"iopub.status.busy":"2025-04-16T22:00:05.388822Z","iopub.execute_input":"2025-04-16T22:00:05.389044Z","iopub.status.idle":"2025-04-16T22:00:05.418469Z","shell.execute_reply.started":"2025-04-16T22:00:05.389022Z","shell.execute_reply":"2025-04-16T22:00:05.413939Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dataset  \n\n","metadata":{"execution":{"iopub.status.busy":"2024-10-10T10:33:09.415085Z","iopub.execute_input":"2024-10-10T10:33:09.415505Z","iopub.status.idle":"2024-10-10T10:33:09.420418Z","shell.execute_reply.started":"2024-10-10T10:33:09.415478Z","shell.execute_reply":"2024-10-10T10:33:09.419745Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import transformers\nprint(tf.__version__)\nprint(transformers.__version__)","metadata":{"execution":{"iopub.status.busy":"2024-10-18T08:09:57.201673Z","iopub.execute_input":"2024-10-18T08:09:57.202436Z","iopub.status.idle":"2024-10-18T08:09:57.206424Z","shell.execute_reply.started":"2024-10-18T08:09:57.202398Z","shell.execute_reply":"2024-10-18T08:09:57.205758Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preprocessed_input_layer = tf.keras.Sequential([\n    tf.keras.layers.Resizing(224,224),\n    tf.keras.layers.Rescaling(1./255),\n    tf.keras.layers.Permute((3,1,2))\n])\ndata_augmentation = tf.keras.Sequential([\n    tf.keras.layers.RandomFlip(mode=\"horizontal\", seed=42),\n    tf.keras.layers.RandomRotation(factor=0.05, seed=42),\n    tf.keras.layers.RandomContrast(factor=0.2, seed=42)\n],name=\"data_augmentation\",)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-10-18T08:09:58.750371Z","iopub.execute_input":"2024-10-18T08:09:58.750780Z","iopub.status.idle":"2024-10-18T08:09:58.788962Z","shell.execute_reply.started":"2024-10-18T08:09:58.750745Z","shell.execute_reply":"2024-10-18T08:09:58.788058Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tf.keras.backend.clear_session()\n# from transformers import TFViTModel\nfrom transformers import TFDeiTModel\n# from transformers import TFSwinModel\n\n# Create an input layer\ninput_layer = tf.keras.layers.Input(shape=(3, 224, 224), name='input_layer')\n\n# Load the ViT model\n\nbase_model = TFDeiTModel.from_pretrained(\"facebook/deit-base-distilled-patch16-224\")\n# base_model = TFViTModel.from_pretrained(\"google/vit-base-patch16-224-in21k\")\n# base_model = TFSwinModel.from_pretrained(\"microsoft/swin-tiny-patch4-window7-224\")\n\n# Extract outputs from the Swin Transformer model\noutputs = base_model.deit(input_layer).last_hidden_state\n\n\n# Apply global average pooling to get a single feature vector\navg = tf.keras.layers.GlobalAveragePooling1D()(outputs)\n\n\n# Add a dense layer with a sigmoid activation for binary classification\noutput = tf.keras.layers.Dense(1, activation=\"sigmoid\")(avg)\n\n# Create the model\nmodel = tf.keras.Model(inputs=input_layer, outputs=output)\n\n# Freeze the ViT model layers\nfor layer in base_model.layers:\n    layer.trainable = False\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T22:00:05.420982Z","iopub.execute_input":"2025-04-16T22:00:05.421193Z","iopub.status.idle":"2025-04-16T22:00:13.917228Z","shell.execute_reply.started":"2025-04-16T22:00:05.421173Z","shell.execute_reply":"2025-04-16T22:00:13.910965Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = tf.keras.optimizers.legacy.Adam(learning_rate=0.0001)\nmodel.compile(loss=\"binary_crossentropy\", optimizer=optimizer, metrics=[\"accuracy\"])\n","metadata":{"execution":{"iopub.status.busy":"2025-04-16T22:00:13.919692Z","iopub.execute_input":"2025-04-16T22:00:13.919966Z","iopub.status.idle":"2025-04-16T22:00:13.949359Z","shell.execute_reply.started":"2025-04-16T22:00:13.919940Z","shell.execute_reply":"2025-04-16T22:00:13.943372Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint, CSVLogger\n\nmodel_name = \"DFDC_Deit\"\n\n\ncsv_logger = CSVLogger(model_name+'.csv',append = False)\n\ncallbacks = [csv_logger]","metadata":{"execution":{"iopub.status.busy":"2025-04-16T22:00:13.950204Z","iopub.execute_input":"2025-04-16T22:00:13.950525Z","iopub.status.idle":"2025-04-16T22:00:14.665546Z","shell.execute_reply.started":"2025-04-16T22:00:13.950502Z","shell.execute_reply":"2025-04-16T22:00:14.659310Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(train_dataset, validation_data=valid_dataset,callbacks = callbacks, epochs=10)\n","metadata":{"execution":{"iopub.status.busy":"2025-04-16T22:00:14.668201Z","iopub.execute_input":"2025-04-16T22:00:14.668550Z","iopub.status.idle":"2025-04-16T22:22:58.701776Z","shell.execute_reply.started":"2025-04-16T22:00:14.668522Z","shell.execute_reply":"2025-04-16T22:22:58.695731Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# history_data(history, model_name)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-14T12:43:06.356851Z","iopub.execute_input":"2025-04-14T12:43:06.357222Z","iopub.status.idle":"2025-04-14T12:43:06.366139Z","shell.execute_reply.started":"2025-04-14T12:43:06.357195Z","shell.execute_reply":"2025-04-14T12:43:06.362234Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install memory_profiler","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T22:23:07.291882Z","iopub.execute_input":"2025-04-16T22:23:07.292291Z","iopub.status.idle":"2025-04-16T22:23:11.556819Z","shell.execute_reply.started":"2025-04-16T22:23:07.292226Z","shell.execute_reply":"2025-04-16T22:23:11.550754Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def profile_model_avg_per_image(model, dataset):\n    import os\n    import psutil\n    import time\n    import tensorflow as tf\n    from memory_profiler import memory_usage\n\n    # --- CPU Memory Utility ---\n    def get_cpu_memory():\n        process = psutil.Process(os.getpid())\n        return process.memory_info().rss  # in bytes\n\n    # Unbatch to ensure single images are processed\n    dataset = dataset.unbatch().batch(1)\n\n    # Lists to track per-image metrics\n    time_list = []\n    cpu_used_list = []\n    cpu_peak_list = []\n\n    total_images = 0\n    max_images = 100  # Limit to 100 images\n    cpu_before = get_cpu_memory()\n    for image_batch, _ in dataset:\n        if total_images >= max_images:\n            break\n\n        total_images += 1\n\n        # Capture memory before\n        \n\n        def inference():\n            model.predict(image_batch, verbose=0)\n\n        start = time.time()\n        cpu_peak = memory_usage((inference,), interval=0.05, max_usage=True)\n        end = time.time()\n\n        # Capture memory after\n        cpu_after = get_cpu_memory()\n\n        # Metrics\n        cpu_used = (cpu_after - cpu_before) / (1024 ** 2)\n        cpu_peak = cpu_peak if isinstance(cpu_peak, float) else max(cpu_peak)\n        time_taken = end - start\n\n        time_list.append(time_taken)\n        cpu_used_list.append(cpu_used)\n        cpu_peak_list.append(cpu_peak)\n\n    # Final averages\n    avg_time = sum(time_list) / total_images\n    avg_cpu_used = sum(cpu_used_list) / total_images\n    avg_cpu_peak = sum(cpu_peak_list) / total_images\n\n    print(\"\\n🧠 Per-Image Average Performance Summary (CPU Only)\")\n    print(\"-----------------------------------------------------\")\n    print(f\"Total Images         : {total_images}\")\n    print(f\"Avg Inference Time   : {avg_time:.4f} sec\")\n    print(f\"Avg CPU Memory Used  : {avg_cpu_used:.2f} MB\")\n    print(f\"Avg CPU Peak Memory  : {avg_cpu_peak:.2f} MB\")\n\n    return {\n        \"time\": time_list,\n        \"cpu_used\": cpu_used_list,\n        \"cpu_peak\": cpu_peak_list,\n    }\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T22:23:11.559412Z","iopub.execute_input":"2025-04-16T22:23:11.559706Z","iopub.status.idle":"2025-04-16T22:23:11.578107Z","shell.execute_reply.started":"2025-04-16T22:23:11.559677Z","shell.execute_reply":"2025-04-16T22:23:11.571676Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# model.save_weights(model_name + \"_weights.h5\")\n# model.save(model_name+\".h5\")\nmodel.load_weights(\"/kaggle/input/deepfake_models/keras/deepfake_models/8/dfdc_New_models/dfdc_Deit_weights.h5\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T20:58:09.319042Z","iopub.execute_input":"2025-04-16T20:58:09.319395Z","iopub.status.idle":"2025-04-16T20:58:11.117458Z","shell.execute_reply.started":"2025-04-16T20:58:09.319364Z","shell.execute_reply":"2025-04-16T20:58:11.112182Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# Run profiling\nprofile = profile_model_avg_per_image(model, test_dataset)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T22:23:14.995039Z","iopub.execute_input":"2025-04-16T22:23:14.995426Z","iopub.status.idle":"2025-04-16T22:25:28.013232Z","shell.execute_reply.started":"2025-04-16T22:23:14.995394Z","shell.execute_reply":"2025-04-16T22:25:28.008320Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df= pd.read_csv(\"/kaggle/working/MemoryProfile.csv\")\ndf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T22:26:30.252179Z","iopub.execute_input":"2025-04-16T22:26:30.252611Z","iopub.status.idle":"2025-04-16T22:26:30.300078Z","shell.execute_reply.started":"2025-04-16T22:26:30.252579Z","shell.execute_reply":"2025-04-16T22:26:30.295477Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# df = pd.read_csv(\"/kaggle/working/MemoryProfile.csv\")\ndf[\"Deit\"] = df['Deit_ft']\ndf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T22:34:20.549130Z","iopub.execute_input":"2025-04-16T22:34:20.549544Z","iopub.status.idle":"2025-04-16T22:34:20.578785Z","shell.execute_reply.started":"2025-04-16T22:34:20.549515Z","shell.execute_reply":"2025-04-16T22:34:20.573713Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Set up plot\nplt.figure(figsize=(16, 8))\n\n# Color mapping for base model names\ncolors = plt.cm.tab20.colors  # Up to 20 unique colors\ncolor_map = {}\n\n# Plot each model\ncolor_index = 0\nfor column in df.columns:\n    base_name = column.replace('_ft', '')\n    \n    # Assign color if not already assigned\n    if base_name not in color_map:\n        color_map[base_name] = colors[color_index % len(colors)]\n        color_index += 1\n    \n    linestyle = '--' if column.endswith('_ft') else '-'\n    plt.plot(df.index, df[column], linestyle=linestyle, color=color_map[base_name], marker='o', label=column)\n\n# Final touches\nplt.title(\"Memory Usage of Models Over 100 Images\")\nplt.xlabel(\"Sample Index\")\nplt.ylabel(\"Memory (MB)\")\nplt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')\nplt.grid(True)\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T22:35:46.415330Z","iopub.execute_input":"2025-04-16T22:35:46.415734Z","iopub.status.idle":"2025-04-16T22:35:46.811010Z","shell.execute_reply.started":"2025-04-16T22:35:46.415701Z","shell.execute_reply":"2025-04-16T22:35:46.806093Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"n = 0\nsum = 0\nfor i in profile['cpu_used']:\n    if i>=0:\n        n+=1\n        sum+=i\nsum/n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-15T20:52:24.827049Z","iopub.execute_input":"2025-04-15T20:52:24.827351Z","iopub.status.idle":"2025-04-15T20:52:24.847919Z","shell.execute_reply.started":"2025-04-15T20:52:24.827326Z","shell.execute_reply":"2025-04-15T20:52:24.843424Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Define input shape\ninput_shape = (1, 3,224, 224)\n# Calculate FLOPs\nflops = get_flops(model, input_shape)\nmacs = flops // 2  # MACs is half the FLOPs for Conv2D","metadata":{}},{"cell_type":"code","source":"print(\"flops: \",flops)\nprint(\"macs: \", macs)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Results(model,test_dataset)","metadata":{"execution":{"iopub.status.busy":"2024-10-18T10:46:21.517112Z","iopub.execute_input":"2024-10-18T10:46:21.517503Z","iopub.status.idle":"2024-10-18T10:46:39.023796Z","shell.execute_reply.started":"2024-10-18T10:46:21.517475Z","shell.execute_reply":"2024-10-18T10:46:39.022734Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Fine-tuning TF","metadata":{}},{"cell_type":"code","source":"\n# from transformers import TFViTModel\n# from transformers import TFDeiTModel\n# from transformers import TFSwinModel\n\n# <!-- # Create an input layer -->\ninput_layer = tf.keras.layers.Input(shape=(3,224, 224), name='input_layer')\n\n# <!-- # Data Augmentation -->\n# data_augmentation = tf.keras.Sequential([\n#     tf.keras.layers.RandomFlip(mode=\"horizontal\", seed=42),\n#     tf.keras.layers.RandomRotation(factor=0.05, seed=42),\n#     tf.keras.layers.RandomContrast(factor=0.2, seed=42)\n# ], name=\"data_augmentation\")(input_layer)\n\n# <!-- # Load the ViT model -->\n# base_model = TFDeiTModel.from_pretrained(\"facebook/deit-base-distilled-patch16-224\")\n# base_model = TFViTModel.from_pretrained(\"google/vit-base-patch16-224-in21k\")\n# base_model = TFSwinModel.from_pretrained(\"microsoft/swin-tiny-patch4-window7-224\")\n\n\noutputs = base_model.deit(input_layer).last_hidden_state\n\n\n\n# <!-- # Apply global average pooling to get a single feature vector -->\navg = tf.keras.layers.GlobalAveragePooling1D()(outputs)\n\n# <!-- # Add a convolutional layer before fully connected layers -->\nx = tf.keras.layers.Reshape((1, 1, -1))(avg)\nx = tf.keras.layers.Conv2D(256, (1, 1), activation='relu')(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Conv2D(128, (1, 1), activation='relu')(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Conv2D(64, (1, 1), activation='relu')(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.GlobalAveragePooling2D()(x)\n\n# <!-- # Fully connected layers -->\nx = tf.keras.layers.Dense(512, activation=\"relu\")(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Dropout(0.5)(x)\nx = tf.keras.layers.Dense(256, activation=\"relu\")(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Dropout(0.5)(x)\nx = tf.keras.layers.Dense(128, activation=\"relu\")(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Dropout(0.5)(x)\n\n# <!-- # Add a dense layer with a sigmoid activation for binary classification -->\noutput = tf.keras.layers.Dense(1, activation=\"sigmoid\")(x)\n\n# <!-- # Create the model -->\nmodel = tf.keras.Model(inputs=input_layer, outputs=output)\n\n# <!-- # Freeze the ViT model layers -->\nfor layer in base_model.layers:\n    layer.trainable = False\n\n","metadata":{"execution":{"iopub.status.busy":"2025-04-16T20:58:28.967513Z","iopub.execute_input":"2025-04-16T20:58:28.967891Z","iopub.status.idle":"2025-04-16T20:58:36.331214Z","shell.execute_reply.started":"2025-04-16T20:58:28.967857Z","shell.execute_reply":"2025-04-16T20:58:36.326074Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# model = tf.keras.models.load_model(\"/kaggle/working/dfdc_vit.h5\"\nmodel.load_weights(\"/kaggle/input/deepfake_models/keras/deepfake_models/8/dfdc_New_models/dfdc_Deit_ft_weights.h5\")\n# model.save(\"celebs_vit_ft.h5\")\n# model_name = \"dfdc_swin\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T20:58:52.281376Z","iopub.execute_input":"2025-04-16T20:58:52.281701Z","iopub.status.idle":"2025-04-16T20:58:52.568735Z","shell.execute_reply.started":"2025-04-16T20:58:52.281674Z","shell.execute_reply":"2025-04-16T20:58:52.565489Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Run profiling\nprofile = profile_model_avg_per_image(model, test_dataset)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T20:58:55.817013Z","iopub.execute_input":"2025-04-16T20:58:55.817386Z","iopub.status.idle":"2025-04-16T21:01:31.336614Z","shell.execute_reply.started":"2025-04-16T20:58:55.817353Z","shell.execute_reply":"2025-04-16T21:01:31.328928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"max(profile['cpu_used'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-15T19:48:24.515769Z","iopub.execute_input":"2025-04-15T19:48:24.516054Z","iopub.status.idle":"2025-04-15T19:48:24.533499Z","shell.execute_reply.started":"2025-04-15T19:48:24.516026Z","shell.execute_reply":"2025-04-15T19:48:24.527482Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# df = pd.read_csv(\"/kaggle/working/MemoryProfile.csv\")\ndf[\"Deit_ft\"] = profile['cpu_used']\ndf.to_csv('MemoryProfile.csv', index=False)  \ndf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T21:02:09.785929Z","iopub.execute_input":"2025-04-16T21:02:09.786250Z","iopub.status.idle":"2025-04-16T21:02:09.822798Z","shell.execute_reply.started":"2025-04-16T21:02:09.786221Z","shell.execute_reply":"2025-04-16T21:02:09.817627Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"max(df.Deit_ft)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-16T22:45:24.811987Z","iopub.execute_input":"2025-04-16T22:45:24.812388Z","iopub.status.idle":"2025-04-16T22:45:24.828650Z","shell.execute_reply.started":"2025-04-16T22:45:24.812334Z","shell.execute_reply":"2025-04-16T22:45:24.822919Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = tf.keras.optimizers.legacy.Adam(learning_rate=0.0001)\nmodel.compile(loss=\"binary_crossentropy\", optimizer=optimizer, metrics=[\"accuracy\"])\n","metadata":{"execution":{"iopub.status.busy":"2024-10-18T10:47:54.176918Z","iopub.execute_input":"2024-10-18T10:47:54.177598Z","iopub.status.idle":"2024-10-18T10:47:54.195196Z","shell.execute_reply.started":"2024-10-18T10:47:54.177566Z","shell.execute_reply":"2024-10-18T10:47:54.194523Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint, CSVLogger\n\n\ncsv_logger = CSVLogger(model_name+'_ft.csv',append = False)\n\ncallbacks = [csv_logger]","metadata":{"execution":{"iopub.status.busy":"2024-10-18T10:47:56.080725Z","iopub.execute_input":"2024-10-18T10:47:56.081404Z","iopub.status.idle":"2024-10-18T10:47:56.084942Z","shell.execute_reply.started":"2024-10-18T10:47:56.081372Z","shell.execute_reply":"2024-10-18T10:47:56.084294Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(train_dataset, validation_data=valid_dataset,callbacks = callbacks, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-10-18T10:47:59.807919Z","iopub.execute_input":"2024-10-18T10:47:59.808394Z","iopub.status.idle":"2024-10-18T10:52:22.670181Z","shell.execute_reply.started":"2024-10-18T10:47:59.808357Z","shell.execute_reply":"2024-10-18T10:52:22.669048Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save_weights(model_name + \"_ft_weights.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-10-18T10:52:22.672446Z","iopub.execute_input":"2024-10-18T10:52:22.672871Z","iopub.status.idle":"2024-10-18T10:52:22.819397Z","shell.execute_reply.started":"2024-10-18T10:52:22.672836Z","shell.execute_reply":"2024-10-18T10:52:22.818518Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Results(model,test_dataset)","metadata":{"execution":{"iopub.status.busy":"2024-10-18T10:52:22.820638Z","iopub.execute_input":"2024-10-18T10:52:22.820997Z","iopub.status.idle":"2024-10-18T10:52:40.583082Z","shell.execute_reply.started":"2024-10-18T10:52:22.820960Z","shell.execute_reply":"2024-10-18T10:52:40.582035Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# model.load_weights(\"/kaggle/input/deepfake_models/keras/deepfake_models/7/dfdc_Swin_ft_weights (1).h5\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define input shape\ninput_shape = (1, 3,224, 224)\n# Calculate FLOPs\nflops = get_flops(model, input_shape)\nmacs = flops // 2  # MACs is half the FLOPs for Conv2D\n\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\"dfdc_vit_ft.h5\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = tf.keras.models.load_model(\"/kaggle/working/celebs_deit_ft.h5\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_name = model_name+\"_ft\"\n# model.load_weights(\"/kaggle/working/dfdc_vit_weights.h5\")\nmodel_name","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\nimport matplotlib.pyplot as plt\n\n# Ensure X_val_preprocessed has the correct shape if necessary\n# X_val_preprocessed = np.transpose(X_val_preprocessed, (0, 3, 1, 2))  # If needed\n\n# Generate predictions\ny_pred = model.predict(X_test_preprocessed)\ny_pred_classes = np.array(list(map(lambda x: 0 if x <= 0.5 else 1, y_pred)))\n\n# Ensure y_val has the correct shape\ny_true = y_test\n\n# Compute confusion matrix\ncm = confusion_matrix(y_true, y_pred_classes)\n\n# Plot confusion matrix \ndisp = ConfusionMatrixDisplay(confusion_matrix=cm)\ndisp.plot(cmap=plt.cm.Blues)\nplt.title(f'Confusion Matrix of {model_name}')\nfile_name = f'{model_name}_Confusion_graph.png'\nplt.savefig(file_name, dpi=1000, bbox_inches='tight')\nprint(f'Plot saved as {file_name}')\nplt.show()\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_curve, auc\n\n# Assuming binary classification\nfpr, tpr, thresholds = roc_curve(y_true, y_pred_classes)\nroc_auc = auc(fpr, tpr)\n\nplt.figure()\nplt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)\nplt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')\nplt.xlim([0.0, 1.0])\nplt.ylim([0.0, 1.05])\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.title(f'ROC of {model_name}')\nplt.legend(loc=\"lower right\")\n\nfile_name = f'{model_name}_ROC_graph.png'\nplt.savefig(file_name, dpi=1000, bbox_inches='tight')\nprint(f'Plot saved as {file_name}')\n    \nplt.show()\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"flops : \",flops)\nprint(\"macs : \",macs)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nmodel = tf.keras.models.load_model(\"/kaggle/input/deepfake_models/keras/deepfake_models/6/dfdc_deit_ft.h5\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Results","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score, average_precision_score, confusion_matrix, cohen_kappa_score\nimport tensorflow as tf\n\ndef Results(model, test_set):\n    # Evaluate the model\n    results = model.evaluate(test_set)\n    print(\"Evaluation results: \", results)\n\n    y_true = []\n    y_pred_probs = []\n\n    print(\"Loading....\", end=\"\", flush=True)\n\n    for images, labels in test_set:\n        y_true.extend(labels.numpy())\n        y_pred_probs.extend(model.predict(images, verbose=0).flatten())\n\n    y_true = tf.convert_to_tensor(y_true)\n    y_pred_probs = tf.convert_to_tensor(y_pred_probs)\n    y_pred = (y_pred_probs > 0.5).numpy().astype(\"int32\")\n\n    acc = accuracy_score(y_true, y_pred)\n    \n    precision = precision_score(y_true, y_pred, zero_division=1)\n    recall = recall_score(y_true, y_pred)\n    f1 = f1_score(y_true, y_pred)\n    roc_auc = roc_auc_score(y_true, y_pred_probs)\n    prc_auc = average_precision_score(y_true, y_pred_probs)\n    conf_matrix = confusion_matrix(y_true, y_pred)\n    kappa = cohen_kappa_score(y_true, y_pred)\n    tn, fp, fn, tp = conf_matrix.ravel()\n    npv = tn / (tn + fn)\n\n    print(f\"Accuracy: {acc}\")\n    print(f\"Precision: {precision}\")\n    print(f\"Recall: {recall}\")\n    print(f\"F1 Score: {f1}\")\n    print(f\"ROC AUC: {roc_auc}\")\n    print(f\"PRC AUC: {prc_auc}\")\n    print(f\"Confusion Matrix: \\n{conf_matrix}\")\n    print(f\"Kappa Coefficient: {kappa}\")\n    print(f\"NPV: {npv}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-10-18T10:32:08.015188Z","iopub.execute_input":"2024-10-18T10:32:08.015875Z","iopub.status.idle":"2024-10-18T10:32:08.024185Z","shell.execute_reply.started":"2024-10-18T10:32:08.015843Z","shell.execute_reply":"2024-10-18T10:32:08.023489Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# history_data","metadata":{}},{"cell_type":"code","source":"\n\ndef history_data(history, file_name ):\n    epoch_data = {\n        'epoch': list(range(len(history.history['accuracy']) )),\n        'accuracy': history.history['accuracy'],\n        'loss': history.history['loss'],\n        'val_accuracy': history.history['val_accuracy'],\n        'val_loss': history.history['val_loss']\n    }\n\n    # Create DataFrame\n    df = pd.DataFrame(epoch_data)\n\n    # Save DataFrame to CSV\n    \n    df.to_csv(file_name, index=False)\n\n    # Return DataFrame if needed\n    return df\n\n\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Graph","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pandas as pd\n\ndef plot_combined_history(csv_path1, csv_path2, save=False):\n    model1_name= csv_path1.split(sep='/')[-1][5:-4]\n    model2_name=csv_path2.split(sep='/')[-1][5:-4]\n    # Load the history from CSV files\n    model1_history = pd.read_csv(csv_path1)\n    model2_history = pd.read_csv(csv_path2)\n    \n    # Plot accuracy\n    plt.figure(figsize=(16, 8))\n    \n    # Accuracy plot\n    plt.subplot(1, 2, 1)\n    plt.plot(model1_history['epoch']+1, model1_history['accuracy'],'b--', label=f'{model1_name} Train Accuracy')\n    plt.plot(model1_history['epoch']+1, model1_history['val_accuracy'],'r-', label=f'{model1_name} Validation Accuracy')\n    plt.plot(model2_history['epoch']+1, model2_history['accuracy'],'c--', label=f'{model2_name} Train Accuracy')\n    plt.plot(model2_history['epoch']+1, model2_history['val_accuracy'],'g-', label=f'{model2_name} Validation Accuracy')\n    plt.title(f'{model1_name} vs {model2_name} Accuracy')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.legend(loc='upper left', bbox_to_anchor=(0.5, -0.1), ncol=1)  # Vertical columns for legend\n\n    # Loss plot\n    plt.subplot(1, 2, 2)\n    plt.plot(model1_history['epoch']+1, model1_history['loss'], 'b--',label=f'{model1_name} Train Loss')\n    plt.plot(model1_history['epoch']+1, model1_history['val_loss'], 'r-', label=f'{model1_name} Validation Loss')\n    plt.plot(model2_history['epoch']+1, model2_history['loss'],'c--', label=f'{model2_name} Train Loss')\n    plt.plot(model2_history['epoch']+1, model2_history['val_loss'],'g-', label=f'{model2_name} Validation Loss')\n    plt.title(f'{model1_name} vs {model2_name} Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.legend(loc='upper left', bbox_to_anchor=(0.5, -0.1), ncol=1)  # Vertical columns for legend\n\n    # Adjust layout to make room for the legend\n    plt.tight_layout()\n\n    # Save the figure if save is True\n    if save:\n        file_name = f'{model1_name}_vs_{model2_name}.png'\n        plt.savefig(file_name, dpi=300, bbox_inches='tight')\n        print(f'Plot saved as {file_name}')\n    \n    # Show the plot\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-10-18T10:53:26.749184Z","iopub.execute_input":"2024-10-18T10:53:26.749812Z","iopub.status.idle":"2024-10-18T10:53:26.761074Z","shell.execute_reply.started":"2024-10-18T10:53:26.749781Z","shell.execute_reply":"2024-10-18T10:53:26.760247Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.load_weights(\"/kaggle/working/FF_Deit_weights.h5\")\n\n# model = tf.keras.models.load_model(\"/kaggle/input/deepfake_models/keras/deepfake_models/8/dfdc_New_models/dfdc_ResNet50_ft.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-10-18T11:07:05.964818Z","iopub.execute_input":"2024-10-18T11:07:05.965532Z","iopub.status.idle":"2024-10-18T11:07:06.045509Z","shell.execute_reply.started":"2024-10-18T11:07:05.965501Z","shell.execute_reply":"2024-10-18T11:07:06.044837Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_name = 'ff_Deit'\nmodel_name","metadata":{"execution":{"iopub.status.busy":"2024-10-18T11:07:23.103491Z","iopub.execute_input":"2024-10-18T11:07:23.104317Z","iopub.status.idle":"2024-10-18T11:07:23.108427Z","shell.execute_reply.started":"2024-10-18T11:07:23.104285Z","shell.execute_reply":"2024-10-18T11:07:23.107722Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_name = model_name+\"_ft\"\nmodel_name","metadata":{"execution":{"iopub.status.busy":"2024-10-18T10:53:41.254862Z","iopub.execute_input":"2024-10-18T10:53:41.255271Z","iopub.status.idle":"2024-10-18T10:53:41.260390Z","shell.execute_reply.started":"2024-10-18T10:53:41.255240Z","shell.execute_reply":"2024-10-18T10:53:41.259609Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"CNN","metadata":{}},{"cell_type":"code","source":"# # #CNN Networks\n# y_true = []\n# y_pred_probs = []\n# X_test = []\n\n# for images, labels in test_set:\n#     X_test.extend(images.numpy())\n#     y_true.extend(labels.numpy())\n#     y_pred_probs.extend(model.predict(images, verbose=0).flatten())\n\n# y_true = tf.convert_to_tensor(y_true).numpy().astype(\"int32\")\n# y_pred_probs = tf.convert_to_tensor(y_pred_probs)\n# y_pred = (y_pred_probs > 0.5).numpy().astype(\"int32\")\n# print('Done')","metadata":{"execution":{"iopub.status.busy":"2024-10-16T08:11:33.706237Z","iopub.execute_input":"2024-10-16T08:11:33.707096Z","iopub.status.idle":"2024-10-16T08:11:34.151663Z","shell.execute_reply.started":"2024-10-16T08:11:33.707043Z","shell.execute_reply":"2024-10-16T08:11:34.150703Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Transformer","metadata":{}},{"cell_type":"code","source":"#Transformers\ny_true = []\ny_pred_probs = []\nX_test = []\n\n# Assuming X_test_preprocessed is a DataLoader or a dataset object with batches\nfor images, labels in test_dataset:\n    X_test.extend(images.numpy())  # Collect test images\n    y_true.extend(labels.numpy())  # Collect true labels\n    \n    y_pred_probs.extend(model.predict(images, verbose=0).flatten())  # Predict probabilities\n\n\n# Convert to tensors and cast to desired types\ny_true = tf.convert_to_tensor(y_true).numpy().astype(\"int32\") \ny_pred_probs = tf.convert_to_tensor(y_pred_probs)\ny_pred = (y_pred_probs > 0.5).numpy().astype(\"int32\")  # Convert probabilities to binary predictions\n\nprint(model_name+' :Done')\n","metadata":{"execution":{"iopub.status.busy":"2024-10-18T11:07:29.478454Z","iopub.execute_input":"2024-10-18T11:07:29.478814Z","iopub.status.idle":"2024-10-18T11:07:40.822684Z","shell.execute_reply.started":"2024-10-18T11:07:29.478784Z","shell.execute_reply":"2024-10-18T11:07:40.821797Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.metrics import roc_curve, precision_recall_curve, roc_auc_score, average_precision_score\n\n# Example values for y_true and y_pred_probs (replace these with your actual data)\n# y_true = ...\n# y_pred_probs = ...\n# y_pred = ...\n\n# Calculate metrics\nfpr, tpr, _ = roc_curve(y_true, y_pred_probs)\nprecision, recall, _ = precision_recall_curve(y_true, y_pred_probs)\nroc_auc = roc_auc_score(y_true, y_pred_probs)\navg_precision = average_precision_score(y_true, y_pred_probs)\n\n# Find the maximum length to pad the arrays\nmax_len = max(len(fpr), len(tpr), len(precision), len(recall))\n\n# Pad the arrays with NaN where necessary to match the maximum length\nFPR = np.pad(fpr, (0, max_len - len(fpr)), constant_values=np.nan)\nTPR = np.pad(tpr, (0, max_len - len(tpr)), constant_values=np.nan)\nPrecision = np.pad(precision, (0, max_len - len(precision)), constant_values=np.nan)\nRecall = np.pad(recall, (0, max_len - len(recall)), constant_values=np.nan)\n\n# Create a DataFrame with the metrics\nmetrics_df = pd.DataFrame({\n    'FPR': FPR,\n    'TPR': TPR,\n    'Precision': Precision,\n    'Recall': Recall\n})\n\nsave = True\n\n# Save to CSV if save is True\nif save:\n    file_name = f'{model_name}_metrics.csv'\n    metrics_df.to_csv(file_name, index=False)\n    print(f'Metrics saved as {file_name}')\nelse:\n    print('Metrics not saved.')\n\n# Display the DataFrame\nmetrics_df\n","metadata":{"execution":{"iopub.status.busy":"2024-10-18T11:07:40.824268Z","iopub.execute_input":"2024-10-18T11:07:40.824642Z","iopub.status.idle":"2024-10-18T11:07:40.853235Z","shell.execute_reply.started":"2024-10-18T11:07:40.824610Z","shell.execute_reply":"2024-10-18T11:07:40.852563Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"(fpr.shape,tpr.shape),(precision.shape,recall.shape)","metadata":{"execution":{"iopub.status.busy":"2024-10-18T11:07:40.854470Z","iopub.execute_input":"2024-10-18T11:07:40.854721Z","iopub.status.idle":"2024-10-18T11:07:40.859689Z","shell.execute_reply.started":"2024-10-18T11:07:40.854696Z","shell.execute_reply":"2024-10-18T11:07:40.859003Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_curve, auc\nimport matplotlib.pyplot as plt\n# Assuming binary classification\n# fpr, tpr, threshold = roc_curve(y_true, y_pred_probs)\nfpr, tpr = metrics_df['FPR'], metrics_df['TPR']\n# roc_auc = metrics_df['ROC_AUC'][0]\n\nplt.figure()\nplt.plot(fpr, tpr, color='darkorange', lw=2)\nplt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')\nplt.xlim([0.0, 1.0])\nplt.ylim([0.0, 1.05])\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.title(f'ROC of {model_name}')\nplt.legend(loc=\"lower right\")\n\nfile_name = f'{model_name}_ROC_graph.png'\nplt.savefig(file_name, dpi=1000, bbox_inches='tight')\nprint(f'Plot saved as {file_name}')\n    \nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-10-18T08:52:48.435136Z","iopub.execute_input":"2024-10-18T08:52:48.435454Z","iopub.status.idle":"2024-10-18T08:52:50.279127Z","shell.execute_reply.started":"2024-10-18T08:52:48.435427Z","shell.execute_reply":"2024-10-18T08:52:50.278336Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xception = pd.read_csv(\"/kaggle/working/ff_Xception_metrics.csv\")\nxception_ft = pd.read_csv(\"/kaggle/working/ff_Xception_ft_metrics.csv\")\ndensenet = pd.read_csv(\"/kaggle/working/ff_DenseNet121_metrics.csv\")\ndensenet_ft = pd.read_csv(\"/kaggle/working/ff_DenseNet121_ft_metrics.csv\")\ninception = pd.read_csv(\"/kaggle/working/ff_InceptionV3_metrics.csv\")\ninception_ft = pd.read_csv(\"/kaggle/working/ff_InceptionV3_ft_metrics.csv\")\nresnet = pd.read_csv(\"/kaggle/working/ff_ResNet50_metrics.csv\")\nresnet_ft = pd.read_csv(\"/kaggle/working/ff_ResNet50_ft_metrics.csv\")\nconvnext = pd.read_csv(\"/kaggle/working/ff_ConvNeXt_metrics.csv\")\nconvnext_ft = pd.read_csv(\"/kaggle/working/ff_ConvNeXt_ft_metrics.csv\")\nvit = pd.read_csv(\"/kaggle/working/FF_Vit_metrics.csv\")\nvit_ft = pd.read_csv(\"/kaggle/working/FF_Vit_ft_metrics.csv\")\nswin = pd.read_csv(\"/kaggle/working/FF_Swin_metrics.csv\")\nswin_ft = pd.read_csv(\"/kaggle/working/FF_Swin_ft_metrics.csv\")\ndeit = pd.read_csv(\"/kaggle/working/FF_Deit_ft_metrics.csv\")\ndeit_ft = pd.read_csv(\"/kaggle/working/FF_Deit_ft_metrics.csv\")\nxception","metadata":{"execution":{"iopub.status.busy":"2024-10-19T09:25:33.118567Z","iopub.execute_input":"2024-10-19T09:25:33.119248Z","iopub.status.idle":"2024-10-19T09:25:33.179362Z","shell.execute_reply.started":"2024-10-19T09:25:33.119202Z","shell.execute_reply":"2024-10-19T09:25:33.178524Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_curve, auc\nimport matplotlib.pyplot as plt\n\n# Define a dictionary for the colors to ensure same model gets the same color\nmodel_colors = {\n    'xception': 'blue',\n    'densenet': 'green',\n    'inception': 'red',\n    'resnet': 'purple',\n    'convnext': 'orange',\n    'vit': 'cyan',\n    'swin': 'magenta',\n    'deit': 'brown'\n}\n\n# Start plotting\nplt.figure()\n\n# Xception\nplt.plot(xception['FPR'], xception['TPR'], color=model_colors['xception'], lw=2, linestyle='--', label='Xception')\nplt.plot(xception_ft['FPR'], xception_ft['TPR'], color=model_colors['xception'], lw=2, linestyle='-', label='Xception_ft')\n\n# # Densenet\nplt.plot(densenet['FPR'], densenet['TPR'], color=model_colors['densenet'], lw=2, linestyle='--', label='Densenet')\nplt.plot(densenet_ft['FPR'], densenet_ft['TPR'], color=model_colors['densenet'], lw=2, linestyle='-', label='Densenet_ft')\n\n# # Inception\nplt.plot(inception['FPR'], inception['TPR'], color=model_colors['inception'], lw=2, linestyle='--', label='Inception')\nplt.plot(inception_ft['FPR'], inception_ft['TPR'], color=model_colors['inception'], lw=2, linestyle='-', label='Inception_ft')\n\n# Resnet\nplt.plot(resnet['FPR'], resnet['TPR'], color=model_colors['resnet'], lw=2, linestyle='--', label='Resnet')\nplt.plot(resnet_ft['FPR'], resnet_ft['TPR'], color=model_colors['resnet'], lw=2, linestyle='-', label='Resnet_ft')\n\n# ConvNext\nplt.plot(convnext['FPR'], convnext['TPR'], color=model_colors['convnext'], lw=2, linestyle='--', label='ConvNext')\nplt.plot(convnext_ft['FPR'], convnext_ft['TPR'], color=model_colors['convnext'], lw=2, linestyle='-', label='ConvNext_ft')\n\n# ViT\nplt.plot(vit['FPR'], vit['TPR'], color=model_colors['vit'], lw=2, linestyle='--', label='ViT')\nplt.plot(vit_ft['FPR'], vit_ft['TPR'], color=model_colors['vit'], lw=2, linestyle='-', label='ViT_ft')\n\n# Swin\nplt.plot(swin['FPR'], swin['TPR'], color=model_colors['swin'], lw=2, linestyle='--', label='Swin')\nplt.plot(swin_ft['FPR'], swin_ft['TPR'], color=model_colors['swin'], lw=2, linestyle='-', label='Swin_ft')\n\n# DeiT\nplt.plot(deit['FPR'], deit['TPR'], color=model_colors['deit'], lw=2, linestyle='--', label='DeiT')\nplt.plot(deit_ft['FPR'], deit_ft['TPR'], color=model_colors['deit'], lw=2, linestyle='-', label='DeiT_ft')\n\n# Add diagonal line for random classifier\nplt.plot([0, 1], [0, 1], color='gray', lw=2, linestyle='--')\n\n# Set plot limits, labels, and title\nplt.xlim([0.0, 1.0])\nplt.ylim([0.0, 1.05])\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.title(f'ROC of FF++ models')\nplt.legend(loc=\"upper left\", ncol=2,  bbox_to_anchor=[0.7, 0.4])\n\n# Save the plot\nfile_name = f'FF_Models_ROC_graph.png'\nplt.savefig(file_name, dpi=1000, bbox_inches='tight')\nprint(f'Plot saved as {file_name}')\n\n# Show plot\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-10-19T09:25:36.765972Z","iopub.execute_input":"2024-10-19T09:25:36.767108Z","iopub.status.idle":"2024-10-19T09:25:40.352953Z","shell.execute_reply.started":"2024-10-19T09:25:36.767061Z","shell.execute_reply":"2024-10-19T09:25:40.352055Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import precision_recall_curve, average_precision_score\n\n# Assuming binary classification\n# precision, recall =  metrics_df['Precision'], metrics_df['Recall']\n# precision, recall, _ = precision_recall_curve(y_true, y_pred_probs)\n# average_precision = metrics_df['Average_Precision'][0]\n# average_precision = average_precision_score(y_true, y_pred_probs)\nplt.figure()\n\n# Xception\nplt.plot(xception['Recall'], xception['Precision'], color=model_colors['xception'], lw=2, linestyle='--', label='Xception')\nplt.plot(xception_ft['Recall'], xception_ft['Precision'], color=model_colors['xception'], lw=2, linestyle='-', label='Xception_ft')\n\n# Densenet\nplt.plot(densenet['Recall'], densenet['Precision'], color=model_colors['densenet'], lw=2, linestyle='--', label='Densenet')\nplt.plot(densenet_ft['Recall'], densenet_ft['Precision'], color=model_colors['densenet'], lw=2, linestyle='-', label='Densenet_ft')\n\n# Inception\nplt.plot(inception['Recall'], inception['Precision'], color=model_colors['inception'], lw=2, linestyle='--', label='Inception')\nplt.plot(inception_ft['Recall'], inception_ft['Precision'], color=model_colors['inception'], lw=2, linestyle='-', label='Inception_ft')\n\n# Resnet\nplt.plot(resnet['Recall'], resnet['Precision'], color=model_colors['resnet'], lw=2, linestyle='--', label='Resnet')\nplt.plot(resnet_ft['Recall'], resnet_ft['Precision'], color=model_colors['resnet'], lw=2, linestyle='-', label='Resnet_ft')\n\n# ConvNext\nplt.plot(convnext['Recall'], convnext['Precision'], color=model_colors['convnext'], lw=2, linestyle='--', label='ConvNext')\nplt.plot(convnext_ft['Recall'], convnext_ft['Precision'], color=model_colors['convnext'], lw=2, linestyle='-', label='ConvNext_ft')\n\n# ViT\nplt.plot(vit['Recall'], vit['Precision'], color=model_colors['vit'], lw=2, linestyle='--', label='ViT')\nplt.plot(vit_ft['Recall'], vit_ft['Precision'], color=model_colors['vit'], lw=2, linestyle='-', label='ViT_ft')\n\n# Swin\nplt.plot(swin['Recall'], swin['Precision'], color=model_colors['swin'], lw=2, linestyle='--', label='Swin')\nplt.plot(swin_ft['Recall'], swin_ft['Precision'], color=model_colors['swin'], lw=2, linestyle='-', label='Swin_ft')\n\n# DeiT\nplt.plot(deit['Recall'], deit['Precision'], color=model_colors['deit'], lw=2, linestyle='--', label='DeiT')\nplt.plot(deit_ft['Recall'], deit_ft['Precision'], color=model_colors['deit'], lw=2, linestyle='-', label='DeiT_ft')\nplt.xlim([0.0, 1.0])\nplt.ylim([0.0, 1.05])\nplt.xlabel('Recall')\nplt.ylabel('Precision')\nplt.title(f'PRC of FF++ Models')\nplt.legend(loc=\"upper left\", ncol=2,  bbox_to_anchor=[0.7, 0.4])\n\nfile_name_pr = f'FF_Models_PRC_graph.png'\nplt.savefig(file_name_pr, dpi=1000, bbox_inches='tight')\nprint(f'Plot saved as {file_name_pr}')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-19T09:26:45.127421Z","iopub.execute_input":"2024-10-19T09:26:45.127790Z","iopub.status.idle":"2024-10-19T09:26:48.855961Z","shell.execute_reply.started":"2024-10-19T09:26:45.127753Z","shell.execute_reply":"2024-10-19T09:26:48.855009Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\nimport matplotlib.pyplot as plt\n\n\n\n# Compute confusion matrix\ncm = confusion_matrix(y_true, y_pred)\n\n# Plot confusion matrix\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm)\ndisp.plot(cmap=plt.cm.Blues)\nplt.title(f'Confusion Matrix of {model_name}')\nfile_name = f'{model_name}_Confusion_graph.png'\nplt.savefig(file_name, dpi=1000, bbox_inches='tight')\nprint(f'Plot saved as {file_name}')\nplt.show()\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Confusion mtrix","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}