def _init_session_manager(self, session_manager=None): if session_manager is None: self._session_manager = SessionManager( local_init_op=self._local_init_op, ready_op=self._ready_op, graph=self._graph, recovery_wait_secs=self._recovery_wait_secs) else: self._session_manager = session_manager
def foolbox_generate_adversarial_example( label: int, create_model, input_fn: Callable[[], tf.Tensor], attack_fn: Callable[..., Attack], model_dir=None, checkpoint_path=None, preprocessing=(0, 1), channel_axis=1, bounds=(0, 1), attack_params={}, **kwargs, ) -> Optional[np.ndarray]: # Check that model has been trained. if not checkpoint_path: checkpoint_path = saver.latest_checkpoint(model_dir) if not checkpoint_path: raise ValueError( "Could not find trained model in model_dir: {}.".format(model_dir)) with tf.Graph().as_default(): features = input_fn() model = create_model() image_tensor = tf.placeholder(features.dtype, features.shape) logits = model(image_tensor) sm = SessionManager() with sm.prepare_session( master="", saver=tf.train.Saver(), checkpoint_filename_with_path=checkpoint_path, config=new_session_config(), ) as sess: image = sess.run(features)[0] attack_model = TensorFlowModel( image_tensor, logits, bounds=bounds, channel_axis=channel_axis, preprocessing=preprocessing, ) if attack_fn is None: return image[np.newaxis] attack = attack_fn(attack_model) # adversarial_example = attack(image, label=label, **kwargs) adversarial_example = attack(image, label=label, **attack_params) # diff = np.abs(image - adversarial_example) # print(diff.max()*255) if adversarial_example is None: return None else: return adversarial_example[np.newaxis]
def generate_adversarial_example( label: int, create_model, input_fn: Callable[[], tf.Tensor], attack_fn: Callable[..., Attack], model_dir=None, checkpoint_path=None, preprocessing=(0, 1), image_id=None, **kwargs, ) -> Optional[np.ndarray]: # Check that model has been trained. if not checkpoint_path: checkpoint_path = saver.latest_checkpoint(model_dir) if not checkpoint_path: raise ValueError( "Could not find trained model in model_dir: {}.".format(model_dir)) with tf.Graph().as_default(): features = input_fn() model = create_model() image_tensor = tf.placeholder(features.dtype, features.shape) logits = model(image_tensor) torch_model = TorchLeNet() path = abspath("lenet_model.pth") torch_model.load_state_dict(torch.load(path)) torch_model.cuda() device_id = torch.cuda.current_device() torch_model.eval() attack_model = foolbox.models.PyTorchModel( torch_model, bounds=(0, 1), num_classes=10, channel_axis=1, preprocessing=preprocessing, ) sm = SessionManager() with sm.prepare_session( master="", saver=tf.train.Saver(), checkpoint_filename_with_path=checkpoint_path, config=new_session_config(), ) as sess: image = sess.run(features)[0] # attack_model = TensorFlowModel( # image_tensor, # logits, # bounds=(0, 1), # channel_axis=1, # preprocessing=preprocessing) # image, _ = torch_mnist.image(image_id, is_val=True) # transform = transforms.Compose([transforms.ToTensor()]) # image = transform(image).cpu().numpy() attack = attack_fn(attack_model) with torch.cuda.device(device_id): adversarial_example = attack(image, label=label, **kwargs) # adversarial_example = attack(image[0], label=label, **kwargs) if adversarial_example is None: return None else: return adversarial_example[np.newaxis]