13
13
import cv2
14
14
import numpy as np
15
15
from datumaro .components .annotation import AnnotationType
16
- from datumaro .components .media import ImageFromFile
17
16
from datumaro .util .image import IMAGE_BACKEND , IMAGE_COLOR_CHANNEL , ImageBackend
18
17
from datumaro .util .image import ImageColorChannel as DatumaroImageColorChannel
19
18
from torch .utils .data import Dataset
20
19
21
20
from otx .core .data .entity .base import T_OTXDataEntity
22
- from otx .core .data .mem_cache import NULL_MEM_CACHE_HANDLER
23
21
from otx .core .data .transform_libs .torchvision import Compose
24
22
from otx .core .types .image import ImageColorChannel
25
23
from otx .core .types .label import LabelInfo , NullLabelInfo
28
26
if TYPE_CHECKING :
29
27
from datumaro import DatasetSubset , Image
30
28
31
- from otx .core .data .mem_cache import MemCacheHandlerBase
32
29
33
30
Transforms = Union [Compose , Callable , List [Callable ], dict [str , Compose | Callable | List [Callable ]]]
34
31
@@ -66,8 +63,6 @@ class OTXDataset(Dataset):
66
63
Args:
67
64
dm_subset: Datumaro subset of a dataset
68
65
transforms: Transforms to apply on images
69
- mem_cache_handler: Handler of the images cache
70
- mem_cache_img_max_size: Max size of images to put in cache
71
66
max_refetch: Maximum number of images to fetch in cache
72
67
image_color_channel: Color channel of images
73
68
stack_images: Whether or not to stack images in collate function in OTXBatchData entity.
@@ -79,8 +74,6 @@ def __init__(
79
74
self ,
80
75
dm_subset : DatasetSubset ,
81
76
transforms : Transforms ,
82
- mem_cache_handler : MemCacheHandlerBase = NULL_MEM_CACHE_HANDLER ,
83
- mem_cache_img_max_size : tuple [int , int ] | None = None ,
84
77
max_refetch : int = 1000 ,
85
78
image_color_channel : ImageColorChannel = ImageColorChannel .RGB ,
86
79
stack_images : bool = True ,
@@ -89,8 +82,6 @@ def __init__(
89
82
) -> None :
90
83
self .dm_subset = dm_subset
91
84
self .transforms = transforms
92
- self .mem_cache_handler = mem_cache_handler
93
- self .mem_cache_img_max_size = mem_cache_img_max_size
94
85
self .max_refetch = max_refetch
95
86
self .image_color_channel = image_color_channel
96
87
self .stack_images = stack_images
@@ -166,12 +157,7 @@ def _get_img_data_and_shape(
166
157
Returns:
167
158
The image data, shape, and ROI meta information
168
159
"""
169
- key = img .path if isinstance (img , ImageFromFile ) else id (img )
170
160
roi_meta = None
171
- # check if the image is already in the cache
172
- img_data , roi_meta = self .mem_cache_handler .get (key = key )
173
- if img_data is not None :
174
- return img_data , img_data .shape [:2 ], roi_meta
175
161
176
162
with image_decode_context ():
177
163
img_data = (
@@ -201,58 +187,8 @@ def _get_img_data_and_shape(
201
187
img_data = img_data [y1 :y2 , x1 :x2 ]
202
188
roi_meta = {"x1" : x1 , "y1" : y1 , "x2" : x2 , "y2" : y2 , "orig_image_shape" : (h , w )}
203
189
204
- img_data = self ._cache_img (key = key , img_data = img_data .astype (np .uint8 ), meta = roi_meta )
205
-
206
190
return img_data , img_data .shape [:2 ], roi_meta
207
191
208
- def _cache_img (self , key : str | int , img_data : np .ndarray , meta : dict [str , Any ] | None = None ) -> np .ndarray :
209
- """Cache an image after resizing.
210
-
211
- If there is available space in the memory pool, the input image is cached.
212
- Before caching, the input image is resized if it is larger than the maximum image size
213
- specified by the memory caching handler.
214
- Otherwise, the input image is directly cached.
215
- After caching, the processed image data is returned.
216
-
217
- Args:
218
- key: The key associated with the image.
219
- img_data: The image data to be cached.
220
-
221
- Returns:
222
- The resized image if it was resized. Otherwise, the original image.
223
- """
224
- if self .mem_cache_handler .frozen :
225
- return img_data
226
-
227
- if self .mem_cache_img_max_size is None :
228
- self .mem_cache_handler .put (key = key , data = img_data , meta = meta )
229
- return img_data
230
-
231
- height , width = img_data .shape [:2 ]
232
- max_height , max_width = self .mem_cache_img_max_size
233
-
234
- if height <= max_height and width <= max_width :
235
- self .mem_cache_handler .put (key = key , data = img_data , meta = meta )
236
- return img_data
237
-
238
- # Preserve the image size ratio and fit to max_height or max_width
239
- # e.g. (1000 / 2000 = 0.5, 1000 / 1000 = 1.0) => 0.5
240
- # h, w = 2000 * 0.5 => 1000, 1000 * 0.5 => 500, bounded by max_height
241
- min_scale = min (max_height / height , max_width / width )
242
- new_height , new_width = int (min_scale * height ), int (min_scale * width )
243
- resized_img = cv2 .resize (
244
- src = img_data ,
245
- dsize = (new_width , new_height ),
246
- interpolation = cv2 .INTER_LINEAR ,
247
- )
248
-
249
- self .mem_cache_handler .put (
250
- key = key ,
251
- data = resized_img ,
252
- meta = meta ,
253
- )
254
- return resized_img
255
-
256
192
@abstractmethod
257
193
def _get_item_impl (self , idx : int ) -> OTXDataItem | None :
258
194
pass
0 commit comments