det_3d_batch_sampler.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import ast
  16. from pathlib import Path
  17. import numpy as np
  18. import pickle
  19. from typing import Any, Dict, List, Optional, Union
  20. from ....utils import logging
  21. from ....utils.download import download
  22. from ....utils.cache import CACHE_DIR
  23. from .base_batch_sampler import BaseBatchSampler
  24. class Det3DBatchSampler(BaseBatchSampler):
  25. # XXX: auto download for url
  26. def _download_from_url(self, in_path: str) -> str:
  27. file_name = Path(in_path).name
  28. save_path = Path(CACHE_DIR) / "predict_input" / file_name
  29. download(in_path, save_path, overwrite=True)
  30. return save_path.as_posix()
  31. @property
  32. def batch_size(self) -> int:
  33. """Gets the batch size."""
  34. return self._batch_size
  35. @batch_size.setter
  36. def batch_size(self, batch_size: int) -> None:
  37. """Sets the batch size.
  38. Args:
  39. batch_size (int): The batch size to set.
  40. """
  41. if batch_size != 1:
  42. logging.warning(
  43. "inference for 3D models only support batch_size equal to 1"
  44. )
  45. self._batch_size = batch_size
  46. def load_annotations(self, ann_file: str) -> List[Dict]:
  47. """Load annotations from ann_file.
  48. Args:
  49. ann_file (str): Path of the annotation file.
  50. Returns:
  51. list[dict]: List of annotations sorted by timestamps.
  52. """
  53. data = pickle.load(open(ann_file, "rb"))
  54. data_infos = list(sorted(data["infos"], key=lambda e: e["timestamp"]))
  55. return data_infos
  56. def sample(self, inputs: Union[List[str], str]):
  57. if not isinstance(inputs, list):
  58. inputs = [inputs]
  59. sample_set = []
  60. for input in inputs:
  61. if isinstance(input, str):
  62. ann_path = (
  63. self._download_from_url(input)
  64. if input.startswith("http")
  65. else input
  66. )
  67. else:
  68. logging.warning(
  69. f"Not supported input data type! Only `str` is supported! So has been ignored: {input}."
  70. )
  71. self.data_infos = self.load_annotations(ann_path)
  72. sample_set.extend(self.data_infos)
  73. batch = []
  74. for sample in sample_set:
  75. batch.append(sample)
  76. if len(batch) == self.batch_size:
  77. yield batch
  78. batch = []
  79. if len(batch) > 0:
  80. yield batch
  81. def _rand_batch(self, data_size: int) -> List[Any]:
  82. raise NotImplementedError(
  83. "rand batch is not supported for 3D detection annotation data"
  84. )