-
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.
Second version of the RAFNI algorithm
- Loading branch information
1 parent
ab87daa
commit 39d0a2e
Showing
9 changed files
with
409 additions
and
140 deletions.
There are no files selected for viewing
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,63 @@ | ||
|
||
import tensorflow as tf | ||
import tensorflow.keras.backend as K | ||
from tensorflow.keras.layers import BatchNormalization, Activation, Conv2D | ||
from tensorflow.keras.layers import MaxPooling2D, Flatten, Dense | ||
from tensorflow.keras.regularizers import l2 | ||
import numpy as np | ||
|
||
# def eight_layer(input_tensor = None, input_shape = None, num_classes = 10): | ||
def eight_layer(num_classes): | ||
# if input_tensor is None: | ||
# img_input = tf.keras.Input(shape = input_shape) | ||
# else: | ||
# if not K.is_keras_tensor(input_shape): | ||
# img_input = tf.keras.Input(tensor = input_tensor, shape = input_shape) | ||
# else: | ||
# img_input = input_tensor | ||
img_input = tf.keras.Input(shape = (32, 32, 3)) | ||
|
||
# Block 1 | ||
x = Conv2D(64, (3, 3), padding = 'same', kernel_initializer = 'he_normal', | ||
name = 'block1_conv1')(img_input) | ||
x = BatchNormalization()(x) | ||
x = Activation('relu')(x) | ||
x = Conv2D(64, (3, 3), padding = 'same', kernel_initializer = 'he_normal', | ||
name = 'block1_conv2')(x) | ||
x = BatchNormalization()(x) | ||
x = Activation('relu')(x) | ||
x = MaxPooling2D((2, 2), strides = (2, 2), name = 'block1_pool')(x) | ||
|
||
# Block 2 | ||
x = Conv2D(128, (3, 3), padding = 'same', kernel_initializer = 'he_normal', | ||
name = 'block2_conv1')(img_input) | ||
x = BatchNormalization()(x) | ||
x = Activation('relu')(x) | ||
x = Conv2D(128, (3, 3), padding = 'same', kernel_initializer = 'he_normal', | ||
name = 'block2_conv2')(x) | ||
x = BatchNormalization()(x) | ||
x = Activation('relu')(x) | ||
x = MaxPooling2D((2, 2), strides = (2, 2), name = 'block2_pool')(x) | ||
|
||
# Block 3 | ||
x = Conv2D(196, (3, 3), padding = 'same', kernel_initializer = 'he_normal', | ||
name = 'block3_conv1')(img_input) | ||
x = BatchNormalization()(x) | ||
x = Activation('relu')(x) | ||
x = Conv2D(196, (3, 3), padding = 'same', kernel_initializer = 'he_normal', | ||
name = 'block3_conv2')(x) | ||
x = BatchNormalization()(x) | ||
x = Activation('relu')(x) | ||
x = MaxPooling2D((2, 2), strides = (2, 2), name = 'block3_pool')(x) | ||
|
||
x = Flatten(name = 'flatten')(x) | ||
|
||
x = Dense(256, kernel_initializer = 'he_normal', kernel_regularizer = l2(0.01), | ||
bias_regularizer = l2(0.01), name = 'fc1')(x) | ||
x = BatchNormalization()(x) | ||
x = Activation('relu', name = 'lid')(x) | ||
|
||
x = Dense(num_classes, kernel_initializer = 'he_normal')(x) | ||
x = Activation('softmax')(x) | ||
|
||
return tf.keras.Model(inputs = img_input, outputs = x) |
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,54 @@ | ||
import tensorflow as tf | ||
from tensorflow.keras.layers import BatchNormalization, Activation, Conv2D, Dropout | ||
from tensorflow.keras.layers import AveragePooling2D, GlobalAveragePooling2D, Dense | ||
import numpy as np | ||
|
||
def conv(input, filters, stride): | ||
return Conv2D(filters, (3, 3), stride, padding = 'same', use_bias = False, | ||
kernel_initializer = tf.keras.initializers.RandomNormal( | ||
stddev = np.sqrt(2.0 / 9 / filters)))(input) | ||
|
||
def add_layer(input, growthRate): | ||
x = BatchNormalization(momentum = 0.9, epsilon = 1e-05)(input) | ||
x = Activation('relu')(x) | ||
x = conv(x, growthRate, (1, 1)) | ||
x = Dropout(rate = 0.2)(x) | ||
return tf.concat([input, x], 3) | ||
|
||
def add_transition(input): | ||
filters = input.shape[3] | ||
x = BatchNormalization(momentum = 0.9, epsilon = 1e-05)(input) | ||
x = Activation('relu')(x) | ||
x = Conv2D(filters, (1, 1), strides = (1, 1), use_bias = False, padding = 'same')(x) | ||
x = Activation('relu')(x) | ||
x = Dropout(rate = 0.2)(x) | ||
x = AveragePooling2D((2, 2), strides = (2, 2))(x) | ||
return x | ||
|
||
def densenet(depth, growthRate, num_classes): | ||
N = int((depth-4)/3) | ||
|
||
input = tf.keras.Input(shape = (32, 32, 3)) | ||
x = conv(input, 16, (1, 1)) | ||
|
||
# Block 1 | ||
for i in range(N): | ||
x = add_layer(x, growthRate) | ||
x = add_transition(x) | ||
|
||
# Block 2 | ||
for i in range(N): | ||
x = add_layer(x, growthRate) | ||
x = add_transition(x) | ||
|
||
# Block 3 | ||
for i in range(N): | ||
x = add_layer(x, growthRate) | ||
|
||
x = BatchNormalization(momentum = 0.9, epsilon = 1e-05)(x) | ||
x = Activation('relu')(x) | ||
x = GlobalAveragePooling2D()(x) | ||
output = Dense(num_classes, activation = 'softmax', | ||
kernel_initializer = tf.keras.initializers.VarianceScaling(2.0))(x) | ||
|
||
return tf.keras.Model(inputs = input, outputs = output) |
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
Oops, something went wrong.