text stringlengths 0 4.99k |
|---|
ENC_PROJECTION_DIM, |
] # Size of the transformer layers. |
DEC_TRANSFORMER_UNITS = [ |
DEC_PROJECTION_DIM * 2, |
DEC_PROJECTION_DIM, |
] |
Load and prepare the CIFAR-10 dataset |
(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data() |
(x_train, y_train), (x_val, y_val) = ( |
(x_train[:40000], y_train[:40000]), |
(x_train[40000:], y_train[40000:]), |
) |
print(f\"Training samples: {len(x_train)}\") |
print(f\"Validation samples: {len(x_val)}\") |
print(f\"Testing samples: {len(x_test)}\") |
train_ds = tf.data.Dataset.from_tensor_slices(x_train) |
train_ds = train_ds.shuffle(BUFFER_SIZE).batch(BATCH_SIZE).prefetch(AUTO) |
val_ds = tf.data.Dataset.from_tensor_slices(x_val) |
val_ds = val_ds.batch(BATCH_SIZE).prefetch(AUTO) |
test_ds = tf.data.Dataset.from_tensor_slices(x_test) |
test_ds = test_ds.batch(BATCH_SIZE).prefetch(AUTO) |
Training samples: 40000 |
Validation samples: 10000 |
Testing samples: 10000 |
2021-11-24 01:10:52.088318: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F FMA |
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. |
2021-11-24 01:10:54.356762: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 38444 MB memory: -> device: 0, name: A100-SXM4-40GB, pci bus id: 0000:00:04.0, compute capability: 8.0 |
Data augmentation |
In previous self-supervised pretraining methodologies (SimCLR alike), we have noticed that the data augmentation pipeline plays an important role. On the other hand the authors of this paper point out that Masked Autoencoders do not rely on augmentations. They propose a simple augmentation pipeline of: |
Resizing |
Random cropping (fixed-sized or random sized) |
Random horizontal flipping |
def get_train_augmentation_model(): |
model = keras.Sequential( |
[ |
layers.Rescaling(1 / 255.0), |
layers.Resizing(INPUT_SHAPE[0] + 20, INPUT_SHAPE[0] + 20), |
layers.RandomCrop(IMAGE_SIZE, IMAGE_SIZE), |
layers.RandomFlip(\"horizontal\"), |
], |
name=\"train_data_augmentation\", |
) |
return model |
def get_test_augmentation_model(): |
model = keras.Sequential( |
[layers.Rescaling(1 / 255.0), layers.Resizing(IMAGE_SIZE, IMAGE_SIZE),], |
name=\"test_data_augmentation\", |
) |
return model |
A layer for extracting patches from images |
This layer takes images as input and divides them into patches. The layer also includes two utility method: |
show_patched_image -- Takes a batch of images and its corresponding patches to plot a random pair of image and patches. |
reconstruct_from_patch -- Takes a single instance of patches and stitches them together into the original image. |
class Patches(layers.Layer): |
def __init__(self, patch_size=PATCH_SIZE, **kwargs): |
super().__init__(**kwargs) |
self.patch_size = patch_size |
# Assuming the image has three channels each patch would be |
# of size (patch_size, patch_size, 3). |
self.resize = layers.Reshape((-1, patch_size * patch_size * 3)) |
def call(self, images): |
# Create patches from the input images |
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\", |
) |
# Reshape the patches to (batch, num_patches, patch_area) and return it. |
patches = self.resize(patches) |
return patches |
def show_patched_image(self, images, patches): |
# This is a utility function which accepts a batch of images and its |
# corresponding patches and help visualize one image and its patches |
# side by side. |
idx = np.random.choice(patches.shape[0]) |
print(f\"Index selected: {idx}.\") |
plt.figure(figsize=(4, 4)) |
plt.imshow(keras.utils.array_to_img(images[idx])) |
plt.axis(\"off\") |
plt.show() |
n = int(np.sqrt(patches.shape[1])) |
plt.figure(figsize=(4, 4)) |
for i, patch in enumerate(patches[idx]): |
ax = plt.subplot(n, n, i + 1) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.