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

update ultralytics #41

Merged
merged 3 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
huggingface_hub
fire
ultralytics>=8.0.17,<8.0.19
ultralytics>=8.0.18,<8.0.20
sahi>=0.11.11,<0.12.0
pandas
pandas
2 changes: 1 addition & 1 deletion ultralyticsplus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .hf_utils import download_from_hub, push_to_hfhub
from .ultralytics_utils import YOLO, postprocess_classify_output, render_result

__version__ = "0.0.19"
__version__ = "0.0.20"
71 changes: 3 additions & 68 deletions ultralyticsplus/ultralytics_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
import numpy as np
from PIL import Image
from sahi.prediction import ObjectPrediction, PredictionScore
from sahi.utils.cv import (
get_bool_mask_from_coco_segmentation,
read_image_as_pil,
visualize_object_predictions,
)
from sahi.utils.cv import (get_bool_mask_from_coco_segmentation,
read_image_as_pil, visualize_object_predictions)
from ultralytics import YOLO as YOLOBase
from ultralytics.nn.tasks import attempt_load_one_weight

Expand Down Expand Up @@ -67,69 +64,7 @@ def _load_from_hf_hub(self, weights: str, hf_token=None):
self.overrides = self.model.args
self._reset_ckpt_args(self.overrides)
self.ModelClass, self.TrainerClass, self.ValidatorClass, self.PredictorClass = \
self._guess_ops_from_task(self.task)


def render_model_output_legacy(image, model: YOLO, model_output: dict) -> Image.Image:
"""
Renders predictions on the image

Args:
image (str, URL, Image.Image): image to be rendered
model (YOLO): YOLO model
model_output: output of the model. This is the output of the model.predict() method.
It is a dictionary with keys "det" and "segment".

Returns:
Image.Image: Image with predictions
"""
if model.overrides["task"] not in ["detect", "segment"]:
raise ValueError(
f"Model task must be either 'detect' or 'segment'. Got {model.overrides['task']}"
)

image = read_image_as_pil(image)
np_image = np.ascontiguousarray(image)

names = model.model.names

det = model_output.get("det", None)
segment = model_output.get("segment", None)

object_predictions = []
if det is not None:
det_ind = 0
for *xyxy, conf, cls in det:
if segment:
segmentation = [segment[det_ind].ravel().tolist()]
bool_mask = get_bool_mask_from_coco_segmentation(
segmentation, width=np_image.shape[1], height=np_image.shape[0]
)
if sum(sum(bool_mask == 1)) == 0:
continue
object_prediction = ObjectPrediction.from_coco_segmentation(
segmentation=segmentation,
category_name=names[int(cls)],
category_id=int(cls),
full_shape=[np_image.shape[0], np_image.shape[1]],
)
object_prediction.score = PredictionScore(value=conf)
else:
object_prediction = ObjectPrediction(
bbox=xyxy,
category_name=names[int(cls)],
category_id=int(cls),
score=conf,
)
object_predictions.append(object_prediction)
det_ind += 1

result = visualize_object_predictions(
image=np_image,
object_prediction_list=object_predictions,
)

return Image.fromarray(result["image"])
self._assign_ops_from_task(self.task)


def render_result(
Expand Down