Skip to content

Commit 15f2742

Browse files
author
chenmoneygithub
committed
qMerge branch 'main' into chen-run-torch-benchmark
2 parents e597ad4 + 7ee847d commit 15f2742

24 files changed

+840
-87
lines changed

benchmarks/model_benchmark/image_classification_benchmark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Image classification benchmark.
22
3-
This scripts runs image classification benchmark with dogs vs cats datasets. It
3+
This script runs image classification benchmark with "dogs vs cats" datasets. It
44
supports the following 3 models:
55
- EfficientNetV2B0
66
- Xception

examples/demo_custom_layer_backend_agnostic.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import keras_core
44
from keras_core import Model
5-
from keras_core import backend
65
from keras_core import initializers
76
from keras_core import layers
87
from keras_core import losses

examples/demo_custom_torch_workflow.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def train(model, train_loader, num_epochs, optimizer, loss_fn):
9898
######## Using a Keras model or layer in a torch Module ########
9999
################################################################
100100

101+
101102
class MyModel(nn.Module):
102103
def __init__(self):
103104
super().__init__()
@@ -126,4 +127,4 @@ def forward(self, x):
126127
# Instantiate the torch loss function
127128
loss_fn = nn.CrossEntropyLoss()
128129

129-
train(torch_module, train_loader, num_epochs, optimizer, loss_fn)
130+
train(torch_module, train_loader, num_epochs, optimizer, loss_fn)

examples/demo_jax_distributed.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ def make_model():
157157
# data will be split along the batch axis
158158
data_mesh = Mesh(devices, axis_names=("batch",)) # naming axes of the mesh
159159
# naming axes of the sharded partition
160-
data_sharding = NamedSharding(data_mesh,P("batch",),)
160+
data_sharding = NamedSharding(
161+
data_mesh,
162+
P(
163+
"batch",
164+
),
165+
)
161166

162167
# all variables will be replicated on all devices
163168
var_mesh = Mesh(devices, axis_names=("_"))

examples/keras_io/structured_data/collaborative_filtering_movielens.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import keras_core as keras
4141
from keras_core import layers
4242
from keras_core import ops
43+
4344
"""
4445
## First, load the data and apply preprocessing
4546
"""
@@ -97,7 +98,11 @@
9798
df = df.sample(frac=1, random_state=42)
9899
x = df[["user", "movie"]].values
99100
# Normalize the targets between 0 and 1. Makes it easy to train.
100-
y = df["rating"].apply(lambda x: (x - min_rating) / (max_rating - min_rating)).values
101+
y = (
102+
df["rating"]
103+
.apply(lambda x: (x - min_rating) / (max_rating - min_rating))
104+
.values
105+
)
101106
# Assuming training on 90% of the data and validating on 10%.
102107
train_indices = int(0.9 * df.shape[0])
103108
x_train, x_val, y_train, y_val = (
@@ -204,7 +209,8 @@ def call(self, inputs):
204209
ratings = model.predict(user_movie_array).flatten()
205210
top_ratings_indices = ratings.argsort()[-10:][::-1]
206211
recommended_movie_ids = [
207-
movie_encoded2movie.get(movies_not_watched[x][0]) for x in top_ratings_indices
212+
movie_encoded2movie.get(movies_not_watched[x][0])
213+
for x in top_ratings_indices
208214
]
209215

210216
print("Showing recommendations for user: {}".format(user_id))

examples/keras_io/tensorflow/vision/image_captioning.py

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ def train_val_split(caption_data, train_size=0.8, shuffle=True):
178178

179179
def custom_standardization(input_string):
180180
lowercase = tf.strings.lower(input_string)
181-
return tf.strings.regex_replace(lowercase, "[%s]" % re.escape(strip_chars), "")
181+
return tf.strings.regex_replace(
182+
lowercase, "[%s]" % re.escape(strip_chars), ""
183+
)
182184

183185

184186
strip_chars = "!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
@@ -263,7 +265,9 @@ def get_cnn_model():
263265
# We freeze our feature extractor
264266
base_model.trainable = False
265267
base_model_out = base_model.output
266-
base_model_out = layers.Reshape((-1, base_model_out.shape[-1]))(base_model_out)
268+
base_model_out = layers.Reshape((-1, base_model_out.shape[-1]))(
269+
base_model_out
270+
)
267271
cnn_model = keras.models.Model(base_model.input, base_model_out)
268272
return cnn_model
269273

@@ -342,7 +346,9 @@ def __init__(self, embed_dim, ff_dim, num_heads, **kwargs):
342346
self.layernorm_3 = layers.LayerNormalization()
343347

344348
self.embedding = PositionalEmbedding(
345-
embed_dim=EMBED_DIM, sequence_length=SEQ_LENGTH, vocab_size=VOCAB_SIZE
349+
embed_dim=EMBED_DIM,
350+
sequence_length=SEQ_LENGTH,
351+
vocab_size=VOCAB_SIZE,
346352
)
347353
self.out = layers.Dense(VOCAB_SIZE, activation="softmax")
348354

@@ -394,7 +400,10 @@ def get_causal_attention_mask(self, inputs):
394400
mask = tf.cast(i >= j, dtype="int32")
395401
mask = tf.reshape(mask, (1, input_shape[1], input_shape[1]))
396402
mult = tf.concat(
397-
[tf.expand_dims(batch_size, -1), tf.constant([1, 1], dtype=tf.int32)],
403+
[
404+
tf.expand_dims(batch_size, -1),
405+
tf.constant([1, 1], dtype=tf.int32),
406+
],
398407
axis=0,
399408
)
400409
return tf.tile(mask, mult)
@@ -431,7 +440,9 @@ def calculate_accuracy(self, y_true, y_pred, mask):
431440
mask = tf.cast(mask, dtype=tf.float32)
432441
return tf.reduce_sum(accuracy) / tf.reduce_sum(mask)
433442

434-
def _compute_caption_loss_and_acc(self, img_embed, batch_seq, training=True):
443+
def _compute_caption_loss_and_acc(
444+
self, img_embed, batch_seq, training=True
445+
):
435446
encoder_out = self.encoder(img_embed, training=training)
436447
batch_seq_inp = batch_seq[:, :-1]
437448
batch_seq_true = batch_seq[:, 1:]
@@ -469,7 +480,8 @@ def train_step(self, batch_data):
469480

470481
# 4. Get the list of all the trainable weights
471482
train_vars = (
472-
self.encoder.trainable_variables + self.decoder.trainable_variables
483+
self.encoder.trainable_variables
484+
+ self.decoder.trainable_variables
473485
)
474486

475487
# 5. Get the gradients
@@ -484,7 +496,10 @@ def train_step(self, batch_data):
484496
self.acc_tracker.update_state(batch_acc)
485497

486498
# 8. Return the loss and accuracy values
487-
return {"loss": self.loss_tracker.result(), "acc": self.acc_tracker.result()}
499+
return {
500+
"loss": self.loss_tracker.result(),
501+
"acc": self.acc_tracker.result(),
502+
}
488503

489504
def test_step(self, batch_data):
490505
batch_img, batch_seq = batch_data
@@ -513,7 +528,10 @@ def test_step(self, batch_data):
513528
self.acc_tracker.update_state(batch_acc)
514529

515530
# 5. Return the loss and accuracy values
516-
return {"loss": self.loss_tracker.result(), "acc": self.acc_tracker.result()}
531+
return {
532+
"loss": self.loss_tracker.result(),
533+
"acc": self.acc_tracker.result(),
534+
}
517535

518536
@property
519537
def metrics(self):
@@ -523,8 +541,12 @@ def metrics(self):
523541

524542

525543
cnn_model = get_cnn_model()
526-
encoder = TransformerEncoderBlock(embed_dim=EMBED_DIM, dense_dim=FF_DIM, num_heads=1)
527-
decoder = TransformerDecoderBlock(embed_dim=EMBED_DIM, ff_dim=FF_DIM, num_heads=2)
544+
encoder = TransformerEncoderBlock(
545+
embed_dim=EMBED_DIM, dense_dim=FF_DIM, num_heads=1
546+
)
547+
decoder = TransformerDecoderBlock(
548+
embed_dim=EMBED_DIM, ff_dim=FF_DIM, num_heads=2
549+
)
528550
caption_model = ImageCaptioningModel(
529551
cnn_model=cnn_model,
530552
encoder=encoder,
@@ -539,15 +561,20 @@ def metrics(self):
539561

540562
# Define the loss function
541563
cross_entropy = keras.losses.SparseCategoricalCrossentropy(
542-
from_logits=False, reduction=None,
564+
from_logits=False,
565+
reduction=None,
543566
)
544567

545568
# EarlyStopping criteria
546-
early_stopping = keras.callbacks.EarlyStopping(patience=3, restore_best_weights=True)
569+
early_stopping = keras.callbacks.EarlyStopping(
570+
patience=3, restore_best_weights=True
571+
)
547572

548573

549574
# Learning Rate Scheduler for the optimizer
550-
class LRSchedule(keras.optimizers.schedules.learning_rate_schedule.LearningRateSchedule):
575+
class LRSchedule(
576+
keras.optimizers.schedules.learning_rate_schedule.LearningRateSchedule
577+
):
551578
def __init__(self, post_warmup_learning_rate, warmup_steps):
552579
super().__init__()
553580
self.post_warmup_learning_rate = post_warmup_learning_rate
@@ -568,10 +595,14 @@ def __call__(self, step):
568595
# Create a learning rate schedule
569596
num_train_steps = len(train_dataset) * EPOCHS
570597
num_warmup_steps = num_train_steps // 15
571-
lr_schedule = LRSchedule(post_warmup_learning_rate=1e-4, warmup_steps=num_warmup_steps)
598+
lr_schedule = LRSchedule(
599+
post_warmup_learning_rate=1e-4, warmup_steps=num_warmup_steps
600+
)
572601

573602
# Compile the model
574-
caption_model.compile(optimizer=keras.optimizers.Adam(lr_schedule), loss=cross_entropy)
603+
caption_model.compile(
604+
optimizer=keras.optimizers.Adam(lr_schedule), loss=cross_entropy
605+
)
575606

576607
# Fit the model
577608
caption_model.fit(
@@ -639,4 +670,4 @@ def generate_caption():
639670
this example easily runnable, we have trained it with a few constraints, like a minimal
640671
number of attention heads. To improve the predictions, you can try changing these training
641672
settings and find a good model for your use case.
642-
"""
673+
"""

examples/keras_io/tensorflow/vision/image_classification_with_vision_transformer.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@
5858
projection_dim,
5959
] # Size of the transformer layers
6060
transformer_layers = 8
61-
mlp_head_units = [2048, 1024] # Size of the dense layers of the final classifier
61+
mlp_head_units = [
62+
2048,
63+
1024,
64+
] # Size of the dense layers of the final classifier
6265

6366

6467
"""
@@ -218,7 +221,9 @@ def create_vit_classifier():
218221
representation = layers.Flatten()(representation)
219222
representation = layers.Dropout(0.5)(representation)
220223
# Add MLP.
221-
features = mlp(representation, hidden_units=mlp_head_units, dropout_rate=0.5)
224+
features = mlp(
225+
representation, hidden_units=mlp_head_units, dropout_rate=0.5
226+
)
222227
# Classify outputs.
223228
logits = layers.Dense(num_classes)(features)
224229
# Create the Keras model.
@@ -241,7 +246,9 @@ def run_experiment(model):
241246
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
242247
metrics=[
243248
keras.metrics.SparseCategoricalAccuracy(name="accuracy"),
244-
keras.metrics.SparseTopKCategoricalAccuracy(5, name="top-5-accuracy"),
249+
keras.metrics.SparseTopKCategoricalAccuracy(
250+
5, name="top-5-accuracy"
251+
),
245252
],
246253
)
247254

@@ -288,4 +295,4 @@ def run_experiment(model):
288295
but also by parameters such as the learning rate schedule, optimizer, weight decay, etc.
289296
In practice, it's recommended to fine-tune a ViT model
290297
that was pre-trained using a large, high-resolution dataset.
291-
"""
298+
"""

examples/keras_io/tensorflow/vision/involution.py

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ def build(self, input_shape):
123123
keras.layers.BatchNormalization(),
124124
keras.layers.ReLU(),
125125
keras.layers.Conv2D(
126-
filters=self.kernel_size * self.kernel_size * self.group_number,
126+
filters=self.kernel_size
127+
* self.kernel_size
128+
* self.group_number,
127129
kernel_size=1,
128130
),
129131
]
@@ -198,22 +200,39 @@ def call(self, x):
198200

199201
# Compute involution with stride 1.
200202
output_tensor, _ = Involution(
201-
channel=3, group_number=1, kernel_size=5, stride=1, reduction_ratio=1, name="inv_1"
203+
channel=3,
204+
group_number=1,
205+
kernel_size=5,
206+
stride=1,
207+
reduction_ratio=1,
208+
name="inv_1",
202209
)(input_tensor)
203210
print(f"with stride 1 ouput shape: {output_tensor.shape}")
204211

205212
# Compute involution with stride 2.
206213
output_tensor, _ = Involution(
207-
channel=3, group_number=1, kernel_size=5, stride=2, reduction_ratio=1, name="inv_2"
214+
channel=3,
215+
group_number=1,
216+
kernel_size=5,
217+
stride=2,
218+
reduction_ratio=1,
219+
name="inv_2",
208220
)(input_tensor)
209221
print(f"with stride 2 ouput shape: {output_tensor.shape}")
210222

211223
# Compute involution with stride 1, channel 16 and reduction ratio 2.
212224
output_tensor, _ = Involution(
213-
channel=16, group_number=1, kernel_size=5, stride=1, reduction_ratio=2, name="inv_3"
225+
channel=16,
226+
group_number=1,
227+
kernel_size=5,
228+
stride=1,
229+
reduction_ratio=2,
230+
name="inv_3",
214231
)(input_tensor)
215232
print(
216-
"with channel 16 and reduction ratio 2 ouput shape: {}".format(output_tensor.shape)
233+
"with channel 16 and reduction ratio 2 ouput shape: {}".format(
234+
output_tensor.shape
235+
)
217236
)
218237

219238
"""
@@ -250,7 +269,9 @@ def call(self, x):
250269
.shuffle(256)
251270
.batch(256)
252271
)
253-
test_ds = tf.data.Dataset.from_tensor_slices((test_images, test_labels)).batch(256)
272+
test_ds = tf.data.Dataset.from_tensor_slices((test_images, test_labels)).batch(
273+
256
274+
)
254275

255276
"""
256277
## Visualise the data
@@ -287,7 +308,9 @@ def call(self, x):
287308
print("building the convolution model...")
288309
conv_model = keras.Sequential(
289310
[
290-
keras.layers.Conv2D(32, (3, 3), input_shape=(32, 32, 3), padding="same"),
311+
keras.layers.Conv2D(
312+
32, (3, 3), input_shape=(32, 32, 3), padding="same"
313+
),
291314
keras.layers.ReLU(name="relu1"),
292315
keras.layers.MaxPooling2D((2, 2)),
293316
keras.layers.Conv2D(64, (3, 3), padding="same"),
@@ -323,17 +346,32 @@ def call(self, x):
323346

324347
inputs = keras.Input(shape=(32, 32, 3))
325348
x, _ = Involution(
326-
channel=3, group_number=1, kernel_size=3, stride=1, reduction_ratio=2, name="inv_1"
349+
channel=3,
350+
group_number=1,
351+
kernel_size=3,
352+
stride=1,
353+
reduction_ratio=2,
354+
name="inv_1",
327355
)(inputs)
328356
x = keras.layers.ReLU()(x)
329357
x = keras.layers.MaxPooling2D((2, 2))(x)
330358
x, _ = Involution(
331-
channel=3, group_number=1, kernel_size=3, stride=1, reduction_ratio=2, name="inv_2"
359+
channel=3,
360+
group_number=1,
361+
kernel_size=3,
362+
stride=1,
363+
reduction_ratio=2,
364+
name="inv_2",
332365
)(x)
333366
x = keras.layers.ReLU()(x)
334367
x = keras.layers.MaxPooling2D((2, 2))(x)
335368
x, _ = Involution(
336-
channel=3, group_number=1, kernel_size=3, stride=1, reduction_ratio=2, name="inv_3"
369+
channel=3,
370+
group_number=1,
371+
kernel_size=3,
372+
stride=1,
373+
reduction_ratio=2,
374+
name="inv_3",
337375
)(x)
338376
x = keras.layers.ReLU()(x)
339377
x = keras.layers.Flatten()(x)

0 commit comments

Comments
 (0)