{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"data_path = \"/kaggle/input/deepfake-detection-challenge/train_sample_videos\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-10T17:19:40.378010Z","iopub.execute_input":"2023-05-10T17:19:40.378520Z","iopub.status.idle":"2023-05-10T17:19:40.384806Z","shell.execute_reply.started":"2023-05-10T17:19:40.378477Z","shell.execute_reply":"2023-05-10T17:19:40.383499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndata_df = pd.read_json(\"/kaggle/input/deepfake-detection-challenge/train_sample_videos/metadata.json\")\ndata_df = data_df.T","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:19:40.387450Z","iopub.execute_input":"2023-05-10T17:19:40.388307Z","iopub.status.idle":"2023-05-10T17:19:40.523056Z","shell.execute_reply.started":"2023-05-10T17:19:40.388273Z","shell.execute_reply":"2023-05-10T17:19:40.521993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_df","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:19:40.524773Z","iopub.execute_input":"2023-05-10T17:19:40.525200Z","iopub.status.idle":"2023-05-10T17:19:40.538539Z","shell.execute_reply.started":"2023-05-10T17:19:40.525161Z","shell.execute_reply":"2023-05-10T17:19:40.537407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_real = data_df[data_df['label'] == 'REAL']\ndf_real","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:19:40.541800Z","iopub.execute_input":"2023-05-10T17:19:40.542510Z","iopub.status.idle":"2023-05-10T17:19:40.570874Z","shell.execute_reply.started":"2023-05-10T17:19:40.542471Z","shell.execute_reply":"2023-05-10T17:19:40.569487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_fake = data_df[data_df['label'] == 'FAKE']\ndf_fake","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:19:40.572744Z","iopub.execute_input":"2023-05-10T17:19:40.573564Z","iopub.status.idle":"2023-05-10T17:19:40.592508Z","shell.execute_reply.started":"2023-05-10T17:19:40.573483Z","shell.execute_reply":"2023-05-10T17:19:40.591240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.concat([df_real.sample(n=30),df_fake.sample(n=30)])","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:19:40.594513Z","iopub.execute_input":"2023-05-10T17:19:40.594918Z","iopub.status.idle":"2023-05-10T17:19:40.602126Z","shell.execute_reply.started":"2023-05-10T17:19:40.594881Z","shell.execute_reply":"2023-05-10T17:19:40.600986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:19:40.604291Z","iopub.execute_input":"2023-05-10T17:19:40.605226Z","iopub.status.idle":"2023-05-10T17:19:40.628383Z","shell.execute_reply.started":"2023-05-10T17:19:40.605183Z","shell.execute_reply":"2023-05-10T17:19:40.627085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"from tensorflow import keras\nmodel1 = keras.models.load_model('/kaggle/input/models/ncs/densenet.h5')","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:19:40.630668Z","iopub.execute_input":"2023-05-10T17:19:40.631584Z","iopub.status.idle":"2023-05-10T17:19:46.559410Z","shell.execute_reply.started":"2023-05-10T17:19:40.631544Z","shell.execute_reply":"2023-05-10T17:19:46.558386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import InceptionResNetV2\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import MaxPooling2D\nfrom tensorflow.keras.layers import Flatten\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.layers import Dropout\nfrom tensorflow.keras.layers import InputLayer\nfrom tensorflow.keras.layers import GlobalAveragePooling2D\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\n\nbase_model2 = InceptionResNetV2(include_top=False, weights='imagenet', input_shape=(128,128,3))\nbase_model2.trainable = True\n\nmodel2 = Sequential()\nmodel2.add(base_model2)\nmodel2.add(GlobalAveragePooling2D())\nmodel2.add(Dense(2, activation='softmax'))\n\nmodel2.summary(expand_nested=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:19:46.560847Z","iopub.execute_input":"2023-05-10T17:19:46.561202Z","iopub.status.idle":"2023-05-10T17:19:54.517700Z","shell.execute_reply.started":"2023-05-10T17:19:46.561169Z","shell.execute_reply":"2023-05-10T17:19:54.516669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.load_weights('/kaggle/input/models/ncs/googlenet.h5')","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:19:54.522901Z","iopub.execute_input":"2023-05-10T17:19:54.523947Z","iopub.status.idle":"2023-05-10T17:19:58.623049Z","shell.execute_reply.started":"2023-05-10T17:19:54.523908Z","shell.execute_reply":"2023-05-10T17:19:58.621983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model3 = keras.models.load_model('/kaggle/input/models/ncs/densenet_sc.h5')","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:19:58.626859Z","iopub.execute_input":"2023-05-10T17:19:58.627272Z","iopub.status.idle":"2023-05-10T17:20:04.762968Z","shell.execute_reply.started":"2023-05-10T17:19:58.627235Z","shell.execute_reply":"2023-05-10T17:20:04.761920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model4 = keras.models.load_model('/kaggle/input/models/ncs/googlenet_sc.h5')","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:20:04.767249Z","iopub.execute_input":"2023-05-10T17:20:04.768021Z","iopub.status.idle":"2023-05-10T17:20:08.545078Z","shell.execute_reply.started":"2023-05-10T17:20:04.767981Z","shell.execute_reply":"2023-05-10T17:20:08.544045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc \ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:20:08.547021Z","iopub.execute_input":"2023-05-10T17:20:08.547390Z","iopub.status.idle":"2023-05-10T17:20:09.077778Z","shell.execute_reply.started":"2023-05-10T17:20:08.547357Z","shell.execute_reply":"2023-05-10T17:20:09.076709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install dlib==19.15.0","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:20:09.079062Z","iopub.execute_input":"2023-05-10T17:20:09.079999Z","iopub.status.idle":"2023-05-10T17:20:21.257382Z","shell.execute_reply.started":"2023-05-10T17:20:09.079964Z","shell.execute_reply":"2023-05-10T17:20:21.255828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:20:21.261415Z","iopub.execute_input":"2023-05-10T17:20:21.261985Z","iopub.status.idle":"2023-05-10T17:20:21.838527Z","shell.execute_reply.started":"2023-05-10T17:20:21.261887Z","shell.execute_reply":"2023-05-10T17:20:21.837454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import dlib\nimport cv2\nfrom tqdm import tqdm\nimport numpy as np\nfrom tensorflow.keras.preprocessing.image import img_to_array\n\ndetector = dlib.get_frontal_face_detector()\nres = []\nfor x in tqdm(df.index):\n    file = df.loc[x]\n    cap = cv2.VideoCapture(\"/kaggle/input/deepfake-detection-challenge/train_sample_videos/\"+x)\n    frameRate = cap.get(5)\n    print(x,file[0])\n    r1 = []\n    r2 = []\n    r3 = []\n    r4 = []\n    while cap.isOpened():\n        frameId = cap.get(1)\n        ret, frame = cap.read()\n        if ret != True:\n            break\n        if frameId % ((int(frameRate)+1)*1) == 0:\n            face_rects, scores, idx = detector.run(frame, 0)\n            for i, d in enumerate(face_rects):\n\n                x1 = d.left()\n                y1 = d.top()\n                x2 = d.right()\n                y2 = d.bottom()\n\n                crop_img = frame[y1:y2, x1:x2]\n                \n\n                data = img_to_array(cv2.resize(crop_img, (128, 128))).flatten() / 255.0\n                data = data.reshape(-1, 128, 128, 3)\n\n                pred1 = model1.predict(data, verbose=0)\n                pred2 = model2.predict(data, verbose=0)\n                pred3 = model3.predict(data, verbose=0)\n                pred4 = model4.predict(data, verbose=0)\n                cl1 = np.argmax(pred1, axis=1)[0]\n                cl2 = np.argmax(pred2, axis=1)[0]\n                cl3 = np.argmax(pred3, axis=1)[0]\n                cl4 = np.argmax(pred4, axis=1)[0]\n                \n                r1.append(cl1)\n                r2.append(cl2)\n                r3.append(cl3)\n                r4.append(cl4)\n    print(r1)\n    print(r2)\n    print(r3)\n    print(r4)\n    row = [x]+[file[0]]\n    if r1!=[]:\n        if 0 in r1:\n            fc = r1.count(0)\n        else:\n            fc = 0\n        if 1 in r1:\n            rc = r1.count(1)\n        else:\n            rc = 0\n        if rc>fc:\n            row+=['REAL']\n            print('REAL',end='\\t')\n        else:\n            row+=['FAKE']\n            print('FAKE',end='\\t')\n    if r2!=[]:\n        if 0 in r2:\n            fc = r2.count(0)\n        else:\n            fc = 0\n        if 1 in r2:\n            rc = r2.count(1)\n        else:\n            rc = 0\n        if rc>fc:\n            row+=['REAL']\n            print('REAL',end='\\t')\n        else:\n            row+=['FAKE']\n            print('FAKE',end='\\t')\n    if r3!=[]:\n        if 0 in r3:\n            fc = r3.count(0)\n        else:\n            fc = 0\n        if 1 in r3:\n            rc = r3.count(1)\n        else:\n            rc = 0\n        if rc>fc:\n            row+=['REAL']\n            print('REAL')\n        else:\n            row+=['FAKE']\n            print('FAKE')\n    if r4!=[]:\n        if 0 in r4:\n            fc = r4.count(0)\n        else:\n            fc = 0\n        if 1 in r4:\n            rc = r4.count(1)\n        else:\n            rc = 0\n        if rc>fc:\n            row+=['REAL']\n            print('REAL',end='\\t')\n        else:\n            row+=['FAKE']\n            print('FAKE',end='\\t')\n            \n    res.append(row)\n    gc.collect()\n    print(\"--------------------------------------\\n\")","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:20:21.840196Z","iopub.execute_input":"2023-05-10T17:20:21.840627Z","iopub.status.idle":"2023-05-10T17:30:41.003891Z","shell.execute_reply.started":"2023-05-10T17:20:21.840580Z","shell.execute_reply":"2023-05-10T17:30:41.002874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:30:41.005777Z","iopub.execute_input":"2023-05-10T17:30:41.006516Z","iopub.status.idle":"2023-05-10T17:30:41.024557Z","shell.execute_reply.started":"2023-05-10T17:30:41.006481Z","shell.execute_reply":"2023-05-10T17:30:41.023416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_res = pd.DataFrame(res,columns=['file','true','densenet','googlenet','densenet_s','googlenet_s'])\ndf_res","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:30:41.026365Z","iopub.execute_input":"2023-05-10T17:30:41.026754Z","iopub.status.idle":"2023-05-10T17:30:41.063901Z","shell.execute_reply.started":"2023-05-10T17:30:41.026720Z","shell.execute_reply":"2023-05-10T17:30:41.062755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_res = df_res.dropna(subset=['densenet'])\n","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:30:41.065630Z","iopub.execute_input":"2023-05-10T17:30:41.066222Z","iopub.status.idle":"2023-05-10T17:30:41.076712Z","shell.execute_reply.started":"2023-05-10T17:30:41.066126Z","shell.execute_reply":"2023-05-10T17:30:41.075710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_res.to_csv('result.csv', index=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:30:41.078175Z","iopub.execute_input":"2023-05-10T17:30:41.079126Z","iopub.status.idle":"2023-05-10T17:30:41.087307Z","shell.execute_reply.started":"2023-05-10T17:30:41.079079Z","shell.execute_reply":"2023-05-10T17:30:41.086133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_res['true'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:30:41.088914Z","iopub.execute_input":"2023-05-10T17:30:41.089932Z","iopub.status.idle":"2023-05-10T17:30:41.098299Z","shell.execute_reply.started":"2023-05-10T17:30:41.089882Z","shell.execute_reply":"2023-05-10T17:30:41.097368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\n\nprint('DenseNet :',metrics.accuracy_score(df_res['true'].values,df_res['densenet'].values))\nprint('GoogLeNet :',metrics.accuracy_score(df_res['true'].values,df_res['googlenet'].values))\nprint('DenseNet S :',metrics.accuracy_score(df_res['true'].values,df_res['densenet_s'].values))\nprint('GoogLeNet S :',metrics.accuracy_score(df_res['true'].values,df_res['googlenet_s'].values))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:30:41.099663Z","iopub.execute_input":"2023-05-10T17:30:41.100128Z","iopub.status.idle":"2023-05-10T17:30:41.112567Z","shell.execute_reply.started":"2023-05-10T17:30:41.100092Z","shell.execute_reply":"2023-05-10T17:30:41.111490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('DenseNet :',metrics.recall_score(df_res['true'].values,df_res['densenet'].values, pos_label='FAKE'))\nprint('GoogLeNet :',metrics.recall_score(df_res['true'].values,df_res['googlenet'].values, pos_label='FAKE'))\nprint('DenseNet S :',metrics.recall_score(df_res['true'].values,df_res['densenet_s'].values, pos_label='FAKE'))\nprint('GoogLeNet S :',metrics.recall_score(df_res['true'].values,df_res['googlenet_s'].values, pos_label='FAKE'))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:30:41.113802Z","iopub.execute_input":"2023-05-10T17:30:41.114920Z","iopub.status.idle":"2023-05-10T17:30:41.133440Z","shell.execute_reply.started":"2023-05-10T17:30:41.114889Z","shell.execute_reply":"2023-05-10T17:30:41.132351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('DenseNet :',metrics.recall_score(df_res['true'].values,df_res['densenet'].values, pos_label='REAL'))\nprint('GoogLeNet :',metrics.recall_score(df_res['true'].values,df_res['googlenet'].values, pos_label='REAL'))\nprint('DenseNet S :',metrics.recall_score(df_res['true'].values,df_res['densenet_s'].values, pos_label='REAL'))\nprint('GoogLeNet S :',metrics.recall_score(df_res['true'].values,df_res['googlenet_s'].values, pos_label='REAL'))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:34:07.997409Z","iopub.execute_input":"2023-05-10T17:34:07.998159Z","iopub.status.idle":"2023-05-10T17:34:08.017142Z","shell.execute_reply.started":"2023-05-10T17:34:07.998123Z","shell.execute_reply":"2023-05-10T17:34:08.016086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\n\nprint('DenseNet :\\n',metrics.classification_report(df_res['true'].values,df_res['densenet'].values),\"\\n\")\nprint('GoogLeNet :\\n',metrics.classification_report(df_res['true'].values,df_res['googlenet'].values),\"\\n\")\nprint('DenseNet S :\\n',metrics.classification_report(df_res['true'].values,df_res['densenet_s'].values),\"\\n\")\nprint('GoogLeNet S :\\n',metrics.classification_report(df_res['true'].values,df_res['googlenet_s'].values),\"\\n\")","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:30:41.134937Z","iopub.execute_input":"2023-05-10T17:30:41.135491Z","iopub.status.idle":"2023-05-10T17:30:41.180378Z","shell.execute_reply.started":"2023-05-10T17:30:41.135456Z","shell.execute_reply":"2023-05-10T17:30:41.179219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt \n\ncm = metrics.confusion_matrix(df_res['true'].values, df_res['densenet'].values)\n\ncm_display = metrics.ConfusionMatrixDisplay(confusion_matrix = cm, display_labels = ['FAKE', 'REAL'])\nfig, ax = plt.subplots(figsize=(10,10))\ncm_display.plot(ax=ax)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:30:41.182005Z","iopub.execute_input":"2023-05-10T17:30:41.182571Z","iopub.status.idle":"2023-05-10T17:30:41.472273Z","shell.execute_reply.started":"2023-05-10T17:30:41.182535Z","shell.execute_reply":"2023-05-10T17:30:41.471339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt \n\ncm = metrics.confusion_matrix(df_res['true'].values, df_res['googlenet'].values)\n\ncm_display = metrics.ConfusionMatrixDisplay(confusion_matrix = cm, display_labels = ['FAKE', 'REAL'])\nfig, ax = plt.subplots(figsize=(10,10))\ncm_display.plot(ax=ax)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:30:41.473601Z","iopub.execute_input":"2023-05-10T17:30:41.474559Z","iopub.status.idle":"2023-05-10T17:30:41.776433Z","shell.execute_reply.started":"2023-05-10T17:30:41.474504Z","shell.execute_reply":"2023-05-10T17:30:41.775464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt \n\ncm = metrics.confusion_matrix(df_res['true'].values, df_res['densenet_s'].values)\n\ncm_display = metrics.ConfusionMatrixDisplay(confusion_matrix = cm, display_labels = ['FAKE', 'REAL'])\nfig, ax = plt.subplots(figsize=(10,10))\ncm_display.plot(ax=ax)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:30:41.777848Z","iopub.execute_input":"2023-05-10T17:30:41.778786Z","iopub.status.idle":"2023-05-10T17:30:42.085171Z","shell.execute_reply.started":"2023-05-10T17:30:41.778748Z","shell.execute_reply":"2023-05-10T17:30:42.084180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt \n\ncm = metrics.confusion_matrix(df_res['true'].values, df_res['googlenet_s'].values)\n\ncm_display = metrics.ConfusionMatrixDisplay(confusion_matrix = cm, display_labels = ['FAKE', 'REAL'])\nfig, ax = plt.subplots(figsize=(10,10))\ncm_display.plot(ax=ax)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T17:30:42.090314Z","iopub.execute_input":"2023-05-10T17:30:42.091267Z","iopub.status.idle":"2023-05-10T17:30:42.393310Z","shell.execute_reply.started":"2023-05-10T17:30:42.091231Z","shell.execute_reply":"2023-05-10T17:30:42.392337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}