NoteDance commited on
Commit
b61368b
1 Parent(s): f5dcddf

Upload ViT.py

Browse files
Files changed (1) hide show
  1. ViT.py +135 -0
ViT.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from tensorflow.keras.layers import Dense,LayerNormalization,Dropout,Identity,Activation
3
+ from tensorflow.keras import Model
4
+
5
+
6
+ def pair(t):
7
+ return t if isinstance(t, tuple) else (t, t)
8
+
9
+
10
+ class FeedForward:
11
+ def __init__(self, dim, hidden_dim, drop_rate = 0.):
12
+ self.net = tf.keras.Sequential()
13
+ self.net.add(LayerNormalization())
14
+ self.net.add(Dense(hidden_dim))
15
+ self.net.add(Activation('gelu'))
16
+ self.net.add(Dropout(drop_rate))
17
+ self.net.add(Dense(dim))
18
+ self.net.add(Dropout(drop_rate))
19
+
20
+ def __call__(self, x):
21
+ return self.net(x)
22
+
23
+
24
+ class Attention:
25
+ def __init__(self, dim, heads = 8, dim_head = 64, drop_rate = 0.):
26
+ inner_dim = dim_head * heads
27
+ project_out = not (heads == 1 and dim_head == dim)
28
+
29
+ self.heads = heads
30
+ self.scale = dim_head ** -0.5
31
+
32
+ self.norm = LayerNormalization()
33
+
34
+ self.attend = tf.nn.softmax
35
+ self.dropout = Dropout(drop_rate)
36
+
37
+ self.to_qkv = Dense(inner_dim * 3, use_bias = False)
38
+
39
+ if project_out:
40
+ self.to_out = tf.keras.Sequential()
41
+ self.to_out.add(Dense(dim))
42
+ self.to_out.add(Dropout(drop_rate))
43
+ else:
44
+ self.to_out = Identity()
45
+
46
+ def __call__(self, x):
47
+ x = self.norm(x)
48
+
49
+ qkv = self.to_qkv(x)
50
+ q, k, v = tf.split(qkv, 3, axis=-1)
51
+ b = q.shape[0]
52
+ h = self.heads
53
+ n = q.shape[1]
54
+ d = q.shape[2] // self.heads
55
+ q = tf.reshape(q, (b, h, n, d))
56
+ k = tf.reshape(k, (b, h, n, d))
57
+ v = tf.reshape(v, (b, h, n, d))
58
+
59
+ dots = tf.matmul(q, tf.transpose(k, [0, 1, 3, 2])) * self.scale
60
+
61
+ attn = self.attend(dots)
62
+ attn = self.dropout(attn)
63
+
64
+ out = tf.matmul(attn, v)
65
+ out = tf.transpose(out, [0, 1, 3, 2])
66
+ out = tf.reshape(out, shape=[-1, n, h*d])
67
+ return self.to_out(out)
68
+
69
+
70
+ class Transformer:
71
+ def __init__(self, dim, depth, heads, dim_head, mlp_dim, dropout = 0.):
72
+ self.norm = LayerNormalization()
73
+ self.layers = []
74
+ for _ in range(depth):
75
+ self.layers.append([Attention(dim, heads = heads, dim_head = dim_head, drop_rate = dropout),
76
+ FeedForward(dim, mlp_dim, drop_rate = dropout)])
77
+
78
+ def __call__(self, x):
79
+ for attn, ff in self.layers:
80
+ x = attn(x) + x
81
+ x = ff(x) + x
82
+
83
+ return self.norm(x)
84
+
85
+
86
+ class ViT(Model):
87
+ def __init__(self, image_size, patch_size, num_classes, dim, depth, heads, mlp_dim, pool = 'cls', channels = 3, dim_head = 64, drop_rate = 0., emb_dropout = 0.):
88
+ super(ViT, self).__init__()
89
+ image_height, image_width = pair(image_size)
90
+ patch_height, patch_width = pair(patch_size)
91
+ self.p1, self.p2 = patch_height, patch_width
92
+ self.dim = dim
93
+
94
+ assert image_height % patch_height == 0 and image_width % patch_width == 0, 'Image dimensions must be divisible by the patch size.'
95
+
96
+ num_patches = (image_height // patch_height) * (image_width // patch_width)
97
+ assert pool in {'cls', 'mean'}, 'pool type must be either cls (cls token) or mean (mean pooling)'
98
+
99
+ self.to_patch_embedding = tf.keras.Sequential()
100
+ self.to_patch_embedding.add(LayerNormalization())
101
+ self.to_patch_embedding.add(Dense(dim))
102
+ self.to_patch_embedding.add(LayerNormalization())
103
+
104
+ self.pos_embedding = tf.Variable(tf.random.normal((1, num_patches + 1, dim)))
105
+ self.cls_token = tf.Variable(tf.random.normal(((1, 1, dim))))
106
+ self.dropout = Dropout(emb_dropout)
107
+
108
+ self.transformer = Transformer(dim, depth, heads, dim_head, mlp_dim, drop_rate)
109
+
110
+ self.pool = pool
111
+ self.to_latent = Identity()
112
+
113
+ self.mlp_head = Dense(num_classes)
114
+
115
+
116
+ def __call__(self, data):
117
+ b = data.shape[0]
118
+ h = data.shape[1] // self.p1
119
+ w = data.shape[2] // self.p2
120
+ c = data.shape[3]
121
+ data = tf.reshape(data, (b, h * w, self.p1 * self.p2 * c))
122
+ x = self.to_patch_embedding(data)
123
+ b, n, _ = x.shape
124
+
125
+ cls_tokens = tf.tile(self.cls_token, multiples=[b, 1, 1])
126
+ x = tf.concat([cls_tokens, x], axis=1)
127
+ x += self.pos_embedding[:, :(n + 1)]
128
+ x = self.dropout(x)
129
+
130
+ x = self.transformer(x)
131
+
132
+ x = tf.reduce_mean(x, axis = 1) if self.pool == 'mean' else x[:, 0]
133
+
134
+ x = self.to_latent(x)
135
+ return tf.nn.softmax(self.mlp_head(x))