Python torch.masked_select() Examples

The following are 30 code examples of torch.masked_select(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torch , or try the search function .
Example #1
Source File: pairwise.py    From airlab with Apache License 2.0 6 votes vote down vote up
def forward(self, displacement):

        # compute displacement field
        displacement = self._grid + displacement

        # compute current mask
        mask = super(MSE, self).GetCurrentMask(displacement)

        # warp moving image with dispalcement field
        self.warped_moving_image = F.grid_sample(self._moving_image.image, displacement)

        # compute squared differences
        value = (self.warped_moving_image - self._fixed_image.image).pow(2)

        # mask values
        value = th.masked_select(value, mask)

        return self.return_loss(value) 
Example #2
Source File: training_fucntion_generator.py    From gan-toolkit with MIT License 6 votes vote down vote up
def forward(self, prob, target, reward):
        """
        Args:
            prob: (N, C), torch Variable 
            target : (N, ), torch Variable
            reward : (N, ), torch Variable
        """
        N = target.size(0)
        C = prob.size(1)
        one_hot = torch.zeros((N, C))
        if prob.is_cuda:
            one_hot = one_hot.cuda()
        one_hot.scatter_(1, target.data.view((-1,1)), 1)
        one_hot = one_hot.type(torch.ByteTensor)
        one_hot = Variable(one_hot)
        if prob.is_cuda:
            one_hot = one_hot.cuda()
        loss = torch.masked_select(prob, one_hot)
        loss = loss * reward
        loss =  -torch.sum(loss)
        return loss 
Example #3
Source File: nnBuildUnits.py    From medSynthesisV1 with MIT License 6 votes vote down vote up
def forward(self, preds, targets):
        """
            Args:
                inputs:(n, h, w, d)
                targets:(n, h, w, d)  
        """
        assert not targets.requires_grad
        assert preds.shape == targets.shape,'dim of preds and targets are different'

        
        dist = torch.abs(preds - targets).view(-1)
#         
        baseV = targets.view(-1)
        
        baseV = torch.abs(baseV + self.eps)
          
        relativeDist = torch.div(dist, baseV)
        
        mask = relativeDist.ge(self.threshold)
        
        largerLossVec = torch.masked_select(dist, mask)
            
        loss = torch.mean(largerLossVec)

        return loss 
Example #4
Source File: bert_tagger.py    From mrc-for-flat-nested-ner with Apache License 2.0 6 votes vote down vote up
def forward(self, input_ids, token_type_ids=None, attention_mask=None, 
        labels=None, input_mask=None):

        last_bert_layer, pooled_output = self.bert(input_ids, token_type_ids, attention_mask, \
            output_all_encoded_layers=False)
        last_bert_layer = last_bert_layer.view(-1, self.hidden_size)
        last_bert_layer = self.dropout(last_bert_layer)
        logits = self.classifier(last_bert_layer) 

        if labels is not None:
            loss_fct = CrossEntropyLoss()
            if input_mask is not None:
                masked_logits = torch.masked_select(logits, input_mask)
                loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1)) 
            else:
                loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
            return loss 
        else:
            return logits 
Example #5
Source File: utils.py    From NeuralBabyTalk with MIT License 6 votes vote down vote up
def forward(self, txt_input, vis_input, target):
        target_copy = target.clone()

        vis_mask = Variable((target.data > self.vocab_size)).view(-1,1)
        txt_mask = target.data.gt(0)  # generate the mask
        txt_mask = torch.cat([txt_mask.new(txt_mask.size(0), 1).fill_(1), txt_mask[:, :-1]], 1)
        txt_mask[target.data > self.vocab_size] = 0

        vis_out = - torch.masked_select(vis_input, vis_mask)
        target.data[target.data > self.vocab_size]=0
        # truncate to the same size

        target = target.view(-1,1)
        txt_select = torch.gather(txt_input, 1, target)
        if isinstance(txt_input, Variable):
            txt_mask = Variable(txt_mask)
        txt_out = - torch.masked_select(txt_select, txt_mask.view(-1,1))

        loss = (torch.sum(txt_out)+torch.sum(vis_out)).float() / (torch.sum(txt_mask.data) + torch.sum(vis_mask.data)).float()

        return loss 
Example #6
Source File: run_kitti.py    From PIXOR with MIT License 6 votes vote down vote up
def postprocess(self, pred):
        cls_pred = pred[..., 0]
        activation = cls_pred > self.config['cls_threshold']
        num_boxes = int(activation.sum())

        if num_boxes == 0:
            print("No bounding box found")
            return [], []

        corners = torch.zeros((num_boxes, 8))
        for i in range(1, 9):
            corners[:, i - 1] = torch.masked_select(pred[i, ...], activation)
        corners = corners.view(-1, 4, 2).numpy()
        scores = torch.masked_select(cls_pred, activation).cpu().numpy()

        # NMS
        selected_ids = non_max_suppression(corners, scores, self.config['nms_iou_threshold'])
        corners = corners[selected_ids]
        scores = scores[selected_ids]

        return corners, scores 
Example #7
Source File: utils.py    From NeuralBabyTalk with MIT License 6 votes vote down vote up
def forward(self, seq, bn_seq, fg_seq, seqLogprobs, bnLogprobs, fgLogprobs, reward):

        seqLogprobs = seqLogprobs.view(-1)
        reward = reward.view(-1)
        fg_seq = fg_seq.squeeze()
        seq_mask = torch.cat((seq.new(seq.size(0),1).fill_(1).byte(), seq.gt(0)[:,:-1]),1).view(-1) #& fg_seq.eq(0)).view(-1)
        seq_mask = Variable(seq_mask)
        seq_out = - torch.masked_select(seqLogprobs * reward, seq_mask)
        seq_out = torch.sum(seq_out) / torch.sum(seq_mask.data)

        bnLogprobs = bnLogprobs.view(-1)
        bn_mask = fg_seq.gt(0).view(-1)
        bn_mask = Variable(bn_mask)

        bn_out = - torch.masked_select(bnLogprobs * reward, bn_mask)
        bn_out = torch.sum(bn_out) / max(torch.sum(bn_mask.data),1)

        fgLogprobs = fgLogprobs.view(-1)
        fg_out = - torch.masked_select(fgLogprobs * reward, bn_mask)
        fg_out = torch.sum(fg_out) / max(torch.sum(bn_mask.data),1)

        return seq_out, bn_out, fg_out 
Example #8
Source File: utils.py    From NeuralBabyTalk with MIT License 6 votes vote down vote up
def forward(self, input, target):

        target = target.view(-1,1)-1
        bn_mask = target.data.ne(-1)  # generate the mask
        if isinstance(input, Variable):
            bn_mask = Variable(bn_mask)

        if torch.sum(bn_mask.data) > 0:
            new_target = target.data.clone()
            new_target[new_target<0] = 0
            select = torch.gather(input.view(-1,2), 1, Variable(new_target))

            out = - torch.masked_select(select, bn_mask)
            loss = torch.sum(out).float() / torch.sum(bn_mask.data).float()
        else:
            loss = Variable(input.data.new(1).zero_()).float()

        return loss 
Example #9
Source File: utils.py    From NeuralBabyTalk with MIT License 6 votes vote down vote up
def forward(self, input, target):

        target = target.view(-1,1)
        input = input.view(-1, input.size(2))

        select = torch.gather(input, 1, target)

        attr_mask = target.data.gt(0)  # generate the mask
        if isinstance(input, Variable):
            attr_mask = Variable(attr_mask)

        if torch.sum(attr_mask.data) > 0:
            out = - torch.masked_select(select, attr_mask)
            loss = torch.sum(out).float() / torch.sum(attr_mask.data).float()
        else:
            loss = Variable(input.data.new(1).zero_()).float()

        return loss 
Example #10
Source File: cpm_loss.py    From landmark-detection with MIT License 6 votes vote down vote up
def compute_stage_loss(criterion, target_var, outputs, mask_var, total_labeled_cpm, weight_of_idt):

  total_loss = 0
  each_stage_loss = []
  mask_outputs = []
  for output_var in outputs:
    stage_loss = 0
    output = torch.masked_select(output_var, mask_var)
    target = torch.masked_select(target_var, mask_var)
    mask_outputs.append(output)

    stage_loss = criterion(output, target) / (total_labeled_cpm*2)
    total_loss = total_loss + stage_loss
    each_stage_loss.append(stage_loss.item())
  if weight_of_idt is not None and weight_of_idt > 0:
    pair_loss_a = torch.sum( torch.abs(mask_outputs[0] - mask_outputs[1]) )
    pair_loss_b = torch.sum( torch.abs(mask_outputs[0] - mask_outputs[2]) )
    pair_loss_c = torch.sum( torch.abs(mask_outputs[1] - mask_outputs[2]) )
    identity_loss = weight_of_idt * (pair_loss_a + pair_loss_b + pair_loss_c) / 3
    each_stage_loss.append(identity_loss.item())
    total_loss = total_loss + identity_loss
  return total_loss, each_stage_loss 
Example #11
Source File: likelihood_eval.py    From latent_ode with MIT License 6 votes vote down vote up
def compute_masked_likelihood(mu, data, mask, likelihood_func):
	# Compute the likelihood per patient and per attribute so that we don't priorize patients with more measurements
	n_traj_samples, n_traj, n_timepoints, n_dims = data.size()

	res = []
	for i in range(n_traj_samples):
		for k in range(n_traj):
			for j in range(n_dims):
				data_masked = torch.masked_select(data[i,k,:,j], mask[i,k,:,j].byte())
				
				#assert(torch.sum(data_masked == 0.) < 10)

				mu_masked = torch.masked_select(mu[i,k,:,j], mask[i,k,:,j].byte())
				log_prob = likelihood_func(mu_masked, data_masked, indices = (i,k,j))
				res.append(log_prob)
	# shape: [n_traj*n_traj_samples, 1]

	res = torch.stack(res, 0).to(get_device(data))
	res = res.reshape((n_traj_samples, n_traj, n_dims))
	# Take mean over the number of dimensions
	res = torch.mean(res, -1) # !!!!!!!!!!! changed from sum to mean
	res = res.transpose(0,1)
	return res 
Example #12
Source File: pairwise.py    From airlab with Apache License 2.0 6 votes vote down vote up
def forward(self, displacement):

        # compute displacement field
        displacement = self._grid + displacement

        # compute current mask
        mask = super(NCC, self).GetCurrentMask(displacement)

        self._warped_moving_image = F.grid_sample(self._moving_image.image, displacement)

        moving_image_valid = th.masked_select(self._warped_moving_image, mask)
        fixed_image_valid = th.masked_select(self._fixed_image.image, mask)


        value = -1.*th.sum((fixed_image_valid - th.mean(fixed_image_valid))*(moving_image_valid - th.mean(moving_image_valid)))\
                /th.sqrt(th.sum((fixed_image_valid - th.mean(fixed_image_valid))**2)*th.sum((moving_image_valid - th.mean(moving_image_valid))**2) + 1e-10)

        return value 
Example #13
Source File: util.py    From multee with Apache License 2.0 6 votes vote down vote up
def sentencewise_scores2paragraph_tokenwise_scores(sentences_scores, sentences_mask):
    """
    # Input:
    # sentences_mask: (batch_size X num_sentences X sent_seq_len)
    # sentences_scores: (batch_size X num_sentences)

    # Output:
    # paragraph_tokenwise_scores: (batch_size X max_para_seq_len)
    """
    paragraph_tokenwise_scores = []
    for instance_sentences_scores, instance_sentences_mask in zip(torch.unbind(sentences_scores, dim=0),
                                                                  torch.unbind(sentences_mask, dim=0)):
        instance_paragraph_tokenwise_scores = torch.masked_select(instance_sentences_scores.unsqueeze(-1),
                                                                    instance_sentences_mask.byte())
        paragraph_tokenwise_scores.append(instance_paragraph_tokenwise_scores)
    paragraph_tokenwise_scores = torch.nn.utils.rnn.pad_sequence(paragraph_tokenwise_scores, batch_first=True)
    return paragraph_tokenwise_scores 
Example #14
Source File: modules.py    From KBRD with MIT License 5 votes vote down vote up
def forward(self, input, target):
        # only consider the observed targets
        mask = (target != -1)
        observed_input = torch.masked_select(input, mask)
        observed_target = torch.masked_select(target, mask)
        # increment nb of observed targets
        self.nb_observed_targets += len(observed_target)
        loss = self.mse_loss(observed_input, observed_target)
        return loss 
Example #15
Source File: regularizations.py    From incremental_learning.pytorch with MIT License 5 votes vote down vote up
def _index_mask(tensor, mask):
    return torch.masked_select(tensor, mask) 
Example #16
Source File: splitcross.py    From trellisnet with MIT License 5 votes vote down vote up
def split_on_targets(self, hiddens, targets):
        # Split the targets into those in the head and in the tail
        split_targets = []
        split_hiddens = []

        # Determine to which split each element belongs (for each start split value, add 1 if equal or greater)
        # This method appears slower at least for WT-103 values for approx softmax

        # This is equally fast for smaller splits as method below but scales linearly
        mask = None
        for idx in range(1, self.nsplits):
            partial_mask = targets >= self.splits[idx]
            mask = mask + partial_mask if mask is not None else partial_mask
        ###
        for idx in range(self.nsplits):
            # If there are no splits, avoid costly masked select
            if self.nsplits == 1:
                split_targets, split_hiddens = [targets], [hiddens]
                continue
            # If all the words are covered by earlier targets, we have empties so later stages don't freak out
            if sum(len(t) for t in split_targets) == len(targets):
                split_targets.append([])
                split_hiddens.append([])
                continue
            # Are you in our split?
            tmp_mask = mask == idx
            split_targets.append(torch.masked_select(targets, tmp_mask))
            split_hiddens.append(hiddens.masked_select(tmp_mask.unsqueeze(1).expand_as(hiddens)).view(-1, hiddens.size(1)))
        return split_targets, split_hiddens 
Example #17
Source File: postprocess.py    From PIXOR with MIT License 5 votes vote down vote up
def filter_pred(config, pred):
    if len(pred.size()) == 4:
        if pred.size(0) == 1:
            pred.squeeze_(0)
        else:
            raise ValueError("Tensor dimension is not right")

    cls_pred = pred[0, ...]
    activation = cls_pred > config['cls_threshold']
    num_boxes = int(activation.sum())

    if num_boxes == 0:
        #print("No bounding box found")
        return [], []

    corners = torch.zeros((num_boxes, 8))
    for i in range(7, 15):
        corners[:, i - 7] = torch.masked_select(pred[i, ...], activation)
    corners = corners.view(-1, 4, 2).numpy()
    scores = torch.masked_select(cls_pred, activation).cpu().numpy()

    # NMS
    selected_ids = non_max_suppression(corners, scores, config['nms_iou_threshold'])
    corners = corners[selected_ids]
    scores = scores[selected_ids]

    return corners, scores 
Example #18
Source File: ioueval.py    From lidar-bonnetal with MIT License 5 votes vote down vote up
def addBorderBatch1d(self, range_y, x, y, px, py):
    '''range_y=target as img, x=preds, y=targets, px,py=idxs of points of
       pointcloud in range img
       WARNING: Only batch size 1 works for now
    '''
    # if numpy, pass to pytorch
    # to tensor
    if isinstance(range_y, np.ndarray):
      range_y = torch.from_numpy(np.array(range_y)).long().to(self.device)
    if isinstance(x, np.ndarray):
      x = torch.from_numpy(np.array(x)).long().to(self.device)
    if isinstance(y, np.ndarray):
      y = torch.from_numpy(np.array(y)).long().to(self.device)
    if isinstance(px, np.ndarray):
      px = torch.from_numpy(np.array(px)).long().to(self.device)
    if isinstance(py, np.ndarray):
      py = torch.from_numpy(np.array(py)).long().to(self.device)

    # get border mask of range_y
    border_mask_2d = self.borderer(range_y)

    # filter px, py according to if they are on border mask or not
    border_mask_1d = border_mask_2d[0, py, px].byte()

    # get proper points from filtered x and y
    x_in_mask = torch.masked_select(x, border_mask_1d)
    y_in_mask = torch.masked_select(y, border_mask_1d)

    # add batch
    self.addBatch(x_in_mask, y_in_mask) 
Example #19
Source File: train.py    From Adv-ED with MIT License 5 votes vote down vote up
def disUnconfLoss(words,inMask,maskL,maskR,label):
    cScores=selector(words.cuda(),inMask.cuda(),maskL.cuda(),maskR.cuda())
    #print("DisUnconfloss")
    #print("label")
    #print(label)
    cScores=torch.pow(cScores,alpha)
    cScores=F.softmax(cScores,dim=0)
    #print("cScores")
    #print(cScores)
    dScores=F.sigmoid(discriminator(words.cuda(),inMask.cuda(),maskL.cuda(),maskR.cuda()))
    mask=genMask(label).cuda()
    dScores=torch.masked_select(dScores,mask)
    return -torch.dot(cScores,torch.log(1.0-dScores)) 
Example #20
Source File: util.py    From multee with Apache License 2.0 5 votes vote down vote up
def sentences2paragraph_tensor(sentencewise_tensor, sentences_mask):
    """
    # Input:
    # sentencewise_tensor: (batch_size, num_sentences, sentence_max_seq_len, ...)
    # sentences_mask: (batch_size, num_sentences, sentence_max_seq_len)

    # Output:
    # paragraphwise_tensor: (batch_size, paragraph_max_seq_len, ...)
    """
    num_sentences = sentencewise_tensor.shape[1]
    trailing_shape = list(sentencewise_tensor.shape[3:])

    sentences_mask = sentences_mask.byte()
    # keep unsqueezing instance_sentences_mask at -1 to make it same shape as sentencewise_tensor and then .byte()
    while len(sentences_mask.shape) < len(sentencewise_tensor.shape):
        sentences_mask = sentences_mask.unsqueeze(-1)

    paragraphwise_tensor = []
    for instance_sentencewise_tensor, instance_sentences_mask in zip(torch.unbind(sentencewise_tensor, dim=0),
                                                                     torch.unbind(sentences_mask, dim=0)):
        instance_paragraphwise_tensor = instance_sentencewise_tensor.masked_select(instance_sentences_mask)
        instance_paragraphwise_tensor = instance_paragraphwise_tensor.reshape([-1]+trailing_shape)
        paragraphwise_tensor.append(instance_paragraphwise_tensor)

    paragraphwise_tensor = torch.nn.utils.rnn.pad_sequence(paragraphwise_tensor, batch_first=True)
    return paragraphwise_tensor 
Example #21
Source File: train.py    From Adv-ED with MIT License 5 votes vote down vote up
def disConfLoss(words,inMask,maskL,maskR,label):
    dScores=F.sigmoid(discriminator(words.cuda(),inMask.cuda(),maskL.cuda(),maskR.cuda()))
    #print("DisConfLoss")
    #print("label")
    #print(label)
    mask=genMask(label).cuda()
    #print("mask")
    #print(mask)
    dScores=torch.masked_select(dScores,mask)
    #print("dScores")
    #print(dScores)
    #print(-torch.mean(torch.log(dScores)))
    return -torch.mean(torch.log(dScores)) 
Example #22
Source File: train.py    From Adv-ED with MIT License 5 votes vote down vote up
def Dscore_G(nwords,nMask,nmaskL,nmaskR,nlabel,uwords,uMask,umaskL,umaskR,ulabel):
    nScores=F.sigmoid(discriminator(nwords.cuda(),nMask.cuda(),nmaskL.cuda(),nmaskR.cuda()))
    nScores=nScores[:,nonNAindex]
    nScores=torch.mean(nScores,dim=1)
    #print(uwords.size())
    if uwords.size(0)==0:
        return nScores
    uScores=F.sigmoid(discriminator(uwords.cuda(),uMask.cuda(),umaskL.cuda(),umaskR.cuda()))
    umask=genMask(ulabel).cuda()
    uScores=torch.masked_select(uScores,umask)
    return torch.cat((nScores,uScores),dim=0) 
Example #23
Source File: models.py    From DFace with Apache License 2.0 5 votes vote down vote up
def cls_loss(self,gt_label,pred_label):
        pred_label = torch.squeeze(pred_label)
        gt_label = torch.squeeze(gt_label)
        # get the mask element which >= 0, only 0 and 1 can effect the detection loss
        mask = torch.ge(gt_label,0)
        valid_gt_label = torch.masked_select(gt_label,mask)
        valid_pred_label = torch.masked_select(pred_label,mask)
        return self.loss_cls(valid_pred_label,valid_gt_label)*self.cls_factor 
Example #24
Source File: pairwise.py    From airlab with Apache License 2.0 5 votes vote down vote up
def forward(self, displacement):
        # compute displacement field
        displacement = self._grid + displacement

        # compute current mask
        mask = super(SSIM, self).GetCurrentMask(displacement)
        mask = 1 - mask
        mask = mask.to(dtype=self._dtype, device=self._device)

        self._warped_moving_image = F.grid_sample(self._moving_image.image, displacement)

        mask = F.conv2d(mask, self._kernel)
        mask = mask == 0

        mean_moving_image = F.conv2d(self._warped_moving_image, self._kernel)

        variance_moving_image = F.conv2d(self._warped_moving_image.pow(2), self._kernel) - (
            mean_moving_image.pow(2))

        mean_fixed_moving_image = F.conv2d(self._fixed_image.image * self._warped_moving_image, self._kernel)

        covariance_fixed_moving = (mean_fixed_moving_image - mean_moving_image * self._mean_fixed_image)

        luminance = (2 * self._mean_fixed_image * mean_moving_image + self._c1) / \
                    (self._mean_fixed_image.pow(2) + mean_moving_image.pow(2) + self._c1)

        contrast = (2 * th.sqrt(self._variance_fixed_image + 1e-10) * th.sqrt(
            variance_moving_image + 1e-10) + self._c2) / \
                   (self._variance_fixed_image + variance_moving_image + self._c2)

        structure = (covariance_fixed_moving + self._c3) / \
                    (th.sqrt(self._variance_fixed_image + 1e-10) * th.sqrt(
                        variance_moving_image + 1e-10) + self._c3)

        sim = luminance.pow(self._alpha) * contrast.pow(self._beta) * structure.pow(self._gamma)

        value = -1.0 * th.masked_select(sim, mask)

        return self.return_loss(value) 
Example #25
Source File: pairwise.py    From airlab with Apache License 2.0 5 votes vote down vote up
def forward(self, displacement):

        # compute displacement field
        displacement = self._grid + displacement

        # compute current mask
        mask = super(MI, self).GetCurrentMask(displacement)

        self._warped_moving_image = F.grid_sample(self._moving_image.image, displacement)

        moving_image_valid = th.masked_select(self._warped_moving_image, mask)
        fixed_image_valid = th.masked_select(self._fixed_image.image, mask)

        mask = (fixed_image_valid > self._background_fixed) & (moving_image_valid > self._background_moving)

        fixed_image_valid = th.masked_select(fixed_image_valid, mask)
        moving_image_valid = th.masked_select(moving_image_valid, mask)

        number_of_pixel = moving_image_valid.shape[0]

        sample = th.zeros(number_of_pixel, device=self._fixed_image.device,
                          dtype=self._fixed_image.dtype).uniform_() < self._spatial_samples

        # compute marginal entropy fixed image
        image_samples_fixed = th.masked_select(fixed_image_valid.view(-1), sample)

        ent_fixed_image, p_f = self._compute_marginal_entropy(image_samples_fixed, self._bins_fixed_image)

        # compute marginal entropy moving image
        image_samples_moving = th.masked_select(moving_image_valid.view(-1), sample)

        ent_moving_image, p_m = self._compute_marginal_entropy(image_samples_moving, self._bins_moving_image)

        # compute joint entropy
        p_joint = th.mm(p_f, p_m.transpose(0, 1)).div(self._normalizer_2d)
        p_joint = p_joint / (th.sum(p_joint) + 1e-10)

        ent_joint = -(p_joint * th.log2(p_joint + 1e-10)).sum()

        return -(ent_fixed_image + ent_moving_image - ent_joint) 
Example #26
Source File: pairwise.py    From airlab with Apache License 2.0 5 votes vote down vote up
def _lcc_loss_3d(self, warped_image, mask):

        mean_moving_image = F.conv3d(warped_image, self._kernel)
        variance_moving_image = F.conv3d(warped_image.pow(2), self._kernel) - (mean_moving_image.pow(2))

        mean_fixed_moving_image = F.conv3d(self._fixed_image.image * warped_image, self._kernel)

        cc = (mean_fixed_moving_image - mean_moving_image*self._mean_fixed_image)**2\
             /(variance_moving_image*self._variance_fixed_image + 1e-10)

        mask = F.conv3d(mask, self._kernel)
        mask = mask == 0

        return -1.0 * th.masked_select(cc, mask) 
Example #27
Source File: pairwise.py    From airlab with Apache License 2.0 5 votes vote down vote up
def _lcc_loss_2d(self, warped_image, mask):


        mean_moving_image = F.conv2d(warped_image, self._kernel)
        variance_moving_image = F.conv2d(warped_image.pow(2), self._kernel) - (mean_moving_image.pow(2))

        mean_fixed_moving_image = F.conv2d(self._fixed_image.image * warped_image, self._kernel)

        cc = (mean_fixed_moving_image - mean_moving_image*self._mean_fixed_image)**2 \
             / (variance_moving_image*self._variance_fixed_image + 1e-10)

        mask = F.conv2d(mask, self._kernel)
        mask = mask == 0

        return -1.0*th.masked_select(cc, mask) 
Example #28
Source File: model.py    From Pytorch-NCE with MIT License 5 votes vote down vote up
def forward(self, input, target, length):

        mask = get_mask(length.data, max_len=input.size(1))
        rnn_output = self._rnn(input)
        loss = self.criterion(target, rnn_output)
        loss = torch.masked_select(loss, mask)

        return loss.mean() 
Example #29
Source File: generic_model.py    From Pytorch-NCE with MIT License 5 votes vote down vote up
def forward(self, input, target, length):

        mask = get_mask(length.data, cut_tail=0)

        # <s> is non-sense in this model, thus the loss should be
        # masked manually
        effective_target = target[:, 1:].contiguous()
        loss = self.criterion(effective_target, input)
        loss = torch.masked_select(loss, mask)

        return loss.mean() 
Example #30
Source File: train.py    From Adv-ED with MIT License 5 votes vote down vote up
def disConfLoss(words,pos,loc,maskL,maskR,label):
    dScores=F.sigmoid(discriminator(words.cuda(),pos.cuda(),loc.cuda(),maskL.cuda(),maskR.cuda()))
    mask=genMask(label).cuda()
    dScores=torch.masked_select(dScores,mask)
    return -torch.mean(torch.log(dScores))