Пример #1
0
    def __call__(self, key, value):
        if self.group is None:
            if not self.strict:
                return value
            else:
                raise ValueError('Inexistent group is specified')
        if not self.strict and key not in self.group:
            return value

        dataset = self.group[key]
        if dataset.shape is None:  # Empty
            return None
        if value is None:
            return numpy.asarray(dataset)
        if isinstance(value, chainerx.ndarray):
            value[...] = _chainerx._array_to_chainerx(numpy.asarray(dataset),
                                                      value.device)
        elif isinstance(value, numpy.ndarray):
            dataset.read_direct(value)
        elif isinstance(value, cuda.ndarray):
            value.set(numpy.asarray(dataset, dtype=value.dtype))
        elif isinstance(value, intel64.mdarray):
            intel64.ideep.basic_copyto(value, numpy.asarray(dataset))
        else:
            value = type(value)(numpy.asarray(dataset))
        return value
Пример #2
0
def copyto(dst, src):
    """Copies the elements of an ndarray to those of another one.

    This function can copy the CPU/GPU arrays to the destination arrays on
    another device.

    Args:
        dst (`numpy.ndarray`, `cupy.ndarray` or `ideep4py.mdarray`):
            Destination array.
        src (`numpy.ndarray`, `cupy.ndarray` or `ideep4py.mdarray`):
            Source array.

    """
    if isinstance(dst, chainerx.ndarray):
        dst[...] = _chainerx._array_to_chainerx(src, dst.device)
    elif isinstance(dst, numpy.ndarray):
        numpy.copyto(dst, _cpu._to_cpu(src))
    elif isinstance(dst, intel64.mdarray):
        intel64.ideep.basic_copyto(dst, _cpu._to_cpu(src))
    elif isinstance(dst, cuda.ndarray):
        if isinstance(src, chainer.get_cpu_array_types()):
            src = numpy.asarray(src)
            if dst.flags.c_contiguous or dst.flags.f_contiguous:
                dst.set(src)
            else:
                cuda.cupy.copyto(dst, cuda.to_gpu(src, device=dst.device))
        elif isinstance(src, cuda.ndarray):
            cuda.cupy.copyto(dst, src)
        else:
            raise TypeError(
                'cannot copy from non-array object of type {}'.format(
                    type(src)))
    else:
        raise TypeError('cannot copy to non-array object of type {}'.format(
            type(dst)))
Пример #3
0
    def __call__(self, key, value):
        key = self.path + key.lstrip('/')
        if not self.strict and key not in self.npz:
            return value

        if isinstance(self.ignore_names, (tuple, list)):
            ignore_names = self.ignore_names
        else:
            ignore_names = (self.ignore_names, )
        for ignore_name in ignore_names:
            if isinstance(ignore_name, str):
                if key == ignore_name:
                    return value
            elif callable(ignore_name):
                if ignore_name(key):
                    return value
            else:
                raise ValueError(
                    'ignore_names needs to be a callable, string or '
                    'list of them.')

        dataset = self.npz[key]
        if dataset[()] is None:
            return None
        if value is None:
            return dataset
        if isinstance(value, chainerx.ndarray):
            value[...] = _chainerx._array_to_chainerx(numpy.asarray(dataset),
                                                      value.device)
        elif isinstance(value, numpy.ndarray):
            numpy.copyto(value, dataset)
        elif isinstance(value, cuda.ndarray):
            value.set(numpy.asarray(dataset, dtype=value.dtype))
        elif isinstance(value, intel64.mdarray):
            intel64.ideep.basic_copyto(value, numpy.asarray(dataset))
        else:
            value_type = type(value)
            dataset_arr = numpy.asarray(dataset)
            if (issubclass(dataset_arr.dtype.type, numpy.number)
                    and not (issubclass(dataset_arr.dtype.type, numpy.integer)
                             and value_type in six.integer_types)
                    # Casting a `numpy.integer` scalar by `int()` case above is
                    # safe as `int()` gives unlimited precision integer (it's
                    # also true for `long()`/`int()` on Python 2). For such a
                    # case, the check below may be too strict. For example,
                    # `numpy.can_cast(numpy.int64, int)`, which checks cast-
                    # ability to `dtype(int)`, gives `False` on a platform
                    # whose `dtype(int)` is `numpy.int32` like Windows/x64.
                    and not numpy.can_cast(dataset_arr.dtype,
                                           value_type,
                                           casting='safe')):
                raise TypeError(
                    'Cannot safely deserialize from numpy array with dtype={} '
                    'into a variable of type {}.'.format(
                        dataset.dtype, type(value)))
            value = value_type(dataset_arr)
        return value