Пример #1
0
def to_np_img(t: torch.tensor):
    if len(t.shape) == 4:
        t = t[0]
    if t.shape[-1] == 3 or t.shape[-1] == 2:
        if type(t) is not np.ndarray:
            return t.cpu().detach().numpy()
        return t
    return t.cpu().detach().numpy().transpose(1, 2, 0)
Пример #2
0
    def add(self, output: torch.tensor, target: torch.tensor):
        output = output.cpu().detach().squeeze().numpy()[np.newaxis]
        target = target.cpu().detach().squeeze().numpy()[np.newaxis]

        assert output.ndim == target.ndim, 'target and output do not match'
        assert output.ndim == 1

        self.output = np.hstack([self.output, output])
        self.target = np.hstack([self.target, target])
Пример #3
0
def imshow(img: Tensor) -> None:
    if len(img.shape) > 3:
        img = img.squeeze()
    img = torch.clamp(img / 2 + 0.5, 0, 1)
    npimg = img.cpu().numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()
Пример #4
0
def act(model: Policy, state: torch.tensor):
    with torch.no_grad():
        p = F.softmax(model.forward(state)).cpu().numpy()
        valid_moves = (state.cpu().numpy().reshape(
            3, 3, 3).argmax(axis=2).reshape(-1) == 0)
        p = valid_moves * p
        return p.argmax()
Пример #5
0
 def _get_recalls(y_true: torch.tensor, pred_labels: List) -> List[float]:
     y_true = y_true.cpu().numpy()
     return [
         metrics.recall_score(pred_labels[idx],
                              y_true[:, idx],
                              average='macro') for idx in range(3)
     ]
Пример #6
0
def frequency_based_masking(tokens: torch.tensor,
                            token_sampling_weights: np.ndarray,
                            mask_prob: float) -> torch.Tensor:
    """
    Function to mask tokens based on frequency.

    Inputs:
        1) tokens: Tensor with token ids of shape (batch_size x seq_len)
        2) token_sampling_weights: numpy array with shape (batch_size x seq_len)
            and each element representing the sampling weight assicated with
            the corresponding token in tokens
        3) mask_prob: Probability of masking a particular token

    Outputs:
        mask: Tensor with same shape as input tokens (batch_size x seq_len)
            with masked  tokens represented by a 1 and everything else as 0.
    """
    batch_size, seq_len = tokens.size()
    num_masked_per_batch = int(batch_size * seq_len * mask_prob)

    indices = tokens.cpu().numpy().flatten()

    # get the weights associated with each token
    weights = np.take(token_sampling_weights, indices)

    # sample tokens based on the computed weights
    tokens_to_mask = np.random.choice(len(weights),
                                      num_masked_per_batch,
                                      replace=False,
                                      p=weights / weights.sum())
    mask = torch.zeros(batch_size * seq_len)
    mask[tokens_to_mask] = 1
    mask = mask.view(batch_size, seq_len).long().to(tokens.device)
    return mask
Пример #7
0
def un_normalize(img: torch.tensor, mean: list, std: list):

    size = img.size()
    n_img = img.cpu().view(size[0], size[2], size[3], size[1]).detach().numpy()

    m = np.concatenate(
        (mean[0] *
         np.ones(shape=[n_img.shape[0], n_img.shape[1], n_img.shape[2], 1]),
         mean[1] *
         np.ones(shape=[n_img.shape[0], n_img.shape[1], n_img.shape[2], 1]),
         mean[2] *
         np.ones(shape=[n_img.shape[0], n_img.shape[1], n_img.shape[2], 1])),
        -1)

    s = np.concatenate(
        (std[0] *
         np.ones(shape=[n_img.shape[0], n_img.shape[1], n_img.shape[2], 1]),
         std[1] *
         np.ones(shape=[n_img.shape[0], n_img.shape[1], n_img.shape[2], 1]),
         std[2] *
         np.ones(shape=[n_img.shape[0], n_img.shape[1], n_img.shape[2], 1])),
        -1)

    res_img = np.multiply(n_img + m, s) * 255
    return res_img
Пример #8
0
def rolling_cumulation(data_ts: torch.tensor, window: int) -> torch.tensor:
    """the input is a variation(ratio) of something, like returns"""
    tensor_np = data_ts.cpu().detach().numpy()
    tensor_df = pd.DataFrame(tensor_np)
    output_df = (1 + tensor_df).rolling(window).apply(np.prod, raw=False) - 1
    output_np = np.array(output_df)
    output_ts = torch.tensor(output_np).squeeze()
    return output_ts
Пример #9
0
def rolling_max_drawdown(data_ts: torch.tensor, window: int) -> torch.tensor:
    ''' input is price '''
    tensor_np = data_ts.cpu().detach().numpy()
    tensor_df = pd.DataFrame(tensor_np)
    output_df = tensor_df.rolling(window).apply(_max_drawdown, raw=False)
    output_np = np.array(output_df)
    output_ts = torch.tensor(output_np).squeeze()
    return output_ts
Пример #10
0
def postprocess(img: torch.tensor):

    # img = img[0][2]
    img = torch.argmax(img, dim=1)
    img = img.cpu().numpy()
    img = np.squeeze(img)
    img = re_normalize(img)
    # img = util.invert(img)
    return img
Пример #11
0
def rolling_max_drawdown_from_returns(data_ts: torch.tensor,
                                      window: int) -> torch.tensor:
    ''' input is returns '''
    returns_np = data_ts.cpu().detach().numpy()
    returns_df = pd.DataFrame(returns_np)
    price = (1 + returns_df).cumprod()
    output_df = price.rolling(window).apply(_max_drawdown, raw=False)
    output_np = np.array(output_df)
    output_ts = torch.tensor(output_np).squeeze()
    return output_ts
Пример #12
0
def torch_to_frame(img: torch.tensor) -> np.array:
    img = (img + 1) / 2
    img = torch.clamp(img, 0, 1)
    img = torch.squeeze(img, dim=0)
    img = img.cpu().detach().numpy()
    img = img.transpose(1, 2, 0)
    img = (img * 255).astype(np.uint8)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    return img
def torch2numpy(tensor: torch.tensor, nptype: str = 'float32'):
    cpu_tensor = tensor.cpu().detach()
    arr = cpu_tensor.numpy().astype(nptype)
    if len(arr.shape) == 4:
        transpose = (0, 2, 3, 1)
    elif len(arr.shape) == 3:
        transpose = (1, 2, 0)
    else:
        transpose = (0, 1)
    arr = arr.transpose(transpose)
    return arr
Пример #14
0
def fix_features(features: torch.tensor) -> torch.tensor:
    features = features.cpu()
    features = features.numpy()
    for i in range(0, features.shape[0]):
        for j in range(0, features.shape[1]):
            if features[i][j] <= -100.0:
                features[i][j] = -99.9999
            if features[i][j] >= 100.0:
                features[i][j] = 99.9999
    features = torch.from_numpy(features)
    features = features.to(device)
    return features
Пример #15
0
 def __init__(
         self,
         ref: torch.tensor,  # [H] torch.tensor
         num_actions: int,
         u_min: float,
         u_max: float):
     super(Linear_Actions, self).__init__()
     # us = np.random.uniform(low=u_min, high=u_max, size=(H, num_actions))
     us = np.reshape(ref.cpu().detach().numpy(), (-1, 1))
     us = np.repeat(us, num_actions, axis=1)
     us = np.clip(us, u_min, u_max)
     self.us = torch.nn.Parameter(torch.from_numpy(us).float())
Пример #16
0
 def __init__(
         self,
         ref: torch.tensor,  # [H] torch.tensor
         num_actions: int,
         action_dim: int,
         u_min: float,
         u_max: float):
     super(Actions, self).__init__()
     us = np.reshape(ref.cpu().detach().numpy(), (1, -1, 1))
     us = np.repeat(us, num_actions, axis=0)
     us = np.repeat(us, action_dim, axis=2)
     us = np.clip(us, u_min, u_max)
     self.us = torch.nn.Parameter(torch.from_numpy(us).float())
Пример #17
0
def encode_torch(obj: torch.tensor) -> str:
    """
    Encode a torch tensor as a bytestring.

    Parameters
    ----------
    obj : ``torch.tensor``
        Object to be encoded

    Returns
    -------
    bytestr : ``str``
        bytestring representation of the passed object
    """
    return encode_numpy(obj.cpu().detach().numpy())
Пример #18
0
def get_template_texture(vertices: torch.tensor, faces: torch.tensor,
                         texture_map: torch.tensor):

    device = vertices.device

    verts_uv = convert_3d_to_uv_coordinates(vertices)
    vertex_rgb = torch.nn.functional.grid_sample(
        texture_map.unsqueeze(0), 2 * verts_uv.unsqueeze(0).unsqueeze(0) - 1)
    vertex_rgb = vertex_rgb.squeeze(2).permute(0, 2, 1) * 255
    texture = Textures([texture_map.cpu().permute(1, 2, 0)],
                       faces_uvs=faces.unsqueeze(0),
                       verts_uvs=verts_uv.unsqueeze(0),
                       verts_rgb=vertex_rgb).to(device)

    return texture
Пример #19
0
def onnx_inference(model_path: str, inputs: tensor) -> tensor:
    """Run ONNX inference

    Args:
        model_path (str): Path to the ONNX model.
        inputs (Tensor): Batch of input image.

    Returns:
        Tensor: Network output.
    """
    inputs = np.array(inputs.cpu())
    ort_session = onnxruntime.InferenceSession(model_path)
    ort_inputs = {ort_session.get_inputs()[0].name: inputs}
    ort_outs = ort_session.run(None, ort_inputs)
    return torch.tensor(ort_outs[0])
Пример #20
0
def drawHeatMapV1(hm:torch.tensor,box:torch.tensor,device="cpu"):
    """
    :param hm: torch.tensor  shape [128,128]
    :param box: torch.tensor  [x1,y1,x2,y2] 缩减到 hm 大小上
    :return:
    """
    hm = hm.cpu().numpy()
    x1,y1,x2,y2 = box
    h,w = y2-y1,x2-x1
    cx,cy = (x2+x1)/2.,(y2+y1)/2.
    cx,cy = cx.int().item(),cy.int().item()
    h,w = h.item(),w.item()
    radius = gaussian_radius((h,w))
    # radius = math.sqrt(h*w) # 不推荐
    radius = max(1, int(radius))
    # hm = torch.from_numpy(draw_msra_gaussian(hm, (cx,cy), radius)).to(device) # 等价于 drawHeatMapV2
    hm = torch.from_numpy(draw_umich_gaussian(hm, (cx,cy), radius)).to(device)
    return hm
Пример #21
0
 def transit(self, a: torch.tensor, ref: torch.tensor, threshold: float):
     """
     MDP transition function
     :param a: list of actions, [t1, t2, t3], shape=(3,)
     :param ref: list of references, [ref_q1, ref_q2, ref_q3], shape=(3,)
     :param threshold: tolerance for derivation
     :return:
     """
     self.step(a.cpu())
     r = reward(self.state, ref, threshold)
     if r == 100:  # terminal state
         self.terminate = True
     if self.show:
         self.show_data.append([[self.l1_x, self.l1_y],
                                [self.l2_x, self.l2_y],
                                [self.l3_x, self.l3_y]])
         self.show_a.append(
             [a[0][0].item(), a[0][1].item(), a[0][2].item()])
     return r
Пример #22
0
def tensor2list(cudatensor: torch.tensor) -> List:
    return list(cudatensor.cpu().numpy())
Пример #23
0
 def decode(self, ids: torch.tensor) -> list:
     ids = np.array(torch.squeeze(ids.cpu())).tolist()    # 2D
     return list(map(lambda x: self.tokenizer.convert_ids_to_tokens(x), ids))
Пример #24
0
    def to_numpy(self, t:tensor): return t.detach.cpu().numpy() if t.requires_grad else t.cpu().numpy()

    def predict(self, inps):
Пример #25
0
def to_numpy(torch_tensor: torch.tensor) -> np.array:
    return torch_tensor.cpu().detach().numpy()
Пример #26
0
def imshow(img: Tensor) -> None:
    img = img / 2 + 0.5
    npimg = img.cpu().numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()
def to_numpy(t: tensor):
    return t.detach.cpu().numpy() if t.requires_grad else t.cpu().numpy()
Пример #28
0
def postprocess(img: torch.tensor):
    img = torch.argmax(img, dim=1)  # perform argmax to generate 1 channel
    img = img.cpu().numpy()  # send to cpu and transform to numpy.ndarray
    img = np.squeeze(img)  # remove batch dim and channel dim -> [H, W]
    img = re_normalize(img)  # scale it to the range [0-255]
    return img
Пример #29
0
    def evaluate(self,
                 gt_points: torch.tensor,
                 source_points: torch.tensor,
                 gt_normals=None):
        """computes the chamfer distance between 2 point clouds

        Arguments:
            gt_points {torch.tensor} -- [description]
            source_points {torch.tensor} -- [description]

        Returns:
            [dict] -- [description]
        """
        ##### Computing Chamfer Distances ######
        gt_points = gt_points.cuda().detach()
        source_points = source_points.cuda().detach()
        d_gt2source, d_source2gt, idx3, idx4 = self.cham_loss(
            gt_points.unsqueeze(0), source_points.unsqueeze(0))
        idx3 = idx3.long().squeeze()
        idx4 = idx4.long().squeeze()
        # mean(squared_d(gt->source)) + mean(squared_d(source->gt))
        chamfer_dist = (d_gt2source.mean() + d_source2gt.mean()) / 2
        chamfer_dist_abs = (d_gt2source.sqrt().mean() +
                            d_source2gt.sqrt().mean()) / 2
        out_dict = {}
        out_dict['chamfer_dist'] = chamfer_dist.cpu().item()
        self.eval_results['chamfer_dist'].append(out_dict['chamfer_dist'])
        out_dict['chamfer_dist_abs'] = chamfer_dist_abs.cpu().item()
        self.eval_results['chamfer_dist_abs'].append(
            out_dict['chamfer_dist_abs'])

        ############ PSNR ##############
        if gt_normals is not None:  # Computing PSNR if we have normals
            gt_normals = gt_normals.cuda().detach()
            d_plane_gt2source = torch.sum(
                (gt_points - source_points[idx3, :]) * gt_normals, dim=1)
            d_plane_source2gt = torch.sum(
                (source_points - gt_points[idx4, :]) * gt_normals[idx4, :],
                dim=1)
            chamfer_plane = (d_plane_gt2source.abs().mean() +
                             d_plane_source2gt.abs().mean()) / 2
            out_dict['chamfer_dist_plane'] = chamfer_plane.cpu().item()
            self.eval_results['chamfer_dist_plane'].append(
                out_dict['chamfer_dist_plane'])

        ###### IOU #######
        gt_points_np = gt_points.cpu().numpy()
        source_points_np = source_points.cpu().numpy()

        # print('gt_points shape',g)
        center = (np.max(gt_points_np, axis=0, keepdims=True) +
                  np.min(gt_points_np, axis=0, keepdims=True)) / 2
        resolution = np.array(
            [self.config['evaluation']['iou_grid']['resolution']])
        size_meter = np.array([self.config['grid']['size']])
        gt_grid = occupancy_grid.OccupancyGrid(center=center,
                                               resolution=resolution,
                                               size_meter=size_meter)
        gt_grid.addPoints(gt_points_np)
        source_grid = occupancy_grid.OccupancyGrid(center=center,
                                                   resolution=resolution,
                                                   size_meter=size_meter)
        source_grid.addPoints(source_points_np)

        out_dict['iou'] = occupancy_grid.gridIOU(gt_grid.grid,
                                                 source_grid.grid)
        self.eval_results['iou'].append(out_dict['iou'])

        return out_dict
Пример #30
0
def to_np(t: torch.tensor) -> np.array:
    """Converts a PyTorch tensor to a Numpy array.
    """
    return t.cpu().detach().numpy()