def bn_inceptionv2_cifar_model(input, labelDim, bnTimeConst): # 32 x 32 x 3 conv1a = conv_bn_relu_layer(input, 32, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 32 conv1b = conv_bn_relu_layer(conv1a, 32, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 32 conv1c = conv_bn_relu_layer(conv1b, 32, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 64 conv2a = conv_bn_relu_layer(conv1c, 64, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 64 conv2b = conv_bn_relu_layer(conv2a, 64, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 64 conv2c = conv_bn_relu_layer(conv2b, 64, (3,3), (1,1), True, bnTimeConst) # Inception Blocks # 32 x 32 x 64 inception3a = inception_block1(conv2b, 32, 32, 32, 32, 48, 16, bnTimeConst) # 32 x 32 x 128 inception3b = inception_block1(inception3a, 0, 64, 80, 32, 48, 0, bnTimeConst) # 32 x 32 x 128 inception3c = inception_block1(inception3b, 0, 64, 80, 32, 48, 0, bnTimeConst) # 32 x 32 x 128 pool1 = AveragePooling(filter_shape=(3,3), strides = (2,2), pad = True)(inception3c) # 16 x 16 x 256 inception4a = inception_block2(inception3c, 96, 48, 64, 48, 64, 64, bnTimeConst) # 16 x 16 x 256 inception4b = inception_block2(inception4a, 96, 48, 64, 48, 64, 64, bnTimeConst) # 16 x 16 x 256 inception4c = inception_block2(inception4b, 96, 48, 64, 48, 64, 64, bnTimeConst) # 16 x 16 x 288 inception4d = inception_block2(inception4c, 48, 64, 96, 80, 96, 64, bnTimeConst) # 16 x 16 x 288 inception4e = inception_block2(inception4d, 0, 128, 192, 192, 256, 0, bnTimeConst) # 16 x 16 x 288 pool2 = AveragePooling(filter_shape=(3,3), strides = (2,2), pad = True)(inception4e) # Inception Blocks # 8 x 8 x 512 inception5a = inception_block1(pool2, 176, 96, 160, 96, 112, 64, bnTimeConst) # 8 x 8 x 512 inception5b = inception_block1(inception5a, 176, 96, 160, 96, 112, 64, bnTimeConst) # Global Average # 8 x 8 x 512 pool3 = AveragePooling(filter_shape=(8,8))(inception5a) # 1 x 1 x 512 z = Dense(labelDim, init=he_normal())(pool3) return z
def inception_block_3(input, num1x1, num7x7, num7x7dbl, numPool, bnTimeConst): # 1x1 Convolution branch1x1 = conv_bn_relu_layer(input, num1x1, (1, 1), (1, 1), True, bnTimeConst) # 1x1 -> 1x3 -> 3x1 branch7x7_1 = conv_bn_relu_layer(input, num7x7[0], (1, 1), (1, 1), True, bnTimeConst) branch7x7_2 = conv_bn_relu_layer(branch7x7_1, num7x7[1], (1, 3), (1, 1), True, bnTimeConst) branch7x7 = conv_bn_relu_layer(branch7x7_2, num7x7[2], (3, 1), (1, 1), True, bnTimeConst) # 1x1 -> 1x3 -> 3x1 -> 1x3 -> 3x1 branch7x7dbl_1 = conv_bn_relu_layer(input, num7x7dbl[0], (1, 1), (1, 1), True, bnTimeConst) branch7x7dbl_2 = conv_bn_relu_layer(branch7x7dbl_1, num7x7dbl[1], (3, 1), (1, 1), True, bnTimeConst) branch7x7dbl_3 = conv_bn_relu_layer(branch7x7dbl_2, num7x7dbl[2], (1, 3), (1, 1), True, bnTimeConst) branch7x7dbl_4 = conv_bn_relu_layer(branch7x7dbl_3, num7x7dbl[3], (3, 1), (1, 1), True, bnTimeConst) branch7x7dbl = conv_bn_relu_layer(branch7x7dbl_4, num7x7dbl[4], (1, 3), (1, 1), True, bnTimeConst) # Average Pooling branchPool_avgpool = AveragePooling((3, 3), strides=(1, 1), pad=True)(input) branchPool = conv_bn_relu_layer(branchPool_avgpool, numPool, (1, 1), (1, 1), True, bnTimeConst) out = splice(branch1x1, branch7x7, branch7x7dbl, branchPool, axis=0) return out
def create_imagenet_model_bottleneck(input, num_stack_layers, num_classes, stride1x1, stride3x3): c_map = [64, 128, 256, 512, 1024, 2048] # conv1 and max pooling conv1 = conv_bn_relu(input, (7, 7), c_map[0], strides=(2, 2)) pool1 = MaxPooling((3, 3), strides=(2, 2), pad=True)(conv1) # conv2_x r2_1 = resnet_bottleneck_inc(pool1, c_map[2], c_map[0], (1, 1), (1, 1)) r2_2 = resnet_bottleneck_stack(r2_1, num_stack_layers[0], c_map[2], c_map[0]) # conv3_x r3_1 = resnet_bottleneck_inc(r2_2, c_map[3], c_map[1], stride1x1, stride3x3) r3_2 = resnet_bottleneck_stack(r3_1, num_stack_layers[1], c_map[3], c_map[1]) # conv4_x r4_1 = resnet_bottleneck_inc(r3_2, c_map[4], c_map[2], stride1x1, stride3x3) r4_2 = resnet_bottleneck_stack(r4_1, num_stack_layers[2], c_map[4], c_map[2]) # conv5_x r5_1 = resnet_bottleneck_inc(r4_2, c_map[5], c_map[3], stride1x1, stride3x3) r5_2 = resnet_bottleneck_stack(r5_1, num_stack_layers[3], c_map[5], c_map[3]) # Global average pooling and output pool = AveragePooling(filter_shape=(7, 7), name='final_avg_pooling')(r5_2) z = Dense(num_classes, init=C.normal(0.01))(pool) return z
def inception_block_5(input, num1x1, num3x3, num3x3_3x3, numPool, bnTimeConst): # 1x1 Convolution branch1x1 = conv_bn_relu_layer(input, num1x1, (1,1), (1,1), True, bnTimeConst) # 3x3 Convolution branch3x3_1 = conv_bn_relu_layer(input, num3x3[0], (1,1), (1,1), True, bnTimeConst) branch3x3_2 = conv_bn_relu_layer(branch3x3_1, num3x3[1], (1,3), (1,1), True, bnTimeConst) branch3x3_3 = conv_bn_relu_layer(branch3x3_1, num3x3[2], (3,1), (1,1), True, bnTimeConst) branch3x3 = splice(branch3x3_2, branch3x3_3, axis=0) # 3x3 3x3 Convolution branch3x3_3x3_1 = conv_bn_relu_layer(input, num3x3_3x3[0], (1,1), (1,1), True, bnTimeConst) branch3x3_3x3_2 = conv_bn_relu_layer(branch3x3_3x3_1, num3x3_3x3[1], (3,3), (1,1), True, bnTimeConst) branch3x3_3x3_3 = conv_bn_relu_layer(branch3x3_3x3_2, num3x3_3x3[1], (1,3), (1,1), True, bnTimeConst) branch3x3_3x3_4 = conv_bn_relu_layer(branch3x3_3x3_2, num3x3_3x3[3], (3,1), (1,1), True, bnTimeConst) branch3x3_3x3 = splice(branch3x3_3x3_3, branch3x3_3x3_4, axis=0) # Average Pooling branchPool_avgpool = AveragePooling((3,3), strides=(1,1), pad=True)(input) branchPool = conv_bn_relu_layer(branchPool_avgpool, numPool, (1,1), (1,1), True, bnTimeConst) out = splice(branch1x1, branch3x3, branch3x3_3x3, branchPool, axis=0) return out
def bn_inception_cifar_model(input, labelDim, bnTimeConst): # 32 x 32 x 3 conv1a = conv_bn_relu_layer(input, 32, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 32 conv1b = conv_bn_relu_layer(conv1a, 32, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 32 conv2a = conv_bn_relu_layer(conv1b, 64, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 64 conv2b = conv_bn_relu_layer(conv2a, 128, (3,3), (1,1), True, bnTimeConst) # Inception Blocks # 32 x 32 x 128 inception3a = inception_block_with_maxpool(conv2b, 32, 32, 32, 32, 48, 16, bnTimeConst) # 32 x 32 x 128 inception3b = inception_block_with_maxpool(inception3a, 32, 32, 32, 32, 48, 16, bnTimeConst) # 16 x 16 x 128 inception4a = inception_block_with_avgpool(inception3b, 96, 48, 64, 48, 64, 64, bnTimeConst) # 16 x 16 x 288 inception4b = inception_block_with_avgpool(inception4a, 48, 64, 96, 80, 96, 64, bnTimeConst) # 16 x 16 x 288 inception4c = inception_block_with_avgpool(inception4b, 48, 64, 96, 80, 96, 64, bnTimeConst) # 16 x 16 x 288 inception4d = inception_block_pass_through(inception4c, 0, 128, 192, 192, 256, 0, bnTimeConst) # 8 x 8 x 512 inception5a = inception_block_with_maxpool(inception4d, 176, 96, 160, 96, 112, 64, bnTimeConst) # Global Average # 8 x 8 x 512 pool1 = AveragePooling(filter_shape=(8,8))(inception5a) # 1 x 1 x 512 z = Dense(labelDim, init=he_normal())(pool1) return z
def inception_block_1(input, num1x1, num5x5, num3x3dbl, numPool, bnTimeConst): # 1x1 branch1x1 = conv_bn_relu_layer(input, num1x1, (1, 1), (1, 1), True, bnTimeConst) # 1x1 -> 5x5 branch5x5_1 = conv_bn_relu_layer(input, num5x5[0], (1, 1), (1, 1), True, bnTimeConst) branch5x5 = conv_bn_relu_layer(branch5x5_1, num5x5[1], (5, 5), (1, 1), True, bnTimeConst) # 1x1 -> 3x3 -> 3x3 branch3x3dbl_1 = conv_bn_relu_layer(input, num3x3dbl[0], (1, 1), (1, 1), True, bnTimeConst) branch3x3dbl_2 = conv_bn_relu_layer(branch3x3dbl_1, num3x3dbl[1], (3, 3), (1, 1), True, bnTimeConst) branch3x3dbl = conv_bn_relu_layer(branch3x3dbl_2, num3x3dbl[2], (3, 3), (1, 1), True, bnTimeConst) # Average Pooling branchPool_avgpool = AveragePooling((3, 3), strides=(1, 1), pad=True)(input) branchPool = conv_bn_relu_layer(branchPool_avgpool, numPool, (1, 1), (1, 1), True, bnTimeConst) out = splice(branch1x1, branch5x5, branch3x3dbl, branchPool, axis=0) return out
def inception_block_with_avgpool(input, num1x1, num3x3r, num3x3, num3x3dblr, num3x3dbl, numPool, bnTimeConst): # 1x1 Convolution branch1x1 = conv_bn_relu_layer(input, num1x1, (1, 1), (1, 1), True, bnTimeConst) # 3x3 branch3x3_reduce = conv_bn_relu_layer(input, num3x3r, (1, 1), (1, 1), True, bnTimeConst) branch3x3 = conv_bn_relu_layer(branch3x3_reduce, num3x3, (3, 3), (1, 1), True, bnTimeConst) # 1x1 -> 3x3 -> 3x3 branch3x3dbl_reduce = conv_bn_relu_layer(input, num3x3dblr, (1, 1), (1, 1), True, bnTimeConst) branch3x3dbl_conv = conv_bn_relu_layer(branch3x3dbl_reduce, num3x3dbl, (3, 3), (1, 1), True, bnTimeConst) branch3x3dbl = conv_bn_relu_layer(branch3x3dbl_conv, num3x3dbl, (3, 3), (1, 1), True, bnTimeConst) # avg pooling -> 1x1 branchPool_avgpool = AveragePooling((3, 3), strides=(1, 1), pad=True)(input) branchPool = conv_bn_relu_layer(branchPool_avgpool, numPool, (1, 1), (1, 1), True, bnTimeConst) out = splice(branch1x1, branch3x3, branch3x3dbl, branchPool, axis=0) return out
def bn_inception_model(input, labelDim, bnTimeConst): # 224 x 224 x 3 conv1 = conv_bn_relu_layer(input, 64, (7, 7), (2, 2), True, bnTimeConst) # 112 x 112 x 64 pool1 = MaxPooling(filter_shape=(3, 3), strides=(2, 2), pad=True)(conv1) # 56 x 56 x 64 conv2a = conv_bn_relu_layer(pool1, 64, (1, 1), (1, 1), True, bnTimeConst) # 56 x 56 x 64 conv2b = conv_bn_relu_layer(conv2a, 192, (3, 3), (1, 1), True, bnTimeConst) # 56 x 56 x 192 pool2 = MaxPooling(filter_shape=(3, 3), strides=(2, 2), pad=True)(conv2b) # Inception Blocks # 28 x 28 x 192 inception3a = inception_block_with_avgpool(pool2, 64, 64, 64, 64, 96, 32, bnTimeConst) # 28 x 28 x 256 inception3b = inception_block_with_avgpool(inception3a, 64, 64, 96, 64, 96, 64, bnTimeConst) # 28 x 28 x 320 inception3c = inception_block_pass_through(inception3b, 0, 128, 160, 64, 96, 0, bnTimeConst) # 14 x 14 x 576 inception4a = inception_block_with_avgpool(inception3c, 224, 64, 96, 96, 128, 128, bnTimeConst) # 14 x 14 x 576 inception4b = inception_block_with_avgpool(inception4a, 192, 96, 128, 96, 128, 128, bnTimeConst) # 14 x 14 x 576 inception4c = inception_block_with_avgpool(inception4b, 160, 128, 160, 128, 160, 128, bnTimeConst) # 14 x 14 x 576 inception4d = inception_block_with_avgpool(inception4c, 96, 128, 192, 160, 192, 128, bnTimeConst) # 14 x 14 x 576 inception4e = inception_block_pass_through(inception4d, 0, 128, 192, 192, 256, 0, bnTimeConst) # 7 x 7 x 1024 inception5a = inception_block_with_avgpool(inception4e, 352, 192, 320, 160, 224, 128, bnTimeConst) # 7 x 7 x 1024 inception5b = inception_block_with_maxpool(inception5a, 352, 192, 320, 192, 224, 128, bnTimeConst) # Global Average # 7 x 7 x 1024 pool3 = AveragePooling(filter_shape=(7, 7))(inception5b) # 1 x 1 x 1024 z = Dense(labelDim, init=he_normal())(pool3) return z
def inceptionv1_cifar_model2(input, labelDim, bnTimeConst): # 32 x 32 x 3 conv1 = conv_bn_relu_layer(input, 32, (3, 3), (1, 1), True, bnTimeConst) # 32 x 32 x 32 conv2 = conv_bn_relu_layer(conv1, 32, (3, 3), (1, 1), True, bnTimeConst) # Inception Blocks # 32 x 32 x 64 inception3a = inception_block_with_maxpool(conv2, 32, 32, 32, 32, 48, 16, bnTimeConst) # 32 x 32 x 128 inception3b = inception_block_with_maxpool(inception3a, 32, 32, 32, 32, 48, 16, bnTimeConst) maxpool1 = MaxPooling((3, 3), strides=(2, 2), pad=True)(inception3b) # 16 x 16 x 128 inception4a = inception_block_with_maxpool(maxpool1, 96, 48, 64, 48, 64, 64, bnTimeConst) # 16 x 16 x 288 inception4b = inception_block_with_maxpool(inception4a, 96, 48, 64, 48, 64, 64, bnTimeConst) # 16 x 16 x 288 inception4c = inception_block_with_maxpool(inception4b, 96, 48, 64, 48, 64, 64, bnTimeConst) # 16 x 16 x 288 inception4d = inception_block_with_maxpool(inception4c, 96, 48, 64, 48, 64, 64, bnTimeConst) # 16 x 16 x 288 inception4e = inception_block_with_maxpool(inception4d, 96, 48, 64, 48, 64, 64, bnTimeConst) maxpool2 = MaxPooling((3, 3), strides=(2, 2), pad=True)(inception4e) # 8 x 8 x 288 inception5a = inception_block_with_maxpool(inception4e, 176, 96, 160, 96, 112, 64, bnTimeConst) # 8 x 8 x 512 inception5b = inception_block_with_maxpool(inception5a, 176, 96, 160, 96, 112, 64, bnTimeConst) # Global Average # 8 x 8 x 512 pool1 = AveragePooling(filter_shape=(8, 8))(inception5b) # 1 x 1 x 512 z = Dense(labelDim, init=he_normal())(pool1) return z
def Inception_A(input, a1, b1, c1, c2, d1, d2, bnTimeConst): A1 = AveragePooling((3, 3), strides=(1, 1), pad=True)(input) A2 = conv_bn_relu_layer(A1, a1, (3, 3), (1, 1), True, bnTimeConst) B1 = conv_bn_relu_layer(input, b1, (1, 1), (1, 1), True, bnTimeConst) C1 = conv_bn_relu_layer(input, c1, (1, 1), (1, 1), True, bnTimeConst) C2 = conv_bn_relu_layer(C1, c2, (3, 3), (1, 1), True, bnTimeConst) D1 = conv_bn_relu_layer(input, d1, (1, 1), (1, 1), True, bnTimeConst) D2 = conv_bn_relu_layer(D1, d2, (3, 3), (1, 1), True, bnTimeConst) D3 = conv_bn_relu_layer(D2, d2, (3, 3), (1, 1), True, bnTimeConst) out = splice(A2, B1, C2, D3, axis=0) return out
def create_resnet_model(input, num_classes): conv = convolution_bn(input, (3, 3), 16) r1_1 = resnet_basic_stack(conv, 16, 3) r2_1 = resnet_basic_inc(r1_1, 32) r2_2 = resnet_basic_stack(r2_1, 32, 2) r3_1 = resnet_basic_inc(r2_2, 64) r3_2 = resnet_basic_stack(r3_1, 64, 2) # Global average pooling pool = AveragePooling(filter_shape=(8, 8), strides=(1, 1))(r3_2) net = Dense(num_classes, init=he_normal(), activation=None)(pool) return net
def create_cifar10_model(input, num_stack_layers, num_classes): c_map = [16, 32, 64] conv = conv_bn_relu(input, (3, 3), c_map[0]) r1 = resnet_basic_stack(conv, num_stack_layers, c_map[0]) r2_1 = resnet_basic_inc(r1, c_map[1]) r2_2 = resnet_basic_stack(r2_1, num_stack_layers - 1, c_map[1]) r3_1 = resnet_basic_inc(r2_2, c_map[2]) r3_2 = resnet_basic_stack(r3_1, num_stack_layers - 1, c_map[2]) # Global average pooling and output pool = AveragePooling(filter_shape=(8, 8), name='final_avg_pooling')(r3_2) z = Dense(num_classes)(pool) return z
def create_cifar10_model(input, num_classes): c_map = [32, 64, 128] conv = conv_bn_relu(input, (3, 3), c_map[0]) r1 = mobilenet_basic_stack(conv, 3, c_map[0]) r2_1 = mobilenet_basic_inc(r1, c_map[1]) r2_2 = mobilenet_basic_stack(r2_1, 3, c_map[1]) r3_1 = mobilenet_basic_inc(r2_2, c_map[2]) r3_2 = mobilenet_basic_stack(r3_1, 3, c_map[2]) # Global average pooling and output pool = AveragePooling(filter_shape=(8, 8))(r3_2) z = Dense(num_classes, init=he_normal())(pool) return z
def create_resnet34(input, num_classes): c_map = [64, 128, 256, 512] num_layers = [3, 3, 5, 2] conv = conv_bn_relu(input, (7, 7), c_map[0], (2, 2)) maxPool = MaxPooling((3, 3), (2, 2), pad=True)(conv) r1 = resnet_basic_stack(maxPool, num_layers[0], c_map[0]) r2_1 = resnet_basic_inc(r1, c_map[1]) r2_2 = resnet_basic_stack(r2_1, num_layers[1], c_map[1]) r3_1 = resnet_basic_inc(r2_2, c_map[2]) r3_2 = resnet_basic_stack(r3_1, num_layers[2], c_map[2]) r4_1 = resnet_basic_inc(r3_2, c_map[3]) r4_2 = resnet_basic_stack(r4_1, num_layers[3], c_map[3]) # Global average pooling and output pool = AveragePooling(filter_shape=(7, 7))(r4_2) z = Dense(num_classes)(pool) return z
def InceptionV4_ResNet_model(input, num_classes): l = pre_block(input) # 32 x 32 x 128 A1 = Res_A_stack(l,5, 64, 128) RA = reduction_A(A1, 32, 32, 64, 64) # 16 x 16 x 224 B1 = Res_B_stack(RA, 10, 128, 224) # 16 x 16 x 448 RB = reduction_B(B1, 32, 64, 32, 64, 128) # 8 x 8 x 480 C1 = Res_C_stack(RB, 5, 192, 480) # Global average pooling and output pool = AveragePooling(filter_shape=(8, 8), name='final_avg_pooling')(C1) z = Dense(num_classes, init=normal(0.01))(pool) return z
def Inception_ResNet_v2_model(input, num_classes): l = pre_block(input) # 32 x 32 x 128 A1 = Res_A_stack(l, 5, 32, 32, 32, 48, 64, 128) # 32 x 32 x 128 RA = reduction_A(A1, 64, 32, 64, 64) # 16 x 16 x 256 B1 = Res_B_stack(RA, 10, 128, 64, 96, 128, 256) # 16 x 16 x 256 RB = reduction_B(B1, 32, 64, 32, 64, 32, 64, 128) # 8 x 8 x 512 C1 = Res_C_stack(RB, 5, 128, 64, 128, 256, 512) # Global average pooling and output pool = AveragePooling(filter_shape=(8, 8))(C1) z = Dense(num_classes, init=normal(0.01))(pool) return z
def inception_v4_model(input, labelDim, bnTimeConst): l = pre_block(input, bnTimeConst) # 32 x 32 A1 = Inception_A(l, 32, 32, 32, 64, 32, 64, bnTimeConst) A2 = Inception_A(A1, 32, 32, 32, 64, 32, 64, bnTimeConst) A3 = Inception_A(A2, 32, 32, 32, 64, 32, 64, bnTimeConst) A4 = Inception_A(A3, 32, 32, 32, 64, 32, 64, bnTimeConst) # 32 x 32 x 192 RA = reduction_A(A4, 32, 32, 64, 64, bnTimeConst) # 16 x 16 x 288 B1 = Inception_B(RA, 128, 32, 32, 64, 96, 32, 64, 96, bnTimeConst) B2 = Inception_B(B1, 128, 32, 32, 64, 96, 32, 64, 96, bnTimeConst) B3 = Inception_B(B2, 128, 32, 32, 64, 96, 32, 64, 96, bnTimeConst) B4 = Inception_B(B3, 128, 32, 32, 64, 96, 32, 64, 96, bnTimeConst) B5 = Inception_B(B4, 128, 32, 32, 64, 96, 32, 64, 96, bnTimeConst) B6 = Inception_B(B5, 128, 32, 32, 64, 96, 32, 64, 96, bnTimeConst) B7 = Inception_B(B6, 128, 32, 32, 64, 96, 32, 64, 96, bnTimeConst) # 16 x 16 x 352 RB = reduction_B(B7, 64, 64, 96, bnTimeConst) # 8 x 8 x 512 C1 = Inception_C(RB, 128, 128, 96, 64, 64, 128, 160, 64, bnTimeConst) C2 = Inception_C(C1, 128, 128, 96, 64, 64, 128, 160, 64, bnTimeConst) C3 = Inception_C(C2, 128, 128, 96, 64, 64, 128, 160, 64, bnTimeConst) # 8 x 8 x 512 pool1 = AveragePooling(filter_shape=(8, 8))(C3) # 1 x 1 x 512 z = Dense(labelDim, init=he_normal())(pool1) return z
def inception_v3_cifar_model(input, labelDim, bnTimeConst): # 32 x 32 x 3 conv1 = conv_bn_relu_layer(input, 32, (3, 3), (1, 1), True, bnTimeConst) # 32 x 32 x 32 conv2 = conv_bn_relu_layer(conv1, 32, (3, 3), (1, 1), True, bnTimeConst) # 32 x 32 x 32 conv3 = conv_bn_relu_layer(conv2, 64, (3, 3), (1, 1), True, bnTimeConst) # 32 x 32 x 64 conv4 = conv_bn_relu_layer(conv3, 80, (1, 1), (1, 1), True, bnTimeConst) # 32 x 32 x 80 conv5 = conv_bn_relu_layer(conv4, 128, (3, 3), (1, 1), True, bnTimeConst) # 32 x 32 x 128 pool1 = MaxPooling(filter_shape=(3, 3), strides=(2, 2), pad=True)(conv5) # # Inception Blocks # 16 x 16 x 128 mixed1 = inception_block_1(pool1, 32, [32, 48], [48, 64, 64], 32, bnTimeConst) # 16 x 16 x 160 mixed2 = inception_block_1(mixed1, 32, [32, 48], [48, 64, 64], 64, bnTimeConst) # 16 x 16 x 160 mixed3 = inception_block_1(mixed2, 32, [32, 48], [48, 64, 64], 64, bnTimeConst) # 16 x 16 x 160 #mixed4 = inception_block_2(mixed3, 32, [48, 64, 64], bnTimeConst) mixed4 = inception_block_pass_through(mixed3, 0, 64, 80, 32, 48, 0, bnTimeConst) # 8 x 8 x 256 mixed5 = inception_block_3(mixed4, 192, [48, 64, 64], [128, 128, 128, 128, 192], 192, bnTimeConst) # 8 x 8 x mixed6 = inception_block_3(mixed5, 192, [160, 160, 192], [160, 160, 160, 160, 192], 192, bnTimeConst) # 8 x 8 x 768 mixed7 = inception_block_3(mixed6, 192, [160, 160, 192], [160, 160, 160, 160, 192], 192, bnTimeConst) # 8 x 8 x 768 mixed8 = inception_block_3(mixed7, 192, [192, 192, 192], [192, 192, 192, 192, 192], 192, bnTimeConst) # 8 x 8 x 768 mixed9 = inception_block_3(mixed8, 192, [192, 192, 192], [192, 192, 192, 192, 192], 192, bnTimeConst) # 8 x 8 x 1280 mixed10 = inception_block_5(mixed9, 320, [384, 384, 384], [448, 384, 384, 384], 192, bnTimeConst) # 8 x 8 x 2048 mixed11 = inception_block_5(mixed10, 320, [384, 384, 384], [448, 384, 384, 384], 192, bnTimeConst) # 8 x 8 x 2048 # # Prediction # pool3 = AveragePooling(filter_shape=(8, 8))(mixed11) # 1 x 1 x 2048 drop = Dropout(dropout_rate=0.2)(pool3) # 1 x 1 x 2048 z = Dense(labelDim, init=he_normal())(drop) # # Auxiliary # # 8 x 8 x 768 auxPool = AveragePooling(filter_shape=(3, 3), strides=(1, 1), pad=True)(mixed8) # 5 x 5 x 768 auxConv1 = conv_bn_relu_layer(auxPool, 128, (1, 1), (1, 1), True, bnTimeConst) # 5 x 5 x 128 auxConv2 = conv_bn_relu_layer(auxConv1, 256, (3, 3), (1, 1), True, bnTimeConst) # 1 x 1 x 768 aux = Dense(labelDim, init=he_normal())(auxConv2) return {'z': z, 'aux': aux}
def inception_v3_cifar_model(input, labelDim, bnTimeConst): # 32 x 32 x 3 conv1 = conv_bn_relu_layer(input, 32, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 32 conv2 = conv_bn_relu_layer(conv1, 32, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 32 conv3 = conv_bn_relu_layer(conv2, 64, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 64 conv4 = conv_bn_relu_layer(conv3, 80, (1,1), (1,1), True, bnTimeConst) # 32 x 32 x 80 conv5 = conv_bn_relu_layer(conv4, 128, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 128 pool1 = MaxPooling(filter_shape=(3,3), strides=(2,2), pad=True)(conv5) # # Inception Blocks # 16 x 16 x 128 mixed1 = inception_block_1(pool1, 32, [32, 48], [48, 64, 64], 32, bnTimeConst) # 16 x 16 x 176 mixed2 = inception_block_1(mixed1, 32, [32, 48], [48, 64, 64], 32, bnTimeConst) # 16 x 16 x 176 mixed3 = inception_block_1(mixed2, 32, [32, 48], [48, 64, 64], 32, bnTimeConst) # 16 x 16 x 176 mixed4 = inception_block_pass_through(mixed3, 0, 32, 48, 32, 48, 0, bnTimeConst) # 8 x 8 x 256 mixed5 = inception_block_3(mixed4, 64, [48, 64, 64], [48, 48, 48, 48, 64], 64, bnTimeConst) # 8 x 8 x 256 mixed6 = inception_block_3(mixed5, 64, [48, 64, 64], [48, 48, 48, 48, 64], 64, bnTimeConst) # 8 x 8 x 256 mixed7 = inception_block_3(mixed6, 64, [48, 64, 64], [48, 48, 48, 48, 64], 64, bnTimeConst) # 8 x 8 x 256 mixed8 = inception_block_3(mixed7, 80, [48, 64, 64], [48, 48, 48, 48, 64], 80, bnTimeConst) # 8 x 8 x 288 mixed9 = inception_block_3(mixed8, 128, [64, 128, 128], [64, 64, 64, 64, 128], 128, bnTimeConst) # 8 x 8 x 512 mixed10 = inception_block_5(mixed9, 128, [64, 128, 128], [64, 64, 64, 128], 128, bnTimeConst) # 8 x 8 x 512 mixed11 = inception_block_5(mixed10, 128, [64, 128, 128], [64, 64, 64, 128], 128, bnTimeConst) # 8 x 8 x 512 # # Prediction # pool2 = AveragePooling(filter_shape=(8,8))(mixed11) # 1 x 1 x 512 z = Dense(labelDim, init=he_normal())(pool2) # # Auxiliary # # 8 x 8 x 288 auxPool = AveragePooling(filter_shape=(3,3), strides=(1,1), pad=True)(mixed8) # 8 x 8 x 288 auxConv1 = conv_bn_relu_layer(auxPool, 320, (1,1), (1,1), True, bnTimeConst) # 8 x 8 x 320 auxConv2 = conv_bn_relu_layer(auxConv1, 512, (3,3), (1,1), True, bnTimeConst) # 8 x 8 x 512 aux = Dense(labelDim, init=he_normal())(auxConv2) return { 'z': z, 'aux': aux }
def clone_cntk_layer(self, feature): """Returns a clone of the CNTK layer for per-layer forward prop""" pad, filterShape, stride = self.get_cntk_parameters() return AveragePooling(filterShape, strides=(stride, stride), pad=pad)(feature)
def inception_v3_model(input, labelDim, dropRate, bnTimeConst): # 299 x 299 x 3 conv1 = conv_bn_relu_layer(input, 32, (3, 3), (2, 2), False, bnTimeConst) # 149 x 149 x 32 conv2 = conv_bn_relu_layer(conv1, 32, (3, 3), (1, 1), False, bnTimeConst) # 147 x 147 x 32 conv3 = conv_bn_relu_layer(conv2, 64, (3, 3), (1, 1), True, bnTimeConst) # 147 x 147 x 64 pool1 = MaxPooling(filter_shape=(3, 3), strides=(2, 2), pad=False)(conv3) # 73 x 73 x 64 conv4 = conv_bn_relu_layer(pool1, 80, (1, 1), (1, 1), False, bnTimeConst) # 73 x 73 x 80 conv5 = conv_bn_relu_layer(conv4, 192, (3, 3), (1, 1), False, bnTimeConst) # 71 x 71 x 192 pool2 = MaxPooling(filter_shape=(3, 3), strides=(2, 2), pad=False)(conv5) # 35 x 35 x 192 # # Inception Blocks # mixed1 = inception_block_1(pool2, 64, [48, 64], [64, 96, 96], 32, bnTimeConst) # 35 x 35 x 256 mixed2 = inception_block_1(mixed1, 64, [48, 64], [64, 96, 96], 64, bnTimeConst) # 35 x 35 x 288 mixed3 = inception_block_1(mixed2, 64, [48, 64], [64, 96, 96], 64, bnTimeConst) # 35 x 35 x 288 mixed4 = inception_block_2(mixed3, 384, [64, 96, 96], bnTimeConst) # 17 x 17 x 768 mixed5 = inception_block_3(mixed4, 192, [128, 128, 192], [128, 128, 128, 128, 192], 192, bnTimeConst) # 17 x 17 x 768 mixed6 = inception_block_3(mixed5, 192, [160, 160, 192], [160, 160, 160, 160, 192], 192, bnTimeConst) # 17 x 17 x 768 mixed7 = inception_block_3(mixed6, 192, [160, 160, 192], [160, 160, 160, 160, 192], 192, bnTimeConst) # 17 x 17 x 768 mixed8 = inception_block_3(mixed7, 192, [192, 192, 192], [192, 192, 192, 192, 192], 192, bnTimeConst) # 17 x 17 x 768 mixed9 = inception_block_4(mixed8, [192, 320], [192, 192, 192, 192], bnTimeConst) # 8 x 8 x 1280 mixed10 = inception_block_5(mixed9, 320, [384, 384, 384], [448, 384, 384, 384], 192, bnTimeConst) # 8 x 8 x 2048 mixed11 = inception_block_5(mixed10, 320, [384, 384, 384], [448, 384, 384, 384], 192, bnTimeConst) # 8 x 8 x 2048 # # Prediction # pool3 = AveragePooling(filter_shape=(8, 8), pad=False)(mixed11) # 1 x 1 x 2048 drop = Dropout(dropout_rate=dropRate)(pool3) # 1 x 1 x 2048 z = Dense(labelDim, init=he_normal())(drop) # # Auxiliary # # 17 x 17 x 768 auxPool = AveragePooling(filter_shape=(5, 5), strides=(3, 3), pad=False)(mixed8) # 5 x 5 x 768 auxConv1 = conv_bn_relu_layer(auxPool, 128, (1, 1), (1, 1), True, bnTimeConst) # 5 x 5 x 128 auxConv2 = conv_bn_relu_layer(auxConv1, 768, (5, 5), (1, 1), False, bnTimeConst) # 1 x 1 x 768 aux = Dense(labelDim, init=he_normal())(auxConv2) return {'z': z, 'aux': aux}