-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating Semi-supervised image classification using contrastive pretraining with SimCLR Keras 3 example (TF-Only) #1777
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dfca98f
Update to the existing keras3 example
sitamgithub-MSIT 9a33aa7
left tf ops replaced with keras ops
sitamgithub-MSIT b4a4546
formatting done
sitamgithub-MSIT 0909cbd
seed generator added
sitamgithub-MSIT eb7332c
.md and .ipynb file added
sitamgithub-MSIT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,7 @@ | |
import tensorflow_datasets as tfds | ||
|
||
import keras | ||
from keras import ops | ||
from keras import layers | ||
|
||
""" | ||
|
@@ -199,24 +200,25 @@ def get_config(self): | |
|
||
def call(self, images, training=True): | ||
if training: | ||
batch_size = tf.shape(images)[0] | ||
batch_size = ops.shape(images)[0] | ||
|
||
# Same for all colors | ||
brightness_scales = 1 + tf.random.uniform( | ||
brightness_scales = 1 + keras.random.uniform( | ||
(batch_size, 1, 1, 1), | ||
minval=-self.brightness, | ||
maxval=self.brightness, | ||
) | ||
# Different for all colors | ||
jitter_matrices = tf.random.uniform( | ||
jitter_matrices = keras.random.uniform( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
(batch_size, 1, 3, 3), minval=-self.jitter, maxval=self.jitter | ||
) | ||
|
||
color_transforms = ( | ||
tf.eye(3, batch_shape=[batch_size, 1]) * brightness_scales | ||
ops.tile(ops.expand_dims(ops.eye(3), axis=0), (batch_size, 1, 1, 1)) | ||
* brightness_scales | ||
+ jitter_matrices | ||
) | ||
images = tf.clip_by_value(tf.matmul(images, color_transforms), 0, 1) | ||
images = ops.clip(ops.matmul(images, color_transforms), 0, 1) | ||
return images | ||
|
||
|
||
|
@@ -416,19 +418,19 @@ def contrastive_loss(self, projections_1, projections_2): | |
# NT-Xent loss (normalized temperature-scaled cross entropy) | ||
|
||
# Cosine similarity: the dot product of the l2-normalized feature vectors | ||
projections_1 = tf.math.l2_normalize(projections_1, axis=1) | ||
projections_2 = tf.math.l2_normalize(projections_2, axis=1) | ||
projections_1 = ops.normalize(projections_1, axis=1) | ||
projections_2 = ops.normalize(projections_2, axis=1) | ||
similarities = ( | ||
tf.matmul(projections_1, projections_2, transpose_b=True) / self.temperature | ||
ops.matmul(projections_1, ops.transpose(projections_2)) / self.temperature | ||
) | ||
|
||
# The similarity between the representations of two augmented views of the | ||
# same image should be higher than their similarity with other views | ||
batch_size = tf.shape(projections_1)[0] | ||
contrastive_labels = tf.range(batch_size) | ||
batch_size = ops.shape(projections_1)[0] | ||
contrastive_labels = ops.arange(batch_size) | ||
self.contrastive_accuracy.update_state(contrastive_labels, similarities) | ||
self.contrastive_accuracy.update_state( | ||
contrastive_labels, tf.transpose(similarities) | ||
contrastive_labels, ops.transpose(similarities) | ||
) | ||
|
||
# The temperature-scaled similarities are used as logits for cross-entropy | ||
|
@@ -437,15 +439,15 @@ def contrastive_loss(self, projections_1, projections_2): | |
contrastive_labels, similarities, from_logits=True | ||
) | ||
loss_2_1 = keras.losses.sparse_categorical_crossentropy( | ||
contrastive_labels, tf.transpose(similarities), from_logits=True | ||
contrastive_labels, ops.transpose(similarities), from_logits=True | ||
) | ||
return (loss_1_2 + loss_2_1) / 2 | ||
|
||
def train_step(self, data): | ||
(unlabeled_images, _), (labeled_images, labels) = data | ||
|
||
# Both labeled and unlabeled images are used, without labels | ||
images = tf.concat((unlabeled_images, labeled_images), axis=0) | ||
images = ops.concatenate((unlabeled_images, labeled_images), axis=0) | ||
# Each image is augmented twice, differently | ||
augmented_images_1 = self.contrastive_augmenter(images, training=True) | ||
augmented_images_2 = self.contrastive_augmenter(images, training=True) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a
seed=self.seed_generator
arg and createself.seed_generator
in__init__()
.