{"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":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport time\nimport os\nimport datetime\nimport pytz\nimport random","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:11:41.497127Z","iopub.execute_input":"2023-04-18T06:11:41.498175Z","iopub.status.idle":"2023-04-18T06:11:41.600384Z","shell.execute_reply.started":"2023-04-18T06:11:41.498123Z","shell.execute_reply":"2023-04-18T06:11:41.599244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:11:41.604078Z","iopub.execute_input":"2023-04-18T06:11:41.605026Z","iopub.status.idle":"2023-04-18T06:11:42.027531Z","shell.execute_reply.started":"2023-04-18T06:11:41.604986Z","shell.execute_reply":"2023-04-18T06:11:42.026504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom PIL import Image\nimport torchvision\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.autograd import Variable","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:11:42.028892Z","iopub.execute_input":"2023-04-18T06:11:42.029283Z","iopub.status.idle":"2023-04-18T06:11:44.465278Z","shell.execute_reply.started":"2023-04-18T06:11:42.029245Z","shell.execute_reply":"2023-04-18T06:11:44.464248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision import datasets,transforms\nfrom torch.utils.data import DataLoader\nfrom torch.utils.data import DataLoader, Dataset,random_split\nfrom skimage import io, transform\nimport torchvision.transforms as transforms\nfrom torchvision import transforms, utils\nfrom torchvision.datasets import ImageFolder","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:11:44.468466Z","iopub.execute_input":"2023-04-18T06:11:44.469433Z","iopub.status.idle":"2023-04-18T06:11:44.906809Z","shell.execute_reply.started":"2023-04-18T06:11:44.469393Z","shell.execute_reply":"2023-04-18T06:11:44.905733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:11:44.908404Z","iopub.execute_input":"2023-04-18T06:11:44.908791Z","iopub.status.idle":"2023-04-18T06:11:44.984920Z","shell.execute_reply.started":"2023-04-18T06:11:44.908752Z","shell.execute_reply":"2023-04-18T06:11:44.983823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = 1000\ntest_images = 200\n\ncsv_PATH = \"/kaggle/input/diabetic-retinopathy-resized/trainLabels_cropped.csv\"\nimages_PATH = \"/kaggle/input/diabetic-retinopathy-resized/resized_train_cropped/resized_train_cropped/\"","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:11:44.986599Z","iopub.execute_input":"2023-04-18T06:11:44.987281Z","iopub.status.idle":"2023-04-18T06:11:44.996520Z","shell.execute_reply.started":"2023-04-18T06:11:44.987237Z","shell.execute_reply":"2023-04-18T06:11:44.995525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(csv_PATH)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:11:45.000154Z","iopub.execute_input":"2023-04-18T06:11:45.000908Z","iopub.status.idle":"2023-04-18T06:11:45.070609Z","shell.execute_reply.started":"2023-04-18T06:11:45.000879Z","shell.execute_reply":"2023-04-18T06:11:45.069454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.transforms as transforms\n\nc_trans = transforms.Compose([ # For ImageFolder\n    transforms.Resize((224,224)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), \n])\n\n\nd_trans = transforms.Compose([ # For the dataset class\n    transforms.ToPILImage(), ## WE NEED THIS OR ELSE IT GIVES TYPE ERROR WITH NUMPY NDARRAY\n    transforms.Resize((224,224)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n])","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:11:45.080171Z","iopub.execute_input":"2023-04-18T06:11:45.080964Z","iopub.status.idle":"2023-04-18T06:11:45.089206Z","shell.execute_reply.started":"2023-04-18T06:11:45.080929Z","shell.execute_reply":"2023-04-18T06:11:45.088214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cropped_new_df = pd.read_csv(csv_PATH)\ncropped_new_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:11:45.094333Z","iopub.execute_input":"2023-04-18T06:11:45.095330Z","iopub.status.idle":"2023-04-18T06:11:45.128320Z","shell.execute_reply.started":"2023-04-18T06:11:45.095293Z","shell.execute_reply":"2023-04-18T06:11:45.127239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom PIL import Image\n\nclass retinaDataset(Dataset):\n    def __init__(self, imagepath = images_PATH, start = 0, total = None, transform = None):\n        self.df = cropped_new_df\n        \n        if (total is not None):\n            self.df = self.df[start:total]\n        \n        self.transform = transform\n        \n        self.imagepath = imagepath\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, index):\n        img_path = os.path.join(self.imagepath, self.df.iloc[index].image +\".jpeg\")\n        # img = Image.open(img_path) # Old\n        img = io.imread(img_path) # New\n        img = torch.from_numpy(img).permute(2, 0, 1)\n        \n        if(self.transform):\n            img = self.transform(img)\n        \n        return img, torch.tensor(self.df.iloc[index].level)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:11:45.129950Z","iopub.execute_input":"2023-04-18T06:11:45.130805Z","iopub.status.idle":"2023-04-18T06:11:45.140467Z","shell.execute_reply.started":"2023-04-18T06:11:45.130775Z","shell.execute_reply":"2023-04-18T06:11:45.139418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainset_DS = retinaDataset(total = train_images, transform = d_trans)\ntestset_DS = retinaDataset(start = train_images, total = train_images + test_images, transform = d_trans)\nprint(len(trainset_DS))\nprint(len(testset_DS))\nprint()\ntrainloader_DS = DataLoader(dataset=trainset_DS, batch_size = len(trainset_DS), shuffle=True)\ntestloader_DS = DataLoader(dataset=testset_DS, batch_size = len(testset_DS), shuffle=True)\nprint(len(trainloader_DS))\nprint(len(testloader_DS))","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:43:11.480971Z","iopub.execute_input":"2023-04-18T06:43:11.481684Z","iopub.status.idle":"2023-04-18T06:43:11.489722Z","shell.execute_reply.started":"2023-04-18T06:43:11.481648Z","shell.execute_reply":"2023-04-18T06:43:11.488445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Verify both sets\nmode = \"test\"\nmode = \"train\"\n\nif mode == \"train\":\n    set = trainset_DS\nelse:\n    set = testset_DS\nfig = plt.figure()\nlimit = 4\nfor i in range(len(set)):\n    img, label = set[i]\n\n    print(i, img.shape, label)\n\n    ax = plt.subplot(1, limit, i + 1)\n    plt.tight_layout()\n    ax.set_title('Sample #{}'.format(i))\n    ax.axis('off')\n    plt.imshow(img.permute(1,2,0))\n    if i == limit - 1:\n        plt.show()\n        break","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:11:45.161339Z","iopub.execute_input":"2023-04-18T06:11:45.161613Z","iopub.status.idle":"2023-04-18T06:11:45.763337Z","shell.execute_reply.started":"2023-04-18T06:11:45.161587Z","shell.execute_reply":"2023-04-18T06:11:45.762319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reshape_correctly(X_prub):\n    shap = X_prub.shape\n    shap_new = (shap[0], shap[2], shap[3], shap[1])\n    X_prub = X_prub.reshape(shap_new)\n    \n    return X_prub\n","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:11:45.764832Z","iopub.execute_input":"2023-04-18T06:11:45.765521Z","iopub.status.idle":"2023-04-18T06:11:45.772106Z","shell.execute_reply.started":"2023-04-18T06:11:45.765481Z","shell.execute_reply":"2023-04-18T06:11:45.771018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_priv = next(iter(trainloader_DS))[0].numpy()\nX_pub = next(iter(testloader_DS))[0].numpy()\n\nX_priv_lab = next(iter(trainloader_DS))[1].numpy()\nX_pub_lab = next(iter(testloader_DS))[1].numpy()\n\nX_priv = reshape_correctly(X_priv)\nX_pub = reshape_correctly(X_pub)\n\nX_priv_lab.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:44:52.596422Z","iopub.execute_input":"2023-04-18T06:44:52.596910Z","iopub.status.idle":"2023-04-18T06:44:53.036253Z","shell.execute_reply.started":"2023-04-18T06:44:52.596861Z","shell.execute_reply":"2023-04-18T06:44:53.035146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:13:00.309790Z","iopub.execute_input":"2023-04-18T06:13:00.310164Z","iopub.status.idle":"2023-04-18T06:13:08.450546Z","shell.execute_reply.started":"2023-04-18T06:13:00.310128Z","shell.execute_reply":"2023-04-18T06:13:08.449365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"XD_priv = tf.data.Dataset.from_tensor_slices((X_priv, X_priv_lab))\nXD_pub = tf.data.Dataset.from_tensor_slices((X_pub, X_pub_lab))","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:13:08.452318Z","iopub.execute_input":"2023-04-18T06:13:08.453216Z","iopub.status.idle":"2023-04-18T06:13:14.212021Z","shell.execute_reply.started":"2023-04-18T06:13:08.453155Z","shell.execute_reply":"2023-04-18T06:13:14.210965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(XD_priv))","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:13:14.213491Z","iopub.execute_input":"2023-04-18T06:13:14.213857Z","iopub.status.idle":"2023-04-18T06:13:14.222687Z","shell.execute_reply.started":"2023-04-18T06:13:14.213820Z","shell.execute_reply":"2023-04-18T06:13:14.220608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_priv.shape)\nprint(X_pub.shape)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:13:14.224299Z","iopub.execute_input":"2023-04-18T06:13:14.225365Z","iopub.status.idle":"2023-04-18T06:13:14.231536Z","shell.execute_reply.started":"2023-04-18T06:13:14.225326Z","shell.execute_reply":"2023-04-18T06:13:14.230307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Attack Part","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/pasquini-dario/SplitNN_FSHA.git\nos.chdir(\"SplitNN_FSHA\")","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:13:14.241969Z","iopub.execute_input":"2023-04-18T06:13:14.243010Z","iopub.status.idle":"2023-04-18T06:13:16.086957Z","shell.execute_reply.started":"2023-04-18T06:13:14.242916Z","shell.execute_reply":"2023-04-18T06:13:16.085662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport tqdm\nimport matplotlib.pyplot as plt\nimport SplitNN_FSHA\n\nfrom SplitNN_FSHA import datasets as datasets\nfrom SplitNN_FSHA import architectures as architectures\nfrom SplitNN_FSHA import FSHA as FSHA # Attack\nfrom SplitNN_FSHA import FSHA_distcor as FSHA_distcor # Defense Technique\n","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:13:16.088840Z","iopub.execute_input":"2023-04-18T06:13:16.089509Z","iopub.status.idle":"2023-04-18T06:13:17.527739Z","shell.execute_reply.started":"2023-04-18T06:13:16.089460Z","shell.execute_reply":"2023-04-18T06:13:17.526616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 4\nid_setup = 4\nhparams = {\n    'WGAN' : True,\n    'gradient_penalty' : 500.,\n    'style_loss' : None,\n    'lr_f' :  0.00001,\n    'lr_tilde' : 0.00001,\n    'lr_D' : 0.0001,\n}\n\nfsha_attack = FSHA.FSHA(XD_priv, XD_pub, id_setup-1, batch_size, hparams)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:13:17.529387Z","iopub.execute_input":"2023-04-18T06:13:17.529791Z","iopub.status.idle":"2023-04-18T06:13:18.066543Z","shell.execute_reply.started":"2023-04-18T06:13:17.529750Z","shell.execute_reply":"2023-04-18T06:13:18.065374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_frequency = 500\niterations = 10000\nLOG1 = fsha_attack(iterations, verbose=True, progress_bar=False, log_frequency=log_frequency)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:15:20.583589Z","iopub.execute_input":"2023-04-18T06:15:20.584205Z","iopub.status.idle":"2023-04-18T06:17:39.811371Z","shell.execute_reply.started":"2023-04-18T06:15:20.584150Z","shell.execute_reply":"2023-04-18T06:17:39.810238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_log(ax, x, y, label):\n    ax.plot(x, y, color='black')\n    ax.set(title=label)\n    ax.grid()\n\nn = 4\nfix, ax = plt.subplots(1, n, figsize=(n*5, 3))\nx = np.arange(0, len(LOG1)) * log_frequency \n\nplot_log(ax[0], x, LOG1[:, 0], label='Loss $f$')\nplot_log(ax[1], x, LOG1[:, 1],  label='Loss $\\\\tilde{f}$ and $\\\\tilde{f}^{-1}$')\nplot_log(ax[2], x, LOG1[:, 2],  label='Loss $D$')\nplot_log(ax[3], x, LOG1[:, 3],  label='Reconstruction error (VALIDATION)')","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:17:39.813642Z","iopub.execute_input":"2023-04-18T06:17:39.814019Z","iopub.status.idle":"2023-04-18T06:17:40.496386Z","shell.execute_reply.started":"2023-04-18T06:17:39.813980Z","shell.execute_reply":"2023-04-18T06:17:40.495317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Save Results","metadata":{}},{"cell_type":"code","source":"results_dic = {\"X_Axis\":[],\n               \"Mode\":[],\n               \"Loss $f$\":[],\n               \"Loss $\\\\tilde{f}$ and $\\\\tilde{f}^{-1}$\":[],\n               \"Loss $D$\":[],\n               \"Reconstruction error (VALIDATION)\":[]}\n\ndef update_results(results_dic, LOG, log_frequency = 500, mode = \"Attack\"):\n    results_dic[\"X_Axis\"].append(np.arange(0, len(LOG)) * log_frequency)\n    results_dic[\"Mode\"].append(mode)\n    results_dic[\"Loss $f$\"].append(list(LOG[:, 0]))\n    results_dic[\"Loss $\\\\tilde{f}$ and $\\\\tilde{f}^{-1}$\"].append(list(LOG[:, 1]))\n    results_dic[\"Loss $D$\"].append(list(LOG[:, 2]))\n    results_dic[\"Reconstruction error (VALIDATION)\"].append(list(LOG[:, 3]))\n    \n    return results_dic\n\n\nresults_dic = update_results(results_dic, LOG1, log_frequency = 500, mode = \"Attack\")\ndf = pd.DataFrame(results_dic)\ndf.to_csv(\"/kaggle/working/Attack_Logs.csv\")\nprint(\"ATTACK LOG SAVED!\")","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:17:54.170060Z","iopub.execute_input":"2023-04-18T06:17:54.170779Z","iopub.status.idle":"2023-04-18T06:17:54.185635Z","shell.execute_reply.started":"2023-04-18T06:17:54.170739Z","shell.execute_reply":"2023-04-18T06:17:54.184045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Malicious Reconstruction","metadata":{}},{"cell_type":"code","source":"n = 5\nX = SplitNN_FSHA.datasets.getImagesDS(XD_priv, n)\nX_recovered_attack, control = fsha_attack.attack(X)\n\ndef plot(X, title):\n    n = len(X)\n    X = (X+1)/2\n#     plt.title(\"Cunt\")\n    fig, ax = plt.subplots(1, n, figsize=(n*3,3))\n    fig.suptitle(title)\n    plt.axis('off')\n    plt.subplots_adjust(wspace=0, hspace=-.05)\n    for i in range(n):\n        ax[i].imshow((X[i]), cmap='inferno');  \n        ax[i].set(xticks=[], yticks=[])\n        ax[i].set_aspect('equal')\n        \n    return fig\n\nfig = plot(X, \"Original\")\nplt.savefig('/kaggle/working/Original1.pdf')\nplt.savefig('/kaggle/working/Original1.png')\nfig = plot(X_recovered_attack, \"Reconstruction: FSHA Attack\")\nplt.savefig('/kaggle/working/Reconstruction_Attack.png')\nplt.savefig('/kaggle/working/Reconstruction_Attack.pdf')","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:27:28.331077Z","iopub.execute_input":"2023-04-18T06:27:28.331794Z","iopub.status.idle":"2023-04-18T06:27:30.574967Z","shell.execute_reply.started":"2023-04-18T06:27:28.331754Z","shell.execute_reply":"2023-04-18T06:27:30.574098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_prub = np.random.rand(15, 3, 32, 32)\n\ndef reshape_correctly(X_prub):\n    shap = X_prub.shape\n    shap_new = (shap[0], shap[2], shap[3], shap[1])\n    X_prub = X_prub.reshape(shap_new)\n    \n    return X_prub\n\nX_prub = reshape_correctly(X_prub)\n\nSplitNN_FSHA.datasets.plot(X_prub)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:17:56.825963Z","iopub.execute_input":"2023-04-18T06:17:56.827244Z","iopub.status.idle":"2023-04-18T06:17:57.458153Z","shell.execute_reply.started":"2023-04-18T06:17:56.827205Z","shell.execute_reply":"2023-04-18T06:17:57.457098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Defense Part","metadata":{}},{"cell_type":"code","source":"batch_size = 4\nid_setup = 4\nhparams = {\n    \n    'alpha1' : 10000, # Extremely high value for alpha1\n    'alpha2' : 50,    # Attacker scales adversarial loss to overwrite alpha1\n    \n    'WGAN' : True,\n    'gradient_penalty' : 500.,\n    'style_loss' : None,\n    'lr_f' :  0.00001,\n    'lr_tilde' : 0.00001,\n    'lr_D' : 0.0001,\n}\n\nfsha_defense = FSHA_distcor.FSHA_dc(XD_priv, XD_pub, id_setup-1, batch_size, hparams)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:17:57.459880Z","iopub.execute_input":"2023-04-18T06:17:57.460626Z","iopub.status.idle":"2023-04-18T06:17:57.878679Z","shell.execute_reply.started":"2023-04-18T06:17:57.460572Z","shell.execute_reply":"2023-04-18T06:17:57.877447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_frequency = 500\niterations = 10000\nLOG2 = fsha_defense(iterations, verbose=True, progress_bar=False, log_frequency=log_frequency)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:17:57.881389Z","iopub.execute_input":"2023-04-18T06:17:57.882029Z","iopub.status.idle":"2023-04-18T06:19:55.706795Z","shell.execute_reply.started":"2023-04-18T06:17:57.881987Z","shell.execute_reply":"2023-04-18T06:19:55.705631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_log(ax, x, y, label):\n    ax.plot(x, y, color='black')\n    ax.set(title=label)\n    ax.grid()\n\nn = 4\nfix, ax = plt.subplots(1, n, figsize=(n*5, 3))\nx = np.arange(0, len(LOG2)) * log_frequency \n\nplot_log(ax[0], x, LOG2[:, 0], label='Loss $f$')\nplot_log(ax[1], x, LOG2[:, 1],  label='Loss $\\\\tilde{f}$ and $\\\\tilde{f}^{-1}$')\nplot_log(ax[2], x, LOG2[:, 2],  label='Loss $D$')\nplot_log(ax[3], x, LOG2[:, 3],  label='Reconstruction error (VALIDATION)')","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:19:55.708461Z","iopub.execute_input":"2023-04-18T06:19:55.708882Z","iopub.status.idle":"2023-04-18T06:19:56.301496Z","shell.execute_reply.started":"2023-04-18T06:19:55.708841Z","shell.execute_reply":"2023-04-18T06:19:56.300369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results_dic = {\"X_Axis\":[],\n               \"Mode\":[],\n               \"Loss $f$\":[],\n               \"Loss $\\\\tilde{f}$ and $\\\\tilde{f}^{-1}$\":[],\n               \"Loss $D$\":[],\n               \"Reconstruction error (VALIDATION)\":[]}\n\nresults_dic = update_results(results_dic, LOG2, log_frequency = 500, mode = \"Defense\")\ndf2 = pd.DataFrame(results_dic)\ndf2.to_csv(\"/kaggle/working/Defense_Logs.csv\")\nprint(\"DEFENSE LOG SAVED!\")","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:19:56.303339Z","iopub.execute_input":"2023-04-18T06:19:56.304109Z","iopub.status.idle":"2023-04-18T06:19:56.315917Z","shell.execute_reply.started":"2023-04-18T06:19:56.304057Z","shell.execute_reply":"2023-04-18T06:19:56.314850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Malicious Reconstruction with Defense Technique","metadata":{}},{"cell_type":"code","source":"n = 7\nX = SplitNN_FSHA.datasets.getImagesDS(XD_priv, n)\n\nX_recovered_defense, control = fsha_defense.attack(X)\n\ndef plot(X, title):\n    n = len(X)\n    X = (X+1)/2\n#     plt.title(\"Cunt\")\n    fig, ax = plt.subplots(1, n, figsize=(n*3,3))\n    fig.suptitle(title)\n    plt.axis('off')\n    plt.subplots_adjust(wspace=0, hspace=-.05)\n    for i in range(n):\n        ax[i].imshow((X[i]), cmap='inferno');  \n        ax[i].set(xticks=[], yticks=[])\n        ax[i].set_aspect('equal')\n        \n    return fig\n\n\nfig = plot(X, \"Original\")\nplt.savefig('/kaggle/working/Original2.pdf')\nplt.savefig('/kaggle/working/Original2.png')\nfig = plot(X_recovered_defense, \"Reconstruction: DC Defense\")\nplt.savefig('/kaggle/working/Reconstruction_Defense.png')\nplt.savefig('/kaggle/working/Reconstruction_Defense.pdf')","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:29:40.951861Z","iopub.execute_input":"2023-04-18T06:29:40.952512Z","iopub.status.idle":"2023-04-18T06:29:43.813114Z","shell.execute_reply.started":"2023-04-18T06:29:40.952473Z","shell.execute_reply":"2023-04-18T06:29:43.812211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plot(X, \"Original\")\nfig = plot(X_recovered_attack, \"Reconstruction: FSHA Attack\")\nfig = plot(X_recovered_defense, \"Reconstruction: DistCor Defense\")","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:27:47.024537Z","iopub.execute_input":"2023-04-18T06:27:47.024942Z","iopub.status.idle":"2023-04-18T06:27:48.339967Z","shell.execute_reply.started":"2023-04-18T06:27:47.024909Z","shell.execute_reply":"2023-04-18T06:27:48.339092Z"},"trusted":true},"execution_count":null,"outputs":[]}]}