Skip to content

Commit c6031a8

Browse files
committed
Make some api's compatible with python3.7+
1 parent b79f641 commit c6031a8

20 files changed

+63
-54
lines changed

official/nlp/modeling/layers/transformer_encoder_block.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
"""Keras-based TransformerEncoder block layer."""
16-
from typing import Any, Optional, Sequence
16+
from typing import Any, Optional, Sequence, Union
1717
from absl import logging
1818
import tensorflow as tf, tf_keras
1919

@@ -28,7 +28,7 @@ class RMSNorm(tf_keras.layers.Layer):
2828

2929
def __init__(
3030
self,
31-
axis: int | Sequence[int] = -1,
31+
axis: Union[int , Sequence[int]] = -1,
3232
epsilon: float = 1e-6,
3333
**kwargs,
3434
):
@@ -43,7 +43,7 @@ def __init__(
4343
self.axis = [axis] if isinstance(axis, int) else axis
4444
self.epsilon = epsilon
4545

46-
def build(self, input_shape: tf.TensorShape | Sequence[int | None]):
46+
def build(self, input_shape: Union[tf.TensorShape, Sequence[Union[int, None]]]):
4747
input_shape = tf.TensorShape(input_shape)
4848
scale_shape = [1] * input_shape.rank
4949
for dim in self.axis:

official/recommendation/uplift/layers/uplift_networks/base_uplift_networks.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
from official.recommendation.uplift import types
2222

23+
from typing import Union
24+
2325

2426
class BaseTwoTowerUpliftNetwork(tf_keras.layers.Layer, metaclass=abc.ABCMeta):
2527
"""Abstract class for uplift layers that compute control and treatment logits.
@@ -33,7 +35,7 @@ class BaseTwoTowerUpliftNetwork(tf_keras.layers.Layer, metaclass=abc.ABCMeta):
3335
def call(
3436
self,
3537
inputs: types.DictOfTensors,
36-
training: bool | None = None,
37-
mask: tf.Tensor | None = None,
38+
training: Union[bool, None] = None,
39+
mask: Union[tf.Tensor, None] = None,
3840
) -> types.TwoTowerTrainingOutputs:
3941
raise NotImplementedError()

official/recommendation/uplift/metrics/label_mean.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from official.recommendation.uplift import types
2020
from official.recommendation.uplift.metrics import treatment_sliced_metric
2121

22+
from typing import Union
2223

2324
@tf_keras.utils.register_keras_serializable(package="Uplift")
2425
class LabelMean(tf_keras.metrics.Metric):
@@ -71,7 +72,7 @@ def update_state(
7172
self,
7273
y_true: tf.Tensor,
7374
y_pred: types.TwoTowerTrainingOutputs,
74-
sample_weight: tf.Tensor | None = None,
75+
sample_weight: Union[tf.Tensor, None] = None,
7576
):
7677
"""Updates the overall, control and treatment label means.
7778

official/recommendation/uplift/metrics/label_variance.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from official.recommendation.uplift.metrics import treatment_sliced_metric
2121
from official.recommendation.uplift.metrics import variance
2222

23+
from typing import Union
2324

2425
@tf_keras.utils.register_keras_serializable(package="Uplift")
2526
class LabelVariance(tf_keras.metrics.Metric):
@@ -72,7 +73,7 @@ def update_state(
7273
self,
7374
y_true: tf.Tensor,
7475
y_pred: types.TwoTowerTrainingOutputs,
75-
sample_weight: tf.Tensor | None = None,
76+
sample_weight: Union[tf.Tensor, None] = None,
7677
):
7778
"""Updates the overall, control and treatment label variances.
7879

official/recommendation/uplift/metrics/metric_configs.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
from collections.abc import Mapping
1818
import dataclasses
19-
from typing import Any
19+
from typing import Any, Union
2020

2121
from official.core.config_definitions import base_config
2222

2323

24-
@dataclasses.dataclass(kw_only=True)
24+
@dataclasses.dataclass
2525
class SlicedMetricConfig(base_config.Config):
2626
"""Sliced metric configuration.
2727
@@ -33,9 +33,9 @@ class SlicedMetricConfig(base_config.Config):
3333
values to slice on.
3434
"""
3535

36-
slicing_feature: str | None = None
37-
slicing_spec: Mapping[str, int] | None = None
38-
slicing_feature_dtype: str | None = None
36+
slicing_feature: Union[str, None] = None
37+
slicing_spec: Union[Mapping[str, int], None] = None
38+
slicing_feature_dtype: Union[str, None ]= None
3939

4040
def __post_init__(
4141
self, default_params: dict[str, Any], restrictions: list[str]

official/recommendation/uplift/metrics/sliced_metric.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import tensorflow as tf, tf_keras
2020

21+
from typing import Union
22+
2123

2224
class SlicedMetric(tf_keras.metrics.Metric):
2325
"""A metric sliced by integer, boolean, or string features.
@@ -66,9 +68,9 @@ class SlicedMetric(tf_keras.metrics.Metric):
6668
def __init__(
6769
self,
6870
metric: tf_keras.metrics.Metric,
69-
slicing_spec: dict[str, str] | dict[str, int],
70-
slicing_feature_dtype: tf.DType | None = None,
71-
name: str | None = None,
71+
slicing_spec: Union[dict[str, str], dict[str, int]],
72+
slicing_feature_dtype: Union[tf.DType, None] = None,
73+
name: Union[str, None] = None,
7274
):
7375
"""Initializes the instance.
7476
@@ -123,7 +125,7 @@ def __init__(
123125
def update_state(
124126
self,
125127
*args: tf.Tensor,
126-
sample_weight: tf.Tensor | None = None,
128+
sample_weight: Union[tf.Tensor, None] = None,
127129
slicing_feature: tf.Tensor,
128130
**kwargs,
129131
):

official/recommendation/uplift/metrics/treatment_fraction.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from official.recommendation.uplift import types
2020

21+
from typing import Union
2122

2223
@tf_keras.utils.register_keras_serializable(package="Uplift")
2324
class TreatmentFraction(tf_keras.metrics.Metric):
@@ -57,7 +58,7 @@ def update_state(
5758
self,
5859
y_true: tf.Tensor,
5960
y_pred: types.TwoTowerTrainingOutputs,
60-
sample_weight: tf.Tensor | None = None,
61+
sample_weight: Union[tf.Tensor, None] = None,
6162
) -> None:
6263
"""Updates the treatment fraction.
6364

official/recommendation/uplift/metrics/uplift_mean.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from official.recommendation.uplift import types
2020
from official.recommendation.uplift.metrics import treatment_sliced_metric
2121

22+
from typing import Union
2223

2324
@tf_keras.utils.register_keras_serializable(package="Uplift")
2425
class UpliftMean(tf_keras.metrics.Metric):
@@ -68,7 +69,7 @@ def update_state(
6869
self,
6970
y_true: tf.Tensor,
7071
y_pred: types.TwoTowerTrainingOutputs,
71-
sample_weight: tf.Tensor | None = None,
72+
sample_weight: Union[tf.Tensor, None] = None,
7273
) -> None:
7374
"""Updates the overall, control and treatment uplift means.
7475

official/recommendation/uplift/types.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
"""Defines types used by the keras uplift modeling library."""
1616

1717
import tensorflow as tf, tf_keras
18+
from typing import Union
1819

19-
TensorType = tf.Tensor | tf.SparseTensor | tf.RaggedTensor
20+
TensorType = Union[tf.Tensor, tf.SparseTensor, tf.RaggedTensor]
2021

2122
ListOfTensors = list[TensorType]
2223
TupleOfTensors = tuple[TensorType, ...]
2324
DictOfTensors = dict[str, TensorType]
2425

25-
CollectionOfTensors = ListOfTensors | TupleOfTensors | DictOfTensors
26+
CollectionOfTensors = Union[ListOfTensors, TupleOfTensors, DictOfTensors]
2627

2728

2829
class TwoTowerNetworkOutputs(tf.experimental.ExtensionType):

official/vision/configs/retinanet.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Parser(hyperparams.Config):
5353
match_threshold: float = 0.5
5454
unmatched_threshold: float = 0.5
5555
aug_rand_hflip: bool = False
56-
aug_rand_jpeg: common.RandJpegQuality | None = None
56+
aug_rand_jpeg: Union[common.RandJpegQuality, None] = None
5757
aug_scale_min: float = 1.0
5858
aug_scale_max: float = 1.0
5959
skip_crowd_during_training: bool = True

official/vision/dataloaders/retinanet_input.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
into (image, labels) tuple for RetinaNet.
1919
"""
2020

21-
from typing import Optional
21+
from typing import Optional, Union, List
2222

2323
# Import libraries
2424

@@ -39,17 +39,17 @@ class Parser(parser.Parser):
3939

4040
def __init__(self,
4141
output_size,
42-
min_level: int | None,
4342
max_level,
44-
num_scales: int | None,
45-
aspect_ratios: list[float] | None,
46-
anchor_size: float | None,
43+
min_level: Union[int, None] = None,
44+
num_scales: Union[int, None] = None,
45+
aspect_ratios: Union[List[float], None] = None,
46+
anchor_size: Union[float, None] = None,
4747
match_threshold=0.5,
4848
unmatched_threshold=0.5,
4949
box_coder_weights=None,
5050
aug_type=None,
5151
aug_rand_hflip=False,
52-
aug_rand_jpeg: cfg.RandJpegQuality | None = None,
52+
aug_rand_jpeg: Union[cfg.RandJpegQuality, None] = None,
5353
aug_scale_min=1.0,
5454
aug_scale_max=1.0,
5555
use_autoaugment=False,

official/vision/modeling/backbones/mobilenet.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""Contains definitions of MobileNet Networks."""
1616

1717
import dataclasses
18-
from typing import Any
18+
from typing import Any, Union
1919

2020
# Import libraries
2121

@@ -92,9 +92,9 @@ class BlockSpec(hyperparams.Config):
9292
use_normalization: bool = True
9393
activation: str = 'relu6'
9494
# Used for block type InvertedResConv.
95-
expand_ratio: float | None = 6.0
95+
expand_ratio: Union[float, None] = 6.0
9696
# Used for block type InvertedResConv with SE.
97-
se_ratio: float | None = None
97+
se_ratio: Union[float, None] = None
9898
use_depthwise: bool = True
9999
use_residual: bool = True
100100
is_output: bool = True
@@ -145,8 +145,8 @@ def __init__(
145145
use_explicit_padding: bool = False,
146146
activation: str = 'relu6',
147147
kernel_initializer: str = 'VarianceScaling',
148-
kernel_regularizer: tf_keras.regularizers.Regularizer | None = None,
149-
bias_regularizer: tf_keras.regularizers.Regularizer | None = None,
148+
kernel_regularizer: Union[tf_keras.regularizers.Regularizer, None] = None,
149+
bias_regularizer: Union[tf_keras.regularizers.Regularizer, None] = None,
150150
use_normalization: bool = True,
151151
use_sync_bn: bool = False,
152152
norm_momentum: float = 0.99,
@@ -1230,10 +1230,10 @@ def __init__(
12301230
norm_momentum: float = 0.99,
12311231
norm_epsilon: float = 0.001,
12321232
kernel_initializer: str = 'VarianceScaling',
1233-
kernel_regularizer: tf_keras.regularizers.Regularizer | None = None,
1234-
bias_regularizer: tf_keras.regularizers.Regularizer | None = None,
1233+
kernel_regularizer: Union[tf_keras.regularizers.Regularizer, None] = None,
1234+
bias_regularizer: Union[tf_keras.regularizers.Regularizer, None] = None,
12351235
# The followings should be kept the same most of the times.
1236-
output_stride: int | None = None,
1236+
output_stride: Union[int, None] = None,
12371237
min_depth: int = 8,
12381238
# divisible is not used in MobileNetV1.
12391239
divisible_by: int = 8,
@@ -1595,7 +1595,7 @@ def build_mobilenet(
15951595
input_specs: tf_keras.layers.InputSpec,
15961596
backbone_config: hyperparams.Config,
15971597
norm_activation_config: hyperparams.Config,
1598-
l2_regularizer: tf_keras.regularizers.Regularizer | None = None,
1598+
l2_regularizer: Union[tf_keras.regularizers.Regularizer, None] = None,
15991599
) -> tf_keras.Model:
16001600
"""Builds MobileNet backbone from a config."""
16011601
backbone_type = backbone_config.type

official/vision/modeling/factory.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"""Factory methods to build models."""
1616

17-
from typing import Mapping, Optional
17+
from typing import Mapping, Optional, Union, Dict
1818

1919
import tensorflow as tf, tf_keras
2020

@@ -263,8 +263,8 @@ def build_retinanet(
263263
l2_regularizer: Optional[tf_keras.regularizers.Regularizer] = None,
264264
backbone: Optional[tf_keras.Model] = None,
265265
decoder: Optional[tf_keras.Model] = None,
266-
num_anchors_per_location: int | dict[str, int] | None = None,
267-
anchor_boxes: Mapping[str, tf.Tensor] | None = None,
266+
num_anchors_per_location: Union[int, Dict[str, int], None] = None,
267+
anchor_boxes: Union[Mapping[str, tf.Tensor], None] = None,
268268
) -> tf_keras.Model:
269269
"""Builds a RetinaNet model.
270270

official/vision/modeling/heads/dense_prediction_heads.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(
3333
min_level: int,
3434
max_level: int,
3535
num_classes: int,
36-
num_anchors_per_location: int | dict[str, int],
36+
num_anchors_per_location: Union[int, Dict[str, int]] = None,
3737
num_convs: int = 4,
3838
num_filters: int = 256,
3939
attribute_heads: Optional[List[Dict[str, Any]]] = None,

official/vision/modeling/layers/detection_generator.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"""Contains definitions of generators to generate the final detections."""
1616
import contextlib
17-
from typing import Any, Dict, List, Optional, Mapping, Sequence, Tuple
17+
from typing import Any, Dict, List, Optional, Mapping, Sequence, Tuple, Union
1818

1919
# Import libraries
2020

@@ -794,7 +794,7 @@ def _generate_detections_tflite(
794794
raw_scores: Mapping[str, tf.Tensor],
795795
anchor_boxes: Mapping[str, tf.Tensor],
796796
config: Dict[str, Any],
797-
box_coder_weights: List[float] | None = None,
797+
box_coder_weights: Union[List[float], None] = None,
798798
) -> Sequence[Any]:
799799
"""Generate detections for conversion to TFLite.
800800

official/vision/modeling/layers/nn_blocks.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -788,12 +788,12 @@ def __init__(
788788
start_dw_kernel_size: int = 0,
789789
middle_dw_kernel_size: int = 3,
790790
end_dw_kernel_size: int = 0,
791-
stochastic_depth_drop_rate: float | None = None,
791+
stochastic_depth_drop_rate: Union[float, None] = None,
792792
kernel_initializer: str = 'VarianceScaling',
793-
kernel_regularizer: tf_keras.regularizers.Regularizer | None = None,
794-
bias_regularizer: tf_keras.regularizers.Regularizer | None = None,
793+
kernel_regularizer: Union[tf_keras.regularizers.Regularizer, None] = None,
794+
bias_regularizer: Union[tf_keras.regularizers.Regularizer, None] = None,
795795
activation: str = 'relu',
796-
depthwise_activation: str | None = None,
796+
depthwise_activation: Union[str, None] = None,
797797
use_sync_bn: bool = False,
798798
dilation_rate: int = 1,
799799
divisible_by: int = 1,

official/vision/modeling/retinanet_model.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(self,
3131
decoder: tf_keras.Model,
3232
head: tf_keras.layers.Layer,
3333
detection_generator: tf_keras.layers.Layer,
34-
anchor_boxes: Mapping[str, tf.Tensor] | None = None,
34+
anchor_boxes: Union[Mapping[str, tf.Tensor], None] = None,
3535
min_level: Optional[int] = None,
3636
max_level: Optional[int] = None,
3737
num_scales: Optional[int] = None,
@@ -85,7 +85,7 @@ def __init__(self,
8585
def call(self,
8686
images: Union[tf.Tensor, Sequence[tf.Tensor]],
8787
image_shape: Optional[tf.Tensor] = None,
88-
anchor_boxes: Mapping[str, tf.Tensor] | None = None,
88+
anchor_boxes: Union[Mapping[str, tf.Tensor], None] = None,
8989
output_intermediate_features: bool = False,
9090
training: bool = None) -> Mapping[str, tf.Tensor]:
9191
"""Forward pass of the RetinaNet model.
@@ -240,7 +240,7 @@ def detection_generator(self) -> tf_keras.layers.Layer:
240240
return self._detection_generator
241241

242242
@property
243-
def anchor_boxes(self) -> Mapping[str, tf.Tensor] | None:
243+
def anchor_boxes(self) -> Union[Mapping[str, tf.Tensor], None]:
244244
return self._anchor_boxes
245245

246246
def get_config(self) -> Mapping[str, Any]:

official/vision/ops/augment.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2856,7 +2856,7 @@ class SSDRandomCrop(ImageAugment):
28562856

28572857
def __init__(
28582858
self,
2859-
params: Sequence[configs.SSDRandomCropParam] | None = None,
2859+
params: Union[Sequence[configs.SSDRandomCropParam], None] = None,
28602860
aspect_ratio_range: tuple[float, float] = (0.5, 2.0),
28612861
area_range: tuple[float, float] = (0.1, 1.0),
28622862
):

official/vision/ops/preprocess_ops.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1217,9 +1217,9 @@ def random_crop(
12171217

12181218
def random_jpeg_quality(
12191219
image: tf.Tensor,
1220-
min_quality: int | tf.Tensor = 20,
1221-
max_quality: int | tf.Tensor = 100,
1222-
prob_to_apply: float | tf.Tensor = 0.6,
1220+
min_quality: Union[int, tf.Tensor] = 20,
1221+
max_quality: Union[int, tf.Tensor] = 100,
1222+
prob_to_apply: Union[float, tf.Tensor] = 0.6,
12231223
) -> tf.Tensor:
12241224
"""Randomly encode the image as jpeg and decode it.
12251225

0 commit comments

Comments
 (0)