diff --git a/cnn_e2e/inception_model.py b/cnn_e2e/inception_model.py index c427dd6b7eee280451b2aa143e93a35ba2ade890..e5f6e172aacc285d18c027c94a4c50759d7486a1 100644 --- a/cnn_e2e/inception_model.py +++ b/cnn_e2e/inception_model.py @@ -3,20 +3,20 @@ from __future__ import division from __future__ import print_function import oneflow as flow -from model_util import conv2d_layer +from model_util import conv2d_layer_with_bn def InceptionA(in_blob, index): with flow.deprecated.variable_scope("mixed_{}".format(index)): with flow.deprecated.variable_scope("branch1x1"): - branch1x1 = conv2d_layer( + branch1x1 = conv2d_layer_with_bn( "conv0", in_blob, filters=64, kernel_size=1, strides=1, padding="SAME" ) with flow.deprecated.variable_scope("branch5x5"): - branch5x5_1 = conv2d_layer( + branch5x5_1 = conv2d_layer_with_bn( "conv0", in_blob, filters=48, kernel_size=1, strides=1, padding="SAME" ) - branch5x5_2 = conv2d_layer( + branch5x5_2 = conv2d_layer_with_bn( "conv1", branch5x5_1, filters=64, @@ -25,10 +25,10 @@ def InceptionA(in_blob, index): padding="SAME", ) with flow.deprecated.variable_scope("branch3x3dbl"): - branch3x3dbl_1 = conv2d_layer( + branch3x3dbl_1 = conv2d_layer_with_bn( "conv0", in_blob, filters=64, kernel_size=1, strides=1, padding="SAME" ) - branch3x3dbl_2 = conv2d_layer( + branch3x3dbl_2 = conv2d_layer_with_bn( "conv1", branch3x3dbl_1, filters=96, @@ -36,7 +36,7 @@ def InceptionA(in_blob, index): strides=1, padding="SAME", ) - branch3x3dbl_3 = conv2d_layer( + branch3x3dbl_3 = conv2d_layer_with_bn( "conv2", branch3x3dbl_2, filters=96, @@ -53,7 +53,7 @@ def InceptionA(in_blob, index): data_format="NCHW", name="pool", ) - branch_pool_2 = conv2d_layer( + branch_pool_2 = conv2d_layer_with_bn( "conv", branch_pool_1, filters=32 if index == 0 else 64, @@ -76,14 +76,14 @@ def InceptionA(in_blob, index): def InceptionB(in_blob, index): with flow.deprecated.variable_scope("mixed_{}".format(index)): with flow.deprecated.variable_scope("branch3x3"): - branch3x3 = conv2d_layer( + branch3x3 = conv2d_layer_with_bn( "conv0", in_blob, filters=384, kernel_size=3, strides=2, padding="VALID" ) with flow.deprecated.variable_scope("branch3x3dbl"): - branch3x3dbl_1 = conv2d_layer( + branch3x3dbl_1 = conv2d_layer_with_bn( "conv0", in_blob, filters=64, kernel_size=1, strides=1, padding="SAME" ) - branch3x3dbl_2 = conv2d_layer( + branch3x3dbl_2 = conv2d_layer_with_bn( "conv1", branch3x3dbl_1, filters=96, @@ -91,7 +91,7 @@ def InceptionB(in_blob, index): strides=1, padding="SAME", ) - branch3x3dbl_3 = conv2d_layer( + branch3x3dbl_3 = conv2d_layer_with_bn( "conv2", branch3x3dbl_2, filters=96, @@ -121,11 +121,11 @@ def InceptionB(in_blob, index): def InceptionC(in_blob, index, filters): with flow.deprecated.variable_scope("mixed_{}".format(index)): with flow.deprecated.variable_scope("branch1x1"): - branch1x1 = conv2d_layer( + branch1x1 = conv2d_layer_with_bn( "conv0", in_blob, filters=192, kernel_size=1, strides=1, padding="SAME" ) with flow.deprecated.variable_scope("branch7x7"): - branch7x7_1 = conv2d_layer( + branch7x7_1 = conv2d_layer_with_bn( "conv0", in_blob, filters=filters, @@ -133,7 +133,7 @@ def InceptionC(in_blob, index, filters): strides=1, padding="SAME", ) - branch7x7_2 = conv2d_layer( + branch7x7_2 = conv2d_layer_with_bn( "conv1", branch7x7_1, filters=filters, @@ -141,7 +141,7 @@ def InceptionC(in_blob, index, filters): strides=1, padding="SAME", ) - branch7x7_3 = conv2d_layer( + branch7x7_3 = conv2d_layer_with_bn( "conv2", branch7x7_2, filters=192, @@ -150,7 +150,7 @@ def InceptionC(in_blob, index, filters): padding="SAME", ) with flow.deprecated.variable_scope("branch7x7dbl"): - branch7x7dbl_1 = conv2d_layer( + branch7x7dbl_1 = conv2d_layer_with_bn( "conv0", in_blob, filters=filters, @@ -158,7 +158,7 @@ def InceptionC(in_blob, index, filters): strides=1, padding="SAME", ) - branch7x7dbl_2 = conv2d_layer( + branch7x7dbl_2 = conv2d_layer_with_bn( "conv1", branch7x7dbl_1, filters=filters, @@ -166,7 +166,7 @@ def InceptionC(in_blob, index, filters): strides=1, padding="SAME", ) - branch7x7dbl_3 = conv2d_layer( + branch7x7dbl_3 = conv2d_layer_with_bn( "conv2", branch7x7dbl_2, filters=filters, @@ -174,7 +174,7 @@ def InceptionC(in_blob, index, filters): strides=1, padding="SAME", ) - branch7x7dbl_4 = conv2d_layer( + branch7x7dbl_4 = conv2d_layer_with_bn( "conv3", branch7x7dbl_3, filters=filters, @@ -182,7 +182,7 @@ def InceptionC(in_blob, index, filters): strides=1, padding="SAME", ) - branch7x7dbl_5 = conv2d_layer( + branch7x7dbl_5 = conv2d_layer_with_bn( "conv4", branch7x7dbl_4, filters=192, @@ -199,7 +199,7 @@ def InceptionC(in_blob, index, filters): data_format="NCHW", name="pool", ) - branch_pool_2 = conv2d_layer( + branch_pool_2 = conv2d_layer_with_bn( "conv", branch_pool_1, filters=192, @@ -221,10 +221,10 @@ def InceptionC(in_blob, index, filters): def InceptionD(in_blob, index): with flow.deprecated.variable_scope("mixed_{}".format(index)): with flow.deprecated.variable_scope("branch3x3"): - branch3x3_1 = conv2d_layer( + branch3x3_1 = conv2d_layer_with_bn( "conv0", in_blob, filters=192, kernel_size=1, strides=1, padding="SAME" ) - branch3x3_2 = conv2d_layer( + branch3x3_2 = conv2d_layer_with_bn( "conv1", branch3x3_1, filters=320, @@ -233,10 +233,10 @@ def InceptionD(in_blob, index): padding="VALID", ) with flow.deprecated.variable_scope("branch7x7x3"): - branch7x7x3_1 = conv2d_layer( + branch7x7x3_1 = conv2d_layer_with_bn( "conv0", in_blob, filters=192, kernel_size=1, strides=1, padding="SAME" ) - branch7x7x3_2 = conv2d_layer( + branch7x7x3_2 = conv2d_layer_with_bn( "conv1", branch7x7x3_1, filters=192, @@ -244,7 +244,7 @@ def InceptionD(in_blob, index): strides=1, padding="SAME", ) - branch7x7x3_3 = conv2d_layer( + branch7x7x3_3 = conv2d_layer_with_bn( "conv2", branch7x7x3_2, filters=192, @@ -252,7 +252,7 @@ def InceptionD(in_blob, index): strides=1, padding="SAME", ) - branch7x7x3_4 = conv2d_layer( + branch7x7x3_4 = conv2d_layer_with_bn( "conv3", branch7x7x3_3, filters=192, @@ -283,14 +283,14 @@ def InceptionD(in_blob, index): def InceptionE(in_blob, index): with flow.deprecated.variable_scope("mixed_{}".format(index)): with flow.deprecated.variable_scope("branch1x1"): - branch1x1 = conv2d_layer( + branch1x1 = conv2d_layer_with_bn( "conv0", in_blob, filters=320, kernel_size=1, strides=1, padding="SAME" ) with flow.deprecated.variable_scope("branch3x3"): - branch3x3_1 = conv2d_layer( + branch3x3_1 = conv2d_layer_with_bn( "conv0", in_blob, filters=384, kernel_size=1, strides=1, padding="SAME" ) - branch3x3_2 = conv2d_layer( + branch3x3_2 = conv2d_layer_with_bn( "conv1", branch3x3_1, filters=384, @@ -298,7 +298,7 @@ def InceptionE(in_blob, index): strides=1, padding="SAME", ) - branch3x3_3 = conv2d_layer( + branch3x3_3 = conv2d_layer_with_bn( "conv2", branch3x3_1, filters=384, @@ -313,10 +313,10 @@ def InceptionE(in_blob, index): values=inceptionE_1_bn, axis=1, name="concat" ) with flow.deprecated.variable_scope("branch3x3dbl"): - branch3x3dbl_1 = conv2d_layer( + branch3x3dbl_1 = conv2d_layer_with_bn( "conv0", in_blob, filters=448, kernel_size=1, strides=1, padding="SAME" ) - branch3x3dbl_2 = conv2d_layer( + branch3x3dbl_2 = conv2d_layer_with_bn( "conv1", branch3x3dbl_1, filters=384, @@ -324,7 +324,7 @@ def InceptionE(in_blob, index): strides=1, padding="SAME", ) - branch3x3dbl_3 = conv2d_layer( + branch3x3dbl_3 = conv2d_layer_with_bn( "conv2", branch3x3dbl_2, filters=384, @@ -332,7 +332,7 @@ def InceptionE(in_blob, index): strides=1, padding="SAME", ) - branch3x3dbl_4 = conv2d_layer( + branch3x3dbl_4 = conv2d_layer_with_bn( "conv3", branch3x3dbl_2, filters=384, @@ -355,7 +355,7 @@ def InceptionE(in_blob, index): data_format="NCHW", name="pool", ) - branch_pool_2 = conv2d_layer( + branch_pool_2 = conv2d_layer_with_bn( "conv", branch_pool_1, filters=192, @@ -382,22 +382,23 @@ def inceptionv3(images, trainable=True, need_transpose=False): images = flow.transpose(images, name="transpose", perm=[0, 3, 1, 2]) with flow.deprecated.variable_scope("InceptionV3"): - conv0 = conv2d_layer( + # conv0: 299 x 299 x 3 + conv0 = conv2d_layer_with_bn( "conv0", images, filters=32, kernel_size=3, strides=2, padding="VALID" ) - conv1 = conv2d_layer( + conv1 = conv2d_layer_with_bn( "conv1", conv0, filters=32, kernel_size=3, strides=1, padding="VALID" ) - conv2 = conv2d_layer( + conv2 = conv2d_layer_with_bn( "conv2", conv1, filters=64, kernel_size=3, strides=1, padding="SAME" ) pool1 = flow.nn.max_pool2d( conv2, ksize=3, strides=2, padding="VALID", data_format="NCHW", name="pool1" ) - conv3 = conv2d_layer( + conv3 = conv2d_layer_with_bn( "conv3", pool1, filters=80, kernel_size=1, strides=1, padding="VALID" ) - conv4 = conv2d_layer( + conv4 = conv2d_layer_with_bn( "conv4", conv3, filters=192, kernel_size=3, strides=1, padding="VALID" ) pool2 = flow.nn.max_pool2d( @@ -408,7 +409,6 @@ def inceptionv3(images, trainable=True, need_transpose=False): mixed_0 = InceptionA(pool2, 0) mixed_1 = InceptionA(mixed_0, 1) mixed_2 = InceptionA(mixed_1, 2) - # mixed_3 mixed_3 = InceptionB(mixed_2, 3) @@ -425,7 +425,6 @@ def inceptionv3(images, trainable=True, need_transpose=False): mixed_9 = InceptionE(mixed_8, 9) mixed_10 = InceptionE(mixed_9, 10) - # pool3 pool3 = flow.nn.avg_pool2d( mixed_10, ksize=8, strides=1, padding="VALID", data_format="NCHW", name="pool3" ) @@ -433,10 +432,10 @@ def inceptionv3(images, trainable=True, need_transpose=False): # TODO: Need to transpose weight when converting model from TF to OF if # you want to use layers.dense interface. fc1 = flow.layers.dense( - flow.reshape(pool3, [pool3.shape[0], -1]), - 1001, + inputs=flow.reshape(pool3, [pool3.shape[0], -1]), + units=1001, activation=None, - use_bias=False, + use_bias=True, kernel_initializer=flow.truncated_normal(0.816496580927726), bias_initializer=flow.constant_initializer(), trainable=trainable,