text stringlengths 0 4.99k |
|---|
h_slices = ( |
slice(0, -self.window_size), |
slice(-self.window_size, -self.shift_size), |
slice(-self.shift_size, None), |
) |
w_slices = ( |
slice(0, -self.window_size), |
slice(-self.window_size, -self.shift_size), |
slice(-self.shift_size, None), |
) |
mask_array = np.zeros((1, height, width, 1)) |
count = 0 |
for h in h_slices: |
for w in w_slices: |
mask_array[:, h, w, :] = count |
count += 1 |
mask_array = tf.convert_to_tensor(mask_array) |
# mask array to windows |
mask_windows = window_partition(mask_array, self.window_size) |
mask_windows = tf.reshape( |
mask_windows, shape=[-1, self.window_size * self.window_size] |
) |
attn_mask = tf.expand_dims(mask_windows, axis=1) - tf.expand_dims( |
mask_windows, axis=2 |
) |
attn_mask = tf.where(attn_mask != 0, -100.0, attn_mask) |
attn_mask = tf.where(attn_mask == 0, 0.0, attn_mask) |
self.attn_mask = tf.Variable(initial_value=attn_mask, trainable=False) |
def call(self, x): |
height, width = self.num_patch |
_, num_patches_before, channels = x.shape |
x_skip = x |
x = self.norm1(x) |
x = tf.reshape(x, shape=(-1, height, width, channels)) |
if self.shift_size > 0: |
shifted_x = tf.roll( |
x, shift=[-self.shift_size, -self.shift_size], axis=[1, 2] |
) |
else: |
shifted_x = x |
x_windows = window_partition(shifted_x, self.window_size) |
x_windows = tf.reshape( |
x_windows, shape=(-1, self.window_size * self.window_size, channels) |
) |
attn_windows = self.attn(x_windows, mask=self.attn_mask) |
attn_windows = tf.reshape( |
attn_windows, shape=(-1, self.window_size, self.window_size, channels) |
) |
shifted_x = window_reverse( |
attn_windows, self.window_size, height, width, channels |
) |
if self.shift_size > 0: |
x = tf.roll( |
shifted_x, shift=[self.shift_size, self.shift_size], axis=[1, 2] |
) |
else: |
x = shifted_x |
x = tf.reshape(x, shape=(-1, height * width, channels)) |
x = self.drop_path(x) |
x = x_skip + x |
x_skip = x |
x = self.norm2(x) |
x = self.mlp(x) |
x = self.drop_path(x) |
x = x_skip + x |
return x |
Model training and evaluation |
Extract and embed patches |
We first create 3 layers to help us extract, embed and merge patches from the images on top of which we will later use the Swin Transformer class we built. |
class PatchExtract(layers.Layer): |
def __init__(self, patch_size, **kwargs): |
super(PatchExtract, self).__init__(**kwargs) |
self.patch_size_x = patch_size[0] |
self.patch_size_y = patch_size[0] |
def call(self, images): |
batch_size = tf.shape(images)[0] |
patches = tf.image.extract_patches( |
images=images, |
sizes=(1, self.patch_size_x, self.patch_size_y, 1), |
strides=(1, self.patch_size_x, self.patch_size_y, 1), |
rates=(1, 1, 1, 1), |
padding=\"VALID\", |
) |
patch_dim = patches.shape[-1] |
patch_num = patches.shape[1] |
return tf.reshape(patches, (batch_size, patch_num * patch_num, patch_dim)) |
class PatchEmbedding(layers.Layer): |
def __init__(self, num_patch, embed_dim, **kwargs): |
super(PatchEmbedding, self).__init__(**kwargs) |
self.num_patch = num_patch |
self.proj = layers.Dense(embed_dim) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.