def test_errors(): schema = SchemaDict( { "a": Tensor((None, None), dtype=int, max_shape=(20, 20)), "b": Sequence( dtype=SchemaDict( {"e": Tensor((None,), max_shape=(10,), dtype=BBox(dtype=float))} ) ), "c": Sequence( dtype=SchemaDict({"d": Sequence((), dtype=Tensor((5, 5), dtype=float))}) ), } ) ds = hub.Dataset("./nested_seq", shape=(5,), mode="w", schema=schema) # Invalid schema with pytest.raises(ValueError): ds["b", 0, "e", 1] # Too many indices with pytest.raises(IndexError): ds["c", 0, "d", 1, 1, 0, 0, 0] with pytest.raises(IndexError): ds["c", :2, "d"][0, 1, 1, 0, 0, 0] ob = ds["c", :2, "d"][0, 2:5, 1, 0, 0] assert str(ob[1]) == "ObjectView(subpath='/c/d', indexes=0, slice=[3, 1, 0, 0])" with pytest.raises(IndexError): ob[1, 0] # Key Errors # wrong key with pytest.raises(KeyError): ds["b", "c"] # too many keys with pytest.raises(KeyError): ds["c", "d", "e"] with pytest.raises(KeyError): ds["c", "d"]["e"]
def bbox_to_hub(tf_dt, max_shape=None): dt = tf_dt.dtype.name return BBox(dtype=dt)
def test_objectview(): schema = SchemaDict({ "a": Tensor((20, 20), dtype=int, max_shape=(20, 20)), "b": Sequence(dtype=BBox(dtype=float)), "c": Sequence(dtype=SchemaDict( {"d": Sequence((), dtype=Tensor((5, 5), dtype=float))})), "e": Sequence(dtype={ "f": { "g": Tensor(5, dtype=int), "h": Tensor((), dtype=int) } }), }) ds = hub.Dataset("./nested_seq", shape=(5, ), mode="w", schema=schema) # dataset view to objectview dv = ds[3:5] dv["c", 0] = {"d": 5 * np.ones((2, 2, 5, 5))} assert (dv[0, "c", 0, "d", 0].compute() == 5 * np.ones((5, 5))).all() # dataset view unsqueezed with pytest.raises(IndexError): dv["c", "d"].compute() # dataset unsqueezed with pytest.raises(IndexError): ds["c", "d"].compute() # tensorview to object view # sequence of tensor ds["b", 0] = 0.5 * np.ones((5, 4)) tv = ds["b", 0] tv[0] = 0.3 * np.ones((4, )) assert (tv[0].compute() == 0.3 * np.ones((4, ))).all() # ds to object view assert (ds[3, "c", "d"].compute() == 5 * np.ones((2, 2, 5, 5))).all() # Sequence of schemadicts ds[0, "e"] = {"f": {"g": np.ones((3, 5)), "h": np.array([42, 25, 15])}} with pytest.raises(KeyError): ds[0, "e", 1].compute() assert (ds[0, "e", "f", "h"].compute() == np.array([42, 25, 15])).all() # With dataset view dv[0, "e"] = {"f": {"g": np.ones((3, 5)), "h": np.array([1, 25, 1])}} # dv[0, "e", 1]["f", "h"] = 25 assert (dv[0, "e", "f", "h"].compute() == np.array([1, 25, 1])).all() # If not lazy mode all slices should be stable ds.lazy = False assert ds[0, "e", 0, "f", "h"] == 42 with pytest.raises(KeyError): ds[0, "e", 1]["f", "h"] == 25 ds.lazy = True # make an objectview ov = ds["c", "d"] with pytest.raises(IndexError): ov.compute() assert (ov[3].compute() == 5 * np.ones((2, 2, 5, 5))).all() # ov[3, 1] = 2 * np.ones((2, 5, 5)) assert (ov[3][0, 0].compute() == 5 * np.ones((5, 5))).all() assert (ov[3][1].compute() == 5 * np.ones((2, 5, 5))).all()
def _from_supervisely(project, scheduler: str = "single", workers: int = 1): try: import supervisely_lib as sly from supervisely_lib.project import project as sly_image_project from supervisely_lib.project import video_project as sly_video_project from skvideo.io import FFmpegReader, vread except ModuleNotFoundError: raise ModuleNotInstalledException("supervisely") if isinstance(project, str): with open(project + "meta.json") as meta_file: project_meta_dict = json.load(meta_file) instantiated = False else: project_meta_dict = project.meta.to_json() instantiated = True project_type = project_meta_dict["projectType"] mode = sly.OpenMode.READ def infer_image(paths): bboxes, masks = [], [] classes_bb, classes_mask = [], [] item_path, item_ann_path = paths ann = sly.Annotation.load_json_file(item_ann_path, project.meta) ann_dict = ann.to_json() sizes = (ann_dict["size"]["height"], ann_dict["size"]["width"]) for obj in ann_dict["objects"]: if obj["geometryType"] == "rectangle": bboxes.append([ item for sublist in obj["points"]["exterior"] for item in sublist ]) classes_bb.append(obj["classTitle"]) elif obj["geometryType"] == "polygon": img = PIL.Image.new("L", (sizes[1], sizes[0]), 0) PIL.ImageDraw.Draw(img).polygon( [tuple(obj) for obj in obj["points"]["exterior"]], outline=1, fill=1, ) masks.append(np.array(img)) classes_mask.append(obj["classTitle"]) return sizes, bboxes, masks, classes_bb, classes_mask def infer_video(paths): item_path, item_ann_path = paths vreader = FFmpegReader(item_path) return (vreader.getShape(), ) def infer_project(project, project_type, read_mode): if project_type == "images": if not instantiated: project = sly_image_project.Project(project, mode) max_shape = (0, 0) return ( project, Image, infer_image, max_shape, ) elif project_type == "videos": if not instantiated: project = sly_video_project.VideoProject(project, mode) max_shape = (0, 0, 0, 0) return ( project, Video, infer_video, max_shape, ) project, main_blob, infer_ds, max_shape = infer_project( project, project_type, mode) image_paths = [] label_names = [] max_num_bboxes = 0 max_num_polys = 0 masks = False datasets = project.datasets.items() uniform = True for ds in datasets: for i, item in enumerate(ds): path = ds.get_item_paths(item) image_paths.append(path) inf = infer_ds(path) if len(inf) > 1: if inf[3]: label_names.extend(inf[3]) if len(inf[3]) > max_num_bboxes: max_num_bboxes = len(inf[3]) if inf[4]: label_names.extend(inf[4]) if len(inf[4]) > max_num_polys: max_num_polys = len(inf[4]) if inf[2]: masks = True shape = inf[0] max_shape = np.maximum(shape, max_shape) if uniform and max_shape.any() and (shape != max_shape).any(): uniform = False label_names = list(np.unique(label_names)) items = chain(*datasets) idatasets = iter(datasets) ds, i = next(idatasets), 0 key = "shape" if uniform else "max_shape" if project_type == "images": read = sly.imaging.image.read blob_shape = {key: (*max_shape.tolist(), 3)} elif project_type == "videos": read = vread blob_shape = {key: max_shape.tolist()} if key == "max_shape": blob_shape["shape"] = (None, None, None, 3) schema = { project_type: main_blob(**blob_shape), } if max_num_bboxes: schema["bbox"] = BBox(shape=(None, 4), max_shape=(max_num_bboxes, 4)) if label_names: schema["label"] = ClassLabel( shape=(None, ), max_shape=(max(max_num_bboxes, max_num_polys), ), names=label_names, ) if masks: schema["mask"] = Mask(shape=(None, None, None), max_shape=(*max_shape.tolist(), 1)) @hub.transform(schema=schema, scheduler=scheduler, workers=workers) def transformation(item): nonlocal i, ds sample = {} if i >= len(ds): ds, i = next(idatasets), 0 item_path, item_ann_path = ds.get_item_paths(item) i += 1 _, bboxes, masks, classes_bbox, classes_mask = infer_ds( (item_path, item_ann_path)) sample[project_type] = read(item_path) if bboxes: sample["bbox"] = np.array(bboxes) sample["label"] = [label_names.index(i) for i in classes_bbox] if masks: sample["mask"] = np.expand_dims(masks[0], -1) sample["label"] = [label_names.index(i) for i in classes_mask] return sample return transformation(list(items))