-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathinception_v4.py
56 lines (45 loc) · 1.77 KB
/
inception_v4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import tensorflow as tf
from inception_modules import Stem, InceptionBlockA, InceptionBlockB, \
InceptionBlockC, ReductionA, ReductionB
NUM_CLASSES = 10
def build_inception_block_a(n):
block = tf.keras.Sequential()
for _ in range(n):
block.add(InceptionBlockA())
return block
def build_inception_block_b(n):
block = tf.keras.Sequential()
for _ in range(n):
block.add(InceptionBlockB())
return block
def build_inception_block_c(n):
block = tf.keras.Sequential()
for _ in range(n):
block.add(InceptionBlockC())
return block
class InceptionV4(tf.keras.Model):
def __init__(self):
super(InceptionV4, self).__init__()
self.stem = Stem()
self.inception_a = build_inception_block_a(4)
self.reduction_a = ReductionA(k=192, l=224, m=256, n=384)
self.inception_b = build_inception_block_b(7)
self.reduction_b = ReductionB()
self.inception_c = build_inception_block_c(3)
self.avgpool = tf.keras.layers.AveragePooling2D(pool_size=(8, 8))
self.dropout = tf.keras.layers.Dropout(rate=0.2)
self.flat = tf.keras.layers.Flatten()
self.fc = tf.keras.layers.Dense(units=NUM_CLASSES,
activation=tf.keras.activations.softmax)
def call(self, inputs, training=True, mask=None):
x = self.stem(inputs, training=training)
x = self.inception_a(x, training=training)
x = self.reduction_a(x, training=training)
x = self.inception_b(x, training=training)
x = self.reduction_b(x, training=training)
x = self.inception_c(x, training=training)
x = self.avgpool(x)
x = self.dropout(x, training=training)
x = self.flat(x)
x = self.fc(x)
return x