1414# ==============================================================================
1515"""Tensorflow Python ops and utilities for generating network inputs."""
1616
17+ import functools
1718import random
1819import re
1920from typing import Any , Callable , Optional , Sequence
@@ -553,7 +554,7 @@ def soften_labels(bool_labels, softness=0.05, scope='soften_labels'):
553554 Tensor with same shape as bool_labels with dtype `float32` and values 0.05
554555 for False and 0.95 for True.
555556 """
556- with tf .op_scope ([ bool_labels , softness ], scope ):
557+ with tf .name_scope ( scope ):
557558 label_shape = tf .shape (bool_labels , name = 'label_shape' )
558559 return tf .where (bool_labels ,
559560 tf .fill (label_shape , 1.0 - softness , name = 'soft_true' ),
@@ -703,6 +704,35 @@ def sample(
703704 return sampled_dataset
704705
705706
707+ @functools .lru_cache (maxsize = None )
708+ def _parse_bounding_boxes (
709+ volinfo_map_string : str , use_bboxes : bool = True
710+ ) -> dict [bytes , list [bounding_box .BoundingBox ]]:
711+ boxes_by_volname = {}
712+ for mapping in volinfo_map_string .split (',' ):
713+ k , volinfo_path = mapping .split (':' )
714+ k = k .encode ('utf-8' )
715+ assert k not in boxes_by_volname
716+
717+ if volinfo_path .endswith ('metadata.json' ):
718+ f = open (volinfo_path , 'r' )
719+ meta = metadata .VolumeMetadata .from_json (f .read ())
720+ if use_bboxes :
721+ bboxes = meta .bounding_boxes
722+ else :
723+ bboxes = [
724+ bounding_box .BoundingBox (
725+ (0 , 0 , 0 ),
726+ (meta .volume_size .x , meta .volume_size .y , meta .volume_size .z ),
727+ )
728+ ]
729+ boxes_by_volname [k ] = bboxes
730+
731+ if not boxes_by_volname :
732+ raise ValueError ('boxes_by_volname is empty.' )
733+ return boxes_by_volname
734+
735+
706736def coordinates_in_bounds (
707737 coordinates : tf .Tensor ,
708738 volname : tf .Tensor ,
@@ -734,28 +764,7 @@ def coordinates_in_bounds(
734764 coordinates or an empty constant of shape `[0, 3]`, which can then be
735765 passed to batching (e.g. see tests).
736766 """
737- boxes_by_volname = {}
738- for mapping in volinfo_map_string .split (',' ):
739- k , volinfo_path = mapping .split (':' )
740- k = k .encode ('utf-8' )
741- assert k not in boxes_by_volname
742-
743- if volinfo_path .endswith ('metadata.json' ):
744- f = open (volinfo_path , 'r' )
745- meta = metadata .VolumeMetadata .from_json (f .read ())
746- if use_bboxes :
747- bboxes = meta .bounding_boxes
748- else :
749- bboxes = [
750- bounding_box .BoundingBox (
751- (0 , 0 , 0 ),
752- (meta .volume_size .x , meta .volume_size .y , meta .volume_size .z ),
753- )
754- ]
755- boxes_by_volname [k ] = bboxes
756-
757- if not boxes_by_volname :
758- raise ValueError ('boxes_by_volname is empty.' )
767+ boxes_by_volname = _parse_bounding_boxes (volinfo_map_string , use_bboxes )
759768
760769 def _in_bounds_fn (coordinates , volname ):
761770 boxes = boxes_by_volname [volname [0 ]]
@@ -767,8 +776,8 @@ def _in_bounds_fn(coordinates, volname):
767776 return False
768777
769778 with tf .name_scope (name , values = [coordinates , volname ]) as scope :
770- assert coordinates .shape_as_list () == [1 , 3 ]
771- assert volname .shape_as_list () == [1 ]
779+ assert coordinates .shape . as_list () == [1 , 3 ]
780+ assert volname .shape . as_list () == [1 ]
772781 in_bounds = tf .py_func (
773782 _in_bounds_fn ,
774783 [coordinates , volname ],
0 commit comments