{"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 -U --upgrade tensorflow","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-30T19:39:23.469319Z","iopub.execute_input":"2023-10-30T19:39:23.469792Z","iopub.status.idle":"2023-10-30T19:41:02.94912Z","shell.execute_reply.started":"2023-10-30T19:39:23.469734Z","shell.execute_reply":"2023-10-30T19:41:02.947854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-10-30T19:41:02.951378Z","iopub.execute_input":"2023-10-30T19:41:02.95172Z","iopub.status.idle":"2023-10-30T19:41:07.870918Z","shell.execute_reply.started":"2023-10-30T19:41:02.951663Z","shell.execute_reply":"2023-10-30T19:41:07.869756Z"},"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":"2023-10-30T19:41:07.874734Z","iopub.execute_input":"2023-10-30T19:41:07.875066Z","iopub.status.idle":"2023-10-30T19:41:07.883058Z","shell.execute_reply.started":"2023-10-30T19:41:07.87501Z","shell.execute_reply":"2023-10-30T19:41:07.881526Z"},"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":"2023-10-30T19:41:07.885135Z","iopub.execute_input":"2023-10-30T19:41:07.885562Z","iopub.status.idle":"2023-10-30T19:41:08.152097Z","shell.execute_reply.started":"2023-10-30T19:41:07.885486Z","shell.execute_reply":"2023-10-30T19:41:08.150837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta=get_data()\nmeta.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T19:41:08.15824Z","iopub.execute_input":"2023-10-30T19:41:08.15914Z","iopub.status.idle":"2023-10-30T19:41:08.416585Z","shell.execute_reply.started":"2023-10-30T19:41:08.158706Z","shell.execute_reply":"2023-10-30T19:41:08.415139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-30T19:41:08.420979Z","iopub.execute_input":"2023-10-30T19:41:08.42135Z","iopub.status.idle":"2023-10-30T19:41:08.42857Z","shell.execute_reply.started":"2023-10-30T19:41:08.421292Z","shell.execute_reply":"2023-10-30T19:41:08.42743Z"},"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":"2023-10-30T19:41:08.430542Z","iopub.execute_input":"2023-10-30T19:41:08.430861Z","iopub.status.idle":"2023-10-30T19:41:08.493542Z","shell.execute_reply.started":"2023-10-30T19:41:08.430809Z","shell.execute_reply":"2023-10-30T19:41:08.491892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"real_df = meta[meta[\"label\"] == \"REAL\"]\nfake_df = meta[meta[\"label\"] == \"FAKE\"]\nsample_size = 8000\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":"2023-10-30T19:41:08.495545Z","iopub.execute_input":"2023-10-30T19:41:08.495894Z","iopub.status.idle":"2023-10-30T19:41:08.569112Z","shell.execute_reply.started":"2023-10-30T19:41:08.495831Z","shell.execute_reply":"2023-10-30T19:41:08.567893Z"},"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.3,random_state=42,stratify=Train_set['label'])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T19:41:08.570992Z","iopub.execute_input":"2023-10-30T19:41:08.57135Z","iopub.status.idle":"2023-10-30T19:41:09.480042Z","shell.execute_reply.started":"2023-10-30T19:41:08.571294Z","shell.execute_reply":"2023-10-30T19:41:09.478356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train_set.shape,Val_set.shape,Test_set.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-30T19:41:09.482115Z","iopub.execute_input":"2023-10-30T19:41:09.482545Z","iopub.status.idle":"2023-10-30T19:41:09.501424Z","shell.execute_reply.started":"2023-10-30T19:41:09.482438Z","shell.execute_reply":"2023-10-30T19:41:09.499883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = dict()\n\ny[0] = []\ny[1] = []\n\nfor set_name in (np.array(Train_set['label']), np.array(Val_set['label']), np.array(Test_set['label'])):\n    y[0].append(np.sum(set_name == 'REAL'))\n    y[1].append(np.sum(set_name == 'FAKE'))\n\ntrace0 = go.Bar(\n    x=['Train Set', 'Validation Set', 'Test Set'],\n    y=y[0],\n    name='REAL',\n    marker=dict(color='#33cc33'),\n    opacity=0.7\n)\ntrace1 = go.Bar(\n    x=['Train Set', 'Validation Set', 'Test Set'],\n    y=y[1],\n    name='FAKE',\n    marker=dict(color='#ff3300'),\n    opacity=0.7\n)\n\ndata = [trace0, trace1]\nlayout = go.Layout(\n    title='Count of classes in each set',\n    xaxis={'title': 'Set'},\n    yaxis={'title': 'Count'}\n)\n\nfig = go.Figure(data, layout)\niplot(fig)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T19:41:09.503295Z","iopub.execute_input":"2023-10-30T19:41:09.503753Z","iopub.status.idle":"2023-10-30T19:41:11.288954Z","shell.execute_reply.started":"2023-10-30T19:41:09.50366Z","shell.execute_reply":"2023-10-30T19:41:11.287566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-10-30T19:41:11.290636Z","iopub.execute_input":"2023-10-30T19:41:11.290951Z","iopub.status.idle":"2023-10-30T19:41:13.621003Z","shell.execute_reply.started":"2023-10-30T19:41:11.290899Z","shell.execute_reply":"2023-10-30T19:41:13.619718Z"},"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":"2023-10-30T19:41:13.622265Z","iopub.execute_input":"2023-10-30T19:41:13.62257Z","iopub.status.idle":"2023-10-30T19:41:13.63201Z","shell.execute_reply.started":"2023-10-30T19:41:13.622524Z","shell.execute_reply":"2023-10-30T19:41:13.630345Z"},"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":"2023-10-30T19:41:13.633751Z","iopub.execute_input":"2023-10-30T19:41:13.634139Z","iopub.status.idle":"2023-10-30T19:43:00.833473Z","shell.execute_reply.started":"2023-10-30T19:41:13.634054Z","shell.execute_reply":"2023-10-30T19:43:00.83196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from functools import partial\n\ntf.random.set_seed(42) \nDefaultConv2D = partial(tf.keras.layers.Conv2D, kernel_size=3, padding=\"same\",\n                        activation=\"relu\", kernel_initializer=\"he_normal\")\n\nmodel = tf.keras.Sequential([\n    DefaultConv2D(filters=64, kernel_size=7, input_shape=[224, 224, 3]),\n    tf.keras.layers.MaxPool2D(),\n    DefaultConv2D(filters=128),\n    DefaultConv2D(filters=128),\n    tf.keras.layers.MaxPool2D(),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(units=128, activation=\"relu\",\n                          kernel_initializer=\"he_normal\"),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(units=64, activation=\"relu\",\n                          kernel_initializer=\"he_normal\"),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(units=1, activation=\"sigmoid\")\n])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T19:43:00.835327Z","iopub.execute_input":"2023-10-30T19:43:00.835675Z","iopub.status.idle":"2023-10-30T19:43:02.004675Z","shell.execute_reply.started":"2023-10-30T19:43:00.835614Z","shell.execute_reply":"2023-10-30T19:43:02.003203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"binary_crossentropy\", optimizer=\"nadam\",\n              metrics=[\"accuracy\"])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T19:43:02.006858Z","iopub.execute_input":"2023-10-30T19:43:02.007326Z","iopub.status.idle":"2023-10-30T19:43:02.051347Z","shell.execute_reply.started":"2023-10-30T19:43:02.00724Z","shell.execute_reply":"2023-10-30T19:43:02.049986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, epochs=5,batch_size=64,\n                    validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2023-10-30T19:43:02.053391Z","iopub.execute_input":"2023-10-30T19:43:02.053859Z","iopub.status.idle":"2023-10-30T23:46:00.946031Z","shell.execute_reply.started":"2023-10-30T19:43:02.053778Z","shell.execute_reply":"2023-10-30T23:46:00.942965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T23:46:00.953742Z","iopub.execute_input":"2023-10-30T23:46:00.954286Z","iopub.status.idle":"2023-10-30T23:50:05.100188Z","shell.execute_reply.started":"2023-10-30T23:46:00.954197Z","shell.execute_reply":"2023-10-30T23:50:05.099245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = 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":"2023-10-30T23:50:05.102229Z","iopub.execute_input":"2023-10-30T23:50:05.102561Z","iopub.status.idle":"2023-10-30T23:50:05.933591Z","shell.execute_reply.started":"2023-10-30T23:50:05.102505Z","shell.execute_reply":"2023-10-30T23:50:05.929828Z"},"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":"2023-10-30T23:50:05.935868Z","iopub.execute_input":"2023-10-30T23:50:05.936204Z","iopub.status.idle":"2023-10-30T23:50:07.728603Z","shell.execute_reply.started":"2023-10-30T23:50:05.93615Z","shell.execute_reply":"2023-10-30T23:50:07.72762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.backend.clear_session() \n\nbatch_size = 32\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":"2023-10-30T23:50:07.731521Z","iopub.execute_input":"2023-10-30T23:50:07.731953Z","iopub.status.idle":"2023-10-30T23:50:07.934028Z","shell.execute_reply.started":"2023-10-30T23:50:07.731876Z","shell.execute_reply":"2023-10-30T23:50:07.932661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nfor X_batch, y_batch in valid_set.take(1):\n    for index in range(9):\n        plt.subplot(3, 3, index + 1)\n        plt.imshow((X_batch[index] + 1) / 2) \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":"2023-10-30T23:50:07.936199Z","iopub.execute_input":"2023-10-30T23:50:07.936532Z","iopub.status.idle":"2023-10-30T23:50:09.73514Z","shell.execute_reply.started":"2023-10-30T23:50:07.936477Z","shell.execute_reply":"2023-10-30T23:50:09.733896Z"},"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":"2023-10-30T23:50:09.737025Z","iopub.execute_input":"2023-10-30T23:50:09.737337Z","iopub.status.idle":"2023-10-30T23:50:09.777511Z","shell.execute_reply.started":"2023-10-30T23:50:09.737276Z","shell.execute_reply":"2023-10-30T23:50:09.776389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.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        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":"2023-10-30T23:50:09.779519Z","iopub.execute_input":"2023-10-30T23:50:09.77982Z","iopub.status.idle":"2023-10-30T23:50:11.855232Z","shell.execute_reply.started":"2023-10-30T23:50:09.779769Z","shell.execute_reply":"2023-10-30T23:50:11.853653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.random.set_seed(42)\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":"2023-10-30T23:50:11.857695Z","iopub.execute_input":"2023-10-30T23:50:11.858167Z","iopub.status.idle":"2023-10-30T23:50:14.647199Z","shell.execute_reply.started":"2023-10-30T23:50:11.858084Z","shell.execute_reply":"2023-10-30T23:50:14.645891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_model.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-10-30T23:50:14.649194Z","iopub.execute_input":"2023-10-30T23:50:14.649583Z","iopub.status.idle":"2023-10-30T23:50:14.663198Z","shell.execute_reply.started":"2023-10-30T23:50:14.649522Z","shell.execute_reply":"2023-10-30T23:50:14.661919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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(train_set, validation_data=valid_set, epochs=3)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T23:50:14.665378Z","iopub.execute_input":"2023-10-30T23:50:14.665801Z","iopub.status.idle":"2023-10-31T01:04:14.096558Z","shell.execute_reply.started":"2023-10-30T23:50:14.665733Z","shell.execute_reply":"2023-10-31T01:04:14.094758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for indices in zip(range(33), range(33, 66), range(66, 99), range(99, 132)):\n    for idx in indices:\n        print(f\"{idx:3}: {base_model.layers[idx].name:22}\", end=\"\")\n    print()","metadata":{"execution":{"iopub.status.busy":"2023-10-31T01:04:14.099542Z","iopub.execute_input":"2023-10-31T01:04:14.099913Z","iopub.status.idle":"2023-10-31T01:04:14.193725Z","shell.execute_reply.started":"2023-10-31T01:04:14.099856Z","shell.execute_reply":"2023-10-31T01:04:14.192215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(test_set)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T01:04:14.1972Z","iopub.execute_input":"2023-10-31T01:04:14.197735Z","iopub.status.idle":"2023-10-31T01:10:37.159242Z","shell.execute_reply.started":"2023-10-31T01:04:14.197637Z","shell.execute_reply":"2023-10-31T01:10:37.157908Z"},"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=10)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T01:10:37.161844Z","iopub.execute_input":"2023-10-31T01:10:37.162301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = 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_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(test_set)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('xception_deepfake_image.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install lime","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lime import lime_image\nexplainer = lime_image.LimeImageExplainer()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x,y in test_set.as_numpy_iterator():\n    print(type(x),type(y))\n    break","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\n\nfor index in range(9):\n    plt.subplot(3, 3, index + 1)\n    plt.imshow((x[index] + 1) / 2)  # rescale to 0–1 for imshow()\n    if(y[index]==1):\n        classt='FAKE'\n    else:\n        classt='REAL'\n    plt.title(f\"Class: {classt}\")\n    plt.axis(\"off\")\n\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=x[2,:,:,:]\ntest_data.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"explanation = explainer.explain_instance(test_data.astype('double'), model.predict,  \n                                         top_labels=3, hide_color=0, num_samples=1000)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom skimage.segmentation import mark_boundaries\n\ntemp_1, mask_1 = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=True, num_features=5, hide_rest=True)\ntemp_2, mask_2 = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=False, num_features=10, hide_rest=False)\n\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15,15))\nax1.imshow(mark_boundaries(temp_1, mask_1))\nax2.imshow(mark_boundaries(temp_2, mask_2))\nax1.axis('off')\nax2.axis('off')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}