text stringlengths 0 4.99k |
|---|
patch_img = tf.reshape(patch, (self.patch_size, self.patch_size, 3)) |
plt.imshow(keras.utils.img_to_array(patch_img)) |
plt.axis(\"off\") |
plt.show() |
# Return the index chosen to validate it outside the method. |
return idx |
# taken from https://stackoverflow.com/a/58082878/10319735 |
def reconstruct_from_patch(self, patch): |
# This utility function takes patches from a *single* image and |
# reconstructs it back into the image. This is useful for the train |
# monitor callback. |
num_patches = patch.shape[0] |
n = int(np.sqrt(num_patches)) |
patch = tf.reshape(patch, (num_patches, self.patch_size, self.patch_size, 3)) |
rows = tf.split(patch, n, axis=0) |
rows = [tf.concat(tf.unstack(x), axis=1) for x in rows] |
reconstructed = tf.concat(rows, axis=0) |
return reconstructed |
Let's visualize the image patches. |
# Get a batch of images. |
image_batch = next(iter(train_ds)) |
# Augment the images. |
augmentation_model = get_train_augmentation_model() |
augmented_images = augmentation_model(image_batch) |
# Define the patch layer. |
patch_layer = Patches() |
# Get the patches from the batched images. |
patches = patch_layer(images=augmented_images) |
# Now pass the images and the corresponding patches |
# to the `show_patched_image` method. |
random_index = patch_layer.show_patched_image(images=augmented_images, patches=patches) |
# Chose the same chose image and try reconstructing the patches |
# into the original image. |
image = patch_layer.reconstruct_from_patch(patches[random_index]) |
plt.imshow(image) |
plt.axis(\"off\") |
plt.show() |
Index selected: 102. |
png |
png |
png |
Patch encoding with masking |
Quoting the paper |
Following ViT, we divide an image into regular non-overlapping patches. Then we sample a subset of patches and mask (i.e., remove) the remaining ones. Our sampling strategy is straightforward: we sample random patches without replacement, following a uniform distribution. We simply refer to this as “random sampling”. |
This layer includes masking and encoding the patches. |
The utility methods of the layer are: |
get_random_indices -- Provides the mask and unmask indices. |
generate_masked_image -- Takes patches and unmask indices, results in a random masked image. This is an essential utility method for our training monitor callback (defined later). |
class PatchEncoder(layers.Layer): |
def __init__( |
self, |
patch_size=PATCH_SIZE, |
projection_dim=ENC_PROJECTION_DIM, |
mask_proportion=MASK_PROPORTION, |
downstream=False, |
**kwargs, |
): |
super().__init__(**kwargs) |
self.patch_size = patch_size |
self.projection_dim = projection_dim |
self.mask_proportion = mask_proportion |
self.downstream = downstream |
# This is a trainable mask token initialized randomly from a normal |
# distribution. |
self.mask_token = tf.Variable( |
tf.random.normal([1, patch_size * patch_size * 3]), trainable=True |
) |
def build(self, input_shape): |
(_, self.num_patches, self.patch_area) = input_shape |
# Create the projection layer for the patches. |
self.projection = layers.Dense(units=self.projection_dim) |
# Create the positional embedding layer. |
self.position_embedding = layers.Embedding( |
input_dim=self.num_patches, output_dim=self.projection_dim |
) |
# Number of patches that will be masked. |
self.num_mask = int(self.mask_proportion * self.num_patches) |
def call(self, patches): |
# Get the positional embeddings. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.