Exemple #1
0
 def __init__(self,
              datapipe: Iterable[Tuple[str, BufferedIOBase]],
              *handlers: Callable,
              key_fn: Callable = extension_extract_fn) -> None:
     super().__init__()
     self.datapipe: Iterable[Tuple[str, BufferedIOBase]] = datapipe
     if not handlers:
         handlers = (decoder_basichandlers, decoder_imagehandler('torch'))
     self.decoder = Decoder(*handlers, key_fn=key_fn)
Exemple #2
0
 def __init__(self,
              datapipe: Iterable[Tuple[str, BufferedIOBase]],
              *,
              handlers: Union[None, List[Callable]] = None,
              length: int = -1):
     super().__init__()
     self.datapipe: Iterable[Tuple[str, BufferedIOBase]] = datapipe
     if handlers:
         self.decoder = Decoder(handlers)
     else:
         self.decoder = Decoder(
             [decoder_basichandlers,
              decoder_imagehandler('torch')])
     self.length: int = length
Exemple #3
0
 def __init__(self,
              datapipe: Iterable[Tuple[str, BufferedIOBase]],
              *handlers: Callable,
              key_fn: Callable = extension_extract_fn) -> None:
     super().__init__()
     self.datapipe: Iterable[Tuple[str, BufferedIOBase]] = datapipe
     if not handlers:
         handlers = (decoder_basichandlers, decoder_imagehandler('torch'))
     self.decoder = Decoder(*handlers, key_fn=key_fn)
     _deprecation_warning(
         type(self).__name__,
         deprecation_version="1.12",
         removal_version="1.13",
         old_functional_name="routed_decode",
     )
Exemple #4
0
class RoutedDecoderIterDataPipe(IterDataPipe[Tuple[str, Any]]):
    r"""
    Decodes binary streams from input DataPipe, yields pathname and decoded data
    in a tuple (functional name: ``routed_decode``).

    Args:
        datapipe: Iterable datapipe that provides pathname and binary stream in tuples
        handlers: Optional user defined decoder handlers. If ``None``, basic and image decoder
            handlers will be set as default. If multiple handles are provided, the priority
            order follows the order of handlers (the first handler has the top priority)
        key_fn: Function for decoder to extract key from pathname to dispatch handlers.
            Default is set to extract file extension from pathname

    Note:
        When ``key_fn`` is specified returning anything other than extension, the default
        handler will not work and users need to specify custom handler. Custom handler
        could use regex to determine the eligibility to handle data.
    """
    def __init__(self,
                 datapipe: Iterable[Tuple[str, BufferedIOBase]],
                 *handlers: Callable,
                 key_fn: Callable = extension_extract_fn) -> None:
        super().__init__()
        self.datapipe: Iterable[Tuple[str, BufferedIOBase]] = datapipe
        if not handlers:
            handlers = (decoder_basichandlers, decoder_imagehandler('torch'))
        self.decoder = Decoder(*handlers, key_fn=key_fn)
        _deprecation_warning(
            type(self).__name__,
            deprecation_version="1.12",
            removal_version="1.13",
            old_functional_name="routed_decode",
        )

    def add_handler(self, *handler: Callable) -> None:
        self.decoder.add_handler(*handler)

    def __iter__(self) -> Iterator[Tuple[str, Any]]:
        for data in self.datapipe:
            pathname = data[0]
            result = self.decoder(data)
            yield (pathname, result[pathname])

    def __len__(self) -> int:
        if isinstance(self.datapipe, Sized):
            return len(self.datapipe)
        raise TypeError("{} instance doesn't have valid length".format(
            type(self).__name__))
Exemple #5
0
class RoutedDecoderIterDataPipe(IterDataPipe[Tuple[str, Any]]):
    r""" :class:`RoutedDecoderIterDataPipe`.

    Iterable datapipe to decode binary streams from input DataPipe, yield pathname
    and decoded data in a tuple.
    args:
        datapipe: Iterable datapipe that provides pathname and binary stream in tuples
        handlers: Optional user defined decoder handlers. If None, basic and image decoder
            handlers will be set as default. If multiple handles are provided, the priority
            order follows the order of handlers (the first handler has the top priority)
        key_fn: Function for decoder to extract key from pathname to dispatch handlers.
            Default is set to extract file extension from pathname

    Note:
        When `key_fn` is specified returning anything other than extension, the default
        handler will not work and users need to specify custom handler. Custom handler
        could use regex to determine the eligibility to handle data.
    """
    def __init__(self,
                 datapipe: Iterable[Tuple[str, BufferedIOBase]],
                 *handlers: Callable,
                 key_fn: Callable = extension_extract_fn) -> None:
        super().__init__()
        self.datapipe: Iterable[Tuple[str, BufferedIOBase]] = datapipe
        if not handlers:
            handlers = (decoder_basichandlers, decoder_imagehandler('torch'))
        self.decoder = Decoder(*handlers, key_fn=key_fn)

    def add_handler(self, *handler: Callable) -> None:
        self.decoder.add_handler(*handler)

    def __iter__(self) -> Iterator[Tuple[str, Any]]:
        for data in self.datapipe:
            pathname = data[0]
            result = self.decoder(data)
            yield (pathname, result[pathname])

    def __len__(self) -> int:
        if isinstance(self.datapipe, Sized):
            return len(self.datapipe)
        raise NotImplementedError
Exemple #6
0
class RoutedDecoderIterDataPipe(IterDataPipe):
    r""" :class:`RoutedDecoderIterDataPipe`.

    Iterable datapipe to decode binary streams from input iterables,
    yield pathname and decoded binary stream in a tuple.
    args:
        datapipe: Iterable datapipe that provides pathname and binary stream in tuples
        handlers: user defined decoder handlers, if None, basic and image decoder handlers will be set as default
        length: a nominal length of the datapipe
    """
    def __init__(self,
                 datapipe: Iterable[Tuple[str, BufferedIOBase]],
                 *,
                 handlers: Union[None, List[Callable]] = None,
                 length: int = -1):
        super().__init__()
        self.datapipe: Iterable[Tuple[str, BufferedIOBase]] = datapipe
        if handlers:
            self.decoder = Decoder(handlers)
        else:
            self.decoder = Decoder(
                [decoder_basichandlers,
                 decoder_imagehandler('torch')])
        self.length: int = length

    def add_handler(self, handler: Callable) -> None:
        self.decoder.add_handler(handler)

    def __iter__(self) -> Iterator[Tuple[str, Any]]:
        for data in self.datapipe:
            pathname = data[0]
            result = self.decoder(data)
            yield (pathname, result[pathname])

    def __len__(self):
        if self.length == -1:
            raise NotImplementedError
        return self.length
Exemple #7
0
    def __init__(
        self,
        root: Union[str, pathlib.Path],
        *,
        year: str = "2012",
        split: str = "train",
        target_type: str = "detection",  # segmentation
        decoder: Optional[str] = "pil",
    ):
        self.target_type = target_type
        self.decoder = Decoder([imagehandler(decoder)]) if decoder else None

        root = pathlib.Path(root).resolve()
        # TODO: make this variable based on the input
        archive = "VOCtrainval_11-May-2012.tar"

        split_folder = SPLIT_FOLDER[target_type]
        target_type_folder = TARGET_TYPE_FOLDER[target_type]

        datapipe = (str(root / archive), )
        datapipe = dp.iter.LoadFilesFromDisk(datapipe)
        datapipe = dp.iter.ReadFilesFromTar(datapipe)

        split_files: Dict[str, Tuple[str, io.BufferedIOBase]] = {}
        self.images: Dict[str, Tuple[str, io.BufferedIOBase]] = {}
        self.targets: Dict[str, Tuple[str, io.BufferedIOBase]] = {}

        for data in datapipe:
            parent_ = pathlib.Path(data[0]).parent
            parent = parent_.name
            grand_parent = parent_.parent.name

            if grand_parent == "ImageSets" and parent == split_folder:
                dct = split_files
            elif parent == "JPEGImages":
                dct = self.images
            elif parent == target_type_folder:
                dct = self.targets
            else:
                continue

            dct[self._data_to_key(data)] = data

        self.keys = ReadLineFromFile((split_files[split], ))