1
1
"""Collection Pipeline for detection task."""
2
- # Copyright (C) 2021 Intel Corporation
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing,
11
- # software distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions
14
- # and limitations under the License.
2
+ # Copyright (C) 2021-2023 Intel Corporation
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
15
5
import copy
16
- from typing import Any , Dict
6
+ from typing import Any , Dict , Optional
17
7
18
- from mmdet .datasets .builder import PIPELINES
8
+ from mmdet .datasets .builder import PIPELINES , build_from_cfg
9
+ from mmdet .datasets .pipelines import Resize
19
10
20
11
import otx .algorithms .common .adapters .mmcv .pipelines .load_image_from_otx_dataset as load_image_base
21
12
from otx .algorithms .detection .adapters .mmdet .datasets .dataset import (
@@ -30,6 +21,50 @@ class LoadImageFromOTXDataset(load_image_base.LoadImageFromOTXDataset):
30
21
"""Pipeline element that loads an image from a OTX Dataset on the fly."""
31
22
32
23
24
+ @PIPELINES .register_module ()
25
+ class LoadResizeDataFromOTXDataset (load_image_base .LoadResizeDataFromOTXDataset ):
26
+ """Load and resize image & annotation with cache support."""
27
+
28
+ def _create_load_ann_op (self , cfg : Optional [Dict ]) -> Optional [Any ]:
29
+ """Creates resize operation."""
30
+ if cfg is None :
31
+ return None
32
+ return build_from_cfg (cfg , PIPELINES )
33
+
34
+ def _create_resize_op (self , cfg : Optional [Dict ]) -> Optional [Any ]:
35
+ """Creates resize operation."""
36
+ if cfg is None :
37
+ return None
38
+ return build_from_cfg (cfg , PIPELINES )
39
+
40
+
41
+ @PIPELINES .register_module ()
42
+ class ResizeTo (Resize ):
43
+ """Resize to specific size.
44
+
45
+ This operation works if the input is not in desired shape.
46
+ If it's already in the shape, it just returns input dict for efficiency.
47
+
48
+ Args:
49
+ img_scale (tuple): Images scales for resizing (w, h).
50
+ """
51
+
52
+ def __init__ (self , ** kwargs ):
53
+ super ().__init__ (override = True , ** kwargs ) # Allow multiple calls
54
+
55
+ def __call__ (self , results : Dict [str , Any ]):
56
+ """Callback function of ResizeTo.
57
+
58
+ Args:
59
+ results: Inputs to be transformed.
60
+ """
61
+ img_shape = results .get ("img_shape" , (0 , 0 ))
62
+ img_scale = self .img_scale [0 ]
63
+ if img_shape [0 ] == img_scale [0 ] and img_shape [1 ] == img_scale [1 ]:
64
+ return results
65
+ return super ().__call__ (results )
66
+
67
+
33
68
@PIPELINES .register_module ()
34
69
class LoadAnnotationFromOTXDataset :
35
70
"""Pipeline element that loads an annotation from a OTX Dataset on the fly.
@@ -84,7 +119,7 @@ def _load_masks(results, ann_info):
84
119
85
120
def __call__ (self , results : Dict [str , Any ]):
86
121
"""Callback function of LoadAnnotationFromOTXDataset."""
87
- dataset_item = results .pop ("dataset_item" )
122
+ dataset_item = results .pop ("dataset_item" ) # Prevent unnecessary deepcopy
88
123
label_list = results .pop ("ann_info" )["label_list" ]
89
124
ann_info = get_annotation_mmdet_format (dataset_item , label_list , self .domain , self .min_size )
90
125
if self .with_bbox :
0 commit comments