|
5 | 5 | # A collection of utilities functions
|
6 | 6 |
|
7 | 7 | from keras import regularizers
|
8 |
| -from keras.initializers import initializers_v1 |
9 |
| -from keras import backend as be |
10 | 8 |
|
11 |
| -from keras.models import Model |
| 9 | +from keras.initializers import initializers_v1 |
12 | 10 | from keras.layers.convolutional import MaxPooling2D, Conv2D, AveragePooling2D
|
13 |
| -from keras.layers import Input, Dropout, Dense, Flatten, Activation |
| 11 | +from keras.layers import Activation |
14 | 12 | from keras.layers.merging import concatenate
|
15 | 13 | from keras.layers.normalization.batch_normalization import BatchNormalization
|
16 |
| -from keras.optimizers import Adam |
17 | 14 |
|
18 | 15 | # Hyperparameters we can adjust
|
19 |
| -DROPOUT_PROBABILITY = 0.1 |
20 |
| -INITIAL_LEARNING_RATE = 0.001 |
21 | 16 | L2_REGULARIZATION_AMOUNT = 0.00004
|
22 | 17 |
|
23 |
| -# Adjust these to match the dimensions of our input image. |
24 |
| -IMAGE_HEIGHT = 299 |
25 |
| -IMAGE_WIDTH = 299 |
26 |
| -IMAGE_CHANNELS = 3 |
27 | 18 |
|
28 |
| -# Reduce this if this model does not fit on our GPU. |
29 |
| -BATCH_SIZE = 24 |
| 19 | +def build_inception_v4_conv_base(input_tensor): |
| 20 | + """ |
| 21 | + Create the convolutions base portion of the InceptionV4 network. |
| 22 | + :param input_tensor: |
| 23 | + :return: |
| 24 | + """ |
| 25 | + # The stem |
| 26 | + conv_base = build_inception_v4_stem(input_tensor) |
| 27 | + # 4 Inception A blocks |
| 28 | + conv_base = build_inception_a_block(conv_base) |
| 29 | + conv_base = build_inception_a_block(conv_base) |
| 30 | + conv_base = build_inception_a_block(conv_base) |
| 31 | + conv_base = build_inception_a_block(conv_base) |
| 32 | + # 1 Reduction A block |
| 33 | + conv_base = build_reduction_a_block(conv_base) |
| 34 | + # 7 Inception B blocks |
| 35 | + conv_base = build_inception_b_block(conv_base) |
| 36 | + conv_base = build_inception_b_block(conv_base) |
| 37 | + conv_base = build_inception_b_block(conv_base) |
| 38 | + conv_base = build_inception_b_block(conv_base) |
| 39 | + conv_base = build_inception_b_block(conv_base) |
| 40 | + conv_base = build_inception_b_block(conv_base) |
| 41 | + conv_base = build_inception_b_block(conv_base) |
| 42 | + # 1 Reduction B block |
| 43 | + conv_base = build_reduction_b_block(conv_base) |
| 44 | + # 3 Inception C blocks |
| 45 | + conv_base = build_inception_c_block(conv_base) |
| 46 | + conv_base = build_inception_c_block(conv_base) |
| 47 | + conv_base = build_inception_c_block(conv_base) |
| 48 | + |
| 49 | + return conv_base |
| 50 | + |
| 51 | + |
| 52 | +def build_inception_v4_stem(input_tensor): |
| 53 | + """ |
| 54 | + Create the Inception-v4 stem of the Inception Architecture |
| 55 | + :param input_tensor: The input image tensor |
| 56 | + :return: outputs of all input branches |
| 57 | + """ |
| 58 | + # First stage of the stem: |
| 59 | + stem = conv2d_batch_norm_relu(input_tensor, 32, 3, 3, strides=(2, 2), padding='valid') |
| 60 | + stem = conv2d_batch_norm_relu(stem, 32, 3, 3, padding='valid') |
| 61 | + stem = conv2d_batch_norm_relu(stem, 64, 3, 3) |
| 62 | + # Second stage of the stem: |
| 63 | + left_1 = MaxPooling2D((3, 3), strides=(2, 2), padding='valid')(stem) |
| 64 | + right_1 = conv2d_batch_norm_relu(stem, 96, 3, 3, strides=(2, 2), padding='valid') |
| 65 | + # Concatenate all the results from the two branches |
| 66 | + stem = concatenate([left_1, right_1], axis=-1) |
| 67 | + # Third stage of the stem: |
| 68 | + left_2 = conv2d_batch_norm_relu(stem, 64, 1, 1) |
| 69 | + left_2 = conv2d_batch_norm_relu(left_2, 96, 3, 3, padding='valid') |
| 70 | + right_2 = conv2d_batch_norm_relu(stem, 64, 1, 1) |
| 71 | + right_2 = conv2d_batch_norm_relu(right_2, 64, 1, 7) |
| 72 | + right_2 = conv2d_batch_norm_relu(right_2, 64, 7, 1) |
| 73 | + right_2 = conv2d_batch_norm_relu(right_2, 96, 3, 3, padding='valid') |
| 74 | + # Concatenate all the results from the two branches |
| 75 | + stem = concatenate([left_2, right_2], axis=-1) |
| 76 | + # Fourth stage of the stem: |
| 77 | + left_3 = conv2d_batch_norm_relu(stem, 192, 3, 3, strides=(2, 2), padding='valid') |
| 78 | + right_3 = MaxPooling2D((3, 3), strides=(2, 2), padding='valid')(stem) |
| 79 | + # Concatenate all the results from the two branches |
| 80 | + stem = concatenate([left_3, right_3], axis=-1) |
| 81 | + return stem |
30 | 82 |
|
31 | 83 |
|
32 | 84 | def build_reduction_b_block(input_tensor):
|
33 | 85 | """
|
34 |
| - A reduction block: Transform a 35x35 input into a 17x17 input in an efficient manner. |
| 86 | + A reduction block: Transform a 17x17 input into a 8x8 input in an efficient manner. |
35 | 87 | :param input_tensor: The input image tensor
|
36 | 88 | :return: outputs of the three input branches
|
37 | 89 | """
|
38 | 90 | # This is the first branch from the left
|
39 | 91 | branch_left = MaxPooling2D((3, 3), strides=(2, 2), padding='valid')(input_tensor)
|
40 | 92 | # This is the middle branch
|
41 |
| - branch_middle = conv2d_batch_norm_relu(input_tensor, 192 , 1, 1) |
| 93 | + branch_middle = conv2d_batch_norm_relu(input_tensor, 192, 1, 1) |
42 | 94 | branch_middle = conv2d_batch_norm_relu(branch_middle, 192, 3, 3, strides=(2, 2), padding='valid')
|
43 | 95 | # This is the right branch
|
44 | 96 | branch_right = conv2d_batch_norm_relu(input_tensor, 256, 1, 1)
|
|
0 commit comments