text stringlengths 0 4.99k |
|---|
def calculate_loss(self, images, test=False): |
# Augment the input images. |
if test: |
augmented_images = self.test_augmentation_model(images) |
else: |
augmented_images = self.train_augmentation_model(images) |
# Patch the augmented images. |
patches = self.patch_layer(augmented_images) |
# Encode the patches. |
( |
unmasked_embeddings, |
masked_embeddings, |
unmasked_positions, |
mask_indices, |
unmask_indices, |
) = self.patch_encoder(patches) |
# Pass the unmaksed patche to the encoder. |
encoder_outputs = self.encoder(unmasked_embeddings) |
# Create the decoder inputs. |
encoder_outputs = encoder_outputs + unmasked_positions |
decoder_inputs = tf.concat([encoder_outputs, masked_embeddings], axis=1) |
# Decode the inputs. |
decoder_outputs = self.decoder(decoder_inputs) |
decoder_patches = self.patch_layer(decoder_outputs) |
loss_patch = tf.gather(patches, mask_indices, axis=1, batch_dims=1) |
loss_output = tf.gather(decoder_patches, mask_indices, axis=1, batch_dims=1) |
# Compute the total loss. |
total_loss = self.compiled_loss(loss_patch, loss_output) |
return total_loss, loss_patch, loss_output |
def train_step(self, images): |
with tf.GradientTape() as tape: |
total_loss, loss_patch, loss_output = self.calculate_loss(images) |
# Apply gradients. |
train_vars = [ |
self.train_augmentation_model.trainable_variables, |
self.patch_layer.trainable_variables, |
self.patch_encoder.trainable_variables, |
self.encoder.trainable_variables, |
self.decoder.trainable_variables, |
] |
grads = tape.gradient(total_loss, train_vars) |
tv_list = [] |
for (grad, var) in zip(grads, train_vars): |
for g, v in zip(grad, var): |
tv_list.append((g, v)) |
self.optimizer.apply_gradients(tv_list) |
# Report progress. |
self.compiled_metrics.update_state(loss_patch, loss_output) |
return {m.name: m.result() for m in self.metrics} |
def test_step(self, images): |
total_loss, loss_patch, loss_output = self.calculate_loss(images, test=True) |
# Update the trackers. |
self.compiled_metrics.update_state(loss_patch, loss_output) |
return {m.name: m.result() for m in self.metrics} |
Model initialization |
train_augmentation_model = get_train_augmentation_model() |
test_augmentation_model = get_test_augmentation_model() |
patch_layer = Patches() |
patch_encoder = PatchEncoder() |
encoder = create_encoder() |
decoder = create_decoder() |
mae_model = MaskedAutoencoder( |
train_augmentation_model=train_augmentation_model, |
test_augmentation_model=test_augmentation_model, |
patch_layer=patch_layer, |
patch_encoder=patch_encoder, |
encoder=encoder, |
decoder=decoder, |
) |
Training callbacks |
Visualization callback |
# Taking a batch of test inputs to measure model's progress. |
test_images = next(iter(test_ds)) |
class TrainMonitor(keras.callbacks.Callback): |
def __init__(self, epoch_interval=None): |
self.epoch_interval = epoch_interval |
def on_epoch_end(self, epoch, logs=None): |
if self.epoch_interval and epoch % self.epoch_interval == 0: |
test_augmented_images = self.model.test_augmentation_model(test_images) |
test_patches = self.model.patch_layer(test_augmented_images) |
( |
test_unmasked_embeddings, |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.