forked from YunYang1994/TensorFlow2.0-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
583 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import numpy as np | ||
import tensorflow as tf | ||
from tensorflow import keras | ||
from tensorflow.keras import layers | ||
import tensorflow_addons as tfa | ||
|
||
|
||
def mlp(x, hidden_units, dropout_rate): | ||
for units in hidden_units: | ||
x = layers.Dense(units, activation=tf.nn.gelu)(x) | ||
x = layers.Dropout(dropout_rate)(x) | ||
return x | ||
|
||
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 | ||
|
||
class Patches_sp(layers.Layer): | ||
def __init__(self, patch_size): | ||
super(Patches_sp, 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[0], self.patch_size[1], 1], | ||
strides=[1, self.patch_size[0], self.patch_size[1], 1], | ||
rates=[1, 1, 1, 1], | ||
padding="VALID", | ||
) | ||
patch_dims = patches.shape[-1] | ||
patches = tf.reshape(patches, [batch_size, -1, patch_dims]) | ||
return patches | ||
|
||
|
||
|
||
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, patch): | ||
positions = tf.range(start=0, limit=self.num_patches, delta=1) | ||
encoded = self.projection(patch) + self.position_embedding(positions) | ||
return encoded | ||
|
||
|
Oops, something went wrong.