@@ -203,7 +203,10 @@ def crop_by_metadata_bbox_(
203
203
when no image has been loaded)
204
204
"""
205
205
if self .bbox_xywh is None :
206
- raise ValueError ("Attempted cropping by metadata with empty bounding box" )
206
+ raise ValueError (
207
+ "Attempted cropping by metadata with empty bounding box. Consider either"
208
+ " to remove_empty_masks or turn off box_crop in the dataset config."
209
+ )
207
210
208
211
if not self ._uncropped :
209
212
raise ValueError (
@@ -528,12 +531,7 @@ def __post_init__(self) -> None:
528
531
"Make sure it is set in either FrameDataBuilder or Dataset params."
529
532
)
530
533
531
- if self .path_manager is None :
532
- dataset_root_exists = os .path .isdir (self .dataset_root ) # pyre-ignore
533
- else :
534
- dataset_root_exists = self .path_manager .isdir (self .dataset_root )
535
-
536
- if load_any_blob and not dataset_root_exists :
534
+ if load_any_blob and not self ._exists_in_dataset_root ("" ):
537
535
raise ValueError (
538
536
f"dataset_root is passed but { self .dataset_root } does not exist."
539
537
)
@@ -604,14 +602,27 @@ def build(
604
602
frame_data .image_size_hw = image_size_hw # original image size
605
603
# image size after crop/resize
606
604
frame_data .effective_image_size_hw = image_size_hw
605
+ image_path = None
606
+ dataset_root = self .dataset_root
607
+ if frame_annotation .image .path is not None and dataset_root is not None :
608
+ image_path = os .path .join (dataset_root , frame_annotation .image .path )
609
+ frame_data .image_path = image_path
607
610
608
611
if load_blobs and self .load_images :
609
- (
610
- frame_data .image_rgb ,
611
- frame_data .image_path ,
612
- ) = self ._load_images (frame_annotation , frame_data .fg_probability )
612
+ if image_path is None :
613
+ raise ValueError ("Image path is required to load images." )
614
+
615
+ image_np = load_image (self ._local_path (image_path ))
616
+ frame_data .image_rgb = self ._postprocess_image (
617
+ image_np , frame_annotation .image .size , frame_data .fg_probability
618
+ )
613
619
614
- if load_blobs and self .load_depths and frame_annotation .depth is not None :
620
+ if (
621
+ load_blobs
622
+ and self .load_depths
623
+ and frame_annotation .depth is not None
624
+ and frame_annotation .depth .path is not None
625
+ ):
615
626
(
616
627
frame_data .depth_map ,
617
628
frame_data .depth_path ,
@@ -652,44 +663,42 @@ def _load_fg_probability(
652
663
653
664
return fg_probability , full_path
654
665
655
- def _load_images (
666
+ def _postprocess_image (
656
667
self ,
657
- entry : types .FrameAnnotation ,
668
+ image_np : np .ndarray ,
669
+ image_size : Tuple [int , int ],
658
670
fg_probability : Optional [torch .Tensor ],
659
- ) -> Tuple [torch .Tensor , str ]:
660
- assert self .dataset_root is not None and entry .image is not None
661
- path = os .path .join (self .dataset_root , entry .image .path )
662
- image_rgb = load_image (self ._local_path (path ))
671
+ ) -> torch .Tensor :
672
+ image_rgb = safe_as_tensor (image_np , torch .float )
663
673
664
- if image_rgb .shape [- 2 :] != entry .image .size :
665
- raise ValueError (
666
- f"bad image size: { image_rgb .shape [- 2 :]} vs { entry .image .size } !"
667
- )
674
+ if image_rgb .shape [- 2 :] != image_size :
675
+ raise ValueError (f"bad image size: { image_rgb .shape [- 2 :]} vs { image_size } !" )
668
676
669
677
if self .mask_images :
670
678
assert fg_probability is not None
671
679
image_rgb *= fg_probability
672
680
673
- return image_rgb , path
681
+ return image_rgb
674
682
675
683
def _load_mask_depth (
676
684
self ,
677
685
entry : types .FrameAnnotation ,
678
686
fg_probability : Optional [torch .Tensor ],
679
687
) -> Tuple [torch .Tensor , str , torch .Tensor ]:
680
688
entry_depth = entry .depth
681
- assert self .dataset_root is not None and entry_depth is not None
682
- path = os .path .join (self .dataset_root , entry_depth .path )
689
+ dataset_root = self .dataset_root
690
+ assert dataset_root is not None
691
+ assert entry_depth is not None and entry_depth .path is not None
692
+ path = os .path .join (dataset_root , entry_depth .path )
683
693
depth_map = load_depth (self ._local_path (path ), entry_depth .scale_adjustment )
684
694
685
695
if self .mask_depths :
686
696
assert fg_probability is not None
687
697
depth_map *= fg_probability
688
698
689
- if self .load_depth_masks :
690
- assert entry_depth .mask_path is not None
691
- # pyre-ignore
692
- mask_path = os .path .join (self .dataset_root , entry_depth .mask_path )
699
+ mask_path = entry_depth .mask_path
700
+ if self .load_depth_masks and mask_path is not None :
701
+ mask_path = os .path .join (dataset_root , mask_path )
693
702
depth_mask = load_depth_mask (self ._local_path (mask_path ))
694
703
else :
695
704
depth_mask = torch .ones_like (depth_map )
@@ -745,6 +754,16 @@ def _local_path(self, path: str) -> str:
745
754
return path
746
755
return self .path_manager .get_local_path (path )
747
756
757
+ def _exists_in_dataset_root (self , relpath ) -> bool :
758
+ if not self .dataset_root :
759
+ return False
760
+
761
+ full_path = os .path .join (self .dataset_root , relpath )
762
+ if self .path_manager is None :
763
+ return os .path .exists (full_path )
764
+ else :
765
+ return self .path_manager .exists (full_path )
766
+
748
767
749
768
@registry .register
750
769
class FrameDataBuilder (GenericWorkaround , GenericFrameDataBuilder [FrameData ]):
0 commit comments