-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
237 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import numpy as np | ||
|
||
from data_juicer.utils.constant import Fields | ||
from data_juicer.utils.lazy_loader import LazyLoader | ||
from data_juicer.utils.mm_utils import load_data_with_context, load_image | ||
from data_juicer.utils.model_utils import get_model, prepare_model | ||
|
||
from ..base_op import OPERATORS, UNFORKABLE, Mapper | ||
from ..op_fusion import LOADED_IMAGES | ||
|
||
OP_NAME = 'image_segment_mapper' | ||
|
||
torch = LazyLoader('torch', 'torch') | ||
ultralytics = LazyLoader('ultralytics', 'ultralytics') | ||
|
||
|
||
@UNFORKABLE.register_module(OP_NAME) | ||
@OPERATORS.register_module(OP_NAME) | ||
@LOADED_IMAGES.register_module(OP_NAME) | ||
class ImageSegmentMapper(Mapper): | ||
"""Perform segment-anything on images and return the bounding boxes.""" | ||
|
||
_accelerator = 'cuda' | ||
|
||
def __init__(self, imgsz=1024, conf=0.05, iou=0.5, *args, **kwargs): | ||
""" | ||
Initialization method. | ||
:param imgsz: resolution for image resizing | ||
:param conf: confidence score threshold | ||
:param iou: IoU (Intersection over Union) score threshold | ||
""" | ||
super().__init__(*args, **kwargs) | ||
|
||
self.model_key = prepare_model(model_type='fastsam', | ||
model_path='FastSAM-x.pt') | ||
|
||
self.imgsz = imgsz | ||
self.conf = conf | ||
self.iou = iou | ||
|
||
def process_single(self, sample, rank=None, context=False): | ||
# there is no image in this sample | ||
if self.image_key not in sample or not sample[self.image_key]: | ||
# N x M x 4 for N images, M boxes, 4 coords | ||
sample[Fields.bbox_tag] = np.empty((0, 0, 4), dtype=np.float32) | ||
return sample | ||
|
||
loaded_image_keys = sample[self.image_key] | ||
sample, images = load_data_with_context(sample, context, | ||
loaded_image_keys, load_image) | ||
|
||
model = get_model(self.model_key, rank=rank, use_cuda=self.use_cuda()) | ||
sample[Fields.bbox_tag] = [] | ||
|
||
for image in images: | ||
masks = model(image, | ||
retina_masks=True, | ||
imgsz=self.imgsz, | ||
conf=self.conf, | ||
iou=self.iou, | ||
verbose=False)[0] | ||
# breakpoint() | ||
sample[Fields.bbox_tag].append(masks.boxes.xywh.cpu().numpy()) | ||
|
||
# match schema | ||
if len(sample[Fields.bbox_tag]) == 0: | ||
sample[Fields.bbox_tag] = np.empty((0, 0, 4), dtype=np.float32) | ||
return sample |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.