Beispiel #1
0
def test_chainer_np():

    for tensor in tensors:
        # regular variable
        assert isinstance(x2num.makenp(tensor), np.ndarray)

    # python primitive type
    assert (isinstance(x2num.makenp(0), np.ndarray))
    assert (isinstance(x2num.makenp(0.1), np.ndarray))
def test_scalar():
    res = x2num.makenp(1.1)
    assert isinstance(res, np.ndarray) and res.shape == (1,)
    res = x2num.makenp(1000000000000000000000)
    assert isinstance(res, np.ndarray) and res.shape == (1,)
    res = x2num.makenp(np.float16(1.00000087))
    assert isinstance(res, np.ndarray) and res.shape == (1,)
    res = x2num.makenp(np.float128(1.00008+9))
    assert isinstance(res, np.ndarray) and res.shape == (1,)
    res = x2num.makenp(np.int64(100000000000))
    assert isinstance(res, np.ndarray) and res.shape == (1,)
Beispiel #3
0
def image(tag, tensor):
    """Outputs a `Summary` protocol buffer with images.
    The summary has up to `max_images` summary values containing images. The
    images are built from `tensor` which must be 3-D with shape `[height, width,
    channels]` and where `channels` can be:
    *  1: `tensor` is interpreted as Grayscale.
    *  3: `tensor` is interpreted as RGB.
    *  4: `tensor` is interpreted as RGBA.
    The `name` in the outputted Summary.Value protobufs is generated based on the
    name, with a suffix depending on the max_outputs setting:
    *  If `max_outputs` is 1, the summary value tag is '*name*/image'.
    *  If `max_outputs` is greater than 1, the summary value tags are
       generated sequentially as '*name*/image/0', '*name*/image/1', etc.
    Args:
      tag: A name for the generated node. Will also serve as a series name in
        TensorBoard.
      tensor: A 3-D `uint8` or `float32` `Tensor` of shape `[height, width,
        channels]` where `channels` is 1, 3, or 4.
    Returns:
      A scalar `Tensor` of type `string`. The serialized `Summary` protocol
      buffer.
    """
    tag = _clean_tag(tag)
    tensor = makenp(tensor, 'IMG')
    tensor = tensor.astype(np.float32)
    tensor = (tensor * 255).astype(np.uint8)
    image = make_image(tensor)
    return Summary(value=[Summary.Value(tag=tag, image=image)])
def test_pytorch_np():

    for tensor in tensors:
        # regular tensor
        assert isinstance(x2num.makenp(tensor), np.ndarray)

        # CUDA tensor
        if torch.cuda.device_count()>0:
            assert isinstance(x2num.makenp(tensor.cuda()), np.ndarray)

        # regular variable
        assert isinstance(x2num.makenp(torch.autograd.variable.Variable(tensor)), np.ndarray)

        # CUDA variable
        if torch.cuda.device_count()>0:
            assert isinstance(x2num.makenp(torch.autograd.variable.Variable(tensor).cuda()), np.ndarray)

    # python primitive type
    assert(isinstance(x2num.makenp(0), np.ndarray))
    assert(isinstance(x2num.makenp(0.1), np.ndarray))
Beispiel #5
0
def test_pytorch_img():
    for s in shapes:
        x = torch.Tensor(np.random.random_sample(s))
        assert x2num.makenp(x, 'IMG').shape[2] == 3
Beispiel #6
0
def test_chainer_img():
    for s in shapes:
        x = chainer.Variable(np.random.random_sample(s))
        assert x2num.makenp(x, 'IMG').shape[2] == 3