Upload processor
Browse files- image_processing_retinanet.py +11 -6
- preprocessor_config.json +1 -0
image_processing_retinanet.py
CHANGED
|
@@ -17,11 +17,12 @@ class RetinaNetImageProcessor(BaseImageProcessor):
|
|
| 17 |
model_name: str='retinanet',
|
| 18 |
min_size: int=800,
|
| 19 |
max_size: int=1333,
|
|
|
|
| 20 |
image_mean: list[int]=[0.485, 0.456, 0.406],
|
| 21 |
image_std: list[int]=[0.229, 0.224, 0.225],
|
| 22 |
topk_candidates: int=1000,
|
| 23 |
-
nms_thresh=0.5,
|
| 24 |
-
detections_per_img=300,
|
| 25 |
**kwargs
|
| 26 |
):
|
| 27 |
super().__init__(**kwargs)
|
|
@@ -30,6 +31,7 @@ class RetinaNetImageProcessor(BaseImageProcessor):
|
|
| 30 |
self.config = {
|
| 31 |
'min_size': min_size,
|
| 32 |
'max_size': max_size,
|
|
|
|
| 33 |
'image_mean': image_mean,
|
| 34 |
'image_std': image_std
|
| 35 |
}
|
|
@@ -42,7 +44,7 @@ class RetinaNetImageProcessor(BaseImageProcessor):
|
|
| 42 |
def post_process_object_detection(
|
| 43 |
self,
|
| 44 |
outputs,
|
| 45 |
-
threshold: float = 0.
|
| 46 |
target_sizes: List[Tuple] = None
|
| 47 |
) -> List[Dict[str, torch.Tensor]]:
|
| 48 |
"""
|
|
@@ -54,10 +56,9 @@ class RetinaNetImageProcessor(BaseImageProcessor):
|
|
| 54 |
logits = outputs.logits
|
| 55 |
pred_boxes = outputs.pred_boxes
|
| 56 |
image_sizes = [(int(image_size[0]), int(image_size[1])) for image_size in outputs.image_sizes]
|
| 57 |
-
anchors = outputs.anchors
|
| 58 |
-
|
| 59 |
|
| 60 |
-
num_anchors_per_level = [x.size(2) * x.size(3) for x in features]
|
| 61 |
HW = 0
|
| 62 |
for v in num_anchors_per_level:
|
| 63 |
HW += v
|
|
@@ -142,6 +143,7 @@ class RetinaNetImageProcessor(BaseImageProcessor):
|
|
| 142 |
def preprocess(
|
| 143 |
self,
|
| 144 |
images,
|
|
|
|
| 145 |
annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None,
|
| 146 |
) -> BatchFeature:
|
| 147 |
"""
|
|
@@ -156,6 +158,9 @@ class RetinaNetImageProcessor(BaseImageProcessor):
|
|
| 156 |
if images is not None and not isinstance(images, list):
|
| 157 |
images = [images]
|
| 158 |
|
|
|
|
|
|
|
|
|
|
| 159 |
transform = GeneralizedRCNNTransform(**self.config)
|
| 160 |
totensor = ToTensor()
|
| 161 |
image_tensors = [totensor(img) for img in images]
|
|
|
|
| 17 |
model_name: str='retinanet',
|
| 18 |
min_size: int=800,
|
| 19 |
max_size: int=1333,
|
| 20 |
+
fixed_size: Optional[Tuple[int, int]]=None,
|
| 21 |
image_mean: list[int]=[0.485, 0.456, 0.406],
|
| 22 |
image_std: list[int]=[0.229, 0.224, 0.225],
|
| 23 |
topk_candidates: int=1000,
|
| 24 |
+
nms_thresh: float=0.5,
|
| 25 |
+
detections_per_img: int=300,
|
| 26 |
**kwargs
|
| 27 |
):
|
| 28 |
super().__init__(**kwargs)
|
|
|
|
| 31 |
self.config = {
|
| 32 |
'min_size': min_size,
|
| 33 |
'max_size': max_size,
|
| 34 |
+
'fixed_size': fixed_size,
|
| 35 |
'image_mean': image_mean,
|
| 36 |
'image_std': image_std
|
| 37 |
}
|
|
|
|
| 44 |
def post_process_object_detection(
|
| 45 |
self,
|
| 46 |
outputs,
|
| 47 |
+
threshold: float = 0.05,
|
| 48 |
target_sizes: List[Tuple] = None
|
| 49 |
) -> List[Dict[str, torch.Tensor]]:
|
| 50 |
"""
|
|
|
|
| 56 |
logits = outputs.logits
|
| 57 |
pred_boxes = outputs.pred_boxes
|
| 58 |
image_sizes = [(int(image_size[0]), int(image_size[1])) for image_size in outputs.image_sizes]
|
| 59 |
+
anchors = list(outputs.anchors)
|
| 60 |
+
num_anchors_per_level = outputs.num_anchors_per_level
|
| 61 |
|
|
|
|
| 62 |
HW = 0
|
| 63 |
for v in num_anchors_per_level:
|
| 64 |
HW += v
|
|
|
|
| 143 |
def preprocess(
|
| 144 |
self,
|
| 145 |
images,
|
| 146 |
+
fixed_size: Optional[Tuple[int, int]]=None,
|
| 147 |
annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None,
|
| 148 |
) -> BatchFeature:
|
| 149 |
"""
|
|
|
|
| 158 |
if images is not None and not isinstance(images, list):
|
| 159 |
images = [images]
|
| 160 |
|
| 161 |
+
if fixed_size is not None:
|
| 162 |
+
self.config['fixed_size'] = fixed_size
|
| 163 |
+
|
| 164 |
transform = GeneralizedRCNNTransform(**self.config)
|
| 165 |
totensor = ToTensor()
|
| 166 |
image_tensors = [totensor(img) for img in images]
|
preprocessor_config.json
CHANGED
|
@@ -3,6 +3,7 @@
|
|
| 3 |
"AutoImageProcessor": "image_processing_retinanet.RetinaNetImageProcessor"
|
| 4 |
},
|
| 5 |
"config": {
|
|
|
|
| 6 |
"image_mean": [
|
| 7 |
0.485,
|
| 8 |
0.456,
|
|
|
|
| 3 |
"AutoImageProcessor": "image_processing_retinanet.RetinaNetImageProcessor"
|
| 4 |
},
|
| 5 |
"config": {
|
| 6 |
+
"fixed_size": null,
|
| 7 |
"image_mean": [
|
| 8 |
0.485,
|
| 9 |
0.456,
|