Skip to content

Commit bf85450

Browse files
authored
[*.py] Use f-strings and os.path.join throughout (#21343)
* [*.py] Use f-strings and `os.path.join` throughout * [*.py] Use f-strings and `os.path.join` throughout ; `ruff format` * [keras/src/backend/common/variables.py] Add missing `import os.path` ; [keras/src/ops/ops_test.py] Use f-string over concatenation * [*] Run `pre-commit run --all-files` and resolve found issues * [keras/src/utils/summary_utils.py] Remove unrelated to f-string & `os.path.join` code * [*.py] Use `os.path.join` and f-strings everywhere * [*.py] `ruff format --config pyproject.toml` * [*.py] `pre-commit run --all-files`
1 parent 0cbd234 commit bf85450

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+388
-386
lines changed

api_gen.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,13 @@ def create_legacy_directory(package_dir):
8484
for fname in fnames:
8585
if fname.endswith(".py"):
8686
legacy_fpath = os.path.join(root, fname)
87-
tf_keras_root = root.replace("/_legacy", "/_tf_keras/keras")
87+
tf_keras_root = root.replace(
88+
os.path.join(os.path.sep, "_legacy"),
89+
os.path.join(os.path.sep, "_tf_keras", "keras"),
90+
)
8891
core_api_fpath = os.path.join(
89-
root.replace("/_legacy", ""), fname
92+
root.replace(os.path.join(os.path.sep, "_legacy"), ""),
93+
fname,
9094
)
9195
if not os.path.exists(tf_keras_root):
9296
os.makedirs(tf_keras_root)
@@ -125,7 +129,7 @@ def create_legacy_directory(package_dir):
125129
r"\n",
126130
core_api_contents,
127131
)
128-
legacy_contents = core_api_contents + "\n" + legacy_contents
132+
legacy_contents = f"{core_api_contents}\n{legacy_contents}"
129133
with open(tf_keras_fpath, "w") as f:
130134
f.write(legacy_contents)
131135

guides/distributed_training_with_tensorflow.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ def make_or_restore_model():
194194
# Either restore the latest model, or create a fresh one
195195
# if there is no checkpoint available.
196196
checkpoints = [
197-
checkpoint_dir + "/" + name for name in os.listdir(checkpoint_dir)
197+
os.path.join(checkpoint_dir, name)
198+
for name in os.listdir(checkpoint_dir)
198199
]
199200
if checkpoints:
200201
latest_checkpoint = max(checkpoints, key=os.path.getctime)
@@ -216,7 +217,7 @@ def run_training(epochs=1):
216217
# This callback saves a SavedModel every epoch
217218
# We include the current epoch in the folder name.
218219
keras.callbacks.ModelCheckpoint(
219-
filepath=checkpoint_dir + "/ckpt-{epoch}.keras",
220+
filepath=os.path.join(checkpoint_dir, "ckpt-{epoch}.keras"),
220221
save_freq="epoch",
221222
)
222223
]

guides/training_with_built_in_methods.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,8 @@ def make_or_restore_model():
11331133
# Either restore the latest model, or create a fresh one
11341134
# if there is no checkpoint available.
11351135
checkpoints = [
1136-
checkpoint_dir + "/" + name for name in os.listdir(checkpoint_dir)
1136+
os.path.join(checkpoint_dir, name)
1137+
for name in os.listdir(checkpoint_dir)
11371138
]
11381139
if checkpoints:
11391140
latest_checkpoint = max(checkpoints, key=os.path.getctime)
@@ -1148,7 +1149,8 @@ def make_or_restore_model():
11481149
# This callback saves the model every 100 batches.
11491150
# We include the training loss in the saved model name.
11501151
keras.callbacks.ModelCheckpoint(
1151-
filepath=checkpoint_dir + "/model-loss={loss:.2f}.keras", save_freq=100
1152+
filepath=os.path.join(checkpoint_dir, "model-loss={loss:.2f}.keras"),
1153+
save_freq=100,
11521154
)
11531155
]
11541156
model.fit(x_train, y_train, epochs=1, callbacks=callbacks)

integration_tests/import_test.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,19 @@ def create_virtualenv():
4242
# Create virtual environment
4343
"python3 -m venv test_env",
4444
]
45-
os.environ["PATH"] = (
46-
"/test_env/bin/" + os.pathsep + os.environ.get("PATH", "")
45+
os.environ["PATH"] = os.pathsep.join(
46+
(
47+
os.path.join(os.getcwd(), "test_env", "bin"),
48+
os.environ.get("PATH", ""),
49+
)
4750
)
51+
if os.name == "nt":
52+
os.environ["PATH"] = os.pathsep.join(
53+
(
54+
os.path.join(os.getcwd(), "test_env", "Scripts"),
55+
os.environ["PATH"],
56+
)
57+
)
4858
run_commands_local(env_setup)
4959

5060

@@ -53,18 +63,17 @@ def manage_venv_installs(whl_path):
5363
backend_pkg, backend_extra_url = BACKEND_REQ[backend.backend()]
5464
install_setup = [
5565
# Installs the backend's package and common requirements
56-
"pip install " + backend_extra_url + backend_pkg,
66+
f"pip install {backend_extra_url}{backend_pkg}",
5767
"pip install -r requirements-common.txt",
5868
"pip install pytest",
5969
# Ensure other backends are uninstalled
60-
"pip uninstall -y "
61-
+ BACKEND_REQ[other_backends[0]][0]
62-
+ " "
63-
+ BACKEND_REQ[other_backends[1]][0]
64-
+ " "
65-
+ BACKEND_REQ[other_backends[2]][0],
70+
"pip uninstall -y {0} {1} {2}".format(
71+
BACKEND_REQ[other_backends[0]][0],
72+
BACKEND_REQ[other_backends[1]][0],
73+
BACKEND_REQ[other_backends[2]][0],
74+
),
6675
# Install `.whl` package
67-
"pip install " + whl_path,
76+
f"pip install {whl_path}",
6877
]
6978
# Install flax for JAX when NNX is enabled
7079
if backend.backend() == "jax" and config.is_nnx_enabled():
@@ -102,7 +111,11 @@ def run_commands_venv(commands):
102111
for command in commands:
103112
print(f"Running command: {command}")
104113
cmd_with_args = command.split(" ")
105-
cmd_with_args[0] = "test_env/bin/" + cmd_with_args[0]
114+
cmd_with_args[0] = os.path.join(
115+
"test_env",
116+
"Scripts" if os.name == "nt" else "bin",
117+
cmd_with_args[0],
118+
)
106119
p = subprocess.Popen(cmd_with_args)
107120
assert p.wait() == 0
108121

integration_tests/model_visualization_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def get_node_dict(graph, path=""):
4444

4545
for subgraph in graph.get_subgraphs():
4646
sub_nodes = get_node_dict(
47-
subgraph, path=path + subgraph.get_label() + " > "
47+
subgraph, path=f"{path}{subgraph.get_label()} > "
4848
)
4949
nodes.update(sub_nodes)
5050

@@ -85,7 +85,7 @@ def get_edges(graph):
8585
class ModelVisualizationTest(testing.TestCase):
8686
def multi_plot_model(self, model, name, expand_nested=False):
8787
if expand_nested:
88-
name = name + "-expand_nested"
88+
name = f"{name}-expand_nested"
8989

9090
TEST_CASES = [
9191
{},
@@ -130,7 +130,7 @@ def multi_plot_model(self, model, name, expand_nested=False):
130130

131131
for test_case in TEST_CASES:
132132
tags = [v if k == "rankdir" else k for k, v in test_case.items()]
133-
file_name = "-".join([name] + tags) + ".png"
133+
file_name = f"{'-'.join([name] + tags)}.png"
134134
plot_model(
135135
model, file_name, expand_nested=expand_nested, **test_case
136136
)

keras/src/applications/convnext.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def ConvNeXtBlock(
244244
A function representing a ConvNeXtBlock block.
245245
"""
246246
if name is None:
247-
name = "prestem" + str(backend.get_uid("prestem"))
247+
name = f"prestem{str(backend.get_uid('prestem'))}"
248248

249249
def apply(inputs):
250250
x = inputs
@@ -254,25 +254,25 @@ def apply(inputs):
254254
kernel_size=7,
255255
padding="same",
256256
groups=projection_dim,
257-
name=name + "_depthwise_conv",
257+
name=f"{name}_depthwise_conv",
258258
)(x)
259-
x = layers.LayerNormalization(epsilon=1e-6, name=name + "_layernorm")(x)
260-
x = layers.Dense(4 * projection_dim, name=name + "_pointwise_conv_1")(x)
261-
x = layers.Activation("gelu", name=name + "_gelu")(x)
262-
x = layers.Dense(projection_dim, name=name + "_pointwise_conv_2")(x)
259+
x = layers.LayerNormalization(epsilon=1e-6, name=f"{name}_layernorm")(x)
260+
x = layers.Dense(4 * projection_dim, name=f"{name}_pointwise_conv_1")(x)
261+
x = layers.Activation("gelu", name=f"{name}_gelu")(x)
262+
x = layers.Dense(projection_dim, name=f"{name}_pointwise_conv_2")(x)
263263

264264
if layer_scale_init_value is not None:
265265
x = LayerScale(
266266
layer_scale_init_value,
267267
projection_dim,
268-
name=name + "_layer_scale",
268+
name=f"{name}_layer_scale",
269269
)(x)
270270
if drop_path_rate:
271271
layer = StochasticDepth(
272-
drop_path_rate, name=name + "_stochastic_depth"
272+
drop_path_rate, name=f"{name}_stochastic_depth"
273273
)
274274
else:
275-
layer = layers.Activation("linear", name=name + "_identity")
275+
layer = layers.Activation("linear", name=f"{name}_identity")
276276

277277
return inputs + layer(x)
278278

@@ -282,7 +282,7 @@ def apply(inputs):
282282
def PreStem(name=None):
283283
"""Normalizes inputs with ImageNet-1k mean and std."""
284284
if name is None:
285-
name = "prestem" + str(backend.get_uid("prestem"))
285+
name = "prestem{0}".format(str(backend.get_uid("prestem")))
286286

287287
def apply(x):
288288
x = layers.Normalization(
@@ -292,7 +292,7 @@ def apply(x):
292292
(0.224 * 255) ** 2,
293293
(0.225 * 255) ** 2,
294294
],
295-
name=name + "_prestem_normalization",
295+
name=f"{name}_prestem_normalization",
296296
)(x)
297297
return x
298298

@@ -314,14 +314,14 @@ def Head(num_classes=1000, classifier_activation=None, name=None):
314314
name = str(backend.get_uid("head"))
315315

316316
def apply(x):
317-
x = layers.GlobalAveragePooling2D(name=name + "_head_gap")(x)
317+
x = layers.GlobalAveragePooling2D(name=f"{name}_head_gap")(x)
318318
x = layers.LayerNormalization(
319-
epsilon=1e-6, name=name + "_head_layernorm"
319+
epsilon=1e-6, name=f"{name}_head_layernorm"
320320
)(x)
321321
x = layers.Dense(
322322
num_classes,
323323
activation=classifier_activation,
324-
name=name + "_head_dense",
324+
name=f"{name}_head_dense",
325325
)(x)
326326
return x
327327

@@ -452,13 +452,13 @@ def ConvNeXt(
452452
projection_dims[0],
453453
kernel_size=4,
454454
strides=4,
455-
name=name + "_stem_conv",
455+
name=f"{name}_stem_conv",
456456
),
457457
layers.LayerNormalization(
458-
epsilon=1e-6, name=name + "_stem_layernorm"
458+
epsilon=1e-6, name=f"{name}_stem_layernorm"
459459
),
460460
],
461-
name=name + "_stem",
461+
name=f"{name}_stem",
462462
)
463463

464464
# Downsampling blocks.
@@ -471,16 +471,16 @@ def ConvNeXt(
471471
[
472472
layers.LayerNormalization(
473473
epsilon=1e-6,
474-
name=name + "_downsampling_layernorm_" + str(i),
474+
name=f"{name}_downsampling_layernorm_{i}",
475475
),
476476
layers.Conv2D(
477477
projection_dims[i + 1],
478478
kernel_size=2,
479479
strides=2,
480-
name=name + "_downsampling_conv_" + str(i),
480+
name=f"{name}_downsampling_conv_{i}",
481481
),
482482
],
483-
name=name + "_downsampling_block_" + str(i),
483+
name=f"{name}_downsampling_block_{i}",
484484
)
485485
downsample_layers.append(downsample_layer)
486486

keras/src/applications/densenet.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@
1010
"https://storage.googleapis.com/tensorflow/keras-applications/densenet/"
1111
)
1212
DENSENET121_WEIGHT_PATH = (
13-
BASE_WEIGHTS_PATH + "densenet121_weights_tf_dim_ordering_tf_kernels.h5"
13+
f"{BASE_WEIGHTS_PATH}densenet121_weights_tf_dim_ordering_tf_kernels.h5"
1414
)
1515
DENSENET121_WEIGHT_PATH_NO_TOP = (
16-
BASE_WEIGHTS_PATH
17-
+ "densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5"
16+
f"{BASE_WEIGHTS_PATH}"
17+
"densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5"
1818
)
1919
DENSENET169_WEIGHT_PATH = (
20-
BASE_WEIGHTS_PATH + "densenet169_weights_tf_dim_ordering_tf_kernels.h5"
20+
f"{BASE_WEIGHTS_PATH}densenet169_weights_tf_dim_ordering_tf_kernels.h5"
2121
)
2222
DENSENET169_WEIGHT_PATH_NO_TOP = (
23-
BASE_WEIGHTS_PATH
24-
+ "densenet169_weights_tf_dim_ordering_tf_kernels_notop.h5"
23+
f"{BASE_WEIGHTS_PATH}"
24+
"densenet169_weights_tf_dim_ordering_tf_kernels_notop.h5"
2525
)
2626
DENSENET201_WEIGHT_PATH = (
27-
BASE_WEIGHTS_PATH + "densenet201_weights_tf_dim_ordering_tf_kernels.h5"
27+
f"{BASE_WEIGHTS_PATH}densenet201_weights_tf_dim_ordering_tf_kernels.h5"
2828
)
2929
DENSENET201_WEIGHT_PATH_NO_TOP = (
30-
BASE_WEIGHTS_PATH
31-
+ "densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5"
30+
f"{BASE_WEIGHTS_PATH}"
31+
"densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5"
3232
)
3333

3434

@@ -44,7 +44,7 @@ def dense_block(x, blocks, name):
4444
Output tensor for the block.
4545
"""
4646
for i in range(blocks):
47-
x = conv_block(x, 32, name=name + "_block" + str(i + 1))
47+
x = conv_block(x, 32, name=f"{name}_block{i + 1}")
4848
return x
4949

5050

@@ -61,16 +61,16 @@ def transition_block(x, reduction, name):
6161
"""
6262
bn_axis = 3 if backend.image_data_format() == "channels_last" else 1
6363
x = layers.BatchNormalization(
64-
axis=bn_axis, epsilon=1.001e-5, name=name + "_bn"
64+
axis=bn_axis, epsilon=1.001e-5, name=f"{name}_bn"
6565
)(x)
66-
x = layers.Activation("relu", name=name + "_relu")(x)
66+
x = layers.Activation("relu", name=f"{name}_relu")(x)
6767
x = layers.Conv2D(
6868
int(x.shape[bn_axis] * reduction),
6969
1,
7070
use_bias=False,
71-
name=name + "_conv",
71+
name=f"{name}_conv",
7272
)(x)
73-
x = layers.AveragePooling2D(2, strides=2, name=name + "_pool")(x)
73+
x = layers.AveragePooling2D(2, strides=2, name=f"{name}_pool")(x)
7474
return x
7575

7676

@@ -87,20 +87,20 @@ def conv_block(x, growth_rate, name):
8787
"""
8888
bn_axis = 3 if backend.image_data_format() == "channels_last" else 1
8989
x1 = layers.BatchNormalization(
90-
axis=bn_axis, epsilon=1.001e-5, name=name + "_0_bn"
90+
axis=bn_axis, epsilon=1.001e-5, name=f"{name}_0_bn"
9191
)(x)
92-
x1 = layers.Activation("relu", name=name + "_0_relu")(x1)
92+
x1 = layers.Activation("relu", name=f"{name}_0_relu")(x1)
9393
x1 = layers.Conv2D(
94-
4 * growth_rate, 1, use_bias=False, name=name + "_1_conv"
94+
4 * growth_rate, 1, use_bias=False, name=f"{name}_1_conv"
9595
)(x1)
9696
x1 = layers.BatchNormalization(
97-
axis=bn_axis, epsilon=1.001e-5, name=name + "_1_bn"
97+
axis=bn_axis, epsilon=1.001e-5, name=f"{name}_1_bn"
9898
)(x1)
99-
x1 = layers.Activation("relu", name=name + "_1_relu")(x1)
99+
x1 = layers.Activation("relu", name=f"{name}_1_relu")(x1)
100100
x1 = layers.Conv2D(
101-
growth_rate, 3, padding="same", use_bias=False, name=name + "_2_conv"
101+
growth_rate, 3, padding="same", use_bias=False, name=f"{name}_2_conv"
102102
)(x1)
103-
x = layers.Concatenate(axis=bn_axis, name=name + "_concat")([x, x1])
103+
x = layers.Concatenate(axis=bn_axis, name=f"{name}_concat")([x, x1])
104104
return x
105105

106106

0 commit comments

Comments
 (0)