{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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},{"sourceId":116207,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":97638,"modelId":121820}],"dockerImageVersionId":30762,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"raw","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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(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"}},{"cell_type":"code","source":"import sys\nimport sklearn\nimport tensorflow as tf\n\nimport cv2\nimport pandas as pd\nimport numpy as np\n\nimport plotly.graph_objs as go\nfrom plotly.offline import iplot\nfrom matplotlib import pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-09-18T13:31:56.236913Z","iopub.execute_input":"2024-09-18T13:31:56.237231Z","iopub.status.idle":"2024-09-18T13:32:09.246127Z","shell.execute_reply.started":"2024-09-18T13:31:56.237198Z","shell.execute_reply":"2024-09-18T13:32:09.245015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.__version__","metadata":{"execution":{"iopub.status.busy":"2024-09-18T13:32:09.247899Z","iopub.execute_input":"2024-09-18T13:32:09.248895Z","iopub.status.idle":"2024-09-18T13:32:09.255925Z","shell.execute_reply.started":"2024-09-18T13:32:09.248855Z","shell.execute_reply":"2024-09-18T13:32:09.254805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\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":"2024-09-18T13:32:09.257342Z","iopub.execute_input":"2024-09-18T13:32:09.257730Z","iopub.status.idle":"2024-09-18T13:32:09.295226Z","shell.execute_reply.started":"2024-09-18T13:32:09.257685Z","shell.execute_reply":"2024-09-18T13:32:09.294356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\ndef get_data():\n    return pd.read_csv('../input/deepfake-faces/metadata.csv')","metadata":{"execution":{"iopub.status.busy":"2024-09-18T13:32:09.297681Z","iopub.execute_input":"2024-09-18T13:32:09.298036Z","iopub.status.idle":"2024-09-18T13:32:09.303395Z","shell.execute_reply.started":"2024-09-18T13:32:09.297989Z","shell.execute_reply":"2024-09-18T13:32:09.302493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta=get_data()\nmeta.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-18T13:32:09.304529Z","iopub.execute_input":"2024-09-18T13:32:09.304896Z","iopub.status.idle":"2024-09-18T13:32:09.514237Z","shell.execute_reply.started":"2024-09-18T13:32:09.304855Z","shell.execute_reply":"2024-09-18T13:32:09.513256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(meta[meta.label=='FAKE']),len(meta[meta.label=='REAL'])","metadata":{"execution":{"iopub.status.busy":"2024-09-18T13:32:09.515777Z","iopub.execute_input":"2024-09-18T13:32:09.516485Z","iopub.status.idle":"2024-09-18T13:32:09.564256Z","shell.execute_reply.started":"2024-09-18T13:32:09.516437Z","shell.execute_reply":"2024-09-18T13:32:09.563347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"real_df = meta[meta[\"label\"] == \"REAL\"]\nfake_df = meta[meta[\"label\"] == \"FAKE\"]\nsample_size = 42000\nfake_df = fake_df.sample(sample_size, random_state=42)\nsample_meta = pd.concat([real_df, fake_df])","metadata":{"execution":{"iopub.status.busy":"2024-09-18T13:32:09.565871Z","iopub.execute_input":"2024-09-18T13:32:09.566552Z","iopub.status.idle":"2024-09-18T13:32:09.622295Z","shell.execute_reply.started":"2024-09-18T13:32:09.566508Z","shell.execute_reply":"2024-09-18T13:32:09.621526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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.15,random_state=42,stratify=Train_set['label'])","metadata":{"execution":{"iopub.status.busy":"2024-09-18T13:32:09.623567Z","iopub.execute_input":"2024-09-18T13:32:09.624272Z","iopub.status.idle":"2024-09-18T13:32:09.877537Z","shell.execute_reply.started":"2024-09-18T13:32:09.624225Z","shell.execute_reply":"2024-09-18T13:32:09.876506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train_set.shape,Val_set.shape,Test_set.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-18T13:32:09.878977Z","iopub.execute_input":"2024-09-18T13:32:09.879328Z","iopub.status.idle":"2024-09-18T13:32:09.885960Z","shell.execute_reply.started":"2024-09-18T13:32:09.879275Z","shell.execute_reply":"2024-09-18T13:32:09.884969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-09-18T13:32:09.889543Z","iopub.execute_input":"2024-09-18T13:32:09.889877Z","iopub.status.idle":"2024-09-18T13:32:09.896432Z","shell.execute_reply.started":"2024-09-18T13:32:09.889834Z","shell.execute_reply":"2024-09-18T13:32:09.895618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-09-18T13:33:54.979883Z","iopub.execute_input":"2024-09-18T13:33:54.980933Z","iopub.status.idle":"2024-09-18T13:44:06.928643Z","shell.execute_reply.started":"2024-09-18T13:33:54.980880Z","shell.execute_reply":"2024-09-18T13:44:06.927790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-09-18T13:44:06.930111Z","iopub.execute_input":"2024-09-18T13:44:06.930435Z","iopub.status.idle":"2024-09-18T13:44:27.459824Z","shell.execute_reply.started":"2024-09-18T13:44:06.930403Z","shell.execute_reply":"2024-09-18T13:44:27.458808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.backend.clear_session()  # extra code – resets layer name counter\n\nbatch_size_per_replica = 32\nbatch_size = batch_size_per_replica\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":{"execution":{"iopub.status.busy":"2024-09-18T13:49:33.647310Z","iopub.execute_input":"2024-09-18T13:49:33.648028Z","iopub.status.idle":"2024-09-18T13:49:35.759101Z","shell.execute_reply.started":"2024-09-18T13:49:33.647991Z","shell.execute_reply":"2024-09-18T13:49:35.758274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-09-18T13:50:07.978829Z","iopub.execute_input":"2024-09-18T13:50:07.979229Z","iopub.status.idle":"2024-09-18T13:50:08.009387Z","shell.execute_reply.started":"2024-09-18T13:50:07.979185Z","shell.execute_reply":"2024-09-18T13:50:08.008492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 valid_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":{"execution":{"iopub.status.busy":"2024-09-18T13:50:10.178986Z","iopub.execute_input":"2024-09-18T13:50:10.179346Z","iopub.status.idle":"2024-09-18T13:50:13.112950Z","shell.execute_reply.started":"2024-09-18T13:50:10.179313Z","shell.execute_reply":"2024-09-18T13:50:13.112080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-09-18T13:50:22.259700Z","iopub.execute_input":"2024-09-18T13:50:22.260114Z","iopub.status.idle":"2024-09-18T13:50:26.151496Z","shell.execute_reply.started":"2024-09-18T13:50:22.260076Z","shell.execute_reply":"2024-09-18T13:50:26.150686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\n\nmodel = load_model(\"/kaggle/input/xceptionv3/tensorflow2/default/1/xception_deepfake_image_3o.h5\")\n","metadata":{"execution":{"iopub.status.busy":"2024-09-18T13:51:17.611863Z","iopub.execute_input":"2024-09-18T13:51:17.612244Z","iopub.status.idle":"2024-09-18T13:51:20.052633Z","shell.execute_reply.started":"2024-09-18T13:51:17.612210Z","shell.execute_reply":"2024-09-18T13:51:20.051649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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(train_set, validation_data=valid_set, epochs=2)","metadata":{"execution":{"iopub.status.busy":"2024-09-18T13:52:16.407550Z","iopub.execute_input":"2024-09-18T13:52:16.408444Z","iopub.status.idle":"2024-09-18T14:05:46.088659Z","shell.execute_reply.started":"2024-09-18T13:52:16.408398Z","shell.execute_reply":"2024-09-18T14:05:46.087812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-09-18T14:09:57.846340Z","iopub.execute_input":"2024-09-18T14:09:57.846707Z","iopub.status.idle":"2024-09-18T14:09:58.355092Z","shell.execute_reply.started":"2024-09-18T14:09:57.846674Z","shell.execute_reply":"2024-09-18T14:09:58.354010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('xception_deepfake_image_4o.h5')","metadata":{"execution":{"iopub.status.busy":"2024-09-18T14:11:07.639528Z","iopub.execute_input":"2024-09-18T14:11:07.639908Z","iopub.status.idle":"2024-09-18T14:11:08.205982Z","shell.execute_reply.started":"2024-09-18T14:11:07.639873Z","shell.execute_reply":"2024-09-18T14:11:08.204967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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.8)\nmodel.compile(loss=\"binary_crossentropy\", optimizer=optimizer,\n              metrics=[\"accuracy\"])\nhistory = model.fit(train_set, validation_data=valid_set, epochs=3)","metadata":{"execution":{"iopub.status.busy":"2024-09-18T14:32:16.457846Z","iopub.execute_input":"2024-09-18T14:32:16.458232Z","iopub.status.idle":"2024-09-18T14:51:33.974414Z","shell.execute_reply.started":"2024-09-18T14:32:16.458200Z","shell.execute_reply":"2024-09-18T14:51:33.973592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(test_set)","metadata":{"execution":{"iopub.status.busy":"2024-09-18T14:51:33.975953Z","iopub.execute_input":"2024-09-18T14:51:33.976296Z","iopub.status.idle":"2024-09-18T14:52:57.141649Z","shell.execute_reply.started":"2024-09-18T14:51:33.976261Z","shell.execute_reply":"2024-09-18T14:52:57.140632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('xception_deepfake_image_5o.h5')","metadata":{"execution":{"iopub.status.busy":"2024-09-18T14:58:10.118655Z","iopub.execute_input":"2024-09-18T14:58:10.119637Z","iopub.status.idle":"2024-09-18T14:58:10.587061Z","shell.execute_reply.started":"2024-09-18T14:58:10.119594Z","shell.execute_reply":"2024-09-18T14:58:10.586261Z"},"trusted":true},"execution_count":null,"outputs":[]}]}