Example #1
0
    def translate(self,
                  indices: torch.Tensor,
                  reduce_token: Optional[str] = None,
                  keep_sos: bool = False,
                  keep_eos: bool = False,
                  keep_pad: bool = False
                  ) -> Union[List[str], List[List[str]]]:
        r"""
        Translate a prediction tensor to its corresponding strings

        Args:
            indices: a tensor of shape :math:`(B, L)` where :math:`B` is the batch size,
                :math:`L` is the sequence length.
            reduce_token: a token to concatenate over :math:`L` dim. *None* to not concat. Default is *None*.
            keep_sos: whether to keep the start of sequence token after translation. Default is `False`.
            keep_eos: whether to keep the end of sequence token after translation. Default is `False`.
            keep_pad: whether to keep the padding token after translation. Default is `False`.

        Return:
            a list of size :math:`B`, each item is a string if *reduce_token* is not *None*, or a list of tokens
        """
        indices = indices.cpu()

        sos_mask = indices == self.SOS_IDX                      # [B, L]
        eos_mask = indices == self.EOS_IDX                      # [B, L]

        sos_pos = sos_mask.max(-1)[1]                           # [B]
        eos_pos = eos_mask.max(-1)[1]                           # [B]

        # TODO: implement keep_pad

        if not keep_sos:
            sos_pos += 1
        sos_pos.masked_fill_(torch.bitwise_not(torch.any(sos_mask, dim=-1)), 0)

        if keep_eos:
            eos_pos += 1
        eos_pos.masked_fill_(torch.bitwise_not(torch.any(eos_mask, dim=-1)), indices.size(1))

        outs: Union[List[str], List[List[str]]] = []

        for indices_, start, end in zip(indices.tolist(), sos_pos, eos_pos):
            tokens = self.lookup_tokens(indices_[start:end])
            if reduce_token is None:
                outs.append(tokens)
            else:
                outs.append(reduce_token.join(tokens))

        return outs
    def _masked_mean(self,
                     vector: torch.Tensor,
                     mask: torch.Tensor,
                     dim: int,
                     keepdim: bool = False,
                     eps: float = 1e-8) -> torch.Tensor:
        """
        To calculate mean along certain dimensions on masked values

        Parameters
        ----------
        vector : ``torch.Tensor``
            The vector to calculate mean.
        mask : ``torch.Tensor``
            The mask of the vector. It must be broadcastable with vector.
        dim : ``int``
            The dimension to calculate mean
        keepdim : ``bool``
            Whether to keep dimension
        eps : ``float``
            A small value to avoid zero division problem.

        Returns
        -------
        A ``torch.Tensor`` of including the mean values.
        """
        one_minus_mask = torch.bitwise_not(mask).to(dtype=torch.bool)
        replaced_vector = vector.masked_fill(one_minus_mask, 0.0)

        value_sum = torch.sum(replaced_vector, dim=dim, keepdim=keepdim)
        value_count = torch.sum(mask.float(), dim=dim, keepdim=keepdim)
        return value_sum / value_count.clamp(min=eps)
    def batch_argmax_adl_Q_predictionNetwork_given_State(self, states_batch):
        """

        :param state:
        :return: argmax(over action) Q(state, action, theta)
        """
        # possible_actions = sorted(self.batch_adl_give_possible_actions(state[2]))
        # output = np.expand_dims(self.prediction_adl_model.forward(torch.FloatTensor(state)).cpu().detach().numpy(),
        #                         axis=0)
        output = self.prediction_adl_model.forward(states_batch)
        output = output + 1.0 - torch.min(output, dim=1).values.unsqueeze_(1)
        batchsize = output.shape[0]
        actionsize = output.shape[1]
        action_indices = torch.arange(0, actionsize).unsqueeze_(0).repeat([batchsize,1])

        # only when an action index bit specifies 1 and this adl load is already satisfied, then this
        # action is not possible so its FLAG is false
        adl_states_batch = states_batch[:,2]
        action_FLAGS_batch = (action_indices & \
        torch.bitwise_not(adl_states_batch.long().unsqueeze_(1).repeat([1,actionsize])) == 0)

        output = output * action_FLAGS_batch
        indexes_batch = torch.argmax(output, dim=1)

        return indexes_batch
Example #4
0
    def _output(self):
        #每个anchor box 对应哪个gt,
        #如果没有anchor box 没有对应的gt,下面这个也会max出一个来
        anchorGtMat = self.anchorGtMat.type(torch.int8)
        anchorBoxIndexGtBox = torch.argmax(anchorGtMat,
                                           dim=1)  #anchor box对应gt bbox的索引
        anchorBoxGtClass = self.labelsGt[
            anchorBoxIndexGtBox]  #anchor box对应gt bbox的标签

        #解决上面的问题, 去掉没有对应上的anchor box
        indexFliter = self.anchorGtMat.any(dim=1)  #.bool()
        posIndex = torch.nonzero(indexFliter).reshape(-1)
        indexFliter = torch.bitwise_not(indexFliter)

        #进行过滤
        anchorBoxGtClass[indexFliter] = -1
        anchorBoxIndexGtBox[indexFliter] = -1
        anchorBoxGt = torch.zeros_like(self.bboxesAnchor)
        anchorBoxGt[posIndex] = self.bboxesGt[anchorBoxIndexGtBox[posIndex]]

        infos = {
            'anchorBoxGt': anchorBoxGt,
            'anchorBoxGtClass': anchorBoxGtClass,
            'anchorBoxIndexGtBox': anchorBoxIndexGtBox,
            'posIndex': posIndex
        }
        return infos
Example #5
0
    def forward(self, hidden_output_nodes : torch.Tensor, input_nodes : torch.Tensor,
                node_mask : torch.Tensor) -> torch.Tensor:
        """
        Defines forward pass.
        """
        Softmax = torch.nn.Softmax(dim=1)

        batch_size = input_nodes.shape[0]
        energy_mask = torch.bitwise_not(node_mask).float() * self.C.big_negative

        lstm_input = torch.zeros(batch_size, self.memory_size, device=self.constants.device)

        cat = torch.cat((hidden_output_nodes, input_nodes), dim=2)
        memory = self.embedding_matrix(cat)

        hidden_state = torch.zeros(batch_size, self.memory_size, device=self.constants.device)
        cell_state = torch.zeros(batch_size, self.memory_size, device=self.constants.device)

        for _ in range(self.lstm_computations):
            query, cell_state = self.lstm(lstm_input, (hidden_state, cell_state))

            # dot product query x memory
            energies = (query.view(batch_size, 1, self.memory_size) * memory).sum(dim=-1)
            attention = Softmax(energies + energy_mask)
            read = (attention.unsqueeze(-1) * memory).sum(dim=1)

            hidden_state = query
            lstm_input = read

        cat = torch.cat((query, read), dim=1)
        return cat
    def func_dawson_2nd(self, x: Tensor) -> Tensor:
        y = torch.zeros_like(x)
        idx1 = torch.lt(x, -10.)
        idx2 = torch.gt(x, 10.)

        y[idx1] = self.func_asym_neg_inf(x[idx1])
        y[idx2] = self.func_asym_pos_inf(x[idx2])

        idx1 = torch.bitwise_not(torch.bitwise_or(idx1, idx2))
        y[idx1] = chebyshev_val_neg(-x[idx1].abs_(),
                                    self.cheb_neg,
                                    num_sub=self.div)

        idx1 = torch.bitwise_and(idx1, x > 0)
        if x.is_cuda:
            if x[idx1].numel() < mnn_config.get_value('cpu_or_gpu'):
                device = x.device
                temp = torch.from_numpy(scipy.erfi(
                    x[idx1].cpu().numpy())).to(device=device)
                y[idx1] = math.sqrt(math.pi) * torch.exp(torch.pow(x[idx1], 2)) * \
                          (0.5 * math.log(2) + 2 * self.dawson1(-x[idx1]) + math.pi / 2 * temp) - y[idx1]
            else:
                y[idx1] = math.sqrt(math.pi) * torch.exp(torch.pow(x[idx1], 2)) * \
                          (0.5 * math.log(2) + 2 * self.dawson1(-x[idx1]) + math.pi / 2 * self.dawson1.erfi(x[idx1])) - \
                          y[idx1]
        else:
            y[idx1] = math.sqrt(math.pi) * torch.exp(torch.pow(x[idx1], 2)) * \
                      (0.5 * math.log(2) + 2 * self.dawson1(-x[idx1]) + math.pi / 2 * torch.from_numpy(
                          scipy.erfi(x[idx1].numpy()))) - y[idx1]
        return y
Example #7
0
    def loss(self, pred_masks, bboxes, gt_masks, img_metas, cfg):
        losses = torch.zeros(1, device=pred_masks[0].device)
        total_pos_num = 0
        final_masks = []
        final_boxes = []
        for pred_mask, bbox, gt_mask, img_meta in zip(pred_masks, bboxes,
                                                      gt_masks, img_metas):
            # pred_mask with size: [N, mask_h, mask_w, 2]; gt_mask with size: [N, h, w]
            assert pred_mask.size(0) == gt_mask.size(0)
            num_pos = pred_mask.size(0)
            if num_pos == 0:
                h, w = img_meta[
                    'pad_shape'][:2] if cfg.upsampling else pred_mask.size(
                    )[1:3]
                final_masks.append(pred_mask.new_zeros(0, h, w))
                final_boxes.append(bbox.new_zeros(0, 4))
                continue
            # The bbox and gt_mask are in 'pad_shape', while pred_mask may be not.
            # So we need rescale the bbox and gt_mask before cropping and calculating losses.
            assert pred_mask.size(1) / img_meta['pad_shape'][
                0] == pred_mask.size(2) / img_meta['pad_shape'][1]
            scale_factor = pred_mask.size(1) / img_meta['pad_shape'][0]
            _bbox = bbox * scale_factor
            if scale_factor != 1:
                gt_mask = F.interpolate(gt_mask.unsqueeze(0),
                                        scale_factor=scale_factor,
                                        mode='bilinear').squeeze(0)
                gt_mask = (gt_mask > 0.5).float()
            assert gt_mask.size()[1:3] == pred_mask.size()[1:3]
            # crop_mask with size: [N, h, w], bool tensor, 1 for pixels in bbox area, 0 for others
            if cfg.crop is not None:
                crop_mask = self.crop(pred_mask, _bbox, cfg.crop)
                gt_mask[torch.bitwise_not(crop_mask)] = -1
            else:
                crop_mask = gt_mask.new_ones(gt_mask.size())

            # pred mask with size: [N, 2, mask_h, mask_w]
            pred_mask = pred_mask.permute(0, 3, 1, 2)
            mask_loss = self.mask_loss(pred_mask, gt_mask, crop_mask,
                                       cfg.mask_loss)
            losses += mask_loss
            total_pos_num += num_pos

            # prepare final_mask and final_bbox
            if cfg.upsampling:
                _bbox = bbox
                pred_mask = F.interpolate(pred_mask,
                                          img_meta['pad_shape'][:2],
                                          mode='bilinear')
            # final_mask should be cropped according to test_cfg
            test_crop_mask = self.crop(pred_mask.permute(0, 2, 3, 1), _bbox,
                                       cfg.test_crop)
            final_mask = F.softmax(pred_mask,
                                   dim=1)[:, 1, :, :] * test_crop_mask.float()
            final_masks.append(final_mask)
            final_boxes.append(_bbox)
        # avoid dividing by zero
        losses = losses / (total_pos_num + 1) * cfg.mask_loss['loss_weight']
        return dict(loss_mask=losses), final_masks, final_boxes
def make_SA_bool(weights, mask, mask1):
    ## Inject errors
    # output = ((weights + mask) > 0.)  # inject stuck at 0
    # output = ((output - mask1)> 0.)   # inject stuck at 1
    not_mask0 = torch.bitwise_not(mask)
    output = torch.bitwise_and(weights, not_mask0)  # inject stuck at 0
    output = torch.bitwise_or(output, mask1)  # inject stuck at 1
    return output
    def forward(self, x: Tensor) -> Tensor:
        idx1 = torch.lt(x, -10)
        idx2 = torch.gt(x, self.cheb_xmas_for_H)
        idx3 = torch.bitwise_and(torch.bitwise_not(idx1), x <= 0)
        idx4 = torch.bitwise_and(torch.bitwise_not(idx2), x > 0)
        y = torch.zeros_like(x)

        y[idx1] = self.func_int_asym_neg_inf(x[idx1])
        y[idx2] = self.func_int_asym_pos_inf(x[idx2])
        y[idx3] = chebyshev_val_neg(x[idx3], self.cheb_H_neg, num_sub=self.div)
        y[idx4] = torch.exp(
            2 * torch.pow(x[idx4], 2)) * chebyshev_val_no_transform(
                x[idx4],
                self.cheb_H_pos,
                x_max=self.cheb_xmas_for_H,
                num_sub=self.div_pos)
        return y
Example #10
0
def build_odims_(x, dims):
    # build indices for other dimensions
    if not isinstance(dims, torch.Tensor):
        dims = torch.tensor(dims).to(x.device)
    odims = torch.arange(0, x.shape[-1]).long().to(dims.device)
    odims = torch.bitwise_not(torch.eq(dims,
                                       odims[:, None])).prod(1).nonzero()[:, 0]
    return odims.detach(), dims
Example #11
0
File: osm.py Project: xbq1994/HAA
    def forward(self, features, targets=None):

        global_feat = self.pool_layer(features)
        bn_feat = self.bnneck(global_feat)
        if not self.training:
            return bn_feat

        bn_feat = F.normalize(bn_feat)
        n = bn_feat.size(0)

        # Compute pairwise distance, replace by the official when merged
        dist = torch.pow(bn_feat, 2).sum(dim=1, keepdim=True).expand(n, n)
        dist = dist + dist.t()
        dist.addmm_(1, -2, bn_feat, bn_feat.t())
        dist = dist.clamp(min=1e-12).sqrt(
        )  # for numerical stability & pairwise distance, dij

        S = torch.exp(-1.0 * torch.pow(dist, 2) /
                      (self.osm_sigma * self.osm_sigma))
        S_ = torch.clamp(
            self.alpha - dist, min=1e-12
        )  # max (0 , \alpha - dij) # 1e-12, 0 may result in nan error

        p_mask = targets.expand(n,
                                n).eq(targets.expand(n,
                                                     n).t())  # same label == 1
        n_mask = torch.bitwise_not(p_mask)  # oposite label == 1

        S = S * p_mask.float()
        S = S + S_ * n_mask.float()

        denominator = torch.exp(F.linear(bn_feat, F.normalize(self.weight)))

        A = []  # attention corresponding to each feature fector
        for i in range(n):
            a_i = denominator[i][targets[i]] / torch.sum(denominator[i])
            A.append(a_i)
        # a_i's
        atten_class = torch.stack(A)
        # a_ij's
        A = torch.min(atten_class.expand(n, n),
                      atten_class.view(-1, 1).expand(
                          n, n))  # pairwise minimum of attention weights

        W = S * A
        W_P = W * p_mask.float()
        W_N = W * n_mask.float()
        W_P = W_P * (
            1 - torch.eye(n, n).float().cuda()
        )  # dist between (xi,xi) not necessarily 0, avoiding precision error
        W_N = W_N * (1 - torch.eye(n, n).float().cuda())

        L_P = 1.0 / 2 * torch.sum(W_P * torch.pow(dist, 2)) / torch.sum(W_P)
        L_N = 1.0 / 2 * torch.sum(W_N * torch.pow(S_, 2)) / torch.sum(W_N)

        L = (1 - self.l) * L_P + self.l * L_N

        return L, global_feat
Example #12
0
    def __init__(self, data=None, device=None, is_test=False):
        """Create a Batch from a list of examples."""

        if data is not None:
            self.batch_size = len(data)
            pre_src = [x[0] for x in data]
            pre_labels = [x[1] for x in data]
            pre_segs = [x[2] for x in data]
            pre_clss = [x[3] for x in data]
            topic_pro = [x[-1] for x in data]
            if not is_test:
                pre_tgt_idxs = [x[4] for x in data]
                pre_decoder_tgt_idxs = [x[5] for x in data]

                tgt_idxs = torch.tensor(self._pad(pre_tgt_idxs, 0))
                decoder_tgt_idxs = torch.tensor(
                    self._pad(pre_decoder_tgt_idxs, 0))

                setattr(self, 'tgt_idxs', tgt_idxs.to(device))
                setattr(self, 'decoder_tgt_idxs', decoder_tgt_idxs.to(device))

            src = torch.tensor(self._pad(pre_src, 0))
            labels = torch.tensor(self._pad(pre_labels, 0))
            segs = torch.tensor(self._pad(pre_segs, 0))
            mask = torch.bitwise_not(src == 0)

            clss = torch.tensor(self._pad(pre_clss, -1))
            mask_cls = torch.bitwise_not(clss == -1)
            clss[clss == -1] = 0

            topic_pro = torch.tensor(topic_pro)

            setattr(self, 'clss', clss.to(device))
            setattr(self, 'mask_cls', mask_cls.to(device))
            setattr(self, 'src', src.to(device))
            setattr(self, 'labels', labels.to(device))
            setattr(self, 'segs', segs.to(device))
            setattr(self, 'mask', mask.to(device))
            setattr(self, 'topic_pro', topic_pro.to(device))

            if (is_test):
                src_str = [x[-3] for x in data]
                setattr(self, 'src_str', src_str)
                tgt_str = [x[-2] for x in data]
                setattr(self, 'tgt_str', tgt_str)
Example #13
0
def bitwise_not(input_):
    """Wrapper of `torch.bitwise_not`.

    Parameters
    ----------
    input_ : DTensor
        Input dense tensor.
    """
    return torch.bitwise_not(input_._data)
Example #14
0
    def forward(self, embeddings, label):
        cos_theta, origin_cos = calc_logits(embeddings, self.kernel)
        cos_theta_, _ = calc_logits(embeddings, self.kernel.detach())

        mask = torch.zeros_like(cos_theta)
        mask.scatter_(1, label.view(-1, 1).long(), 1.0)

        sample_num = embeddings.size(0)
        tmp_cos_theta = cos_theta - 2 * mask
        tmp_cos_theta_ = cos_theta_ - 2 * mask
        target_cos_theta = cos_theta[torch.arange(0, sample_num),
                                     label].view(-1, 1)
        target_cos_theta_ = cos_theta_[torch.arange(0, sample_num),
                                       label].view(-1, 1)

        target_cos_theta_m = target_cos_theta - self.margin

        far = 1 / (self.out_features - 1)
        # far = 1e-4
        topk_mask = torch.greater(tmp_cos_theta, target_cos_theta)
        topk_sum = torch.sum(topk_mask.to(torch.int32))
        dist.all_reduce(topk_sum)
        far_rank = math.ceil(
            far * (sample_num *
                   (self.out_features - 1) * dist.get_world_size() - topk_sum))
        cos_theta_neg_topk = torch.topk(
            (tmp_cos_theta - 2 * topk_mask.to(torch.float32)).flatten(),
            k=far_rank)[0]
        cos_theta_neg_topk = all_gather_tensor(cos_theta_neg_topk.contiguous())
        cos_theta_neg_th = torch.topk(cos_theta_neg_topk, k=far_rank)[0][-1]

        cond = torch.mul(torch.bitwise_not(topk_mask),
                         torch.greater(tmp_cos_theta, cos_theta_neg_th))
        _, cos_theta_neg_topk_index = torch.where(cond)
        cos_theta_neg_topk = torch.mul(cond.to(torch.float32), tmp_cos_theta)
        cos_theta_neg_topk_ = torch.mul(cond.to(torch.float32), tmp_cos_theta_)

        cond = torch.greater(target_cos_theta_m, cos_theta_neg_topk)
        cos_theta_neg_topk = torch.where(cond, cos_theta_neg_topk,
                                         cos_theta_neg_topk_)
        cos_theta_neg_topk = torch.pow(cos_theta_neg_topk, 2)
        times = torch.sum(torch.greater(cos_theta_neg_topk,
                                        0).to(torch.float32),
                          dim=1,
                          keepdim=True)
        times = torch.where(torch.greater(times, 0), times,
                            torch.ones_like(times))
        cos_theta_neg_topk = torch.sum(cos_theta_neg_topk, dim=1,
                                       keepdim=True) / times
        target_cos_theta_m = target_cos_theta_m - (
            1 + target_cos_theta_) * cos_theta_neg_topk

        cos_theta.scatter_(1, label.view(-1, 1).long(), target_cos_theta_m)
        output = cos_theta * self.scale

        return output, origin_cos * self.scale
Example #15
0
    def non_nan(self):

        actual = torch.Tensor(self.actual)
        pred = torch.Tensor(self.pred)

        non_nan_idx = torch.bitwise_not(torch.isnan(pred))
        pred = pred[non_nan_idx].numpy().tolist()
        actual = actual[non_nan_idx].numpy().tolist()

        return pred, actual
Example #16
0
 def _adjust_pred(self, pred_coords, distmaps, gt_coords):
     gt_coords = gt_coords.to(pred_coords.device)
     # 获取mask
     new_masks = self._mask_pred(pred_coords, distmaps)
     # 将mask所有值取反
     new_masks = torch.bitwise_not(new_masks)
     # 获取到预测的坐标
     pred_coords[new_masks] = gt_coords[new_masks]
     # 返回预测的坐标
     return pred_coords
Example #17
0
def mux_p_cuda(x, y, rands):
    #global mask 
    #mask = torch.cuda.ByteTensor([2 ** x for x in range(8)])

    xs = x.shape[0]
    ys = y.shape[0]
    assert xs == ys
    #rands = torch.cuda.FloatTensor(xs << 3).uniform_() > p
    #rands = torch.sum(rands.view(xs, 8) * mask, 1)
    top = torch.bitwise_and(x, rands)
    bot = torch.bitwise_and(y, torch.bitwise_not(rands))
    return torch.bitwise_or(top, bot)
Example #18
0
def _get_dp_dm(distances, targets, plabels, with_indices=False):
    """Returns the d+ and d- values for a batch of distances."""
    matcher = _get_matcher(targets, plabels)
    not_matcher = torch.bitwise_not(matcher)

    inf = torch.full_like(distances, fill_value=float("inf"))
    d_matching = torch.where(matcher, distances, inf)
    d_unmatching = torch.where(not_matcher, distances, inf)
    dp = torch.min(d_matching, dim=-1, keepdim=True)
    dm = torch.min(d_unmatching, dim=-1, keepdim=True)
    if with_indices:
        return dp, dm
    return dp.values, dm.values
Example #19
0
def maj_p_cuda(x, y, rands):
    #FIX THIS ASAP
    #global mask 
    #mask = torch.cuda.ByteTensor([2 ** x for x in range(8)])

    xs = x.shape[0]
    ys = y.shape[0]
    assert xs == ys
    and_ = torch.bitwise_and(x, y)
    or_ = torch.bitwise_or(x, y)
    top = torch.bitwise_and(and_, torch.bitwise_not(rands))
    bot = torch.bitwise_and(or_, rands)
    return torch.bitwise_or(top, bot)
    def __getitem__(self, index):
        # Grab and transform RGB image
        X = Image.open(self.imageList[index]).convert('RGB')
        X = transforms.Compose(self.transform)(X)

        # Get all masks associated with RGB image
        skin_mask = Image.open(self.maskList[index]).convert('L')
        skin_mask = transforms.Compose(self.transform)(skin_mask)
        skin_mask = skin_mask.type(torch.BoolTensor)
        no_skin_mask = torch.bitwise_not(skin_mask)
        # Construct Construct masks of both classes
        y = torch.cat([no_skin_mask, skin_mask], dim=0)
        return X.float(), y.float()
Example #21
0
    def forward(self, input: List[torch.Tensor], target: List[torch.Tensor]):
        loss = 0
        weight_sum = 0
        for i, (_input, _target) in enumerate(zip(input, target)):
            if self.weights:
                weight = self.weights[i]
            else:
                weight = 1
            _target = _target.to(_input.device)
            mask = torch.bitwise_not(torch.isnan(_target))
            loss += weight * self.torch_loss(_input[mask], _target[mask])
            weight_sum += weight

        return loss / weight_sum
Example #22
0
def unique_and_padding(mat, padding_idx, dim=-1):
    """Conducts unique operation along dim and pads to the same length."""
    samples, _ = torch.sort(mat, dim=dim)
    samples_roll = torch.roll(samples, -1, dims=dim)
    samples_diff = samples - samples_roll
    samples_diff[:,
                 -1] = 1  # deal with the edge case that there is only one unique sample in a row
    samples_mask = torch.bitwise_not(samples_diff == 0)  # unique mask
    samples *= samples_mask.to(dtype=samples.dtype)
    samples += (1 - samples_mask.to(dtype=samples.dtype)) * padding_idx
    samples, _ = torch.sort(samples, dim=dim)
    # shrink size to max unique length
    samples = torch.unique(samples, dim=dim)
    return samples
Example #23
0
    def forward(self, input: Dict[str, torch.Tensor],
                target: Dict[str, torch.Tensor]):
        loss = 0
        weight_sum = 0
        for key, _target in target.items():
            if key not in input:
                continue
            weight = self.weights.get(key, 1)
            mask = torch.bitwise_not(torch.isnan(_target))
            _target = _target.to(input[key].device)
            loss += weight * self.torch_loss(input[key][mask], _target[mask])
            weight_sum += weight

        return loss / weight_sum
Example #24
0
        def del_word(
            output_tokens,
            output_scores,
            attn: Optional[Tensor],
            word_del_attn: Optional[Tensor],
            word_del_out,
            can_del_word,
            pad_idx: int,
            bos_idx: int,
            eos_idx: int,
        ):
            # delete words
            # do not delete tokens if it is <s> </s>
            if can_del_word.sum() != 0:  # we cannot delete, skip
                word_del_score = F.log_softmax(word_del_out, 2)
                word_del_pred = word_del_score.max(-1)[1].to(torch.bool)
                in_tokens = output_tokens[can_del_word]
                in_scores = output_scores[can_del_word]
                # apply deletion to a tensor
                in_masks = in_tokens.ne(pad_idx)
                bos_eos_masks = in_tokens.eq(bos_idx) + in_tokens.eq(eos_idx)

                max_len = in_tokens.size(1)
                word_del_pred.masked_fill_(torch.bitwise_not(in_masks), 1)
                word_del_pred.masked_fill_(bos_eos_masks, 0)

                reordering = (torch.arange(max_len, device=in_tokens.device)[
                    None, :].expand_as(in_tokens).contiguous().masked_fill(
                        word_del_pred, max_len).sort(1)[1])

                _tokens = in_tokens.masked_fill(word_del_pred,
                                                pad_idx).gather(1, reordering)

                _scores = in_scores.masked_fill(word_del_pred,
                                                0).gather(1, reordering)
                if word_del_attn is not None:
                    _mask = word_del_pred[:, :, None].expand_as(word_del_attn)
                    _reordering = reordering[:, :,
                                             None].expand_as(word_del_attn)
                    _attn = word_del_attn.masked_fill(_mask, 0.0).gather(
                        1, _reordering)
                    attn = _fill(attn, can_del_word, _attn, 0)

                output_tokens = coalesce(
                    _fill(output_tokens, can_del_word, _tokens, pad_idx),
                    output_tokens)
                output_scores = coalesce(
                    _fill(output_scores, can_del_word, _scores, 0),
                    output_scores)
            return output_tokens, output_scores, attn
Example #25
0
def _get_dp_dm(distances, targets, plabels):
    matcher = torch.eq(targets.unsqueeze(dim=1), plabels)
    if plabels.ndim == 2:
        # if the labels are one-hot vectors
        nclasses = targets.size()[1]
        matcher = torch.eq(torch.sum(matcher, dim=-1), nclasses)
    not_matcher = torch.bitwise_not(matcher)

    inf = torch.full_like(distances, fill_value=float('inf'))
    d_matching = torch.where(matcher, distances, inf)
    d_unmatching = torch.where(not_matcher, distances, inf)
    dp = torch.min(d_matching, dim=1, keepdim=True).values
    dm = torch.min(d_unmatching, dim=1, keepdim=True).values
    return dp, dm
Example #26
0
def glvq_loss(distances, target_labels, prototype_labels):
    """GLVQ loss function with support for one-hot labels."""
    matcher = torch.eq(target_labels.unsqueeze(dim=1), prototype_labels)
    if prototype_labels.ndim == 2:
        # if the labels are one-hot vectors
        nclasses = target_labels.size()[1]
        matcher = torch.eq(torch.sum(matcher, dim=-1), nclasses)
    not_matcher = torch.bitwise_not(matcher)

    inf = torch.full_like(distances, fill_value=float('inf'))
    distances_to_wpluses = torch.where(matcher, distances, inf)
    distances_to_wminuses = torch.where(not_matcher, distances, inf)
    dpluses = torch.min(distances_to_wpluses, dim=1, keepdim=True).values
    dminuses = torch.min(distances_to_wminuses, dim=1, keepdim=True).values

    mu = (dpluses - dminuses) / (dpluses + dminuses)
    return mu
Example #27
0
    def forward(self, x, encoder_padding_mask):
        residual = x
        x = self.maybe_layer_norm(x, before=True)
        converted_mask = None
        if encoder_padding_mask is not None:
            x = x.masked_fill(
                encoder_padding_mask.transpose(0, 1).unsqueeze(2), 0)
            if self.left_pad:
                x, converted_mask = convert_padding_direction(
                    x, encoder_padding_mask, left_to_right=True)
        max_len, bsz, _ = x.size()
        src_lengths = torch.bitwise_not(converted_mask).long().sum(dim=1).data.tolist() if \
                        converted_mask is not None else [max_len for _ in range(bsz)]
        packed_x = nn.utils.rnn.pack_padded_sequence(x, src_lengths)

        if self.bidirectional:
            state_size = 2 * self.num_layers, bsz, self.hidden_dim
        else:
            state_size = self.num_layers, bsz, self.hidden_dim
        h0 = x.new_zeros(*state_size)
        c0 = x.new_zeros(*state_size)
        packed_outs, (final_hiddens,
                      final_cells) = self.lstm(packed_x, (h0, c0))
        x, _ = nn.utils.rnn.pad_packed_sequence(packed_outs, padding_value=0)
        if encoder_padding_mask is not None:
            if self.left_pad:
                x, _ = convert_padding_direction(x,
                                                 converted_mask,
                                                 right_to_left=True)
        assert list(x.size()) == [max_len, bsz, self.output_units]
        x = self.linear(x)
        x = F.dropout(x, p=self.dropout, training=self.training)
        x = residual + x
        x = self.maybe_layer_norm(x, after=True)

        if self.bidirectional:

            def combine_bidir(outs):
                out = outs.view(self.num_layers, 2, bsz,
                                -1).transpose(1, 2).contiguous()
                return out.view(self.num_layers, bsz, -1)

            final_hiddens = combine_bidir(final_hiddens)
            final_cells = combine_bidir(final_cells)

        return x, {"lstm_hidden_state": (final_hiddens, final_cells)}
Example #28
0
    def forward(self, top_vecs, mask):
        """ See :obj:`EncoderBase.forward()`"""

        batch_size, n_sents = top_vecs.size(0), top_vecs.size(1)
        pos_emb = self.pos_emb.pe[:, :n_sents]
        x = top_vecs * mask[:, :, None].float()
        x = x + pos_emb

        for i in range(self.num_inter_layers):
            x = self.transformer_inter[i](
                i, x, x,
                torch.bitwise_not(mask))  # all_sents * max_tokens * dim

        x = self.layer_norm(x)
        sent_scores = self.sigmoid(self.wo(x))
        sent_scores = sent_scores.squeeze(-1) * mask.float()

        return sent_scores
Example #29
0
def cnn_kernel_3x3(img, kernel, N):
    """Unipolar kernel convolve -> AND gates
       Bipolar kernel convolve -> XNOR gates"""
    is_bp = np.any(kernel < 0)

    h, w = img.shape
    rng = bs.SC_RNG()
    rng_type = rng.bs_bp_uniform if is_bp else rng.bs_uniform
    img_bs = img_io.img_to_bs(img, rng_type, bs_len=N) #Correlated at +1
    img_bs = torch.from_numpy(img_bs).to(device)
    rng.reset()

    kernel_bs = img_io.img_to_bs(kernel, rng_type, bs_len=N, scale=False)
    kernel_bs = torch.from_numpy(kernel_bs).to(device)

    nb = N>>3
    rc_mat = torch.cuda.ByteTensor(h-2, w-2, nb).fill_(0)
    m = torch.cuda.ByteTensor(3, 3, nb).fill_(0)
    z = torch.cuda.ByteTensor(nb).fill_(0)
    for i in range(h-2):
        for j in range(w-2):
            for k in range(3):
                for l in range(3):
                    if is_bp:
                        m[k][l] = torch.bitwise_not(torch.bitwise_xor(img_bs[i + k][j + l], kernel_bs[k][l]))
                    else:
                        m[k][l] = torch.bitwise_and(img_bs[i + k][j + l], kernel_bs[k][l])
            #mux sum tree
            l1_1 = mux_p_cuda(m[0][0], m[0][1], 0.5)
            l1_2 = mux_p_cuda(m[0][2], m[1][0], 0.5)
            l1_3 = mux_p_cuda(m[1][1], m[1][2], 0.5)
            l1_4 = mux_p_cuda(m[2][0], m[2][1], 0.5)
            l2_1 = mux_p_cuda(l1_1, l1_2, 0.5)
            l2_2 = mux_p_cuda(l1_3, l1_4, 0.5)
            l3 = mux_p_cuda(l2_1, l2_2, 0.5)
            rc_mat[i][j] = mux_p_cuda(l3, mux_p_cuda(m[2][2], z, 1.0/8.0), 1.0/9.0)
    
    #mean_type = bs.bs_mean_bp if is_bp else bs.bs_mean
    #rc_mat = rc_mat.to(cpu)
    #img_io.disp_img(img_io.bs_to_img(rc_mat, mean_type, scaling=9))
    return bs.get_corr_mat_cuda(rc_mat.view((h-2)*(w-2), nb)).to(cpu).numpy()
Example #30
0
    def __getitem__(self, index):
        preprocess = transforms.Compose([
            transforms.Resize((384, 288), 2),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
        ])

        X = Image.open(self.imageList[index]).convert('RGB')
        X = preprocess(X)

        trfresize = transforms.Resize((384, 288), 2)
        trftensor = transforms.ToTensor()

        yimg = Image.open(self.maskList[index]).convert('L')
        y1 = trftensor(trfresize(yimg))
        y1 = y1.type(torch.BoolTensor)
        y2 = torch.bitwise_not(y1)
        y = torch.cat([y2, y1], dim=0)

        return X, y