コード例 #1
0
ファイル: interp1d.py プロジェクト: dmarnerides/boardom
 def _check_input(self, z, name):
     if bd.is_array(z):
         z = torch.from_numpy(z)
     if not bd.is_tensor(z):
         raise RuntimeError(
             f'Expected PyTorch Tensor or Numpy Array for {name}.')
     return z.view(-1)
コード例 #2
0
ファイル: hdr.py プロジェクト: dmarnerides/boardom
    def evaluate(t_in, kind):
        if kind not in KINDS:
            raise RuntimeError(f'Unknown kind for pu_encode: {kind}'
                               f'\nChoices: {KINDS}')
        f = _PU_INTERPOLATOR.f

        is_array = bd.is_array(t_in)
        if kind == 'torchlinear':
            if is_array:
                t_in = torch.from_numpy(t_in)
            t_in = t_in.clamp(1e-5, 1e10).log10()

            # Account for device and dtype for mlp to automatically cast
            if t_in.dtype not in TTYPES:
                raise RuntimeError(
                    f'Invalid dtype: {t_in.dtype}\nExpected one of {TTYPES}')
            key = (kind, t_in.device, t_in.dtype)
            if not f.get(key, False):
                x, y = bd.assets.pu_space
                interpolator = bd.Interp1d(x, y)
                interpolator.to(key[1])
                interpolator.type(key[2])
                f[key] = interpolator
            interpolator = f[key]
        else:
            if not is_array:
                t_in = t_in.numpy()
            t_in = np.log10(t_in.clip(1e-5, 1e10))

            key = (kind, t_in.dtype)
            if not f.get(key, False):
                x, y = bd.assets.pu_space
                x, y = x.numpy().astype(key[1]), y.numpy().astype(key[1])
                f[key] = scipy.interpolate.interp1d(x, y, kind=kind)
            interpolator = f[key]

        result = interpolator(t_in)
        if is_array and (not bd.is_array(result)):
            result = result.numpy()
        if (not is_array) and bd.is_array(result):
            result = torch.from_numpy(result)
        return result
コード例 #3
0
ファイル: csvplot.py プロジェクト: dmarnerides/boardom
 def _set_labels():
     if bd.is_array(axes):
         for i, a in enumerate(axes):
             a.set_xlabel(opt_dict['xlabel'][i] if isinstance(
                 opt_dict['xlabel'], list) else opt_dict['xlabel']
                          if opt_dict['xlabel'] is not None else 'x')
             a.set_ylabel(opt_dict['ylabel'][i] if isinstance(
                 opt_dict['ylabel'], list) else opt_dict['ylabel']
                          if opt_dict['ylabel'] is not None else 'y')
     else:
         axes.set_xlabel(
             opt_dict['xlabel'] if opt_dict['xlabel'] is not None else 'x')
         axes.set_ylabel(
             opt_dict['ylabel'] if opt_dict['ylabel'] is not None else 'y')
コード例 #4
0
def make_grid(
    images,
    view=None,
    size=None,
    inter_pad=0,
    fill_value=0,
    scale_each=False,
):
    """Creates a single image grid from a set of images.

    Args:
        images (Tensor, Array, list or tuple): Torch Tensor(s) and/or Numpy Array(s).
        view (str, optional): The image view e.g. 'hwc-bgr' or 'torch'
            (default 'torch').
        color (bool, optional): Treat images as colored or not (default True).
        size (list or tuple, optional): Grid dimensions, rows x columns. (default None).
        inter_pad (int or list/tuple, optional): Padding separating the images (default None).
        fill_value (int, optional): Fill value for inter-padding (default 0).
        scale_each (bool, optional): Scale each image to [0-1] (default False).

    Returns:
        Tensor or Array: The resulting grid. If any of the inputs is an Array
        then the result is an Array, otherwise a Tensor.

    Notes:
        - Images of **different sizes are padded** to match the largest.
        - Works for **color** (3 channels) or **grey** (1 channel/0 channel)
          images.
        - Images must have the same view (e.g. chw-rgb (torch))
        - The Tensors/Arrays can be of **any dimension >= 2**. The last 2 (grey)
          or last 3 (color) dimensions are the images and all other dimensions
          are stacked. E.g. a 4x5x3x256x256 (torch view) input will be treated:

            - As 20 3x256x256 color images if color is True.
            - As 60 256x256 grey images if color is False.

        - If color is False, then only the last two channels are considered
          (as hw) thus any colored images will be split into their channels.
        - The image list can contain both **Torch Tensors and Numpy Arrays**.
          at the same time as long as they have the same view.
        - If size is not given, the resulting grid will be the smallest square
          in which all the images fit. If the images are more than the given
          size then the default smallest square is used.

    Raises:
        TypeError: If images are not Arrays, Tensors, a list or a tuple
        ValueError: If channels or dimensions are wrong.

    """

    view = view or bd.default_view
    # Determine view
    orig_view = bd.determine_view(view)

    # Flag if we need to convert back to array
    should_convert_to_array = False

    images = [x for x in bd.recurse_get_elements(images)]
    images = [x for x in images if bd.is_array(x) or bd.is_tensor(x)]
    should_convert_to_array = any([bd.is_array(x) for x in images])
    images = [_make_tensor_4d_with_3_channels(x, orig_view) for x in images]
    if not images:
        return None
    # Pad images to match largest
    if len(images) > 1:
        maxh, maxw = (
            max([x.size(-2) for x in images]),
            max([x.size(-1) for x in images]),
        )
        for i, img in enumerate(images):
            imgh, imgw = img.size(-2), img.size(-1)
            if (img.size(-2) < maxh) or (img.size(-1) < maxw):
                padhl = int((maxh - imgh) / 2)
                padhr = maxh - imgh - padhl
                padwl = int((maxw - imgw) / 2)
                padwr = maxw - imgw - padwl
                images[i] = torch.nn.functional.pad(
                    img, (padwl, padwr, padhl, padhr), value=fill_value)
    images = torch.cat(images, 0)

    # Scale each
    if scale_each:
        for i in range(images.size(0)):
            images[i] = bd.map_range(images[i])

    # Create grid
    b, c, im_w, im_h = (
        images.size()[0],
        images.size()[1],
        images.size()[2],
        images.size()[3],
    )
    # Get number of columns and rows (width and height)
    if (size is not None and b > size[0] * size[1]) or size is None:
        n_row = int(np.ceil(np.sqrt(b)))
        n_col = int(np.ceil(b / n_row))
    else:
        n_col = size[0]
        n_row = size[1]

    if isinstance(inter_pad, int):
        inter_pad = (inter_pad, inter_pad)

    w_pad, h_pad = inter_pad[1], inter_pad[0]
    total_w_padding = max(w_pad, 0) * (n_col - 1)
    total_h_padding = max(h_pad, 0) * (n_row - 1)

    w = int(im_w * n_col) + total_w_padding
    h = int(im_h * n_row) + total_h_padding
    grid = torch.Tensor(c, w, h).type_as(images).fill_(fill_value)
    for i in range(b):
        i_row = i % n_row
        i_col = int(i / n_row)
        grid[:, i_col * (im_w + w_pad):(i_col) * (im_w + w_pad) + im_w, i_row *
             (im_h + h_pad):(i_row) * (im_h + h_pad) + im_h, ].copy_(images[i])

    if should_convert_to_array:
        grid = bd.to_array(grid)
    if orig_view != 'torch':
        grid = bd.change_view(grid, 'torch', orig_view)
    return grid