{"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 torch torchvision torchaudio\n!pip install breaching\n!pip install datasets transformers\n!pip install transformers\n!pip install datasets\n!pip install timm==0.6.7\n!pip install hydra-core\n!pip install scipy","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-14T20:37:41.714739Z","iopub.execute_input":"2023-06-14T20:37:41.715106Z","iopub.status.idle":"2023-06-14T20:39:20.972583Z","shell.execute_reply.started":"2023-06-14T20:37:41.715076Z","shell.execute_reply":"2023-06-14T20:39:20.971431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    import breaching\nexcept ModuleNotFoundError:\n    # You only really need this safety net if you want to run these notebooks directly in the examples directory\n    # Don't worry about this if you installed the package or moved the notebook to the main directory.\n    import os; os.chdir(\"..\")\n    import breaching\n    \nimport torch\nimport matplotlib.pyplot as plt\n%load_ext autoreload\n%autoreload 2\n\n# Redirects logs directly into the jupyter notebook\nimport logging, sys\nlogging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler(sys.stdout)], format='%(message)s')\nlogger = logging.getLogger()","metadata":{"execution":{"iopub.status.busy":"2023-06-14T20:41:48.500311Z","iopub.execute_input":"2023-06-14T20:41:48.501018Z","iopub.status.idle":"2023-06-14T20:41:48.569965Z","shell.execute_reply.started":"2023-06-14T20:41:48.500985Z","shell.execute_reply":"2023-06-14T20:41:48.568696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg = breaching.get_config(overrides=[\"case/server=malicious-fishing\", \"attack=clsattack\"])\n          \ndevice = torch.device(f'cuda') if torch.cuda.is_available() else torch.device('cpu')\ntorch.backends.cudnn.benchmark = cfg.case.impl.benchmark\nsetup = dict(device=device, dtype=getattr(torch, cfg.case.impl.dtype))\nsetup","metadata":{"execution":{"iopub.status.busy":"2023-06-14T20:41:54.040806Z","iopub.execute_input":"2023-06-14T20:41:54.041192Z","iopub.status.idle":"2023-06-14T20:41:54.629272Z","shell.execute_reply.started":"2023-06-14T20:41:54.041163Z","shell.execute_reply":"2023-06-14T20:41:54.628458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg.case.data.partition = \"unique-class\" # This is the worst-case for the attack, as each user owns a unique class\ncfg.case.user.num_data_points = 50 # maximum number of data points in the validation set\ncfg.case.user.user_idx = 1\n\ncfg.case.user.provide_labels = True # Mostly out of convenience\ncfg.case.server.target_cls_idx = 0 # Which class to attack, if multiple are present?","metadata":{"execution":{"iopub.status.busy":"2023-06-14T20:42:42.223709Z","iopub.execute_input":"2023-06-14T20:42:42.224056Z","iopub.status.idle":"2023-06-14T20:42:42.283714Z","shell.execute_reply.started":"2023-06-14T20:42:42.224027Z","shell.execute_reply":"2023-06-14T20:42:42.282785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd .. \n%cd root\n!mkdir data\n%cd data \n!mkdir imagenet\n%cd imagenet\n!wget -O ILSVRC2012_devkit_t12.tar.gz https://image-net.org/data/ILSVRC/2012/ILSVRC2012_devkit_t12.tar.gz\n!wget -O ILSVRC2012_img_val.tar https://image-net.org/data/ILSVRC/2012/ILSVRC2012_img_val.tar","metadata":{"execution":{"iopub.status.busy":"2023-06-14T21:05:54.192033Z","iopub.execute_input":"2023-06-14T21:05:54.192423Z","iopub.status.idle":"2023-06-14T21:06:05.06111Z","shell.execute_reply.started":"2023-06-14T21:05:54.192388Z","shell.execute_reply":"2023-06-14T21:06:05.059434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user, server, model, loss_fn = breaching.cases.construct_case(cfg.case, setup)\nattacker = breaching.attacks.prepare_attack(server.model, server.loss, cfg.attack, setup)\nbreaching.utils.overview(server, user, attacker)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T21:15:09.476459Z","iopub.execute_input":"2023-06-14T21:15:09.476895Z","iopub.status.idle":"2023-06-14T21:16:41.795596Z","shell.execute_reply.started":"2023-06-14T21:15:09.476856Z","shell.execute_reply":"2023-06-14T21:16:41.794601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"[shared_data], [server_payload], true_user_data = server.run_protocol(user)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T21:17:32.263033Z","iopub.execute_input":"2023-06-14T21:17:32.264045Z","iopub.status.idle":"2023-06-14T21:17:49.17747Z","shell.execute_reply.started":"2023-06-14T21:17:32.26399Z","shell.execute_reply":"2023-06-14T21:17:49.176523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user.plot(true_user_data)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T21:18:03.510449Z","iopub.execute_input":"2023-06-14T21:18:03.510859Z","iopub.status.idle":"2023-06-14T21:18:10.276579Z","shell.execute_reply.started":"2023-06-14T21:18:03.510826Z","shell.execute_reply":"2023-06-14T21:18:10.275286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from breaching.cases.malicious_modifications.classattack_utils import print_gradients_norm, cal_single_gradients\nsingle_gradients, single_losses = cal_single_gradients(user.model, loss_fn, true_user_data, setup=setup)\nprint_gradients_norm(single_gradients, single_losses)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T21:18:26.991954Z","iopub.execute_input":"2023-06-14T21:18:26.992329Z","iopub.status.idle":"2023-06-14T21:18:27.873325Z","shell.execute_reply.started":"2023-06-14T21:18:26.992297Z","shell.execute_reply":"2023-06-14T21:18:27.87154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reconstructed_user_data, stats = attacker.reconstruct([server_payload], [shared_data], \n                                                      server.secrets, dryrun=cfg.dryrun)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T21:18:45.311211Z","iopub.execute_input":"2023-06-14T21:18:45.312191Z","iopub.status.idle":"2023-06-14T21:34:06.995563Z","shell.execute_reply.started":"2023-06-14T21:18:45.312151Z","shell.execute_reply":"2023-06-14T21:34:06.994554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fished_data = dict(data=reconstructed_user_data[\"data\"][server.secrets[\"ClassAttack\"][\"target_indx\"]][None], \n                   labels=None)\nuser.plot(fished_data)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T21:34:25.865785Z","iopub.execute_input":"2023-06-14T21:34:25.866155Z","iopub.status.idle":"2023-06-14T21:34:26.181369Z","shell.execute_reply.started":"2023-06-14T21:34:25.866125Z","shell.execute_reply":"2023-06-14T21:34:26.180468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics = breaching.analysis.report(reconstructed_user_data, true_user_data, [server_payload], \n                                    server.model, order_batch=True, compute_full_iip=False, \n                                    cfg_case=cfg.case, setup=setup)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T21:35:17.787175Z","iopub.execute_input":"2023-06-14T21:35:17.787566Z","iopub.status.idle":"2023-06-14T21:36:58.037959Z","shell.execute_reply.started":"2023-06-14T21:35:17.787535Z","shell.execute_reply":"2023-06-14T21:36:58.036884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user.plot(reconstructed_user_data)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T21:37:26.176739Z","iopub.execute_input":"2023-06-14T21:37:26.177123Z","iopub.status.idle":"2023-06-14T21:37:30.698785Z","shell.execute_reply.started":"2023-06-14T21:37:26.177092Z","shell.execute_reply":"2023-06-14T21:37:30.697236Z"},"trusted":true},"execution_count":null,"outputs":[]}]}