text stringlengths 0 4.99k |
|---|
x_test shape: (10000, 32, 32, 3) - y_test shape: (10000, 1) |
Configure the hyperparameters |
learning_rate = 0.001 |
weight_decay = 0.0001 |
batch_size = 64 |
num_epochs = 50 |
dropout_rate = 0.2 |
image_size = 64 # We'll resize input images to this size. |
patch_size = 2 # Size of the patches to be extract from the input images. |
num_patches = (image_size // patch_size) ** 2 # Size of the data array. |
latent_dim = 256 # Size of the latent array. |
projection_dim = 256 # Embedding size of each element in the data and latent arrays. |
num_heads = 8 # Number of Transformer heads. |
ffn_units = [ |
projection_dim, |
projection_dim, |
] # Size of the Transformer Feedforward network. |
num_transformer_blocks = 4 |
num_iterations = 2 # Repetitions of the cross-attention and Transformer modules. |
classifier_units = [ |
projection_dim, |
num_classes, |
] # Size of the Feedforward network of the final classifier. |
print(f\"Image size: {image_size} X {image_size} = {image_size ** 2}\") |
print(f\"Patch size: {patch_size} X {patch_size} = {patch_size ** 2} \") |
print(f\"Patches per image: {num_patches}\") |
print(f\"Elements per patch (3 channels): {(patch_size ** 2) * 3}\") |
print(f\"Latent array shape: {latent_dim} X {projection_dim}\") |
print(f\"Data array shape: {num_patches} X {projection_dim}\") |
Image size: 64 X 64 = 4096 |
Patch size: 2 X 2 = 4 |
Patches per image: 1024 |
Elements per patch (3 channels): 12 |
Latent array shape: 256 X 256 |
Data array shape: 1024 X 256 |
Note that, in order to use each pixel as an individual input in the data array, set patch_size to 1. |
Use data augmentation |
data_augmentation = keras.Sequential( |
[ |
layers.Normalization(), |
layers.Resizing(image_size, image_size), |
layers.RandomFlip(\"horizontal\"), |
layers.RandomZoom( |
height_factor=0.2, width_factor=0.2 |
), |
], |
name=\"data_augmentation\", |
) |
# Compute the mean and the variance of the training data for normalization. |
data_augmentation.layers[0].adapt(x_train) |
Implement Feedforward network (FFN) |
def create_ffn(hidden_units, dropout_rate): |
ffn_layers = [] |
for units in hidden_units[:-1]: |
ffn_layers.append(layers.Dense(units, activation=tf.nn.gelu)) |
ffn_layers.append(layers.Dense(units=hidden_units[-1])) |
ffn_layers.append(layers.Dropout(dropout_rate)) |
ffn = keras.Sequential(ffn_layers) |
return ffn |
Implement patch creation as a layer |
class Patches(layers.Layer): |
def __init__(self, patch_size): |
super(Patches, self).__init__() |
self.patch_size = patch_size |
def call(self, images): |
batch_size = tf.shape(images)[0] |
patches = tf.image.extract_patches( |
images=images, |
sizes=[1, self.patch_size, self.patch_size, 1], |
strides=[1, self.patch_size, self.patch_size, 1], |
rates=[1, 1, 1, 1], |
padding=\"VALID\", |
) |
patch_dims = patches.shape[-1] |
patches = tf.reshape(patches, [batch_size, -1, patch_dims]) |
return patches |
Implement the patch encoding layer |
The PatchEncoder layer will linearly transform a patch by projecting it into a vector of size latent_dim. In addition, it adds a learnable position embedding to the projected vector. |
Note that the orginal Perceiver paper uses the Fourier feature positional encodings. |
class PatchEncoder(layers.Layer): |
def __init__(self, num_patches, projection_dim): |
super(PatchEncoder, self).__init__() |
self.num_patches = num_patches |
self.projection = layers.Dense(units=projection_dim) |
self.position_embedding = layers.Embedding( |
input_dim=num_patches, output_dim=projection_dim |
) |
def call(self, patches): |
positions = tf.range(start=0, limit=self.num_patches, delta=1) |
encoded = self.projection(patches) + self.position_embedding(positions) |
return encoded |
Build the Perceiver model |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.