deepvisiontools 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- deepvisiontools/__init__.py +16 -0
- deepvisiontools/config/__init__.py +3 -0
- deepvisiontools/config/config.py +176 -0
- deepvisiontools/config/errors.py +12 -0
- deepvisiontools/data/__init__.py +24 -0
- deepvisiontools/data/additional_augmentations.py +201 -0
- deepvisiontools/data/augmentation_class.py +37 -0
- deepvisiontools/data/batch_augmentations.py +211 -0
- deepvisiontools/data/data_reader/__init__.py +3 -0
- deepvisiontools/data/data_reader/readers.py +490 -0
- deepvisiontools/data/dataset.py +357 -0
- deepvisiontools/data/errors.py +23 -0
- deepvisiontools/formats/__init__.py +46 -0
- deepvisiontools/formats/base_data.py +802 -0
- deepvisiontools/formats/base_formats.py +321 -0
- deepvisiontools/formats/errors.py +167 -0
- deepvisiontools/formats/formats.py +282 -0
- deepvisiontools/formats/utils.py +174 -0
- deepvisiontools/inference/__init__.py +4 -0
- deepvisiontools/inference/patchifier.py +343 -0
- deepvisiontools/inference/predictor.py +208 -0
- deepvisiontools/metrics/__init__.py +50 -0
- deepvisiontools/metrics/available_metrics.py +215 -0
- deepvisiontools/metrics/base_metric.py +436 -0
- deepvisiontools/metrics/func.py +34 -0
- deepvisiontools/metrics/matcher.py +99 -0
- deepvisiontools/metrics/utils.py +39 -0
- deepvisiontools/models/__init__.py +14 -0
- deepvisiontools/models/basemodel.py +100 -0
- deepvisiontools/models/mask2former/__init__.py +3 -0
- deepvisiontools/models/mask2former/mask2former.py +285 -0
- deepvisiontools/models/smp/__init__.py +0 -0
- deepvisiontools/models/smp/errors.py +4 -0
- deepvisiontools/models/smp/smp.py +149 -0
- deepvisiontools/models/yolov8/__init__.py +15 -0
- deepvisiontools/models/yolov8/errors.py +25 -0
- deepvisiontools/models/yolov8/utils.py +92 -0
- deepvisiontools/models/yolov8/yolov8.py +302 -0
- deepvisiontools/models/yolov8seg/__init__.py +4 -0
- deepvisiontools/models/yolov8seg/errors.py +14 -0
- deepvisiontools/models/yolov8seg/utils.py +135 -0
- deepvisiontools/models/yolov8seg/yolov8seg.py +370 -0
- deepvisiontools/preprocessing/__init__.py +19 -0
- deepvisiontools/preprocessing/image.py +79 -0
- deepvisiontools/preprocessing/preprocessing.py +65 -0
- deepvisiontools/train/__init__.py +3 -0
- deepvisiontools/train/trainer.py +335 -0
- deepvisiontools/utils/__init__.py +3 -0
- deepvisiontools/utils/errors.py +0 -0
- deepvisiontools/utils/visualization.py +318 -0
- deepvisiontools-0.0.1.dist-info/LICENSE.txt +661 -0
- deepvisiontools-0.0.1.dist-info/METADATA +725 -0
- deepvisiontools-0.0.1.dist-info/RECORD +55 -0
- deepvisiontools-0.0.1.dist-info/WHEEL +5 -0
- deepvisiontools-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
from typing import Tuple, List
|
|
2
|
+
from torch import Tensor
|
|
3
|
+
from math import ceil
|
|
4
|
+
import torch
|
|
5
|
+
from torchvision.transforms.v2 import Pad
|
|
6
|
+
from itertools import product
|
|
7
|
+
from deepvisiontools.formats import (
|
|
8
|
+
BatchedFormat,
|
|
9
|
+
BaseFormat,
|
|
10
|
+
BboxFormat,
|
|
11
|
+
InstanceMaskFormat,
|
|
12
|
+
)
|
|
13
|
+
import copy
|
|
14
|
+
from torchvision.ops import nms
|
|
15
|
+
from abc import ABC, abstractmethod
|
|
16
|
+
from deepvisiontools import visualization
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class BasePatchifier(ABC):
|
|
20
|
+
"""Abstract class for patchifier. If you want to implement a custom one you need to implement unpatchify method"""
|
|
21
|
+
|
|
22
|
+
def pad_to(
|
|
23
|
+
self, image: Tensor, new_size: Tuple[int, int]
|
|
24
|
+
) -> Tuple[Tensor, Tuple[int, int, int, int]]:
|
|
25
|
+
"""Pad image to given size
|
|
26
|
+
Args:
|
|
27
|
+
image (Tensor).
|
|
28
|
+
new_size (Tuple[int, int]).
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
Tuple[Tensor, Tuple[int, int, int, int]]: padded image, (t, l, r, b)
|
|
32
|
+
"""
|
|
33
|
+
t = int((new_size[0] - image.shape[-2]) / 2)
|
|
34
|
+
l = int((new_size[1] - image.shape[-1]) / 2)
|
|
35
|
+
b = int((new_size[0] - image.shape[-2]) - t)
|
|
36
|
+
r = int((new_size[1] - image.shape[-1]) - l)
|
|
37
|
+
padder = Pad((l, t, r, b))
|
|
38
|
+
pad_coord = (t, l, r, b)
|
|
39
|
+
return padder(image), pad_coord
|
|
40
|
+
|
|
41
|
+
def patchify(
|
|
42
|
+
self, image: Tensor
|
|
43
|
+
) -> Tuple[Tensor, List[Tuple[int, int]], Tensor, Tuple[int, int, int, int]]:
|
|
44
|
+
"""Create patches for image prediction : 1) Pad image to fit all patches, 2) create patches
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
image (``Tensor``)
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
``Tuple[ Tensor, List[Tuple[int, int]], Tuple[int, int], Tensor, Tuple[int, int]]``:
|
|
51
|
+
- patches stacked (N_patch, c, h, w), List of (top, left) pad coordinates, padded image, image pad coordinates
|
|
52
|
+
"""
|
|
53
|
+
h, w = image.shape[-2:]
|
|
54
|
+
assert (
|
|
55
|
+
self.patch_size[0] < h and self.patch_size[1] < w
|
|
56
|
+
), f"Can't patchify image of shape {(h, w)} with patch size {self.patch_size}."
|
|
57
|
+
c, h, w = image.shape[-3:]
|
|
58
|
+
h_patch, w_patch = self.patch_size
|
|
59
|
+
# compute strides values
|
|
60
|
+
stride_h = h_patch - round(h_patch * self.overlap)
|
|
61
|
+
stride_w = w_patch - round(w_patch * self.overlap)
|
|
62
|
+
# get number of pacthes on axis
|
|
63
|
+
nb_h_patches = ceil(h / stride_h)
|
|
64
|
+
nb_w_patches = ceil(w / stride_w)
|
|
65
|
+
# padded image shape (H,W)
|
|
66
|
+
h_padded = (nb_h_patches - 1) * stride_h + h_patch
|
|
67
|
+
w_padded = (nb_w_patches - 1) * stride_w + w_patch
|
|
68
|
+
# pad image
|
|
69
|
+
padded_image, pad_coord = self.pad_to(image, (h_padded, w_padded))
|
|
70
|
+
# get coordinates
|
|
71
|
+
top_corners = range(0, nb_h_patches * stride_h, stride_h)
|
|
72
|
+
left_corners = range(0, nb_w_patches * stride_w, stride_w)
|
|
73
|
+
origins = list(product(top_corners, left_corners))
|
|
74
|
+
# Create patches tensors
|
|
75
|
+
patches = torch.zeros(
|
|
76
|
+
(nb_h_patches * nb_w_patches, c, h_patch, w_patch), device=image.device
|
|
77
|
+
).to(image.device)
|
|
78
|
+
# Fill the patches tensors
|
|
79
|
+
for idx, (y, x) in enumerate(origins):
|
|
80
|
+
patches[idx] = padded_image[:, y : y + h_patch, x : x + w_patch]
|
|
81
|
+
|
|
82
|
+
return patches, origins, padded_image, pad_coord
|
|
83
|
+
|
|
84
|
+
@abstractmethod
|
|
85
|
+
def unpatchify():
|
|
86
|
+
pass
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class DetectPatchifier(BasePatchifier):
|
|
90
|
+
"""Handle patchification and unpatchification
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
patch_size (``Tuple[int, int]``): size of patches to create
|
|
94
|
+
overlap (``float``): overlap between patches
|
|
95
|
+
border_penalty (``float``, **optional**): penalty to apply on patch border objects before nms. Defaults to 0.5.
|
|
96
|
+
nms_iou_threshold (``float``, **optional**): nms threshold. Defaults to 0.45.
|
|
97
|
+
final_score_threshold (``float``, **optional**): final score (after penalty) threshold. Defaults to 0.4.
|
|
98
|
+
|
|
99
|
+
Attributes
|
|
100
|
+
----------
|
|
101
|
+
|
|
102
|
+
Attributes:
|
|
103
|
+
patch_size (``Tuple[int, int]``)
|
|
104
|
+
overlap (``float``): overlap between patches
|
|
105
|
+
border_penalty (``float``)
|
|
106
|
+
postprocess (```PostProcesser```)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
**Methods**
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
def __init__(
|
|
113
|
+
self,
|
|
114
|
+
patch_size: Tuple[int, int],
|
|
115
|
+
overlap: float,
|
|
116
|
+
border_penalty: float = 0.5,
|
|
117
|
+
nms_iou_threshold: float = 0.45,
|
|
118
|
+
final_score_threshold: float = 0.4,
|
|
119
|
+
):
|
|
120
|
+
self.patch_size = patch_size
|
|
121
|
+
self.overlap = overlap
|
|
122
|
+
self.border_effect_penality = border_penalty
|
|
123
|
+
self.postprocess = PostProcesser(nms_iou_threshold, final_score_threshold)
|
|
124
|
+
|
|
125
|
+
def _merge_prediction_patches(
|
|
126
|
+
self,
|
|
127
|
+
pred_patches: BatchedFormat,
|
|
128
|
+
origins: List[Tuple[int, int]],
|
|
129
|
+
image_padded_size: Tuple[int, int],
|
|
130
|
+
) -> BaseFormat:
|
|
131
|
+
"""combine the patchs predictions together"""
|
|
132
|
+
# add patch to full sized format
|
|
133
|
+
full_pred: BaseFormat = type(pred_patches.formats[0]).empty(
|
|
134
|
+
canvas_size=image_padded_size
|
|
135
|
+
)
|
|
136
|
+
scores = torch.tensor([]).to(pred_patches.device)
|
|
137
|
+
for pred, ori in zip(pred_patches.formats, origins):
|
|
138
|
+
r_pad = image_padded_size[1] - ori[1] - pred.canvas_size[1]
|
|
139
|
+
b_pad = image_padded_size[0] - ori[0] - pred.canvas_size[0]
|
|
140
|
+
full_pred += pred.pad(ori[0], ori[1], r_pad, b_pad)[0]
|
|
141
|
+
if pred.scores != None:
|
|
142
|
+
scores = torch.cat([scores, pred.scores])
|
|
143
|
+
assert (
|
|
144
|
+
scores.nelement() == full_pred.nb_object
|
|
145
|
+
), "Inconsistency between scores and object number."
|
|
146
|
+
full_pred._scores = scores
|
|
147
|
+
return full_pred
|
|
148
|
+
|
|
149
|
+
def unpatchify(
|
|
150
|
+
self,
|
|
151
|
+
pred_patches: BatchedFormat,
|
|
152
|
+
origins: List[Tuple[int, int]],
|
|
153
|
+
image_padded_size: Tuple[int, int],
|
|
154
|
+
padded_image_coords: Tuple[int, int, int, int],
|
|
155
|
+
original_image_size: Tuple[int, int],
|
|
156
|
+
):
|
|
157
|
+
"""merge patchs predictions together while applying penalty, postprocess etc.
|
|
158
|
+
Note that since InstanceMasks do not handle overlapping objects, you need to treat directly the patches in the postprocess.
|
|
159
|
+
To do so you derive boxes, then use boxes to filter the masks."""
|
|
160
|
+
# apply penalty
|
|
161
|
+
pred_patches = BatchedFormat(
|
|
162
|
+
[self._scores_penalty(pred) for pred in pred_patches]
|
|
163
|
+
)
|
|
164
|
+
if pred_patches.formats == []:
|
|
165
|
+
# handles empty preds
|
|
166
|
+
return BboxFormat.empty((original_image_size[0], original_image_size[1]))
|
|
167
|
+
# if instance_masks, need to filter patchs objects before merging (otherwise will override objects ...)
|
|
168
|
+
if isinstance(pred_patches.formats[0], InstanceMaskFormat):
|
|
169
|
+
box_patches = [
|
|
170
|
+
BboxFormat.from_instance_mask(patch) for patch in pred_patches
|
|
171
|
+
]
|
|
172
|
+
associated_boxes = self._merge_prediction_patches(
|
|
173
|
+
BatchedFormat(box_patches), origins, image_padded_size
|
|
174
|
+
)
|
|
175
|
+
pred_patches = self.postprocess.handle_patches_mask(
|
|
176
|
+
pred_patches, associated_boxes
|
|
177
|
+
)
|
|
178
|
+
preds = self._merge_prediction_patches(
|
|
179
|
+
pred_patches, origins, image_padded_size
|
|
180
|
+
)
|
|
181
|
+
else:
|
|
182
|
+
# merge patchs
|
|
183
|
+
preds = self._merge_prediction_patches(
|
|
184
|
+
pred_patches, origins, image_padded_size
|
|
185
|
+
)
|
|
186
|
+
# Duplicates handling
|
|
187
|
+
preds = self.postprocess.handle_box_duplicates(preds)
|
|
188
|
+
# crop to original image
|
|
189
|
+
t, l, _, _ = padded_image_coords
|
|
190
|
+
h = original_image_size[0]
|
|
191
|
+
w = original_image_size[1]
|
|
192
|
+
preds, _ = preds.crop(t, l, h, w)
|
|
193
|
+
return preds
|
|
194
|
+
|
|
195
|
+
def _scores_penalty(self, patch: BaseFormat) -> BaseFormat:
|
|
196
|
+
"""apply score penalty on border. Method is linear"""
|
|
197
|
+
if isinstance(patch, BboxFormat):
|
|
198
|
+
return self._box_penalty(patch)
|
|
199
|
+
elif isinstance(patch, InstanceMaskFormat):
|
|
200
|
+
# if InstanceMaskFormat create boxes, apply penalty on objects boxes scores, return InstanceMaskFormat with replaced scores
|
|
201
|
+
boxes = BboxFormat.from_instance_mask(patch)
|
|
202
|
+
new_scores = self._box_penalty(boxes)
|
|
203
|
+
new_patch = InstanceMaskFormat(
|
|
204
|
+
data=patch.data, labels=patch.labels, scores=new_scores.scores
|
|
205
|
+
)
|
|
206
|
+
return new_patch
|
|
207
|
+
|
|
208
|
+
def _box_penalty(self, patch: BaseFormat) -> BaseFormat:
|
|
209
|
+
"""Apply box scores penalty to reduce border effect. Helps patchification multiple objects removal.
|
|
210
|
+
|
|
211
|
+
Args:
|
|
212
|
+
patch (``BaseFormat``)
|
|
213
|
+
|
|
214
|
+
Returns:
|
|
215
|
+
``BaseFormat``:
|
|
216
|
+
- patch with reduced scores on border.
|
|
217
|
+
"""
|
|
218
|
+
if patch.nb_object == 0:
|
|
219
|
+
return patch
|
|
220
|
+
assert (
|
|
221
|
+
patch.scores != None
|
|
222
|
+
), "Can't apply penalty on scores since scores = None."
|
|
223
|
+
patch: BaseFormat = copy.copy(patch)
|
|
224
|
+
patch.format = "XYXY"
|
|
225
|
+
patch_xcenter = patch.canvas_size[0] / 2
|
|
226
|
+
patch_ycenter = patch.canvas_size[1] / 2
|
|
227
|
+
max_devs = []
|
|
228
|
+
for box in patch.data.value:
|
|
229
|
+
centro_x = int((box[0] + box[2]) / 2)
|
|
230
|
+
centro_y = int((box[1] + box[3]) / 2)
|
|
231
|
+
relative_xdeviation = abs(centro_x - patch_xcenter) / patch.canvas_size[1]
|
|
232
|
+
relative_ydeviation = abs(centro_y - patch_ycenter) / patch.canvas_size[0]
|
|
233
|
+
# max and *2 because relative_dev in [0, 0.5] range (but we want in [0, 1])
|
|
234
|
+
max_devs.append(max(2 * relative_xdeviation, 2 * relative_ydeviation))
|
|
235
|
+
# Apply linear penalty
|
|
236
|
+
relu = torch.nn.ReLU(inplace=True)
|
|
237
|
+
penalty = relu(1 - self.border_effect_penality * torch.tensor(max_devs)).to(
|
|
238
|
+
patch.device
|
|
239
|
+
)
|
|
240
|
+
new_scores = patch.scores * penalty
|
|
241
|
+
patch._scores = new_scores
|
|
242
|
+
return patch
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
class SemanticPatchifier(BasePatchifier):
|
|
246
|
+
"""Semantic segmentation patchifier/unpatchifier
|
|
247
|
+
|
|
248
|
+
Args:
|
|
249
|
+
patch_size (``Tuple[int, int]``)
|
|
250
|
+
overlap (``float``)
|
|
251
|
+
"""
|
|
252
|
+
|
|
253
|
+
def __init__(self, patch_size: Tuple[int, int], overlap: float):
|
|
254
|
+
|
|
255
|
+
self.patch_size = patch_size
|
|
256
|
+
self.overlap = overlap
|
|
257
|
+
|
|
258
|
+
def unpatchify(
|
|
259
|
+
self,
|
|
260
|
+
pred_patches,
|
|
261
|
+
origins,
|
|
262
|
+
image_padded_size,
|
|
263
|
+
padded_image_coords,
|
|
264
|
+
original_image_size,
|
|
265
|
+
):
|
|
266
|
+
full_pred: BaseFormat = type(pred_patches.formats[0]).empty(
|
|
267
|
+
canvas_size=image_padded_size
|
|
268
|
+
)
|
|
269
|
+
full_pred = full_pred.generate_scores_from_mask()
|
|
270
|
+
for i, item in enumerate(zip(pred_patches, origins)):
|
|
271
|
+
pred, ori = item
|
|
272
|
+
r_pad = image_padded_size[1] - ori[1] - pred.canvas_size[1]
|
|
273
|
+
b_pad = image_padded_size[0] - ori[0] - pred.canvas_size[0]
|
|
274
|
+
pred = pred.pad(ori[0], ori[1], r_pad, b_pad)[0]
|
|
275
|
+
full_pred += pred
|
|
276
|
+
return full_pred
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
class PostProcesser:
|
|
280
|
+
"""Handles postprocessing
|
|
281
|
+
|
|
282
|
+
Args:
|
|
283
|
+
nms_iou_th (``float``): nms threshold
|
|
284
|
+
final_score_threshold (``float``): final score thresholding
|
|
285
|
+
"""
|
|
286
|
+
|
|
287
|
+
def __init__(self, nms_iou_th: float, final_score_threshold: float):
|
|
288
|
+
self.nms_iou_th = nms_iou_th
|
|
289
|
+
self.final_score_threshold = final_score_threshold
|
|
290
|
+
|
|
291
|
+
def handle_patches_mask(
|
|
292
|
+
self, pred_batch: BatchedFormat, associated_boxes: BboxFormat
|
|
293
|
+
):
|
|
294
|
+
# nms suppression
|
|
295
|
+
nms_filter = self._box_nms_suppress(associated_boxes)
|
|
296
|
+
pred_batch, nms_bool_filter = self._apply_index(pred_batch, nms_filter)
|
|
297
|
+
associated_boxes, _ = associated_boxes[nms_bool_filter]
|
|
298
|
+
pred_batch = BatchedFormat(
|
|
299
|
+
[pred[pred.scores > self.final_score_threshold][0] for pred in pred_batch]
|
|
300
|
+
)
|
|
301
|
+
pred_batch.sanitize()
|
|
302
|
+
return pred_batch
|
|
303
|
+
|
|
304
|
+
def _apply_index(self, pred_batch: BatchedFormat, index: Tensor):
|
|
305
|
+
index_slicer = [patch.nb_object for patch in pred_batch]
|
|
306
|
+
# convert index to bool tensor with shape [N_objs]
|
|
307
|
+
if not index.dtype == torch.bool:
|
|
308
|
+
idx = range(sum(index_slicer))
|
|
309
|
+
index = torch.tensor([i in index for i in idx])
|
|
310
|
+
# create index parser for batch
|
|
311
|
+
index_slicer.insert(0, 0)
|
|
312
|
+
slicer = []
|
|
313
|
+
s = 0
|
|
314
|
+
for i in index_slicer:
|
|
315
|
+
s += i
|
|
316
|
+
slicer.append(s)
|
|
317
|
+
# filter patches
|
|
318
|
+
patch_list = []
|
|
319
|
+
for i, patch in enumerate(pred_batch):
|
|
320
|
+
patch_list.append(patch[index[slicer[i] : slicer[i + 1]]][0])
|
|
321
|
+
return BatchedFormat(patch_list), index
|
|
322
|
+
|
|
323
|
+
def handle_box_duplicates(self, pred: BaseFormat) -> BaseFormat:
|
|
324
|
+
"""Main function that call further function depending on handling mode
|
|
325
|
+
|
|
326
|
+
Args:
|
|
327
|
+
pred (``BaseFormat``): data to handle duplicates
|
|
328
|
+
|
|
329
|
+
Returns:
|
|
330
|
+
``BaseFormat``:
|
|
331
|
+
- handled duplicate new data
|
|
332
|
+
"""
|
|
333
|
+
# nms suppression
|
|
334
|
+
nms_filter = self._box_nms_suppress(pred)
|
|
335
|
+
new_pred, _ = pred[nms_filter]
|
|
336
|
+
new_pred, _ = new_pred.sanitize()
|
|
337
|
+
new_pred, _ = new_pred[new_pred.scores > self.final_score_threshold]
|
|
338
|
+
return new_pred
|
|
339
|
+
|
|
340
|
+
def _box_nms_suppress(self, to_handle: BboxFormat) -> Tensor:
|
|
341
|
+
"""return a filter tensor of objects to keep after nms"""
|
|
342
|
+
indexes_filter = nms(to_handle.data.value, to_handle.scores, self.nms_iou_th)
|
|
343
|
+
return indexes_filter
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
from deepvisiontools import Configuration
|
|
2
|
+
from deepvisiontools.models.basemodel import BaseModel
|
|
3
|
+
from typing import Tuple, Union, Callable, Dict
|
|
4
|
+
from deepvisiontools.preprocessing.preprocessing import build_preprocessing
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
import torch
|
|
7
|
+
from torch import Tensor
|
|
8
|
+
from deepvisiontools.formats import BatchedFormat, BaseFormat
|
|
9
|
+
from typing import List, Union
|
|
10
|
+
from deepvisiontools.utils import visualization
|
|
11
|
+
from deepvisiontools.preprocessing.image import load_image
|
|
12
|
+
from deepvisiontools.inference.patchifier import (
|
|
13
|
+
DetectPatchifier,
|
|
14
|
+
SemanticPatchifier,
|
|
15
|
+
BasePatchifier,
|
|
16
|
+
)
|
|
17
|
+
from torchvision.transforms.v2 import Pad, CenterCrop
|
|
18
|
+
from tqdm import tqdm
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def build_patchifier(*args, **kwargs):
|
|
22
|
+
if Configuration().data_type == "semantic_mask":
|
|
23
|
+
return SemanticPatchifier(*args, **kwargs)
|
|
24
|
+
else:
|
|
25
|
+
return DetectPatchifier(*args, **kwargs)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class Predictor:
|
|
29
|
+
"""Predictor class for deepvisiontools. Load a model and apply on image, get prediction. Can handle patchification for large image prediction.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
model (``Union[BaseModel, str, Path]``): model path / instance of BaseModel to be used.
|
|
33
|
+
preprocessing (``Callable``, **optional**): used preprocesser. Defaults to build_preprocessing().
|
|
34
|
+
patch_size (``Union[Tuple[int, int], None]``, **optional**): size of the patchs to be used for large image inference. If None will run the full image. Defaults to None.
|
|
35
|
+
overlap (``float``, **optional**): Overlap between patches used in case of patchification. Defaults to 0.4.
|
|
36
|
+
border_padding (``int``, **optional**): default image padding when using patchification. Defaults to 100.
|
|
37
|
+
batch_size (``int``, **optional**): batch size for patchification. Defaults to 1.
|
|
38
|
+
border_penalty (``float``, **optional**): apply a penalty on patch border predictions : makes nms more efficient. Higher is more stringent. Max to 1 and Min to 0. Defaults to 0.5.
|
|
39
|
+
nms_iou_threshold (``float``, **optional**): nms threshold to be used when upatchifying. Defaults to 0.45.
|
|
40
|
+
final_score_threshold (``float``, **optional**): Apply a score thresholding after penalty and after nms. Defaults to 0.4.
|
|
41
|
+
categories (``Dict[int, str]``, **optional**): To rename your categories in the visualization.
|
|
42
|
+
patchifier (``Union[BasePatchifier, None], **optional``): If None use default SemanticPatchifier or DetectPatchifier according to Configuration().data_type. Default to None.
|
|
43
|
+
|
|
44
|
+
Example:
|
|
45
|
+
----------
|
|
46
|
+
|
|
47
|
+
.. highlight:: python
|
|
48
|
+
.. code-block:: python
|
|
49
|
+
|
|
50
|
+
>>> from deepvisiontools import Predictor
|
|
51
|
+
>>> img = \"path/to/img\"
|
|
52
|
+
>>> predictor = Predictor(model=\path\to\model.pth)
|
|
53
|
+
>>> results = predictor.predict(img)
|
|
54
|
+
|
|
55
|
+
Attributes
|
|
56
|
+
----------
|
|
57
|
+
|
|
58
|
+
Attributes:
|
|
59
|
+
model (``BaseModel``)
|
|
60
|
+
preprocessing (``Callable``)
|
|
61
|
+
patch_size (``Union[Tuple[int, int], None]``)
|
|
62
|
+
padder (``Transform``)
|
|
63
|
+
batch_size (``int``)
|
|
64
|
+
cropper (``Transform``)
|
|
65
|
+
patchifier (``BasePatchifier``)
|
|
66
|
+
categories (``Dict[int, str]``)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
**Methods**
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
def __init__(
|
|
73
|
+
self,
|
|
74
|
+
model: Union[BaseModel, str, Path],
|
|
75
|
+
preprocessing: Callable = build_preprocessing(),
|
|
76
|
+
patch_size: Union[Tuple[int, int], None] = None,
|
|
77
|
+
overlap: float = 0.4,
|
|
78
|
+
border_padding: int = 100,
|
|
79
|
+
batch_size: int = 1,
|
|
80
|
+
categories: Dict[int, str] = None,
|
|
81
|
+
patchifier: Union[BasePatchifier, None] = None,
|
|
82
|
+
):
|
|
83
|
+
assert any(
|
|
84
|
+
[
|
|
85
|
+
isinstance(model, BaseModel),
|
|
86
|
+
isinstance(model, Path),
|
|
87
|
+
isinstance(model, str),
|
|
88
|
+
]
|
|
89
|
+
), "model must be instance of BaseModel or path to model"
|
|
90
|
+
if not isinstance(model, BaseModel):
|
|
91
|
+
model = Path(model) if isinstance(model, str) else model
|
|
92
|
+
assert model.exists(), f"model path does not exists, got {model.as_posix()}"
|
|
93
|
+
model = torch.load(model, map_location=Configuration().device)
|
|
94
|
+
self.model: BaseModel = model.to(Configuration().device).eval()
|
|
95
|
+
self.patch_size = patch_size
|
|
96
|
+
self.batch_size = batch_size
|
|
97
|
+
self.preprocessing = preprocessing
|
|
98
|
+
self.padder = Pad(border_padding)
|
|
99
|
+
self.cropper = None # is updated in self.predict
|
|
100
|
+
|
|
101
|
+
if patchifier == None:
|
|
102
|
+
self.patchifier = build_patchifier(self.patch_size, overlap)
|
|
103
|
+
else:
|
|
104
|
+
self.patchifier = patchifier
|
|
105
|
+
self.categories = categories
|
|
106
|
+
|
|
107
|
+
def forward_pass(self, batch_patchs: Tensor) -> BatchedFormat:
|
|
108
|
+
"""Run predictions on image / batch of patches"""
|
|
109
|
+
with torch.no_grad():
|
|
110
|
+
loader = PredictorDataLoader(batch_patchs, self.batch_size)
|
|
111
|
+
# wrap in tqdm if more than one batch
|
|
112
|
+
loader = (
|
|
113
|
+
tqdm(loader, desc="Predict on batch of patches : ", total=len(loader))
|
|
114
|
+
if len(loader) > 1
|
|
115
|
+
else loader
|
|
116
|
+
)
|
|
117
|
+
predictions = BatchedFormat([])
|
|
118
|
+
for patch in loader:
|
|
119
|
+
predictions += self.model.get_predictions(patch)
|
|
120
|
+
return predictions
|
|
121
|
+
|
|
122
|
+
def predict(
|
|
123
|
+
self, image: Union[str, Path, Tensor], visu_path: Union[str, Path] = ""
|
|
124
|
+
) -> BaseFormat:
|
|
125
|
+
"""Main function of ```Predictor``` : call everything needed for prediction.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
image (``Union[str, Path, Tensor]``): _description_
|
|
129
|
+
visu_path (``Union[str, Path]``, **optional**): path to visualization to be saved. Defaults to "".
|
|
130
|
+
|
|
131
|
+
Returns:
|
|
132
|
+
``BaseFormat``:
|
|
133
|
+
- prediction as deepvisiontools format.
|
|
134
|
+
"""
|
|
135
|
+
# Load image if needed
|
|
136
|
+
if isinstance(image, str):
|
|
137
|
+
image = Path(image)
|
|
138
|
+
image: Tensor = load_image(image)
|
|
139
|
+
if isinstance(image, Path):
|
|
140
|
+
image = load_image(image)
|
|
141
|
+
# pad image
|
|
142
|
+
self.cropper = CenterCrop(image.shape[-2:])
|
|
143
|
+
h_original, w_original = image.shape[-2:]
|
|
144
|
+
image = image.to(Configuration().device)
|
|
145
|
+
# preprocess
|
|
146
|
+
preprocessed_image = self.preprocessing(image[None, :])[0]
|
|
147
|
+
# handles patch if needed
|
|
148
|
+
if self.patch_size != None and self.patch_size != (
|
|
149
|
+
image.shape[-2],
|
|
150
|
+
image.shape[-1],
|
|
151
|
+
):
|
|
152
|
+
batch_patch, pad_origins, padded_image, pad_coord = (
|
|
153
|
+
self.patchifier.patchify(preprocessed_image)
|
|
154
|
+
)
|
|
155
|
+
h_pad, w_pad = padded_image.shape[-2:]
|
|
156
|
+
preds = self.forward_pass(batch_patch)
|
|
157
|
+
# filter empty patches and associated origins
|
|
158
|
+
preds, pad_origins = self.filter_empty_patches(preds, pad_origins)
|
|
159
|
+
# unpatchification
|
|
160
|
+
preds = self.patchifier.unpatchify(
|
|
161
|
+
preds, pad_origins, (h_pad, w_pad), pad_coord, (h_original, w_original)
|
|
162
|
+
)
|
|
163
|
+
else:
|
|
164
|
+
preds = self.forward_pass(
|
|
165
|
+
preprocessed_image[None, :]
|
|
166
|
+
) # forward_pass need batch image -> add dummy dim
|
|
167
|
+
preds = preds.formats[0]
|
|
168
|
+
# recover pre-border-padding shape
|
|
169
|
+
preds, _, _ = preds.apply_augmentation(image, self.cropper)
|
|
170
|
+
# Create visualization if needed
|
|
171
|
+
if visu_path:
|
|
172
|
+
visu_path = visu_path if isinstance(visu_path, Path) else Path(visu_path)
|
|
173
|
+
visualization(image, preds, categories=self.categories, save_path=visu_path)
|
|
174
|
+
return preds
|
|
175
|
+
|
|
176
|
+
def filter_empty_patches(
|
|
177
|
+
self, preds_batch_patch: BatchedFormat, pad_origins: List[Tuple[int, int]]
|
|
178
|
+
):
|
|
179
|
+
"""remove empty patches for unpatchification"""
|
|
180
|
+
filter_empty = [
|
|
181
|
+
False if pred.nb_object == 0 else True for pred in preds_batch_patch
|
|
182
|
+
]
|
|
183
|
+
pad_origins = [el for i, el in enumerate(pad_origins) if filter_empty[i]]
|
|
184
|
+
preds_batch_patch = preds_batch_patch[torch.tensor(filter_empty)]
|
|
185
|
+
return preds_batch_patch, pad_origins
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
class PredictorDataLoader:
|
|
189
|
+
"""Wrap predictor patchification output as loader with given batch_size for forward"""
|
|
190
|
+
|
|
191
|
+
def __init__(self, patches: Tensor, batch_size: int = 1):
|
|
192
|
+
N_ = patches.shape[0]
|
|
193
|
+
batchs = []
|
|
194
|
+
for i in range(N_ // batch_size + 1):
|
|
195
|
+
if i * batch_size >= N_:
|
|
196
|
+
break
|
|
197
|
+
if (i + 1) * batch_size < N_:
|
|
198
|
+
batchs.append(patches[i * batch_size : (i + 1) * batch_size])
|
|
199
|
+
else:
|
|
200
|
+
batchs.append(patches[i * batch_size :])
|
|
201
|
+
break
|
|
202
|
+
self.batchs = batchs
|
|
203
|
+
|
|
204
|
+
def __iter__(self):
|
|
205
|
+
return iter(self.batchs)
|
|
206
|
+
|
|
207
|
+
def __len__(self):
|
|
208
|
+
return len(self.batchs)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from deepvisiontools.metrics.base_metric import (
|
|
2
|
+
DetectMetric,
|
|
3
|
+
ClassWiseDetectMetric,
|
|
4
|
+
ClassifMetric,
|
|
5
|
+
SemanticSegmentationMetric,
|
|
6
|
+
)
|
|
7
|
+
from deepvisiontools.metrics.available_metrics import (
|
|
8
|
+
DetectAccuracy,
|
|
9
|
+
DetectF1score,
|
|
10
|
+
DetectPrecision,
|
|
11
|
+
DetectRecall,
|
|
12
|
+
ClassWiseDetectAccuracy,
|
|
13
|
+
ClassWiseDetectF1score,
|
|
14
|
+
ClassWiseDetectPrecision,
|
|
15
|
+
ClassWiseDetectRecall,
|
|
16
|
+
ClassifAccuracy,
|
|
17
|
+
ClassifF1score,
|
|
18
|
+
ClassifRecall,
|
|
19
|
+
SemanticAccuracy,
|
|
20
|
+
SemanticF1score,
|
|
21
|
+
SemanticIoU,
|
|
22
|
+
SemanticPrecision,
|
|
23
|
+
SemanticRecall,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
from deepvisiontools.metrics.matcher import Matcher
|
|
27
|
+
|
|
28
|
+
__all__ = (
|
|
29
|
+
"DetectMetric",
|
|
30
|
+
"ClassWiseDetectMetric",
|
|
31
|
+
"ClassifMetric",
|
|
32
|
+
"SemanticSegmentationMetric",
|
|
33
|
+
"Matcher",
|
|
34
|
+
"DetectAccuracy",
|
|
35
|
+
"DetectF1score",
|
|
36
|
+
"DetectPrecision",
|
|
37
|
+
"DetectRecall",
|
|
38
|
+
"ClassWiseDetectAccuracy",
|
|
39
|
+
"ClassWiseDetectF1score",
|
|
40
|
+
"ClassWiseDetectPrecision",
|
|
41
|
+
"ClassWiseDetectRecall",
|
|
42
|
+
"ClassifAccuracy",
|
|
43
|
+
"ClassifF1score",
|
|
44
|
+
"ClassifRecall",
|
|
45
|
+
"SemanticAccuracy",
|
|
46
|
+
"SemanticF1score",
|
|
47
|
+
"SemanticIoU",
|
|
48
|
+
"SemanticPrecision",
|
|
49
|
+
"SemanticRecall",
|
|
50
|
+
)
|