Beispiel #1
0
    def __init__(self,
                 source=None,
                 num_outputs=None,
                 batch_size=-1,
                 cycle=None,
                 name=None,
                 layout=None,
                 batch=None,
                 batch_info=None):
        if name is not None and num_outputs is not None:
            raise ValueError(
                "`num_outputs` is not compatible with named `ExternalSource`")

        callback, source_desc = _get_callback_from_source(
            source, cycle, batch_info or False)

        self._name = name
        self._layout = layout
        self._num_outputs = num_outputs
        self._batch = batch
        self._batch_size = batch_size
        self._callback = callback
        self._source_desc = source_desc
        self._batch_info = batch_info
        self._current_iter = 0
        self._current_sample = 0
        self._feed_inputs = Queue()

        if callback is not None:
            arg_count = _accepted_arg_count(callback)
            if arg_count not in [0, 1]:
                raise TypeError(
                    "External source callback must be a callable with 0 or 1 argument"
                )
            self.accepts_arg = arg_count > 0
Beispiel #2
0
    def __init__(
            self, source=None, num_outputs=None, *, cycle=None, layout=None, dtype=None, name=None,
            device="cpu", cuda_stream=None, use_copy_kernel=None, batch=None, parallel=None,
            no_copy=None, prefetch_queue_depth=None, batch_info=None, **kwargs):
        self._schema = _b.GetSchema("ExternalSource")
        self._spec = _b.OpSpec("ExternalSource")
        self._device = device
        self._layout = layout
        self._dtype = dtype
        self._cuda_stream = cuda_stream
        self._use_copy_kernel = use_copy_kernel

        import nvidia.dali.ops
        kwargs, self._call_args = nvidia.dali.ops._separate_kwargs(kwargs)

        callback, source_desc = _get_callback_from_source(source, cycle, batch_info or False)

        if name is not None and num_outputs is not None:
            raise ValueError("`num_outputs` is not compatible with named `ExternalSource`")

        self._name = name
        self._num_outputs = num_outputs
        self._batch = batch
        self._callback = callback
        self._source_desc = source_desc
        self._parallel = parallel
        self._no_copy = no_copy
        self._prefetch_queue_depth = prefetch_queue_depth
        self._batch_info = batch_info

        self._spec.AddArg("device", device)
        for key, value in kwargs.items():
            self._spec.AddArg(key, value)
Beispiel #3
0
    def __call__(
            self, *, source=None, cycle=None, name=None, layout=None, dtype=None, cuda_stream=None,
            use_copy_kernel=None, batch=None, parallel=None, no_copy=None,
            prefetch_queue_depth=None, batch_info=None, **kwargs):
        ""
        from nvidia.dali.ops import _OperatorInstance

        if batch_info is None:
            batch_info = self._batch_info or False
        elif self._batch_info is not None:
            raise ValueError(
                "The argument ``batch_info`` already specified in constructor.")

        if source is None:
            if cycle is not None:
                if self._callback:
                    raise ValueError("The argument ``cycle`` can only be specified if ``source`` is an iterable object "
                        "or a generator function specified in this call. To cycle through an iterable specified in "
                        "``__init__``, set ``cycle`` there.")
                else:
                    raise ValueError("The argument ``cycle`` can only be specified if ``source`` is a "
                                     "reusable iterable or a generator function.")
            callback = self._callback
            source_desc = self._source_desc
        else:
            if self._callback is not None:
                raise RuntimeError("``source`` already specified in constructor.")
            callback, source_desc = _get_callback_from_source(source, cycle, self._batch_info)

            # Keep the metadata for Pipeline inspection
            self._source_desc = source_desc

        if parallel is None:
            parallel = self._parallel or False
        elif self._parallel is not None:
            raise ValueError("The argument ``parallel`` already specified in constructor.")

        if batch is None:
            batch = self._batch
        elif self._batch is not None:
            raise ValueError("The argument ``batch`` already specified in constructor.")

        # By default parallel is False, so batch will be True
        if batch is None:
            batch = not parallel

        if prefetch_queue_depth is None:
            prefetch_queue_depth = self._prefetch_queue_depth
        elif self._prefetch_queue_depth is not None:
            raise ValueError(
                "The argument ``prefetch_queue_depth`` already specified in constructor.")

        if no_copy is None:
            no_copy = self._no_copy
        elif self._no_copy is not None:
            raise ValueError("The argument ``no_copy`` already specified in constructor.")

        if parallel:
            if prefetch_queue_depth is None:
                prefetch_queue_depth = 1
            if no_copy is None:
                no_copy = True
            if not no_copy:
                raise ValueError("The argument ``no_copy`` cannot be specified to False " +
                    " when used with ``parallel=True``.")
            if prefetch_queue_depth < 1:
                raise ValueError(
                    "``prefetch_queue_depth`` must be a positive integer, got {}.".format(
                        prefetch_queue_depth))
            if source_desc.kind == _SourceKind.CALLABLE:
                if not source_desc.has_inputs:
                    raise TypeError(("Callable passed to External Source in parallel mode (when `parallel=True`) "
                            "must accept exactly one argument: `nvidia.dali.types.SampleInfo` "
                            "if run with `batch=False` or either `nvidia.dali.types.BatchInfo` or integer that "
                            "represents the index of the batch within the epoch if `batch=True`. "
                            "Got a callable that does not accept arguments instead."))
            elif not batch:
                what = "an iterable" if source_desc.kind == _SourceKind.ITERABLE else "a generator function"
                raise TypeError("Parallel external source with {} must be run in a batch mode "
                        "(specify `batch=True` in the external source definition and make sure "
                        "your source returns batches)".format(what))
        else:
            if prefetch_queue_depth is not None:
                raise ValueError("The argument `prefetch_queue_depth` is valid only for " +
                    "parallel external sources (when ``parallel`` is True).")

        if self._layout is not None:
            if layout is not None:
                raise RuntimeError("``layout`` already specified in constructor.")
            else:
                layout = self._layout

        if self._dtype is not None:
            if dtype is not None:
                raise RuntimeError("``dtype`` already specified in constructor.")
            else:
                dtype = self._dtype

        if self._cuda_stream is not None:
            if cuda_stream is not None:
                raise RuntimeError("``cuda_stream`` already specified in constructor.")
            else:
                cuda_stream = self._cuda_stream

        if self._use_copy_kernel is not None:
            if use_copy_kernel is not None:
                raise RuntimeError("``use_copy_kernel`` already specified in constructor.")
            else:
                use_copy_kernel = self._use_copy_kernel

        if name is None:
            name = self._name
        else:
            self._name = name

        if name is not None and self._num_outputs is not None:
            raise RuntimeError("``num_outputs`` is not compatible with named ``ExternalSource``.")

        group_common_kwargs = {
            'cuda_stream': cuda_stream,
            'use_copy_kernel': use_copy_kernel,
            'batch': batch,
            'batch_info': batch_info,
            'parallel': parallel,
            'prefetch_queue_depth': prefetch_queue_depth,
        }

        if self._num_outputs is not None:
            outputs = []
            kwargs = {"no_copy": no_copy}
            group = _ExternalSourceGroup(callback, source_desc, True, **group_common_kwargs)
            for i in range(self._num_outputs):
                if dtype is not None:
                    if isinstance(dtype, (list, tuple)):
                        kwargs['dtype'] = dtype[i] if i < len(dtype) else nvidia.dali.types.DALIDataType.NO_TYPE
                    else:
                        kwargs['dtype'] = dtype
                op_instance = _OperatorInstance([], self, **kwargs)
                op_instance._callback = callback
                op_instance._output_index = i
                op_instance._group = group
                if layout is not None:
                    if isinstance(layout, (list, tuple)):
                        op_instance._layout = layout[i] if i < len(layout) else ""
                    else:
                        op_instance._layout = layout
                else:
                    op_instance._layout = None

                op_instance._batch = batch

                group.append(op_instance)
                op_instance.generate_outputs()
                outputs.append(op_instance.unwrapped_outputs)

            return outputs
        else:
            if name is not None:
                kwargs["name"] = name
            if no_copy is not None:
                kwargs["no_copy"] = no_copy
            if dtype is not None:
                kwargs['dtype'] = dtype
            op_instance = _OperatorInstance([], self, **kwargs)
            op_instance._callback = callback
            op_instance._output_index = None
            op_instance._group = _ExternalSourceGroup(
                callback, source_desc, False, [op_instance], **group_common_kwargs)
            op_instance._layout = layout
            op_instance._batch = batch
            op_instance.generate_outputs()

            return op_instance.unwrapped_outputs