Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skip_resize for model.predict #1605

Merged
merged 22 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a6e8d9c
first working version without test
Louis-Dupont Nov 2, 2023
3a447d8
Merge branch 'master' into feature/SG-1146-add_skip_resize_in_predict
Louis-Dupont Nov 6, 2023
6cf8cc9
add
Louis-Dupont Nov 6, 2023
93b1827
Merge branch 'master' into feature/SG-1146-add_skip_resize_in_predict
Louis-Dupont Nov 6, 2023
9117d58
add tests
Louis-Dupont Nov 6, 2023
14cff69
Merge branch 'master' into feature/SG-1146-add_skip_resize_in_predict
Louis-Dupont Nov 6, 2023
2922599
move to _get_pipeline
Louis-Dupont Nov 6, 2023
94d5eb2
fix
Louis-Dupont Nov 6, 2023
04687cc
Merge branch 'master' into feature/SG-1146-add_skip_resize_in_predict
Louis-Dupont Nov 7, 2023
8b428da
fix test
Louis-Dupont Nov 7, 2023
0292e05
Merge branch 'master' into feature/SG-1146-add_skip_resize_in_predict
Louis-Dupont Nov 8, 2023
e68a3a2
Way to fix bug with validation frequency (#1601)
hakuryuu96 Nov 8, 2023
1e21627
Merge branch 'master' into feature/SG-1146-add_skip_resize_in_predict
Louis-Dupont Nov 9, 2023
3874c86
add images and update autopadding responsability
Louis-Dupont Nov 9, 2023
d4300af
add example of visualization w/o resizing
Louis-Dupont Nov 9, 2023
919fb57
add docstring
Louis-Dupont Nov 9, 2023
b4bd4bd
remove unwanted prints
Louis-Dupont Nov 9, 2023
7fcafa4
Merge branch 'master' into feature/SG-1146-add_skip_resize_in_predict
BloodAxe Nov 9, 2023
a22b56f
Merge branch 'master' into feature/SG-1146-add_skip_resize_in_predict
Louis-Dupont Nov 12, 2023
31cbaeb
Merge branch 'master' into feature/SG-1146-add_skip_resize_in_predict
BloodAxe Nov 13, 2023
5a07a43
add explicit auto_paddign
Louis-Dupont Nov 13, 2023
b57718e
Merge branch 'master' into feature/SG-1146-add_skip_resize_in_predict
BloodAxe Nov 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ def set_dataset_processing_params(self, class_names: Optional[List[str]] = None,
self._image_processor = image_processor or self._image_processor

@lru_cache(maxsize=1)
def _get_pipeline(self, fuse_model: bool = True) -> ClassificationPipeline:
def _get_pipeline(self, fuse_model: bool = True, skip_image_resizing: bool = False) -> ClassificationPipeline:
"""Instantiate the prediction pipeline of this model.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
"""
if None in (self._class_names, self._image_processor):
raise RuntimeError(
Expand All @@ -44,22 +45,25 @@ def _get_pipeline(self, fuse_model: bool = True) -> ClassificationPipeline:
image_processor=self._image_processor,
class_names=self._class_names,
fuse_model=fuse_model,
skip_image_resizing=skip_image_resizing,
)
return pipeline

def predict(self, images: ImageSource, batch_size: int = 32, fuse_model: bool = True) -> ImagesClassificationPrediction:
def predict(self, images: ImageSource, batch_size: int = 32, fuse_model: bool = True, skip_image_resizing: bool = False) -> ImagesClassificationPrediction:
"""Predict an image or a list of images.

:param images: Images to predict.
:param batch_size: Maximum number of images to process at the same time.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
BloodAxe marked this conversation as resolved.
Show resolved Hide resolved
"""
pipeline = self._get_pipeline(fuse_model=fuse_model)
pipeline = self._get_pipeline(fuse_model=fuse_model, skip_image_resizing=skip_image_resizing)
return pipeline(images, batch_size=batch_size) # type: ignore

def predict_webcam(self, fuse_model: bool = True) -> None:
def predict_webcam(self, fuse_model: bool = True, skip_image_resizing: bool = False) -> None:
"""Predict using webcam.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
"""
pipeline = self._get_pipeline(fuse_model=fuse_model)
pipeline = self._get_pipeline(fuse_model=fuse_model, skip_image_resizing=skip_image_resizing)
pipeline.predict_webcam()
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,16 @@ def get_processing_params(self) -> Optional[Processing]:
return self._image_processor

@lru_cache(maxsize=1)
def _get_pipeline(self, iou: Optional[float] = None, conf: Optional[float] = None, fuse_model: bool = True) -> DetectionPipeline:
def _get_pipeline(
self, iou: Optional[float] = None, conf: Optional[float] = None, fuse_model: bool = True, skip_image_resizing: bool = False
) -> DetectionPipeline:
"""Instantiate the prediction pipeline of this model.

:param iou: (Optional) IoU threshold for the nms algorithm. If None, the default value associated to the training is used.
:param conf: (Optional) Below the confidence threshold, prediction are discarded.
If None, the default value associated to the training is used.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
"""
if None in (self._class_names, self._image_processor, self._default_nms_iou, self._default_nms_conf):
raise RuntimeError(
Expand All @@ -178,6 +181,7 @@ def _get_pipeline(self, iou: Optional[float] = None, conf: Optional[float] = Non
post_prediction_callback=self.get_post_prediction_callback(iou=iou, conf=conf),
class_names=self._class_names,
fuse_model=fuse_model,
skip_image_resizing=skip_image_resizing,
)
return pipeline

Expand All @@ -188,6 +192,7 @@ def predict(
conf: Optional[float] = None,
batch_size: int = 32,
fuse_model: bool = True,
skip_image_resizing: bool = False,
) -> ImagesDetectionPrediction:
"""Predict an image or a list of images.

Expand All @@ -197,19 +202,21 @@ def predict(
If None, the default value associated to the training is used.
:param batch_size: Maximum number of images to process at the same time.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
"""
pipeline = self._get_pipeline(iou=iou, conf=conf, fuse_model=fuse_model)
pipeline = self._get_pipeline(iou=iou, conf=conf, fuse_model=fuse_model, skip_image_resizing=skip_image_resizing)
return pipeline(images, batch_size=batch_size) # type: ignore

def predict_webcam(self, iou: Optional[float] = None, conf: Optional[float] = None, fuse_model: bool = True):
def predict_webcam(self, iou: Optional[float] = None, conf: Optional[float] = None, fuse_model: bool = True, skip_image_resizing: bool = False):
"""Predict using webcam.

:param iou: (Optional) IoU threshold for the nms algorithm. If None, the default value associated to the training is used.
:param conf: (Optional) Below the confidence threshold, prediction are discarded.
If None, the default value associated to the training is used.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
"""
pipeline = self._get_pipeline(iou=iou, conf=conf, fuse_model=fuse_model)
pipeline = self._get_pipeline(iou=iou, conf=conf, fuse_model=fuse_model, skip_image_resizing=skip_image_resizing)
pipeline.predict_webcam()

def train(self, mode: bool = True):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,16 @@ def set_dataset_processing_params(
self._default_nms_conf = conf or self._default_nms_conf

@lru_cache(maxsize=1)
def _get_pipeline(self, iou: Optional[float] = None, conf: Optional[float] = None, fuse_model: bool = True) -> DetectionPipeline:
def _get_pipeline(
self, iou: Optional[float] = None, conf: Optional[float] = None, fuse_model: bool = True, skip_image_resizing: bool = False
) -> DetectionPipeline:
"""Instantiate the prediction pipeline of this model.

:param iou: (Optional) IoU threshold for the nms algorithm. If None, the default value associated to the training is used.
:param conf: (Optional) Below the confidence threshold, prediction are discarded.
If None, the default value associated to the training is used.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
"""
if None in (self._class_names, self._image_processor, self._default_nms_iou, self._default_nms_conf):
raise RuntimeError(
Expand All @@ -181,6 +184,7 @@ def predict(
conf: Optional[float] = None,
batch_size: int = 32,
fuse_model: bool = True,
skip_image_resizing: bool = False,
) -> ImagesDetectionPrediction:
"""Predict an image or a list of images.

Expand All @@ -190,19 +194,21 @@ def predict(
If None, the default value associated to the training is used.
:param batch_size: Maximum number of images to process at the same time.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
"""
pipeline = self._get_pipeline(iou=iou, conf=conf, fuse_model=fuse_model)
pipeline = self._get_pipeline(iou=iou, conf=conf, fuse_model=fuse_model, skip_image_resizing=skip_image_resizing)
return pipeline(images, batch_size=batch_size) # type: ignore

def predict_webcam(self, iou: Optional[float] = None, conf: Optional[float] = None, fuse_model: bool = True):
def predict_webcam(self, iou: Optional[float] = None, conf: Optional[float] = None, fuse_model: bool = True, skip_image_resizing: bool = False):
"""Predict using webcam.

:param iou: (Optional) IoU threshold for the nms algorithm. If None, the default value associated to the training is used.
:param conf: (Optional) Below the confidence threshold, prediction are discarded.
If None, the default value associated to the training is used.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
"""
pipeline = self._get_pipeline(iou=iou, conf=conf, fuse_model=fuse_model)
pipeline = self._get_pipeline(iou=iou, conf=conf, fuse_model=fuse_model, skip_image_resizing=skip_image_resizing)
pipeline.predict_webcam()

def train(self, mode: bool = True):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,16 @@ def set_dataset_processing_params(
self._default_nms_conf = conf or self._default_nms_conf

@lru_cache(maxsize=1)
def _get_pipeline(self, iou: Optional[float] = None, conf: Optional[float] = None, fuse_model: bool = True) -> DetectionPipeline:
def _get_pipeline(
self, iou: Optional[float] = None, conf: Optional[float] = None, fuse_model: bool = True, skip_image_resizing: bool = False
) -> DetectionPipeline:
"""Instantiate the prediction pipeline of this model.

:param iou: (Optional) IoU threshold for the nms algorithm. If None, the default value associated to the training is used.
:param conf: (Optional) Below the confidence threshold, prediction are discarded.
If None, the default value associated to the training is used.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
"""
if None in (self._class_names, self._image_processor, self._default_nms_iou, self._default_nms_conf):
raise RuntimeError(
Expand All @@ -558,6 +561,7 @@ def _get_pipeline(self, iou: Optional[float] = None, conf: Optional[float] = Non
post_prediction_callback=self.get_post_prediction_callback(iou=iou, conf=conf),
class_names=self._class_names,
fuse_model=fuse_model,
skip_image_resizing=skip_image_resizing,
)
return pipeline

Expand All @@ -568,6 +572,7 @@ def predict(
conf: Optional[float] = None,
batch_size: int = 32,
fuse_model: bool = True,
skip_image_resizing: bool = False,
) -> ImagesDetectionPrediction:
"""Predict an image or a list of images.

Expand All @@ -577,19 +582,21 @@ def predict(
If None, the default value associated to the training is used.
:param batch_size: Maximum number of images to process at the same time.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
"""
pipeline = self._get_pipeline(iou=iou, conf=conf, fuse_model=fuse_model)
pipeline = self._get_pipeline(iou=iou, conf=conf, fuse_model=fuse_model, skip_image_resizing=skip_image_resizing)
return pipeline(images, batch_size=batch_size) # type: ignore

def predict_webcam(self, iou: Optional[float] = None, conf: Optional[float] = None, fuse_model: bool = True):
def predict_webcam(self, iou: Optional[float] = None, conf: Optional[float] = None, fuse_model: bool = True, skip_image_resizing: bool = False):
"""Predict using webcam.

:param iou: (Optional) IoU threshold for the nms algorithm. If None, the default value associated to the training is used.
:param conf: (Optional) Below the confidence threshold, prediction are discarded.
If None, the default value associated to the training is used.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
"""
pipeline = self._get_pipeline(iou=iou, conf=conf, fuse_model=fuse_model)
pipeline = self._get_pipeline(iou=iou, conf=conf, fuse_model=fuse_model, skip_image_resizing=skip_image_resizing)
pipeline.predict_webcam()

def train(self, mode: bool = True):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,12 +583,13 @@ def set_dataset_processing_params(
self._default_nms_conf = conf or self._default_nms_conf

@lru_cache(maxsize=1)
def _get_pipeline(self, conf: Optional[float] = None, fuse_model: bool = True) -> PoseEstimationPipeline:
def _get_pipeline(self, conf: Optional[float] = None, fuse_model: bool = True, skip_image_resizing: bool = False) -> PoseEstimationPipeline:
"""Instantiate the prediction pipeline of this model.

:param conf: (Optional) Below the confidence threshold, prediction are discarded.
If None, the default value associated to the training is used.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
"""
if None in (self._edge_links, self._image_processor, self._default_nms_conf):
raise RuntimeError(
Expand All @@ -614,29 +615,34 @@ def _get_pipeline(self, conf: Optional[float] = None, fuse_model: bool = True) -
keypoint_colors=self._keypoint_colors,
post_prediction_callback=self.get_post_prediction_callback(conf=conf),
fuse_model=fuse_model,
skip_image_resizing=skip_image_resizing,
)
return pipeline

def predict(self, images: ImageSource, conf: Optional[float] = None, batch_size: int = 32, fuse_model: bool = True) -> ImagesPoseEstimationPrediction:
def predict(
self, images: ImageSource, conf: Optional[float] = None, batch_size: int = 32, fuse_model: bool = True, skip_image_resizing: bool = False
) -> ImagesPoseEstimationPrediction:
"""Predict an image or a list of images.

:param images: Images to predict.
:param conf: (Optional) Below the confidence threshold, prediction are discarded.
If None, the default value associated to the training is used.
:param batch_size: Maximum number of images to process at the same time.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
"""
pipeline = self._get_pipeline(conf=conf, fuse_model=fuse_model)
pipeline = self._get_pipeline(conf=conf, fuse_model=fuse_model, skip_image_resizing=skip_image_resizing)
return pipeline(images, batch_size=batch_size) # type: ignore

def predict_webcam(self, conf: Optional[float] = None, fuse_model: bool = True):
def predict_webcam(self, conf: Optional[float] = None, fuse_model: bool = True, skip_image_resizing: bool = False):
"""Predict using webcam.

:param conf: (Optional) Below the confidence threshold, prediction are discarded.
If None, the default value associated to the training is used.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param fuse_model: If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
:param skip_image_resizing: If True, the image processor will not resize the images.
"""
pipeline = self._get_pipeline(conf=conf, fuse_model=fuse_model)
pipeline = self._get_pipeline(conf=conf, fuse_model=fuse_model, skip_image_resizing=skip_image_resizing)
pipeline.predict_webcam()

def train(self, mode: bool = True):
Expand Down
Loading