{"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":"# !pip install --upgrade tensorflow","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install keras","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport keras\nimport os\nimport numpy as np\nfrom sklearn.metrics import log_loss\nfrom keras import Model,Sequential\nfrom keras.layers import *\nfrom keras.optimizers import *\nfrom sklearn.model_selection import train_test_split\nimport cv2\nfrom tqdm.auto import tqdm\nimport glob\nfrom matplotlib import pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-26T08:38:45.522042Z","iopub.execute_input":"2022-05-26T08:38:45.522361Z","iopub.status.idle":"2022-05-26T08:38:51.746163Z","shell.execute_reply.started":"2022-05-26T08:38:45.522309Z","shell.execute_reply":"2022-05-26T08:38:51.745383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(keras.__version__)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:40:01.097532Z","iopub.execute_input":"2022-05-26T08:40:01.097837Z","iopub.status.idle":"2022-05-26T08:40:01.102304Z","shell.execute_reply.started":"2022-05-26T08:40:01.097789Z","shell.execute_reply":"2022-05-26T08:40:01.101404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Train Data","metadata":{}},{"cell_type":"code","source":"df_trains = [pd.read_json(f'../input/deepfake/metadata{i}.json') for i in range(40)]\nval_nums=range(40,45)\ndf_vals = [pd.read_json(f'../input/deepfake/metadata{i}.json') for i in val_nums]\nnums = list(range(len(df_trains)+1))\nLABELS = ['REAL','FAKE']","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:40:03.084838Z","iopub.execute_input":"2022-05-26T08:40:03.085648Z","iopub.status.idle":"2022-05-26T08:40:39.993426Z","shell.execute_reply.started":"2022-05-26T08:40:03.085579Z","shell.execute_reply":"2022-05-26T08:40:39.992680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get Paths","metadata":{}},{"cell_type":"code","source":"def get_path(num,x):\n    num=str(num)\n    if len(num)==2:\n        path='../input/deepfake/DeepFake'+num+'/DeepFake'+num+'/' + x.replace('.mp4', '') + '.jpg'\n    else:\n        path='../input/deepfake/DeepFake0'+num+'/DeepFake0'+num+'/' + x.replace('.mp4', '') + '.jpg'\n    if not os.path.exists(path):raise Exception\n    return path\npaths=[]\ny=[]\nfor df_train,num in tqdm(zip(df_trains,nums),total=len(df_trains)):\n    images = list(df_train.columns.values)\n    for x in images:\n        try:\n            paths.append(get_path(num,x))\n            y.append(LABELS.index(df_train[x]['label']))\n        except Exception as err:\n            pass\n\nval_paths=[]\nval_y=[]\nfor df_val,num in tqdm(zip(df_vals,val_nums),total=len(df_vals)):\n    images = list(df_val.columns.values)\n    for x in images:\n        try:\n            val_paths.append(get_path(num,x))\n            val_y.append(LABELS.index(df_val[x]['label']))\n        except : \n            num= str(num)\n            path='../input/deepfake/DeepFake'+num+'/DeepFake'+num+'/' + x.replace('.mp4', '') + '.jpg'\n            ","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:40:39.995367Z","iopub.execute_input":"2022-05-26T08:40:39.995665Z","iopub.status.idle":"2022-05-26T08:43:50.127333Z","shell.execute_reply.started":"2022-05-26T08:40:39.995619Z","shell.execute_reply":"2022-05-26T08:43:50.126541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Apply Underbalancing Techinique","metadata":{}},{"cell_type":"code","source":"print('There are '+str(y.count(1))+' fake train samples')\nprint('There are '+str(y.count(0))+' real train samples')\nprint('There are '+str(val_y.count(1))+' fake val samples')\nprint('There are '+str(val_y.count(0))+' real val samples')","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:43:50.128757Z","iopub.execute_input":"2022-05-26T08:43:50.129252Z","iopub.status.idle":"2022-05-26T08:43:50.140854Z","shell.execute_reply.started":"2022-05-26T08:43:50.129201Z","shell.execute_reply":"2022-05-26T08:43:50.139941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The data is not balanced. We are going to use the undersampling technique.","metadata":{}},{"cell_type":"code","source":"import random\nreal=[]\nfake=[]\nfor m,n in zip(paths,y):\n    if n==0:\n        real.append(m)\n    else:\n        fake.append(m)\nfake=random.sample(fake,len(real))\npaths,y=[],[]\nfor x in real:\n    paths.append(x)\n    y.append(0)\nfor x in fake:\n    paths.append(x)\n    y.append(1)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:43:50.142345Z","iopub.execute_input":"2022-05-26T08:43:50.142994Z","iopub.status.idle":"2022-05-26T08:43:50.200708Z","shell.execute_reply.started":"2022-05-26T08:43:50.142652Z","shell.execute_reply":"2022-05-26T08:43:50.200113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"real=[]\nfake=[]\nfor m,n in zip(val_paths,val_y):\n    if n==0:\n        real.append(m)\n    else:\n        fake.append(m)\nfake=random.sample(fake,len(real))\nval_paths,val_y=[],[]\nfor x in real:\n    val_paths.append(x)\n    val_y.append(0)\nfor x in fake:\n    val_paths.append(x)\n    val_y.append(1)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:43:50.204256Z","iopub.execute_input":"2022-05-26T08:43:50.204478Z","iopub.status.idle":"2022-05-26T08:43:50.217961Z","shell.execute_reply.started":"2022-05-26T08:43:50.204435Z","shell.execute_reply":"2022-05-26T08:43:50.217249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('There are '+str(y.count(1))+' fake train samples')\nprint('There are '+str(y.count(0))+' real train samples')\nprint('There are '+str(val_y.count(1))+' fake val samples')\nprint('There are '+str(val_y.count(0))+' real val samples')","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:43:50.219456Z","iopub.execute_input":"2022-05-26T08:43:50.219937Z","iopub.status.idle":"2022-05-26T08:43:50.230804Z","shell.execute_reply.started":"2022-05-26T08:43:50.219882Z","shell.execute_reply":"2022-05-26T08:43:50.229899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now, the data is balanced.","metadata":{}},{"cell_type":"markdown","source":"# Read Images","metadata":{}},{"cell_type":"code","source":"IMG_SIZE = (128,128,1)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:43:50.232206Z","iopub.execute_input":"2022-05-26T08:43:50.232646Z","iopub.status.idle":"2022-05-26T08:43:50.238232Z","shell.execute_reply.started":"2022-05-26T08:43:50.232462Z","shell.execute_reply":"2022-05-26T08:43:50.237308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\n# sharpen kernel\nkernel = np.array([[0, -1, 0],\n                   [-1, 5,-1],\n                   [0, -1, 0]])\n\ndef read_img(path):\n    img = cv2.imread(path)\n    img =  cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n    img = cv2.filter2D(src=img, ddepth=-1, kernel=kernel)\n    img = cv2.resize(img,(IMG_SIZE[0],IMG_SIZE[1]))\n    return np.reshape(img,IMG_SIZE)\nX=[]\nfor img in tqdm(paths):\n    X.append(read_img(img))\nval_X=[]\nfor img in tqdm(val_paths):\n    val_X.append(read_img(img))","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:43:50.239514Z","iopub.execute_input":"2022-05-26T08:43:50.240150Z","iopub.status.idle":"2022-05-26T08:46:23.463914Z","shell.execute_reply.started":"2022-05-26T08:43:50.240016Z","shell.execute_reply":"2022-05-26T08:46:23.463158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot as plt\nplt.imshow(np.reshape(X[1],(IMG_SIZE[0],IMG_SIZE[1])))","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:46:23.465197Z","iopub.execute_input":"2022-05-26T08:46:23.465629Z","iopub.status.idle":"2022-05-26T08:46:23.761512Z","shell.execute_reply.started":"2022-05-26T08:46:23.465577Z","shell.execute_reply":"2022-05-26T08:46:23.760814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DEFINE MODEL","metadata":{}},{"cell_type":"code","source":"def InceptionLayer(a, b, c, d):\n    def func(x):\n        x1 = Conv2D(a, (1, 1), padding='same', activation='elu')(x)\n        \n        x2 = Conv2D(b, (1, 1), padding='same', activation='elu')(x)\n        x2 = Conv2D(b, (3, 3), padding='same', activation='elu')(x2)\n            \n        x3 = Conv2D(c, (1, 1), padding='same', activation='elu')(x)\n        x3 = Conv2D(c, (3, 3), dilation_rate = 2, strides = 1, padding='same', activation='elu')(x3)\n        \n        x4 = Conv2D(d, (1, 1), padding='same', activation='elu')(x)\n        x4 = Conv2D(d, (3, 3), dilation_rate = 3, strides = 1, padding='same', activation='elu')(x4)\n        \n        y = Concatenate(axis = -1)([x1, x2, x3, x4])\n            \n        return y\n    return func\n    \ndef define_model(shape=(128,128,1)):\n    x = Input(shape = shape)\n    \n    x1 = InceptionLayer(1, 4, 4, 1)(x)\n    x1 = BatchNormalization()(x1)\n    x1 = MaxPooling2D(pool_size=(2, 2), padding='same')(x1)\n    \n    x2 = InceptionLayer(1, 4, 4, 1)(x)\n    x2 = BatchNormalization()(x2)        \n    x2 = MaxPooling2D(pool_size=(2, 2), padding='same')(x2)     \n    \n    x3 = InceptionLayer(1, 4, 4, 1)(x1)\n    x3 = BatchNormalization()(x3)        \n    x3 = MaxPooling2D(pool_size=(2, 2), padding='same')(x3)     \n    \n    x4 = InceptionLayer(1, 4, 4, 1)(x2)\n    x4 = BatchNormalization()(x4)        \n    x4 = MaxPooling2D(pool_size=(2, 2), padding='same')(x4)  \n    \n    x5 = InceptionLayer(1, 4, 4, 1)(x1)\n    x5 = BatchNormalization()(x5)        \n    x5 = MaxPooling2D(pool_size=(3, 3), padding='same')(x5)  \n    \n    x6 = InceptionLayer(1, 4, 4, 1)(x2)\n    x6 = BatchNormalization()(x6)        \n    x6 = MaxPooling2D(pool_size=(3, 3), padding='same')(x6)  \n    \n    \n    x7 = Concatenate(axis = -1)([x6,x5])\n    x8 = Concatenate(axis = -1)([x5,x6])\n    \n    x9 = Conv2D(16, (2, 2),  padding='same',activation='elu')(x7)\n    x10 = Conv2D(16, (2, 2),  padding='same',activation='elu')(x8)\n    x11 = Conv2D(16, (2, 2),  padding='same',activation='elu')(x7)\n    x12 = Conv2D(16, (2, 2),  padding='same',activation='elu')(x8)\n    x13 = Concatenate(axis = -1)([x9,x10, x11,x12])\n    x14 = Concatenate(axis = -1)([x9,x10, x11,x12])\n    x15 = Concatenate(axis = -1)([x13,x14])\n    x14 = Concatenate(axis = -1)([x13,x14])\n    x13 = Concatenate(axis = -1)([x15,x14])\n    \n    x15 = Conv2D(16, (2, 2),  padding='same',activation='elu')(x13)\n    x16 = Conv2D(16, (2, 2),  padding='same',activation='elu')(x13)\n    \n    \n    x17 = Concatenate(axis = -1)([x15,x16])\n    y = Flatten()(x17)\n    y = Dense(16, activation = 'sigmoid')(y)\n    y = Dropout(0.2)(y)\n    y = Dense(1, activation = 'sigmoid')(y)\n    model=Model(inputs = x, outputs = y)\n    model.compile(loss='binary_crossentropy',optimizer=Adam(lr=1e-2),metrics=['accuracy'])\n    return model\n\n","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:46:23.762908Z","iopub.execute_input":"2022-05-26T08:46:23.763411Z","iopub.status.idle":"2022-05-26T08:46:23.808743Z","shell.execute_reply.started":"2022-05-26T08:46:23.763360Z","shell.execute_reply":"2022-05-26T08:46:23.807146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = define_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:46:23.810322Z","iopub.execute_input":"2022-05-26T08:46:23.810801Z","iopub.status.idle":"2022-05-26T08:46:26.992318Z","shell.execute_reply.started":"2022-05-26T08:46:23.810751Z","shell.execute_reply":"2022-05-26T08:46:26.991521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import plot_model\nplot_model(model, to_file='model.png')","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:46:26.993736Z","iopub.execute_input":"2022-05-26T08:46:26.994008Z","iopub.status.idle":"2022-05-26T08:46:28.887771Z","shell.execute_reply.started":"2022-05-26T08:46:26.993962Z","shell.execute_reply":"2022-05-26T08:46:28.886825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Model","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import LearningRateScheduler\nfrom keras.callbacks import ModelCheckpoint\n\n\nlosses=[]\nhist = []\n\nlrs=[1e-3,1e-3,1e-4,1e-5,8e-6,3e-6,1e-6,1e-7]\n\n\nk = 1\nl = 1\nm = 0\ndef schedule(epoch):\n    global k,l,m\n    \n    if m + 1 > lrs.__len__() - 1:\n        return lrs[-1]\n    else:\n        if k==0:\n            m+=1\n            l += 2\n            k = l\n        k -= 1\n        return lrs[m]","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:46:28.889165Z","iopub.execute_input":"2022-05-26T08:46:28.889445Z","iopub.status.idle":"2022-05-26T08:46:28.899885Z","shell.execute_reply.started":"2022-05-26T08:46:28.889402Z","shell.execute_reply":"2022-05-26T08:46:28.899147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train for 15 epochs","metadata":{}},{"cell_type":"code","source":"checkpoint = ModelCheckpoint(filepath='./model', \n                             monitor='loss',\n                             verbose=2, \n                             save_best_only=True,\n                             mode='min')","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:47:53.584895Z","iopub.execute_input":"2022-05-26T08:47:53.585362Z","iopub.status.idle":"2022-05-26T08:47:53.591960Z","shell.execute_reply.started":"2022-05-26T08:47:53.585283Z","shell.execute_reply":"2022-05-26T08:47:53.590960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\nkfolds=1\nif True:\n    models=[]\n    model=define_model(shape=IMG_SIZE)\n    h = model.fit([X],[y],epochs=200,batch_size=100,validation_data = ([val_X], [val_y]),callbacks=[checkpoint,LearningRateScheduler(schedule)]) #original 2 epochs\n    hist.append(h)\n    pred=model.predict([val_X])\n    loss=log_loss(val_y,pred)\n    losses.append(loss)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:47:54.764254Z","iopub.execute_input":"2022-05-26T08:47:54.764562Z","iopub.status.idle":"2022-05-26T08:48:46.117586Z","shell.execute_reply.started":"2022-05-26T08:47:54.764510Z","shell.execute_reply":"2022-05-26T08:48:46.116732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:52:40.277134Z","iopub.execute_input":"2022-05-26T08:52:40.277451Z","iopub.status.idle":"2022-05-26T08:52:40.284441Z","shell.execute_reply.started":"2022-05-26T08:52:40.277397Z","shell.execute_reply":"2022-05-26T08:52:40.283496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimg =  cv2.imread(paths[14144])\nimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\nimg.resize((1,)+IMG_SIZE)\nLABELS[int(model.predict(img)[0])],LABELS[y[14144]]","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:55:03.613199Z","iopub.execute_input":"2022-05-26T08:55:03.613511Z","iopub.status.idle":"2022-05-26T08:55:03.631767Z","shell.execute_reply.started":"2022-05-26T08:55:03.613458Z","shell.execute_reply":"2022-05-26T08:55:03.630797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hola2 = {\n    'accuracy':[],\n    'val_accuracy':[],\n    'loss':[],\n    'val_loss':[]\n}\nfor i in range(1):\n    for hk in hola2.keys():\n        hola2[hk] += hist[i].history[hk][:50]","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:48:57.884345Z","iopub.execute_input":"2022-05-26T08:48:57.884659Z","iopub.status.idle":"2022-05-26T08:48:57.889984Z","shell.execute_reply.started":"2022-05-26T08:48:57.884598Z","shell.execute_reply":"2022-05-26T08:48:57.888974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(hola2['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:48:58.771926Z","iopub.execute_input":"2022-05-26T08:48:58.772236Z","iopub.status.idle":"2022-05-26T08:48:58.996534Z","shell.execute_reply.started":"2022-05-26T08:48:58.772161Z","shell.execute_reply":"2022-05-26T08:48:58.995549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(hola2['val_accuracy'],color='r')","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:48:59.528073Z","iopub.execute_input":"2022-05-26T08:48:59.528362Z","iopub.status.idle":"2022-05-26T08:48:59.748529Z","shell.execute_reply.started":"2022-05-26T08:48:59.528310Z","shell.execute_reply":"2022-05-26T08:48:59.747716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(hola2['loss'])\n","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:49:01.140313Z","iopub.execute_input":"2022-05-26T08:49:01.140619Z","iopub.status.idle":"2022-05-26T08:49:01.359541Z","shell.execute_reply.started":"2022-05-26T08:49:01.140569Z","shell.execute_reply":"2022-05-26T08:49:01.358717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(hola2['val_loss'])","metadata":{"execution":{"iopub.status.busy":"2022-05-26T08:49:02.393085Z","iopub.execute_input":"2022-05-26T08:49:02.393501Z","iopub.status.idle":"2022-05-26T08:49:02.660219Z","shell.execute_reply.started":"2022-05-26T08:49:02.393446Z","shell.execute_reply":"2022-05-26T08:49:02.659311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}