forked from commaai/speedchallenge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
keras_resnext.py
56 lines (45 loc) · 2.43 KB
/
keras_resnext.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
import group_conv
def res_bottleneck_block(input, num_channels, stride=1, groups=1, bottleneck_factor=2):
if not stride == 1 or not input.shape[-1] == num_channels * bottleneck_factor:
shortcut = tf.keras.layers.Conv2D(num_channels * bottleneck_factor, 1, strides=stride, padding='same')(input)
shortcut = tf.keras.layers.BatchNormalization()(shortcut)
else:
shortcut = input
# Residual
res = tf.keras.layers.Conv2D(num_channels, 1, padding='same')(input)
res = tf.keras.layers.BatchNormalization()(res)
res = tf.keras.layers.Activation(tf.nn.swish)(res)
res = tf.keras.layers.Conv2D(num_channels, 3, groups=groups, strides=stride, padding='same')(res)
res = tf.keras.layers.BatchNormalization()(res)
res = tf.keras.layers.Activation(tf.nn.swish)(res)
res = tf.keras.layers.Conv2D(num_channels * bottleneck_factor, 1, padding='same')(res)
res = tf.keras.layers.BatchNormalization()(res)
# Merge
out = tf.keras.layers.add([res, shortcut])
out = tf.keras.layers.Activation(tf.nn.swish)(out)
return out
def resnext50_encoder(image):
encoder_filters = [64, 128, 256, 512, 1024]
stride = 2
conv1 = tf.keras.layers.Conv2D(encoder_filters[0], 7, strides=stride, padding='same')(image)
conv1 = tf.keras.layers.BatchNormalization()(conv1)
conv1 = tf.keras.layers.Activation(tf.nn.swish)(conv1)
conv1 = tf.keras.layers.MaxPool2D(3, 2, 'same')(conv1)
conv2 = res_bottleneck_block(conv1, encoder_filters[1])
conv2 = res_bottleneck_block(conv2, encoder_filters[1])
conv2 = res_bottleneck_block(conv2, encoder_filters[1])
conv3 = res_bottleneck_block(conv2, encoder_filters[2], stride)
conv3 = res_bottleneck_block(conv3, encoder_filters[2])
conv3 = res_bottleneck_block(conv3, encoder_filters[2])
conv3 = res_bottleneck_block(conv3, encoder_filters[2])
conv4 = res_bottleneck_block(conv3, encoder_filters[3], stride)
conv4 = res_bottleneck_block(conv4, encoder_filters[3])
conv4 = res_bottleneck_block(conv4, encoder_filters[3])
conv4 = res_bottleneck_block(conv4, encoder_filters[3])
conv4 = res_bottleneck_block(conv4, encoder_filters[3])
conv4 = res_bottleneck_block(conv4, encoder_filters[3])
conv5 = res_bottleneck_block(conv4, encoder_filters[4], stride)
conv5 = res_bottleneck_block(conv5, encoder_filters[4])
conv5 = res_bottleneck_block(conv5, encoder_filters[4])
return conv5