{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":16880,"databundleVersionId":858837,"sourceType":"competition"},{"sourceId":924245,"sourceType":"datasetVersion","datasetId":464091}],"dockerImageVersionId":29845,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         os.path.join(dirname, filename)\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-11-15T05:15:44.787111Z","iopub.execute_input":"2024-11-15T05:15:44.787410Z","iopub.status.idle":"2024-11-15T05:15:44.792184Z","shell.execute_reply.started":"2024-11-15T05:15:44.787369Z","shell.execute_reply":"2024-11-15T05:15:44.791253Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataset_path = '/kaggle/input/deepfake-faces/metadata.csv'","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:44.794508Z","iopub.execute_input":"2024-11-15T05:15:44.794816Z","iopub.status.idle":"2024-11-15T05:15:44.804106Z","shell.execute_reply.started":"2024-11-15T05:15:44.794760Z","shell.execute_reply":"2024-11-15T05:15:44.803325Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(dataset_path)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:44.805265Z","iopub.execute_input":"2024-11-15T05:15:44.805524Z","iopub.status.idle":"2024-11-15T05:15:44.978516Z","shell.execute_reply.started":"2024-11-15T05:15:44.805483Z","shell.execute_reply":"2024-11-15T05:15:44.977872Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:44.980204Z","iopub.execute_input":"2024-11-15T05:15:44.980550Z","iopub.status.idle":"2024-11-15T05:15:45.004781Z","shell.execute_reply.started":"2024-11-15T05:15:44.980490Z","shell.execute_reply":"2024-11-15T05:15:45.003966Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.tail()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.008844Z","iopub.execute_input":"2024-11-15T05:15:45.009148Z","iopub.status.idle":"2024-11-15T05:15:45.021398Z","shell.execute_reply.started":"2024-11-15T05:15:45.009090Z","shell.execute_reply":"2024-11-15T05:15:45.020326Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.024919Z","iopub.execute_input":"2024-11-15T05:15:45.025304Z","iopub.status.idle":"2024-11-15T05:15:45.031512Z","shell.execute_reply.started":"2024-11-15T05:15:45.025238Z","shell.execute_reply":"2024-11-15T05:15:45.030527Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.033129Z","iopub.execute_input":"2024-11-15T05:15:45.033622Z","iopub.status.idle":"2024-11-15T05:15:45.041555Z","shell.execute_reply.started":"2024-11-15T05:15:45.033411Z","shell.execute_reply":"2024-11-15T05:15:45.040645Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.043094Z","iopub.execute_input":"2024-11-15T05:15:45.043439Z","iopub.status.idle":"2024-11-15T05:15:45.094910Z","shell.execute_reply.started":"2024-11-15T05:15:45.043372Z","shell.execute_reply":"2024-11-15T05:15:45.094207Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.096057Z","iopub.execute_input":"2024-11-15T05:15:45.096309Z","iopub.status.idle":"2024-11-15T05:15:45.132194Z","shell.execute_reply.started":"2024-11-15T05:15:45.096261Z","shell.execute_reply":"2024-11-15T05:15:45.131244Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.134042Z","iopub.execute_input":"2024-11-15T05:15:45.134411Z","iopub.status.idle":"2024-11-15T05:15:45.173274Z","shell.execute_reply.started":"2024-11-15T05:15:45.134340Z","shell.execute_reply":"2024-11-15T05:15:45.172526Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.nunique()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.174634Z","iopub.execute_input":"2024-11-15T05:15:45.174932Z","iopub.status.idle":"2024-11-15T05:15:45.247928Z","shell.execute_reply.started":"2024-11-15T05:15:45.174878Z","shell.execute_reply":"2024-11-15T05:15:45.247290Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"object_columns = df.select_dtypes(include=['object']).columns\nprint(\"Object type columns:\")\nprint(object_columns)\n\nnumerical_columns = df.select_dtypes(include=['int64', 'float64']).columns\nprint(\"\\nNumerical type columns:\")\nprint(numerical_columns)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.249225Z","iopub.execute_input":"2024-11-15T05:15:45.249533Z","iopub.status.idle":"2024-11-15T05:15:45.267728Z","shell.execute_reply.started":"2024-11-15T05:15:45.249473Z","shell.execute_reply":"2024-11-15T05:15:45.266974Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def classify_features(df):\n    categorical_features = []\n    non_categorical_features = []\n    discrete_features = []\n    continuous_features = []\n\n    for column in df.columns:\n        if df[column].dtype == 'object':\n            if df[column].nunique() < 10:\n                categorical_features.append(column)\n            else:\n                non_categorical_features.append(column)\n        elif df[column].dtype in ['int64', 'float64']:\n            if df[column].nunique() < 10:\n                discrete_features.append(column)\n            else:\n                continuous_features.append(column)\n\n    return categorical_features, non_categorical_features, discrete_features, continuous_features","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.268867Z","iopub.execute_input":"2024-11-15T05:15:45.269152Z","iopub.status.idle":"2024-11-15T05:15:45.278734Z","shell.execute_reply.started":"2024-11-15T05:15:45.269096Z","shell.execute_reply":"2024-11-15T05:15:45.277838Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical, non_categorical, discrete, continuous = classify_features(df)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.280120Z","iopub.execute_input":"2024-11-15T05:15:45.280460Z","iopub.status.idle":"2024-11-15T05:15:45.325875Z","shell.execute_reply.started":"2024-11-15T05:15:45.280375Z","shell.execute_reply":"2024-11-15T05:15:45.325251Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Categorical Features:\", categorical)\nprint(\"Non-Categorical Features:\", non_categorical)\nprint(\"Discrete Features:\", discrete)\nprint(\"Continuous Features:\", continuous)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.326975Z","iopub.execute_input":"2024-11-15T05:15:45.327244Z","iopub.status.idle":"2024-11-15T05:15:45.332238Z","shell.execute_reply.started":"2024-11-15T05:15:45.327197Z","shell.execute_reply":"2024-11-15T05:15:45.331449Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.fillna(\"Not Available\")","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.333668Z","iopub.execute_input":"2024-11-15T05:15:45.333939Z","iopub.status.idle":"2024-11-15T05:15:45.375770Z","shell.execute_reply.started":"2024-11-15T05:15:45.333894Z","shell.execute_reply":"2024-11-15T05:15:45.375093Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in categorical:\n    print(i,':', df[i].unique())\n    print()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.376907Z","iopub.execute_input":"2024-11-15T05:15:45.377161Z","iopub.status.idle":"2024-11-15T05:15:45.385857Z","shell.execute_reply.started":"2024-11-15T05:15:45.377120Z","shell.execute_reply":"2024-11-15T05:15:45.385173Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in categorical:\n    print(df[i].value_counts())\n    print()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.387073Z","iopub.execute_input":"2024-11-15T05:15:45.387334Z","iopub.status.idle":"2024-11-15T05:15:45.414674Z","shell.execute_reply.started":"2024-11-15T05:15:45.387288Z","shell.execute_reply":"2024-11-15T05:15:45.414022Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:45.416158Z","iopub.execute_input":"2024-11-15T05:15:45.416435Z","iopub.status.idle":"2024-11-15T05:15:47.024699Z","shell.execute_reply.started":"2024-11-15T05:15:45.416389Z","shell.execute_reply":"2024-11-15T05:15:47.024052Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:47.025851Z","iopub.execute_input":"2024-11-15T05:15:47.026087Z","iopub.status.idle":"2024-11-15T05:15:47.029649Z","shell.execute_reply.started":"2024-11-15T05:15:47.026049Z","shell.execute_reply":"2024-11-15T05:15:47.028854Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in categorical:\n    plt.figure(figsize=(15,6))\n    sns.countplot(x = df[i], data = df, palette = 'hls')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:47.031085Z","iopub.execute_input":"2024-11-15T05:15:47.031518Z","iopub.status.idle":"2024-11-15T05:15:47.421841Z","shell.execute_reply.started":"2024-11-15T05:15:47.031304Z","shell.execute_reply":"2024-11-15T05:15:47.420529Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in categorical:\n    plt.figure(figsize=(30,20)) \n    plt.pie(df[i].value_counts(), labels=df[i].value_counts().index, \n            autopct='%1.1f%%', textprops={ 'fontsize': 20,\n                                           'color': 'black',\n                                           'weight': 'bold',\n                                           'family': 'serif' }) \n    hfont = {'fontname':'serif', 'weight': 'bold'}\n    plt.title(i, size=20, **hfont) \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:15:47.423832Z","iopub.execute_input":"2024-11-15T05:15:47.424395Z","iopub.status.idle":"2024-11-15T05:15:47.946516Z","shell.execute_reply.started":"2024-11-15T05:15:47.424183Z","shell.execute_reply":"2024-11-15T05:15:47.945730Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in numerical_columns:\n    plt.figure(figsize=(15,6))\n    plt.hist(df[i], bins = 20, color = 'skyblue')\n    plt.xticks(rotation = 90)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:20:21.552439Z","iopub.execute_input":"2024-11-15T05:20:21.552745Z","iopub.status.idle":"2024-11-15T05:20:22.207085Z","shell.execute_reply.started":"2024-11-15T05:20:21.552689Z","shell.execute_reply":"2024-11-15T05:20:22.205729Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in numerical_columns:\n    plt.figure(figsize=(15,6))\n    sns.distplot(df[i], kde = True, bins = 20)\n    plt.xticks(rotation = 90)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:18:04.447577Z","iopub.execute_input":"2024-11-15T05:18:04.447940Z","iopub.status.idle":"2024-11-15T05:18:05.029013Z","shell.execute_reply.started":"2024-11-15T05:18:04.447877Z","shell.execute_reply":"2024-11-15T05:18:05.028084Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in numerical_columns:\n    plt.figure(figsize=(15,6))\n    sns.boxplot(x = df[i],data = df, palette = 'hls')\n    plt.xticks(rotation = 90)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:20:28.955385Z","iopub.execute_input":"2024-11-15T05:20:28.955694Z","iopub.status.idle":"2024-11-15T05:20:29.596398Z","shell.execute_reply.started":"2024-11-15T05:20:28.955651Z","shell.execute_reply":"2024-11-15T05:20:29.595064Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in numerical_columns:\n    plt.figure(figsize=(15,6))\n    sns.violinplot(x = df[i],data = df, palette = 'hls')\n    plt.xticks(rotation = 90)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:20:31.596511Z","iopub.execute_input":"2024-11-15T05:20:31.596806Z","iopub.status.idle":"2024-11-15T05:20:32.793767Z","shell.execute_reply.started":"2024-11-15T05:20:31.596763Z","shell.execute_reply":"2024-11-15T05:20:32.792584Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in categorical:\n    for j in numerical_columns:\n        plt.figure(figsize=(15,6))\n        sns.barplot(x = df[i], y = df[j], data = df, ci = None, palette = 'hls')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:20:36.340879Z","iopub.execute_input":"2024-11-15T05:20:36.341197Z","iopub.status.idle":"2024-11-15T05:20:37.034056Z","shell.execute_reply.started":"2024-11-15T05:20:36.341147Z","shell.execute_reply":"2024-11-15T05:20:37.032986Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in categorical:\n    for j in numerical_columns:\n        plt.figure(figsize=(15,6))\n        sns.boxplot(x = df[i], y = df[j], data = df, palette = 'hls')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:20:42.786878Z","iopub.execute_input":"2024-11-15T05:20:42.787187Z","iopub.status.idle":"2024-11-15T05:20:43.513148Z","shell.execute_reply.started":"2024-11-15T05:20:42.787137Z","shell.execute_reply":"2024-11-15T05:20:43.511902Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in categorical:\n    for j in numerical_columns:\n        plt.figure(figsize=(15,6))\n        sns.violinplot(x = df[i], y = df[j], data = df, palette = 'hls')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:20:44.150143Z","iopub.execute_input":"2024-11-15T05:20:44.150432Z","iopub.status.idle":"2024-11-15T05:20:45.626645Z","shell.execute_reply.started":"2024-11-15T05:20:44.150390Z","shell.execute_reply":"2024-11-15T05:20:45.625463Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in numerical_columns:\n    for j in numerical_columns:\n        if i != j:\n            plt.figure(figsize=(15,6))\n            sns.scatterplot(x = df[j], y = df[i], data = df, palette = 'hls')\n            plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:20:49.676930Z","iopub.execute_input":"2024-11-15T05:20:49.677242Z","iopub.status.idle":"2024-11-15T05:20:52.836827Z","shell.execute_reply.started":"2024-11-15T05:20:49.677196Z","shell.execute_reply":"2024-11-15T05:20:52.835379Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"real_df = df[df[\"label\"] == \"REAL\"]\nfake_df = df[df[\"label\"] == \"FAKE\"]\nsample_size = 10000\n\nreal_df = real_df.sample(sample_size, random_state=42)\nfake_df = fake_df.sample(sample_size, random_state=42)\n\nsample_meta = pd.concat([real_df, fake_df])","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:20:52.839440Z","iopub.execute_input":"2024-11-15T05:20:52.840009Z","iopub.status.idle":"2024-11-15T05:20:52.910666Z","shell.execute_reply.started":"2024-11-15T05:20:52.839775Z","shell.execute_reply":"2024-11-15T05:20:52.910085Z"},"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":"2024-11-15T05:20:54.902665Z","iopub.execute_input":"2024-11-15T05:20:54.902946Z","iopub.status.idle":"2024-11-15T05:20:55.257768Z","shell.execute_reply.started":"2024-11-15T05:20:54.902904Z","shell.execute_reply":"2024-11-15T05:20:55.257005Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Train_set.shape,Val_set.shape,Test_set.shape","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:21:01.302904Z","iopub.execute_input":"2024-11-15T05:21:01.303234Z","iopub.status.idle":"2024-11-15T05:21:01.308955Z","shell.execute_reply.started":"2024-11-15T05:21:01.303185Z","shell.execute_reply":"2024-11-15T05:21:01.308200Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:21:02.842754Z","iopub.execute_input":"2024-11-15T05:21:02.843058Z","iopub.status.idle":"2024-11-15T05:21:03.016957Z","shell.execute_reply.started":"2024-11-15T05:21:02.843008Z","shell.execute_reply":"2024-11-15T05:21:03.016357Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_path = '/kaggle/input/deepfake-faces/faces_224/'\n\nimage_files = os.listdir(image_path)\n\nimage_files.sort()\n\nselected_images = image_files[:9]\n\nplt.figure(figsize=(10, 10))\n\nfor index, image_file in enumerate(selected_images):\n    image = cv2.imread(os.path.join(image_path, image_file))\n\n    plt.subplot(3, 3, index + 1)\n    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n    plt.title(f'Image {index + 1}')\n    plt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:21:04.988758Z","iopub.execute_input":"2024-11-15T05:21:04.989083Z","iopub.status.idle":"2024-11-15T05:21:07.172208Z","shell.execute_reply.started":"2024-11-15T05:21:04.989024Z","shell.execute_reply":"2024-11-15T05:21:07.171463Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i, image_file in enumerate(image_files[:10]):\n    image = cv2.imread(os.path.join(image_path, image_file))\n    if image is not None:\n        height, width, _ = image.shape\n        print(f\"Resolution of image {i+1}: {width} x {height}\")\n    else:\n        print(f\"Error reading image {i+1}\")\n\nif len(image_files) < 10:\n    print(f\"Only {len(image_files)} images found in the directory.\")","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:21:10.413150Z","iopub.execute_input":"2024-11-15T05:21:10.413460Z","iopub.status.idle":"2024-11-15T05:21:10.445726Z","shell.execute_reply.started":"2024-11-15T05:21:10.413413Z","shell.execute_reply":"2024-11-15T05:21:10.444882Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(15,15))\nfor cur,i in enumerate(Train_set.index[25:50]):\n    plt.subplot(5,5,cur+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    \n    plt.imshow(cv2.imread('../input/deepfake-faces/faces_224/'+Train_set.loc[i,'videoname'][:-4]+'.jpg'))\n    \n    if(Train_set.loc[i,'label']=='FAKE'):\n        plt.xlabel('FAKE Image')\n    else:\n        plt.xlabel('REAL Image')\n        \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:21:12.040051Z","iopub.execute_input":"2024-11-15T05:21:12.040346Z","iopub.status.idle":"2024-11-15T05:21:13.827521Z","shell.execute_reply.started":"2024-11-15T05:21:12.040303Z","shell.execute_reply":"2024-11-15T05:21:13.826767Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def retreive_dataset(set_name):\n    images,labels=[],[]\n    for (img, imclass) in zip(set_name['videoname'], set_name['label']):\n        images.append(cv2.imread('../input/deepfake-faces/faces_224/'+img[:-4]+'.jpg'))\n        if(imclass=='FAKE'):\n            labels.append(1)\n        else:\n            labels.append(0)\n    \n    return np.array(images),np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:21:32.691389Z","iopub.execute_input":"2024-11-15T05:21:32.691689Z","iopub.status.idle":"2024-11-15T05:21:32.698659Z","shell.execute_reply.started":"2024-11-15T05:21:32.691645Z","shell.execute_reply":"2024-11-15T05:21:32.697817Z"},"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":"2024-11-15T05:21:34.680235Z","iopub.execute_input":"2024-11-15T05:21:34.680538Z","iopub.status.idle":"2024-11-15T05:24:28.596676Z","shell.execute_reply.started":"2024-11-15T05:21:34.680495Z","shell.execute_reply":"2024-11-15T05:24:28.595598Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom functools import partial","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:24:28.598676Z","iopub.execute_input":"2024-11-15T05:24:28.598975Z","iopub.status.idle":"2024-11-15T05:24:32.473520Z","shell.execute_reply.started":"2024-11-15T05:24:28.598923Z","shell.execute_reply":"2024-11-15T05:24:32.472668Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tf.random.set_seed(42)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:24:32.474802Z","iopub.execute_input":"2024-11-15T05:24:32.475071Z","iopub.status.idle":"2024-11-15T05:24:32.481414Z","shell.execute_reply.started":"2024-11-15T05:24:32.475021Z","shell.execute_reply":"2024-11-15T05:24:32.480469Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DefaultConv2D = partial(layers.Conv2D, kernel_size=3, padding=\"same\",\n                        activation=\"relu\", kernel_initializer=\"he_normal\")\n\n# Model Definition\nmodel = models.Sequential([\n    DefaultConv2D(filters=64, kernel_size=7, input_shape=[224, 224, 3]),\n    layers.MaxPooling2D(),\n    layers.BatchNormalization(),\n    DefaultConv2D(filters=128),\n    DefaultConv2D(filters=128),\n    layers.MaxPooling2D(),\n    layers.BatchNormalization(),\n    layers.Flatten(),\n    layers.Dense(units=128, activation=\"relu\",\n                 kernel_initializer=\"he_normal\"),\n    layers.BatchNormalization(),\n    layers.Dropout(0.5),\n    layers.Dense(units=64, activation=\"relu\",\n                 kernel_initializer=\"he_normal\"),\n    layers.BatchNormalization(),\n    layers.Dropout(0.5),\n    layers.Dense(units=1, activation=\"sigmoid\")\n])","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:24:32.482844Z","iopub.execute_input":"2024-11-15T05:24:32.483103Z","iopub.status.idle":"2024-11-15T05:24:36.247758Z","shell.execute_reply.started":"2024-11-15T05:24:32.483060Z","shell.execute_reply":"2024-11-15T05:24:36.247104Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"initial_learning_rate = 0.001\nlr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate, decay_steps=100000, decay_rate=0.96, staircase=True\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:24:36.250819Z","iopub.execute_input":"2024-11-15T05:24:36.251063Z","iopub.status.idle":"2024-11-15T05:24:36.255401Z","shell.execute_reply.started":"2024-11-15T05:24:36.251022Z","shell.execute_reply":"2024-11-15T05:24:36.254647Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=lr_schedule),\n              loss=\"binary_crossentropy\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:24:36.258308Z","iopub.execute_input":"2024-11-15T05:24:36.258604Z","iopub.status.idle":"2024-11-15T05:24:36.306483Z","shell.execute_reply.started":"2024-11-15T05:24:36.258558Z","shell.execute_reply":"2024-11-15T05:24:36.305882Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:24:36.307945Z","iopub.execute_input":"2024-11-15T05:24:36.308277Z","iopub.status.idle":"2024-11-15T05:24:36.317362Z","shell.execute_reply.started":"2024-11-15T05:24:36.308220Z","shell.execute_reply":"2024-11-15T05:24:36.316665Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    X_train, y_train,\n    epochs=10,  # Adjust as needed\n    batch_size=32,  # Adjust as needed\n    validation_data=(X_val, y_val),\n    verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:24:36.319643Z","iopub.execute_input":"2024-11-15T05:24:36.319949Z","iopub.status.idle":"2024-11-15T05:35:24.942203Z","shell.execute_reply.started":"2024-11-15T05:24:36.319898Z","shell.execute_reply":"2024-11-15T05:35:24.941191Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:24.944106Z","iopub.execute_input":"2024-11-15T05:35:24.944428Z","iopub.status.idle":"2024-11-15T05:35:31.586309Z","shell.execute_reply.started":"2024-11-15T05:35:24.944370Z","shell.execute_reply":"2024-11-15T05:35:31.585547Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, confusion_matrix, classification_report, roc_auc_score","metadata":{"execution":{"iopub.status.busy":"2024-03-30T03:21:32.271636Z","iopub.status.idle":"2024-03-30T03:21:32.272102Z","shell.execute_reply.started":"2024-03-30T03:21:32.271849Z","shell.execute_reply":"2024-03-30T03:21:32.27187Z"}}},{"cell_type":"code","source":"y_test_pred_binary = (y_pred > 0.5).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:31.587644Z","iopub.execute_input":"2024-11-15T05:35:31.587875Z","iopub.status.idle":"2024-11-15T05:35:31.592055Z","shell.execute_reply.started":"2024-11-15T05:35:31.587835Z","shell.execute_reply":"2024-11-15T05:35:31.591115Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_pred = model.predict(X_train)\ny_train_pred_binary = (y_train_pred > 0.5).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:31.593537Z","iopub.execute_input":"2024-11-15T05:35:31.593915Z","iopub.status.idle":"2024-11-15T05:35:49.805008Z","shell.execute_reply.started":"2024-11-15T05:35:31.593813Z","shell.execute_reply":"2024-11-15T05:35:49.804017Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, confusion_matrix, classification_report, roc_auc_score\n\ntrain_accuracy = accuracy_score(y_train, y_train_pred_binary)\nprint(f\"Training Accuracy: {train_accuracy * 100:.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:49.806800Z","iopub.execute_input":"2024-11-15T05:35:49.807155Z","iopub.status.idle":"2024-11-15T05:35:49.814445Z","shell.execute_reply.started":"2024-11-15T05:35:49.807078Z","shell.execute_reply":"2024-11-15T05:35:49.813732Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_accuracy = accuracy_score(y_test, y_test_pred_binary)\nprint(f\"Test Accuracy: {test_accuracy * 100:.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:49.815680Z","iopub.execute_input":"2024-11-15T05:35:49.815899Z","iopub.status.idle":"2024-11-15T05:35:49.825467Z","shell.execute_reply.started":"2024-11-15T05:35:49.815861Z","shell.execute_reply":"2024-11-15T05:35:49.824724Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"f1 = f1_score(y_test, y_test_pred_binary)\nprint(f\"F1 Score: {f1:.4f}\")\n\nprecision = precision_score(y_test, y_test_pred_binary)\nprint(f\"Precison: {precision:.4f}\")\n\nrecall = recall_score(y_test, y_test_pred_binary)\nprint(f\"Recall: {recall:.4f}\")\n\n# Calculate AUC-ROC\nauc_roc = roc_auc_score(y_test, y_test_pred_binary)\nprint(f\"AUC-ROC: {auc_roc:.4f}\")","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:49.826778Z","iopub.execute_input":"2024-11-15T05:35:49.827064Z","iopub.status.idle":"2024-11-15T05:35:49.845326Z","shell.execute_reply.started":"2024-11-15T05:35:49.827011Z","shell.execute_reply":"2024-11-15T05:35:49.844678Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conf_matrix = confusion_matrix(y_test, y_test_pred_binary)\nprint(\"Confusion Matrix:\")\nprint(conf_matrix)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:49.846767Z","iopub.execute_input":"2024-11-15T05:35:49.847004Z","iopub.status.idle":"2024-11-15T05:35:49.858678Z","shell.execute_reply.started":"2024-11-15T05:35:49.846949Z","shell.execute_reply":"2024-11-15T05:35:49.858033Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import scikitplot as skplt","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:49.859736Z","iopub.execute_input":"2024-11-15T05:35:49.859945Z","iopub.status.idle":"2024-11-15T05:35:50.107470Z","shell.execute_reply.started":"2024-11-15T05:35:49.859910Z","shell.execute_reply":"2024-11-15T05:35:50.106611Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"skplt.metrics.plot_confusion_matrix(y_test, y_test_pred_binary, normalize=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:50.108903Z","iopub.execute_input":"2024-11-15T05:35:50.109246Z","iopub.status.idle":"2024-11-15T05:35:50.404364Z","shell.execute_reply.started":"2024-11-15T05:35:50.109182Z","shell.execute_reply":"2024-11-15T05:35:50.403156Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_report = classification_report(y_test, y_test_pred_binary)\nprint(\"Classification Report:\")\nprint(class_report)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:50.406172Z","iopub.execute_input":"2024-11-15T05:35:50.406564Z","iopub.status.idle":"2024-11-15T05:35:50.431302Z","shell.execute_reply.started":"2024-11-15T05:35:50.406493Z","shell.execute_reply":"2024-11-15T05:35:50.430017Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:50.433219Z","iopub.execute_input":"2024-11-15T05:35:50.433765Z","iopub.status.idle":"2024-11-15T05:35:51.012278Z","shell.execute_reply.started":"2024-11-15T05:35:50.433551Z","shell.execute_reply":"2024-11-15T05:35:51.010992Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:51.014208Z","iopub.execute_input":"2024-11-15T05:35:51.014589Z","iopub.status.idle":"2024-11-15T05:35:51.376918Z","shell.execute_reply.started":"2024-11-15T05:35:51.014528Z","shell.execute_reply":"2024-11-15T05:35:51.375580Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:51.379062Z","iopub.execute_input":"2024-11-15T05:35:51.379621Z","iopub.status.idle":"2024-11-15T05:35:51.391485Z","shell.execute_reply.started":"2024-11-15T05:35:51.379403Z","shell.execute_reply":"2024-11-15T05:35:51.390091Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"input_shape = (224, 224, 3)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:51.393696Z","iopub.execute_input":"2024-11-15T05:35:51.394259Z","iopub.status.idle":"2024-11-15T05:35:51.400208Z","shell.execute_reply.started":"2024-11-15T05:35:51.394041Z","shell.execute_reply":"2024-11-15T05:35:51.398881Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model = ResNet50(weights='imagenet', include_top=False, input_shape=input_shape)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:51.402291Z","iopub.execute_input":"2024-11-15T05:35:51.402899Z","iopub.status.idle":"2024-11-15T05:35:56.927117Z","shell.execute_reply.started":"2024-11-15T05:35:51.402655Z","shell.execute_reply":"2024-11-15T05:35:56.926318Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for layer in base_model.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:56.928396Z","iopub.execute_input":"2024-11-15T05:35:56.928672Z","iopub.status.idle":"2024-11-15T05:35:56.947467Z","shell.execute_reply.started":"2024-11-15T05:35:56.928618Z","shell.execute_reply":"2024-11-15T05:35:56.946736Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_resnet50 = models.Sequential()\nmodel_resnet50.add(base_model)\nmodel_resnet50.add(layers.GlobalAveragePooling2D())\nmodel_resnet50.add(layers.Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:56.948997Z","iopub.execute_input":"2024-11-15T05:35:56.949442Z","iopub.status.idle":"2024-11-15T05:35:58.556600Z","shell.execute_reply.started":"2024-11-15T05:35:56.949243Z","shell.execute_reply":"2024-11-15T05:35:58.555885Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_resnet50.summary()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:58.558050Z","iopub.execute_input":"2024-11-15T05:35:58.558378Z","iopub.status.idle":"2024-11-15T05:35:58.581184Z","shell.execute_reply.started":"2024-11-15T05:35:58.558318Z","shell.execute_reply":"2024-11-15T05:35:58.580221Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras import optimizers","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:58.582520Z","iopub.execute_input":"2024-11-15T05:35:58.582800Z","iopub.status.idle":"2024-11-15T05:35:58.591666Z","shell.execute_reply.started":"2024-11-15T05:35:58.582751Z","shell.execute_reply":"2024-11-15T05:35:58.590734Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_resnet50.compile(optimizer=optimizers.Adam(lr=0.001), loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:58.593063Z","iopub.execute_input":"2024-11-15T05:35:58.593402Z","iopub.status.idle":"2024-11-15T05:35:58.663829Z","shell.execute_reply.started":"2024-11-15T05:35:58.593325Z","shell.execute_reply":"2024-11-15T05:35:58.663223Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model_resnet50.fit(\n    X_train, y_train,\n    epochs=10,  \n    validation_data=(X_val, y_val),\n    verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:35:58.665263Z","iopub.execute_input":"2024-11-15T05:35:58.665616Z","iopub.status.idle":"2024-11-15T05:44:56.805922Z","shell.execute_reply.started":"2024-11-15T05:35:58.665556Z","shell.execute_reply":"2024-11-15T05:44:56.804812Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model_resnet50.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:44:56.807522Z","iopub.execute_input":"2024-11-15T05:44:56.807788Z","iopub.status.idle":"2024-11-15T05:45:09.976126Z","shell.execute_reply.started":"2024-11-15T05:44:56.807733Z","shell.execute_reply":"2024-11-15T05:45:09.975353Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_test_pred_binary = (y_pred > 0.5).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:45:09.977475Z","iopub.execute_input":"2024-11-15T05:45:09.977712Z","iopub.status.idle":"2024-11-15T05:45:09.981189Z","shell.execute_reply.started":"2024-11-15T05:45:09.977673Z","shell.execute_reply":"2024-11-15T05:45:09.980494Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_pred = model_resnet50.predict(X_train)\ny_train_pred_binary = (y_train_pred > 0.5).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:45:09.986589Z","iopub.execute_input":"2024-11-15T05:45:09.986991Z","iopub.status.idle":"2024-11-15T05:45:44.234794Z","shell.execute_reply.started":"2024-11-15T05:45:09.986784Z","shell.execute_reply":"2024-11-15T05:45:44.234131Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_accuracy = accuracy_score(y_train, y_train_pred_binary)\nprint(f\"Training Accuracy: {train_accuracy * 100:.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:45:44.236387Z","iopub.execute_input":"2024-11-15T05:45:44.236632Z","iopub.status.idle":"2024-11-15T05:45:44.242471Z","shell.execute_reply.started":"2024-11-15T05:45:44.236594Z","shell.execute_reply":"2024-11-15T05:45:44.241596Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_accuracy = accuracy_score(y_test, y_test_pred_binary)\nprint(f\"Test Accuracy: {test_accuracy * 100:.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:45:44.243885Z","iopub.execute_input":"2024-11-15T05:45:44.244211Z","iopub.status.idle":"2024-11-15T05:45:44.253944Z","shell.execute_reply.started":"2024-11-15T05:45:44.244153Z","shell.execute_reply":"2024-11-15T05:45:44.253247Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"f1 = f1_score(y_test, y_test_pred_binary)\nprint(f\"F1 Score: {f1:.4f}\")\n\nprecision = precision_score(y_test, y_test_pred_binary)\nprint(f\"Precison: {precision:.4f}\")\n\nrecall = recall_score(y_test, y_test_pred_binary)\nprint(f\"Recall: {recall:.4f}\")","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:45:44.255390Z","iopub.execute_input":"2024-11-15T05:45:44.255712Z","iopub.status.idle":"2024-11-15T05:45:44.270729Z","shell.execute_reply.started":"2024-11-15T05:45:44.255647Z","shell.execute_reply":"2024-11-15T05:45:44.270056Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conf_matrix = confusion_matrix(y_test, y_test_pred_binary)\nprint(\"Confusion Matrix:\")\nprint(conf_matrix)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:45:44.273871Z","iopub.execute_input":"2024-11-15T05:45:44.274128Z","iopub.status.idle":"2024-11-15T05:45:44.286373Z","shell.execute_reply.started":"2024-11-15T05:45:44.274081Z","shell.execute_reply":"2024-11-15T05:45:44.285702Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"skplt.metrics.plot_confusion_matrix(y_test, y_test_pred_binary, normalize=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:45:44.287394Z","iopub.execute_input":"2024-11-15T05:45:44.287603Z","iopub.status.idle":"2024-11-15T05:45:44.665885Z","shell.execute_reply.started":"2024-11-15T05:45:44.287568Z","shell.execute_reply":"2024-11-15T05:45:44.664638Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_report = classification_report(y_test, y_test_pred_binary)\nprint(\"Classification Report:\")\nprint(class_report)","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:45:44.667850Z","iopub.execute_input":"2024-11-15T05:45:44.668276Z","iopub.status.idle":"2024-11-15T05:45:44.693694Z","shell.execute_reply.started":"2024-11-15T05:45:44.668206Z","shell.execute_reply":"2024-11-15T05:45:44.690869Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:45:44.695835Z","iopub.execute_input":"2024-11-15T05:45:44.696892Z","iopub.status.idle":"2024-11-15T05:45:45.009848Z","shell.execute_reply.started":"2024-11-15T05:45:44.696813Z","shell.execute_reply":"2024-11-15T05:45:45.008857Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-15T05:45:45.011750Z","iopub.execute_input":"2024-11-15T05:45:45.012463Z","iopub.status.idle":"2024-11-15T05:45:45.349555Z","shell.execute_reply.started":"2024-11-15T05:45:45.012390Z","shell.execute_reply":"2024-11-15T05:45:45.348528Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history2 = model.fit(\n    X_train, y_train,\n    epochs=10,  # Adjust as needed\n    batch_size=64,  # Adjust as needed\n    validation_data=(X_val, y_val),\n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T05:45:45.351337Z","iopub.execute_input":"2024-11-15T05:45:45.351993Z","iopub.status.idle":"2024-11-15T05:56:16.080319Z","shell.execute_reply.started":"2024-11-15T05:45:45.351912Z","shell.execute_reply":"2024-11-15T05:56:16.079608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T05:56:16.081596Z","iopub.execute_input":"2024-11-15T05:56:16.081820Z","iopub.status.idle":"2024-11-15T05:56:22.662108Z","shell.execute_reply.started":"2024-11-15T05:56:16.081782Z","shell.execute_reply":"2024-11-15T05:56:22.661380Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_test_pred_binary = (y_pred > 0.5).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T05:56:22.663327Z","iopub.execute_input":"2024-11-15T05:56:22.663567Z","iopub.status.idle":"2024-11-15T05:56:22.667393Z","shell.execute_reply.started":"2024-11-15T05:56:22.663527Z","shell.execute_reply":"2024-11-15T05:56:22.666669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_pred = model.predict(X_train)\ny_train_pred_binary = (y_train_pred > 0.5).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T05:56:22.668612Z","iopub.execute_input":"2024-11-15T05:56:22.668940Z","iopub.status.idle":"2024-11-15T05:56:41.029157Z","shell.execute_reply.started":"2024-11-15T05:56:22.668898Z","shell.execute_reply":"2024-11-15T05:56:41.028382Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_accuracy = accuracy_score(y_train, y_train_pred_binary)\nprint(f\"Training Accuracy: {train_accuracy * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T05:56:41.030695Z","iopub.execute_input":"2024-11-15T05:56:41.031040Z","iopub.status.idle":"2024-11-15T05:56:41.037134Z","shell.execute_reply.started":"2024-11-15T05:56:41.030963Z","shell.execute_reply":"2024-11-15T05:56:41.036359Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_accuracy = accuracy_score(y_test, y_test_pred_binary)\nprint(f\"Test Accuracy: {test_accuracy * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T05:56:41.038450Z","iopub.execute_input":"2024-11-15T05:56:41.038742Z","iopub.status.idle":"2024-11-15T05:56:41.050280Z","shell.execute_reply.started":"2024-11-15T05:56:41.038690Z","shell.execute_reply":"2024-11-15T05:56:41.049282Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"f1 = f1_score(y_test, y_test_pred_binary)\nprint(f\"F1 Score: {f1:.4f}\")\n\nprecision = precision_score(y_test, y_test_pred_binary)\nprint(f\"Precison: {precision:.4f}\")\n\nrecall = recall_score(y_test, y_test_pred_binary)\nprint(f\"Recall: {recall:.4f}\")\n\n# Calculate AUC-ROC\nauc_roc = roc_auc_score(y_test, y_test_pred_binary)\nprint(f\"AUC-ROC: {auc_roc:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T05:56:41.051475Z","iopub.execute_input":"2024-11-15T05:56:41.051944Z","iopub.status.idle":"2024-11-15T05:56:41.069169Z","shell.execute_reply.started":"2024-11-15T05:56:41.051884Z","shell.execute_reply":"2024-11-15T05:56:41.067993Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"skplt.metrics.plot_confusion_matrix(y_test, y_test_pred_binary, normalize=True)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T05:56:41.084022Z","iopub.execute_input":"2024-11-15T05:56:41.084293Z","iopub.status.idle":"2024-11-15T05:56:41.508598Z","shell.execute_reply.started":"2024-11-15T05:56:41.084241Z","shell.execute_reply":"2024-11-15T05:56:41.507300Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conf_matrix = confusion_matrix(y_test, y_test_pred_binary)\nprint(\"Confusion Matrix:\")\nprint(conf_matrix)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T06:12:15.151022Z","iopub.execute_input":"2024-11-15T06:12:15.151509Z","iopub.status.idle":"2024-11-15T06:12:15.164215Z","shell.execute_reply.started":"2024-11-15T06:12:15.151432Z","shell.execute_reply":"2024-11-15T06:12:15.163454Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T05:56:41.511526Z","iopub.execute_input":"2024-11-15T05:56:41.511921Z","iopub.status.idle":"2024-11-15T05:56:41.837759Z","shell.execute_reply.started":"2024-11-15T05:56:41.511859Z","shell.execute_reply":"2024-11-15T05:56:41.836357Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T05:56:41.839917Z","iopub.execute_input":"2024-11-15T05:56:41.840526Z","iopub.status.idle":"2024-11-15T05:56:42.046148Z","shell.execute_reply.started":"2024-11-15T05:56:41.840266Z","shell.execute_reply":"2024-11-15T05:56:42.045063Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tf.random.set_seed(42)  # extra code – ensures reproducibility\nbase_model = tf.keras.applications.xception.Xception(weights=\"imagenet\",\n                                                     include_top=False)\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)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T06:16:11.173075Z","iopub.execute_input":"2024-11-15T06:16:11.173428Z","iopub.status.idle":"2024-11-15T06:16:14.878261Z","shell.execute_reply.started":"2024-11-15T06:16:11.173369Z","shell.execute_reply":"2024-11-15T06:16:14.877337Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for layer in base_model.layers:\n    layer.trainable = False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T06:42:16.435160Z","iopub.execute_input":"2024-11-15T06:42:16.435462Z","iopub.status.idle":"2024-11-15T06:42:16.445102Z","shell.execute_reply.started":"2024-11-15T06:42:16.435418Z","shell.execute_reply":"2024-11-15T06:42:16.444275Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = tf.keras.optimizers.SGD(learning_rate=0.1, momentum=0.9)\nmodel.compile(loss=\"binary_crossentropy\", optimizer=optimizer,\n              metrics=[\"accuracy\"])\nhistory = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=10,verbose=2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T06:42:16.923277Z","iopub.execute_input":"2024-11-15T06:42:16.923525Z","iopub.status.idle":"2024-11-15T06:54:54.100856Z","shell.execute_reply.started":"2024-11-15T06:42:16.923484Z","shell.execute_reply":"2024-11-15T06:54:54.099887Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T06:54:54.103015Z","iopub.execute_input":"2024-11-15T06:54:54.103277Z","iopub.status.idle":"2024-11-15T06:55:13.345549Z","shell.execute_reply.started":"2024-11-15T06:54:54.103231Z","shell.execute_reply":"2024-11-15T06:55:13.344170Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_test_pred_binary = (y_pred > 0.5).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T06:55:13.347190Z","iopub.execute_input":"2024-11-15T06:55:13.347597Z","iopub.status.idle":"2024-11-15T06:55:13.354553Z","shell.execute_reply.started":"2024-11-15T06:55:13.347518Z","shell.execute_reply":"2024-11-15T06:55:13.353233Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_pred = model.predict(X_train)\ny_train_pred_binary = (y_train_pred > 0.5).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T06:55:13.355792Z","iopub.execute_input":"2024-11-15T06:55:13.356169Z","iopub.status.idle":"2024-11-15T06:56:05.194903Z","shell.execute_reply.started":"2024-11-15T06:55:13.355999Z","shell.execute_reply":"2024-11-15T06:56:05.194080Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_accuracy = accuracy_score(y_train, y_train_pred_binary)\nprint(f\"Training Accuracy: {train_accuracy * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T06:56:05.197754Z","iopub.execute_input":"2024-11-15T06:56:05.198010Z","iopub.status.idle":"2024-11-15T06:56:05.203638Z","shell.execute_reply.started":"2024-11-15T06:56:05.197952Z","shell.execute_reply":"2024-11-15T06:56:05.202928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_accuracy = accuracy_score(y_test, y_test_pred_binary)\nprint(f\"Test Accuracy: {test_accuracy * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T06:56:05.205165Z","iopub.execute_input":"2024-11-15T06:56:05.205447Z","iopub.status.idle":"2024-11-15T06:56:05.215667Z","shell.execute_reply.started":"2024-11-15T06:56:05.205402Z","shell.execute_reply":"2024-11-15T06:56:05.214956Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"f1 = f1_score(y_test, y_test_pred_binary)\nprint(f\"F1 Score: {f1:.4f}\")\n\nprecision = precision_score(y_test, y_test_pred_binary)\nprint(f\"Precison: {precision:.4f}\")\n\nrecall = recall_score(y_test, y_test_pred_binary)\nprint(f\"Recall: {recall:.4f}\")\n\n# Calculate AUC-ROC\nauc_roc = roc_auc_score(y_test, y_test_pred_binary)\nprint(f\"AUC-ROC: {auc_roc:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T06:56:05.216838Z","iopub.execute_input":"2024-11-15T06:56:05.217133Z","iopub.status.idle":"2024-11-15T06:56:05.234721Z","shell.execute_reply.started":"2024-11-15T06:56:05.217089Z","shell.execute_reply":"2024-11-15T06:56:05.234099Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"skplt.metrics.plot_confusion_matrix(y_test, y_test_pred_binary, normalize=True)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T06:56:05.235821Z","iopub.execute_input":"2024-11-15T06:56:05.236101Z","iopub.status.idle":"2024-11-15T06:56:05.652048Z","shell.execute_reply.started":"2024-11-15T06:56:05.236046Z","shell.execute_reply":"2024-11-15T06:56:05.651303Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conf_matrix = confusion_matrix(y_test, y_test_pred_binary)\nprint(\"Confusion Matrix:\")\nprint(conf_matrix)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T06:56:05.653309Z","iopub.execute_input":"2024-11-15T06:56:05.653541Z","iopub.status.idle":"2024-11-15T06:56:05.665577Z","shell.execute_reply.started":"2024-11-15T06:56:05.653502Z","shell.execute_reply":"2024-11-15T06:56:05.664552Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for layer in base_model.layers[56:]:\n    layer.trainable = True\n\noptimizer = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9)\nmodel.compile(loss=\"binary_crossentropy\", optimizer=optimizer,\n              metrics=[\"accuracy\"])\nhistory = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=10,verbose=2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T07:22:31.661893Z","iopub.execute_input":"2024-11-15T07:22:31.662257Z","iopub.status.idle":"2024-11-15T07:42:19.800118Z","shell.execute_reply.started":"2024-11-15T07:22:31.662206Z","shell.execute_reply":"2024-11-15T07:42:19.799127Z"}},"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,"execution":{"iopub.status.busy":"2024-11-15T07:42:19.802236Z","iopub.execute_input":"2024-11-15T07:42:19.802583Z","iopub.status.idle":"2024-11-15T07:42:20.442483Z","shell.execute_reply.started":"2024-11-15T07:42:19.802518Z","shell.execute_reply":"2024-11-15T07:42:20.441154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T07:42:20.444465Z","iopub.execute_input":"2024-11-15T07:42:20.445053Z","iopub.status.idle":"2024-11-15T07:42:39.605216Z","shell.execute_reply.started":"2024-11-15T07:42:20.444801Z","shell.execute_reply":"2024-11-15T07:42:39.604332Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_test_pred_binary = (y_pred > 0.5).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T07:42:39.606471Z","iopub.execute_input":"2024-11-15T07:42:39.606706Z","iopub.status.idle":"2024-11-15T07:42:39.610806Z","shell.execute_reply.started":"2024-11-15T07:42:39.606667Z","shell.execute_reply":"2024-11-15T07:42:39.609909Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_accuracy = accuracy_score(y_train, y_train_pred_binary)\nprint(f\"Training Accuracy: {train_accuracy * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T08:12:35.128457Z","iopub.execute_input":"2024-11-15T08:12:35.128851Z","iopub.status.idle":"2024-11-15T08:12:35.135468Z","shell.execute_reply.started":"2024-11-15T08:12:35.128801Z","shell.execute_reply":"2024-11-15T08:12:35.134684Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_accuracy = accuracy_score(y_test, y_test_pred_binary)\nprint(f\"Test Accuracy: {test_accuracy * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T08:12:48.974619Z","iopub.execute_input":"2024-11-15T08:12:48.974898Z","iopub.status.idle":"2024-11-15T08:12:48.980428Z","shell.execute_reply.started":"2024-11-15T08:12:48.974855Z","shell.execute_reply":"2024-11-15T08:12:48.979565Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"f1 = f1_score(y_test, y_test_pred_binary)\nprint(f\"F1 Score: {f1:.4f}\")\n\nprecision = precision_score(y_test, y_test_pred_binary)\nprint(f\"Precison: {precision:.4f}\")\n\nrecall = recall_score(y_test, y_test_pred_binary)\nprint(f\"Recall: {recall:.4f}\")\n\n# Calculate AUC-ROC\nauc_roc = roc_auc_score(y_test, y_test_pred_binary)\nprint(f\"AUC-ROC: {auc_roc:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T08:12:59.026141Z","iopub.execute_input":"2024-11-15T08:12:59.026532Z","iopub.status.idle":"2024-11-15T08:12:59.043219Z","shell.execute_reply.started":"2024-11-15T08:12:59.026455Z","shell.execute_reply":"2024-11-15T08:12:59.042462Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"skplt.metrics.plot_confusion_matrix(y_test, y_test_pred_binary, normalize=True)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T08:13:27.980335Z","iopub.execute_input":"2024-11-15T08:13:27.980631Z","iopub.status.idle":"2024-11-15T08:13:28.152599Z","shell.execute_reply.started":"2024-11-15T08:13:27.980586Z","shell.execute_reply":"2024-11-15T08:13:28.151711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}