{"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":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Lets define the mesonet model - mesonet.py","metadata":{}},{"cell_type":"code","source":"from keras.models import Model as KerasModel\nfrom keras.layers import Input, Dense,Flatten, Conv2D, MaxPooling2D, BatchNormalization, Dropout, Reshape, Concatenate, LeakyReLU\nfrom keras.optimizers import Adam\n\nIMGWIDTH = 256","metadata":{"execution":{"iopub.status.busy":"2022-05-06T00:54:33.512444Z","iopub.execute_input":"2022-05-06T00:54:33.513065Z","iopub.status.idle":"2022-05-06T00:54:41.312445Z","shell.execute_reply.started":"2022-05-06T00:54:33.513009Z","shell.execute_reply":"2022-05-06T00:54:41.311318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Classifier:\n    def __init__():\n        self.model = 0\n    \n    def predict(self, x):\n        return self.model.predict(x)\n    \n    def fit(self, x, y):\n        return self.model.train_on_batch(x, y)\n    \n    def get_accuracy(self, x, y):\n        return self.model.test_on_batch(x, y)\n    \n    def load(self, path):\n        self.model.load_weights(path)\n\nclass Meso4(Classifier):\n    def __init__(self, learning_rate=0.001):\n        self.model = self.init_model()\n        optimizer = Adam(lr = learning_rate)\n        self.model.compile(optimizer = optimizer, loss='mean_squared_error', metrics=['accuracy'])\n    \n    def init_model(self):\n        x = Input(shape=(IMGWIDTH, IMGWIDTH, 3))\n        \n        x1 = Conv2D(16, (3,3), padding='same', activation='relu')(x)\n        x1 = BatchNormalization()(x1)\n        x1 = MaxPooling2D(pool_size=(2,2), padding='same')(x1)\n        \n        x2 = Conv2D(16,(5,5), padding='same', activation='relu')(x1)\n        x2 = BatchNormalization()(x2)\n        x2 = MaxPooling2D(pool_size=(2,2), padding='same')(x2)\n        \n        x3 = Conv2D(16, (5,5), padding='same', activation='relu')(x2)\n        x3 = BatchNormalization()(x3)\n        x3 = MaxPooling2D(pool_size=(2,2), padding='same')(x3)\n        \n        x4 = Conv2D(16,(5,5), padding='same', activation='relu')(x3)\n        x4 = BatchNormalization()(x4)\n        x4 = MaxPooling2D(pool_size=(4,4), padding='same')(x4)\n        \n        x5 = Conv2D(16,(5,5), padding='same', activation='relu')(x4)\n        x5 = BatchNormalization()(x5)\n        x5 = MaxPooling2D(pool_size=(4,4), padding='same')(x5)\n        \n        y = Flatten()(x5)\n        y = Dropout(0.5)(y)\n        y = Dense(16)(y)\n        y = LeakyReLU(alpha=0.1)(y)\n        y = Dropout(0.5)(y)\n        y = Dense(1, activation='sigmoid')(y)\n        \n        return KerasModel(input=x, outputs=y)","metadata":{"execution":{"iopub.status.busy":"2022-05-06T00:54:47.429728Z","iopub.execute_input":"2022-05-06T00:54:47.430235Z","iopub.status.idle":"2022-05-06T00:54:47.455623Z","shell.execute_reply.started":"2022-05-06T00:54:47.430161Z","shell.execute_reply":"2022-05-06T00:54:47.454015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Getting the trained model weights","metadata":{}},{"cell_type":"code","source":"!wget https://github.com/PacktPublishing/Machine-Learning-for-Cybersecurity-Cookbook/raw/master/Chapter04/Deepfake%20Recognition/mesonet_weights/Meso4_DF","metadata":{"execution":{"iopub.status.busy":"2022-05-06T00:54:55.147402Z","iopub.execute_input":"2022-05-06T00:54:55.147838Z","iopub.status.idle":"2022-05-06T00:54:57.581425Z","shell.execute_reply.started":"2022-05-06T00:54:55.147763Z","shell.execute_reply":"2022-05-06T00:54:57.580092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Lets get some sample images","metadata":{}},{"cell_type":"code","source":"!wget -O df1.jpg https://github.com/PacktPublishing/Machine-Learning-for-Cybersecurity-Cookbook/raw/master/Chapter04/Deepfake%20Recognition/mesonet_test_images/df00204.jpg","metadata":{"execution":{"iopub.status.busy":"2022-05-06T00:55:01.3099Z","iopub.execute_input":"2022-05-06T00:55:01.310255Z","iopub.status.idle":"2022-05-06T00:55:03.640018Z","shell.execute_reply.started":"2022-05-06T00:55:01.310197Z","shell.execute_reply":"2022-05-06T00:55:03.638958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget -O df2.jpg https://github.com/PacktPublishing/Machine-Learning-for-Cybersecurity-Cookbook/raw/master/Chapter04/Deepfake%20Recognition/mesonet_test_images/df01254.jpg","metadata":{"execution":{"iopub.status.busy":"2022-05-06T00:55:06.243661Z","iopub.execute_input":"2022-05-06T00:55:06.244151Z","iopub.status.idle":"2022-05-06T00:55:08.579374Z","shell.execute_reply.started":"2022-05-06T00:55:06.244075Z","shell.execute_reply":"2022-05-06T00:55:08.578102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget -O real.jpg https://github.com/PacktPublishing/Machine-Learning-for-Cybersecurity-Cookbook/raw/master/Chapter04/Deepfake%20Recognition/mesonet_test_images/real00240.jpg","metadata":{"execution":{"iopub.status.busy":"2022-05-06T00:55:11.078981Z","iopub.execute_input":"2022-05-06T00:55:11.079422Z","iopub.status.idle":"2022-05-06T00:55:13.390115Z","shell.execute_reply.started":"2022-05-06T00:55:11.079358Z","shell.execute_reply":"2022-05-06T00:55:13.389057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\nMesoNet_classifier = Meso4()\nMesoNet_classifier.load(\"Meso4_DF\")","metadata":{"execution":{"iopub.status.busy":"2022-05-06T00:55:28.776914Z","iopub.execute_input":"2022-05-06T00:55:28.777457Z","iopub.status.idle":"2022-05-06T00:55:29.173579Z","shell.execute_reply.started":"2022-05-06T00:55:28.777366Z","shell.execute_reply":"2022-05-06T00:55:29.172051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir test_images\n!mv *.jpg test_images/","metadata":{"execution":{"iopub.status.busy":"2022-05-06T00:56:00.822173Z","iopub.execute_input":"2022-05-06T00:56:00.822759Z","iopub.status.idle":"2022-05-06T00:56:02.367385Z","shell.execute_reply.started":"2022-05-06T00:56:00.822698Z","shell.execute_reply":"2022-05-06T00:56:02.366011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_data_generator = ImageDataGenerator(rescale=1.0/255)\ndata_generator = image_data_generator.flow_from_directory(\"./\", classes=[\"test_images\"])","metadata":{"execution":{"iopub.status.busy":"2022-05-06T00:56:06.586059Z","iopub.execute_input":"2022-05-06T00:56:06.586708Z","iopub.status.idle":"2022-05-06T00:56:06.700008Z","shell.execute_reply.started":"2022-05-06T00:56:06.586649Z","shell.execute_reply":"2022-05-06T00:56:06.698669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_to_label = {1:\"real\", 0:\"fake\"}","metadata":{"execution":{"iopub.status.busy":"2022-05-06T00:56:09.73933Z","iopub.execute_input":"2022-05-06T00:56:09.739999Z","iopub.status.idle":"2022-05-06T00:56:09.746594Z","shell.execute_reply.started":"2022-05-06T00:56:09.739919Z","shell.execute_reply":"2022-05-06T00:56:09.74507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![](test_images/df1.jpg)\n![](test_images/df2.jpg)\n![](test_images/real.jpg)","metadata":{}},{"cell_type":"code","source":"\n\nX, y = data_generator.next()\nprobabilistic_predictions = MesoNet_classifier.predict(X)\n\npredictions = [num_to_label[round(x[0])] for x in probabilistic_predictions]\n\n\n\n\nprint(predictions)","metadata":{"execution":{"iopub.status.busy":"2022-05-06T00:56:12.956624Z","iopub.execute_input":"2022-05-06T00:56:12.956993Z","iopub.status.idle":"2022-05-06T00:56:13.518592Z","shell.execute_reply.started":"2022-05-06T00:56:12.956929Z","shell.execute_reply":"2022-05-06T00:56:13.517706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#threshold\ni=0    \nwhile i < len(probabilistic_predictions):\n   if(probabilistic_predictions[i]<=0.5):\n    probabilistic_predictions[i]=0\n   else:\n    probabilistic_predictions[i]=1\n   i+=1  ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_names = data_generator.filenames","metadata":{"execution":{"iopub.status.busy":"2022-04-15T04:13:15.345447Z","iopub.execute_input":"2022-04-15T04:13:15.345793Z","iopub.status.idle":"2022-04-15T04:13:15.350439Z","shell.execute_reply.started":"2022-04-15T04:13:15.345738Z","shell.execute_reply":"2022-04-15T04:13:15.349361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_names","metadata":{"execution":{"iopub.status.busy":"2022-04-15T04:13:15.352235Z","iopub.execute_input":"2022-04-15T04:13:15.352866Z","iopub.status.idle":"2022-04-15T04:13:15.366265Z","shell.execute_reply.started":"2022-04-15T04:13:15.352806Z","shell.execute_reply":"2022-04-15T04:13:15.36522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pyspark","metadata":{"execution":{"iopub.status.busy":"2022-04-15T04:13:15.367704Z","iopub.execute_input":"2022-04-15T04:13:15.368205Z","iopub.status.idle":"2022-04-15T04:14:07.106927Z","shell.execute_reply.started":"2022-04-15T04:13:15.368152Z","shell.execute_reply":"2022-04-15T04:14:07.105582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install face_recognition","metadata":{"execution":{"iopub.status.busy":"2022-04-15T04:14:07.79905Z","iopub.status.idle":"2022-04-15T04:14:07.799709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MesoNet_classifier.get_accuracy(X, y)","metadata":{"execution":{"iopub.status.busy":"2022-05-06T00:56:32.018942Z","iopub.execute_input":"2022-05-06T00:56:32.019313Z","iopub.status.idle":"2022-05-06T00:56:32.09251Z","shell.execute_reply.started":"2022-05-06T00:56:32.019248Z","shell.execute_reply":"2022-05-06T00:56:32.091406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MesoNet_classifer.fit(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-04-15T04:37:59.225861Z","iopub.execute_input":"2022-04-15T04:37:59.226527Z","iopub.status.idle":"2022-04-15T04:37:59.252148Z","shell.execute_reply.started":"2022-04-15T04:37:59.226452Z","shell.execute_reply":"2022-04-15T04:37:59.250931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2022-04-15T04:30:52.265266Z","iopub.execute_input":"2022-04-15T04:30:52.265903Z","iopub.status.idle":"2022-04-15T04:30:52.270993Z","shell.execute_reply.started":"2022-04-15T04:30:52.265853Z","shell.execute_reply":"2022-04-15T04:30:52.270134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install pysimplegui","metadata":{"execution":{"iopub.status.busy":"2022-04-18T13:26:48.298064Z","iopub.execute_input":"2022-04-18T13:26:48.298804Z","iopub.status.idle":"2022-04-18T13:26:57.481316Z","shell.execute_reply.started":"2022-04-18T13:26:48.298731Z","shell.execute_reply":"2022-04-18T13:26:57.480024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import PySimpleGUI as sg\n\nsg.Window(title=\"Deepfake Detector\", layout=[[]], margins=(100, 50)).read()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T13:27:58.539718Z","iopub.execute_input":"2022-04-18T13:27:58.540037Z","iopub.status.idle":"2022-04-18T13:27:58.786476Z","shell.execute_reply.started":"2022-04-18T13:27:58.539982Z","shell.execute_reply":"2022-04-18T13:27:58.784919Z"},"trusted":true},"execution_count":null,"outputs":[]}]}