Пример #1
0
Файл: model.py Проект: wkiri/MTE
    def forward(self, item):

        sent_inputids = to_device(item["sent_inputids"], self.gpu_id)
        sent_attention_masks = to_device(item["sent_attention_masks"],
                                         self.gpu_id)
        starts1 = to_device(item["bert_starts"], self.gpu_id)
        ends1 = to_device(item["bert_ends"], self.gpu_id)

        last_hidden_states, cls_embds = self.bert_encoder(
            sent_inputids, attention_mask=sent_attention_masks)

        batch_size, seq_len, dimension = last_hidden_states.size(
        )  # get (batch, 2*dimension), [start_embedding, end_embedding]

        start_indices1 = starts1.view(batch_size, -1).repeat(
            1, dimension).unsqueeze(1)  # (batch, 1, dimension)
        start_embeddings1 = torch.gather(last_hidden_states,
                                         1, start_indices1).view(
                                             batch_size,
                                             -1)  # shape (batch, dimension)

        end_indices1 = ends1.view(batch_size, -1).repeat(
            1, dimension).unsqueeze(1)  # (batch, 1, dimension)
        end_embeddings1 = torch.gather(
            last_hidden_states, 1,
            end_indices1).view(batch_size, -1)  # shape (batch, dimension)

        logits = self.linear(
            self.layernorm(
                torch.cat((start_embeddings1, end_embeddings1, cls_embds), 1)))

        return logits
Пример #2
0
    def transfer_loss(self, x, labels):
        loss = torch.zeros([1], device=self.args_dict['device'])

        for arch, current_model in zip(self.similarity_coeffs.keys(), self.surrogate_models):
            predictions = predict(to_device(current_model, self.args_dict['device']), x)

            to_device(current_model, 'cpu')

            current_loss = self.criterion(predictions, labels)
            loss = torch.add(loss, self.optimization_direction * self.similarity_coeffs[arch] * current_loss)

        return loss
Пример #3
0
    def selective_transfer(self, image_batch, mask_batch, targets, step):
        model_scores = dict(zip(self.available_surrogates_list, [0] * len(self.available_surrogates_list)))
        mse_criterion = torch.nn.MSELoss(reduction='mean')
        batch_indices = torch.arange(image_batch.size(0))

        step.eps = self.args_dict['sigma']

        x = image_batch.clone().detach().requires_grad_(False)
        if self.args_dict['targeted']:
            original_labels = torch.argmax(predict(self.model, x), dim=1)
        else:
            original_labels = targets

        x = torch.cat([x.unsqueeze(0)] * self.args_dict['num_transformations'])
        x = step.random_perturb(x, mask_batch)

        predictions = []
        labels = []
        for current_x in x:
            predictions.append(predict(self.model, current_x))
            current_labels = torch.argmax(predictions[-1], dim=1)
            labels.append(current_labels)

            self.args_dict['label_shifts'] += torch.sum(~torch.eq(current_labels, original_labels)).item()

        for arch in self.available_surrogates_list:
            current_model = get_model(arch, 'standard', freeze=True, device=self.args_dict['device']).eval()

            for index, current_x in enumerate(x):
                current_predictions = predict(current_model, current_x)
                current_loss = mse_criterion(current_predictions[batch_indices, labels[index]],
                                             predictions[index][batch_indices, labels[index]])
                model_scores[arch] += current_loss.item()

            to_device(current_model, 'cpu')

        surrogates_list = [arch
                           for arch in sorted(model_scores, key=model_scores.get)
                           [:self.args_dict['num_surrogates']]]

        if self.args_dict['similarity_coeffs']:
            scores_reversed = torch.FloatTensor([model_scores[arch] for arch in surrogates_list][::-1])
            coeffs = (scores_reversed / torch.sum(scores_reversed)).tolist()
        else:
            coeffs = [1 / len(surrogates_list)] * len(surrogates_list)

        self.similarity_coeffs = (dict(zip(surrogates_list, coeffs)))
        ALL_SIMILARITY_COEFFS.append(self.similarity_coeffs)

        surrogate_models = [get_model(arch, pretrained=True, freeze=True).eval()
                            for arch in surrogates_list]
        return surrogate_models
Пример #4
0
    def __init__(self,
                 corenlp_server_url,
                 ner_model_file,
                 containee_model_file,
                 container_model_file,
                 gpu_id=0):
        """
        Args:
            containee_model_file: 
                pretrained model file (.ckpt) for Containee 
            
            container_model_file:
                pretrained model file (.ckpt) for Container  
            
            gpu_id:
                id of GPU. Negative gpu_id means no GPU to be used. 
        """
        # super(UnaryParser, self).__init__(corenlp_server_url,ner_model_file,'jsre_parser')

        self.corenlp_server_url = corenlp_server_url
        self.ner_model_file = ner_model_file
        self.containee_model_file = containee_model_file
        self.container_model_file = container_model_file
        self.containee = None
        self.container = None
        self.gpu_id = gpu_id

        logging.info('Loading pretrained Containee')
        self.containee = to_device(self.load_unary_model('Containee'),
                                   self.gpu_id)
        self.containee.eval()

        logging.info('Loading pretrained Container')
        self.container = to_device(self.load_unary_model('Container'),
                                   self.gpu_id)
        self.container.eval()
Пример #5
0
def main():
    args_dict = normalize_args_dict(get_args_dict())

    print('Running PGD experiment with the following arguments:')
    print(str(args_dict) + '\n')

    if args_dict['seed'] is not None:
        torch.manual_seed(args_dict['seed'])

    if args_dict['checkpoint_location'] is None:
        model = get_model(arch=args_dict['arch'], pretrained=True, freeze=True, device=args_dict['device']).eval()
    else:
        model = to_device(load_model(location=args_dict['checkpoint_location'],
                                     arch=args_dict['arch'],
                                     from_robustness=args_dict['from_robustness']).eval(),
                          'cuda')

    attacker = Attacker(model, args_dict)

    print('Loading dataset...')
    if args_dict['masks']:
        loader = torch.load(args_dict['dataset'])
    else:
        dataset = load_imagenet(args_dict['dataset'])
        loader, _ = dataset.make_loaders(workers=10, batch_size=args_dict['batch_size'])
    print('Finished!\n')

    mask_batch = 1
    total_num_samples = 0
    adversarial_examples_list = []
    predictions_list = []

    print('Starting PGD...')
    for index, batch in enumerate(loader):
        if args_dict['masks']:
            image_batch, mask_batch = batch
            image_batch.unsqueeze_(0)
            mask_batch.unsqueeze_(0)

            label_batch = torch.argmax(predict(model, to_device(image_batch, args_dict['device'])), dim=1)
            if mask_batch.size != image_batch.size():
                mask_batch = 1
            else:
                mask_batch = to_device(mask_batch, device=args_dict['device'])
        else:
            image_batch, label_batch = batch

        image_batch = to_device(image_batch, device=args_dict['device'])
        label_batch = to_device(label_batch, device=args_dict['device'])

        if not args_dict['targeted'] and not args_dict['masks']:
            predicted_label_batch = torch.argmax(predict(model, image_batch), dim=1)
            matching_labels = torch.eq(label_batch, predicted_label_batch)
            num_matching_labels = torch.sum(matching_labels)
            if num_matching_labels == 0:
                continue

            image_batch, label_batch = (image_batch[matching_labels],
                                        label_batch[matching_labels])

            if mask_batch != 1:
                mask_batch = mask_batch[matching_labels]

            targets = label_batch
        else:
            targets = TARGET_CLASS * torch.ones_like(label_batch)

        adversarial_examples = attacker(image_batch, mask_batch, targets, False)
        adversarial_predictions = predict(model, adversarial_examples)

        adversarial_examples_list.append(to_device(adversarial_examples, device='cpu'))
        predictions_list.append({'original': to_device(targets, device='cpu'),
                                 'adversarial': to_device(adversarial_predictions, device='cpu')})

        total_num_samples += image_batch.size(0)
        if total_num_samples >= args_dict['num_samples']:
            break

    args_dict['num_samples'] = total_num_samples
    print('Finished!')

    print('Serializing results...')
    torch.save({'adversarial_examples': adversarial_examples_list,
                'predictions': predictions_list,
                'similarity': ALL_SIMILARITY_COEFFS,
                'args_dict': args_dict},
               args_dict['save_file_location'])
    print('Finished!\n')