udpate inception_v4 for the final and aux endpoints; update the meta.yml as the protobuf binary file is not backward compatible. singa compiled on protobuf3.0 cannot run with protobuf 3.2 runtime.
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/40124db7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/40124db7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/40124db7 Branch: refs/heads/master Commit: 40124db7f6f1bb59ed33ea9b39c3fcb31b3fd02d Parents: 59b0167 Author: wang wei <[email protected]> Authored: Fri Jul 7 12:26:20 2017 +0800 Committer: wang wei <[email protected]> Committed: Fri Jul 7 12:26:20 2017 +0800 ---------------------------------------------------------------------- examples/imagenet/inception/convert.py | 23 +- examples/imagenet/inception/inception_v4.py | 346 +++++++++++++---------- tool/conda/meta.yaml | 16 +- 3 files changed, 222 insertions(+), 163 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/40124db7/examples/imagenet/inception/convert.py ---------------------------------------------------------------------- diff --git a/examples/imagenet/inception/convert.py b/examples/imagenet/inception/convert.py index e3f5adc..c9f92e4 100644 --- a/examples/imagenet/inception/convert.py +++ b/examples/imagenet/inception/convert.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== -"""A simple script for inspect checkpoint files.""" +"""Converting tensorflow checkpoint file to key-val pkl file.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function @@ -25,7 +25,8 @@ import os import numpy as np from tensorflow.python import pywrap_tensorflow from tensorflow.python.platform import app -import model +import inception_v4 +import inception_v3 FLAGS = None @@ -38,8 +39,11 @@ def rename(name, suffix): return name[0:p+1] + suffix -def convert(file_name): - net, _ = model.create_net() +def convert(model, file_name): + if model == 'v3': + net, _ = inception_v3.create_net() + else: + net, _ = inception_v4.create_net() params = {'SINGA_VERSION': 1101} try: reader = pywrap_tensorflow.NewCheckpointReader(file_name) @@ -61,7 +65,8 @@ def convert(file_name): val = np.ones(pval.shape) else: print('not matched param %s' % pname) - assert val.shape == pval.shape, ('the shapes not match ', val.shape, pval.shape) + assert val.shape == pval.shape, ('the shapes not match ', + val.shape, pval.shape) params[pname] = val.astype(np.float32) print('converting:', pname, pval.shape) var_to_shape_map = reader.get_variable_to_shape_map() @@ -103,15 +108,13 @@ def main(unused_argv): print("Usage: convert.py --file_name=checkpoint_file_name ") sys.exit(1) else: - convert(FLAGS.file_name) + convert(FLAGS.model, FLAGS.file_name) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.register("type", "bool", lambda v: v.lower() == "true") - parser.add_argument( - "--file_name", type=str, default="", help="Checkpoint filename. " - "Note, if using Checkpoint V2 format, file_name is the " - "shared prefix between all files in the checkpoint.") + parser.add_argument("model", choices=['v3', 'v4'], help="inception version") + parser.add_argument("file_name", help="Checkpoint path") FLAGS, unparsed = parser.parse_known_args() app.run(main=main, argv=[sys.argv[0]] + unparsed) http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/40124db7/examples/imagenet/inception/inception_v4.py ---------------------------------------------------------------------- diff --git a/examples/imagenet/inception/inception_v4.py b/examples/imagenet/inception/inception_v4.py index baab522..b641706 100644 --- a/examples/imagenet/inception/inception_v4.py +++ b/examples/imagenet/inception/inception_v4.py @@ -39,193 +39,245 @@ from singa import net as ffnet ffnet.verbose = True -def conv2d(net, name, nb_filter, k, s=1, padding='SAME', src=None): - net.add(Conv2D(name, nb_filter, k, s, border_mode=padding, use_bias=False), src) +def conv2d(net, name, nb_filter, k, s=1, border_mode='SAME', src=None): + net.add(Conv2D(name, nb_filter, k, s, border_mode=border_mode, + use_bias=False), src) net.add(BatchNormalization('%s/BatchNorm' % name)) return net.add(Activation(name+'/relu')) -def block_inception_a(name, net): +def block_inception_a(blk, net): """Builds Inception-A block for Inception v4 network.""" # By default use stride=1 and SAME padding - split = net.add(Split('%s/Split' % name, 4)) - br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % name, 96, 1, src=split) - conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % name, 64, 1, src=split) - br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_3x3' % name, 96, 3) - conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % name, 64, 1, src=split) - conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % name, 96, 3) - br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_3x3' % name, 96, 3) - net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % name, 3, stride=1), split) - br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % name, 96, 1) - return net.add(Concat('%s/Concat' % name, 1), [br0, br1, br2, br3]) - - -def block_reduction_a(name, net): + s = net.add(Split('%s/Split' % blk, 4)) + br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, 96, 1, src=s) + conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, 64, 1, src=s) + br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_3x3' % blk, 96, 3) + conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % blk, 64, 1, src=s) + conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % blk, 96, 3) + br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_3x3' % blk, 96, 3) + net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % blk, 3, stride=1), s) + br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % blk, 96, 1) + return net.add(Concat('%s/Concat' % blk, 1), [br0, br1, br2, br3]) + + +def block_reduction_a(blk, net): """Builds Reduction-A block for Inception v4 network.""" # By default use stride=1 and SAME padding - split = net.add(Split('%s/Split' % name, 3)) - br0 = conv2d(net, '%s/Branch_0/Conv2d_1a_3x3' % name, 384, 3, 2, padding='VALID', src=split) - conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % name, 192, 1, src=split) - conv2d(net, '%s/Branch_1/Conv2d_0b_3x3' % name, 224, 3) - br1 = conv2d(net, '%s/Branch_1/Conv2d_1a_3x3' % name, 256, 3, 2, padding='VALID') - br2 = net.add(MaxPooling2D('%s/Branch_2/MaxPool_1a_3x3' % name, 3, 2, border_mode='VALID'), split) - return net.add(Concat('%s/Concat' % name, 1), [br0, br1, br2]) - - -def block_inception_b(name, net): + s = net.add(Split('%s/Split' % blk, 3)) + br0 = conv2d(net, '%s/Branch_0/Conv2d_1a_3x3' % blk, 384, 3, 2, + border_mode='VALID', src=s) + br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, 192, 1, src=s) + br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_3x3' % blk, 224, 3) + br1 = conv2d(net, '%s/Branch_1/Conv2d_1a_3x3' % blk, 256, 3, 2, + border_mode='VALID') + br2 = net.add(MaxPooling2D('%s/Branch_2/MaxPool_1a_3x3' % blk, 3, 2, + border_mode='VALID'), s) + return net.add(Concat('%s/Concat' % blk, 1), [br0, br1, br2]) + + +def block_inception_b(blk, net): """Builds Inception-B block for Inception v4 network.""" # By default use stride=1 and SAME padding - split = net.add(Split('%s/Split' % name, 4)) - br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % name, 384, 1, src=split) - conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % name, 192, 1, src=split) - conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % name, 224, (1, 7)) - br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % name, 256, (7, 1)) - conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % name, 192, 1, src=split) - conv2d(net, '%s/Branch_2/Conv2d_0b_7x1' % name, 192, (7, 1)) - conv2d(net, '%s/Branch_2/Conv2d_0c_1x7' % name, 224, (1, 7)) - conv2d(net, '%s/Branch_2/Conv2d_0d_7x1' % name, 224, (7, 1)) - br2 = conv2d(net, '%s/Branch_2/Conv2d_0e_1x7' % name, 256, (1, 7)) - net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % name, 3, 1), split) - br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % name, 128, 1) - return net.add(Concat('%s/Concat' % name, 1), [br0, br1, br2, br3]) - - -def block_reduction_b(name, net): + s = net.add(Split('%s/Split' % blk, 4)) + br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, 384, 1, src=s) + br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, 192, 1, src=s) + br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % blk, 224, (1, 7)) + br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % blk, 256, (7, 1)) + br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % blk, 192, 1, src=s) + br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_7x1' % blk, 192, (7, 1)) + br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x7' % blk, 224, (1, 7)) + br2 = conv2d(net, '%s/Branch_2/Conv2d_0d_7x1' % blk, 224, (7, 1)) + br2 = conv2d(net, '%s/Branch_2/Conv2d_0e_1x7' % blk, 256, (1, 7)) + br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % blk, 3, 1), s) + br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % blk, 128, 1) + return net.add(Concat('%s/Concat' % blk, 1), [br0, br1, br2, br3]) + + +def block_reduction_b(blk, net): """Builds Reduction-B block for Inception v4 network.""" # By default use stride=1 and SAME padding - split = net.add(Split('%s/Split', 3)) - conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % name, 192, 1, src=split) - br0 = conv2d(net, '%s/Branch_0/Conv2d_1a_3x3' % name, 192, 3, 2, padding='VALID') - conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % name, 256, 1, src=split) - conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % name, 256, (1, 7)) - conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % name, 320, (7, 1)) - br1 = conv2d(net, '%s/Branch_1/Conv2d_1a_3x3' % name, 320, 3, 2, padding='VALID') - br2 = net.add(MaxPooling2D('%s/Branch_2/MaxPool_1a_3x3' % name, 3, 2, border_mode='VALID'), split) - return net.add(Concat('%s/Concat' % name, 1), [br0, br1, br2]) - - -def block_inception_c(name, net): + s = net.add(Split('%s/Split' % blk , 3)) + br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, 192, 1, src=s) + br0 = conv2d(net, '%s/Branch_0/Conv2d_1a_3x3' % blk, 192, 3, 2, + border_mode='VALID') + br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, 256, 1, src=s) + br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % blk, 256, (1, 7)) + br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % blk, 320, (7, 1)) + br1 = conv2d(net, '%s/Branch_1/Conv2d_1a_3x3' % blk, 320, 3, 2, + border_mode='VALID') + br2 = net.add(MaxPooling2D('%s/Branch_2/MaxPool_1a_3x3' % blk, 3, 2, + border_mode='VALID'), s) + return net.add(Concat('%s/Concat' % blk, 1), [br0, br1, br2]) + + +def block_inception_c(blk, net): """Builds Inception-C block for Inception v4 network.""" # By default use stride=1 and SAME padding - split = net.add(Split('%s/Split' % name, 4)) - br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % name, 256, 1, src=split) - conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % name, 384, 1, src=split) - br1_split = net.add(Split('%s/Branch_1/Split' % name, 2)) - br1_0 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x3' % name, 256, (1, 3), src=br1_split) - br1_1 = conv2d(net, '%s/Branch_1/Conv2d_0c_3x1' % name, 256, (3, 1), src=br1_split) - br1 = net.add(Concat('%s/Branch_1/Concat' % name, 1), [br1_0, br1_1]) - conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % name, 384, 1, src=split) - conv2d(net, '%s/Branch_2/Conv2d_0b_3x1' % name, 448, (3, 1)) - conv2d(net, '%s/Branch_2/Conv2d_0c_1x3' % name, 512, (1, 3)) - br2_split = net.add(Split('%s/Branch_2/Split' % name, 2)) - br2_0 = conv2d(net, '%s/Branch_2/Conv2d_0d_1x3' % name, 256, (1, 3), src=br2_split) - br2_1 = conv2d(net, '%s/Branch_2/Conv2d_0e_3x1' % name, 256, (3, 1), src=br2_split) - br2 = net.add(Concat('%s/Branch_2/Concat' % name, 1), [br2_0, br2_1]) - net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % name, 3, 1), split) - br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % name, 256, 1) - return net.add(Concat('%s/Concat' % name, 1), [br0, br1, br2, br3]) - - -def inception_v4_base(name, sample_shape, final_endpoint='Mixed_7d', aux_name=None): + s = net.add(Split('%s/Split' % blk, 4)) + br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, 256, 1, src=s) + + br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, 384, 1, src=s) + br1 = net.add(Split('%s/Branch_1/Split' % blk, 2)) + br10 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x3' % blk, 256, (1, 3), src=br1) + br11 = conv2d(net, '%s/Branch_1/Conv2d_0c_3x1' % blk, 256, (3, 1), src=br1) + br1 = net.add(Concat('%s/Branch_1/Concat' % blk, 1), [br10, br11]) + + br2 =conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % blk, 384, 1, src=s) + br2 =conv2d(net, '%s/Branch_2/Conv2d_0b_3x1' % blk, 448, (3, 1)) + br2 =conv2d(net, '%s/Branch_2/Conv2d_0c_1x3' % blk, 512, (1, 3)) + br2 = net.add(Split('%s/Branch_2/Split' % blk, 2)) + br20 = conv2d(net, '%s/Branch_2/Conv2d_0d_1x3' % blk, 256, (1, 3), src=br2) + br21 = conv2d(net, '%s/Branch_2/Conv2d_0e_3x1' % blk, 256, (3, 1), src=br2) + br2 = net.add(Concat('%s/Branch_2/Concat' % blk, 1), [br20, br21]) + + br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % blk, 3, 1), s) + br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % blk, 256, 1) + return net.add(Concat('%s/Concat' % blk, 1), [br0, br1, br2, br3]) + + +def inception_v4_base(name, sample_shape, final_endpoint='Inception/Mixed_7d', + aux_endpoint='Inception/Mixed_6e'): """Creates the Inception V4 network up to the given final endpoint. - Args: - inputs: a 4-D tensor of size [batch_size, height, width, 3]. - final_endpoint: specifies the endpoint to construct the network up to. - It can be one of [ 'Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', + Endpoint name list: 'InceptionV4/' + + ['Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'Mixed_3a', 'Mixed_4a', 'Mixed_5a', 'Mixed_5b', 'Mixed_5c', 'Mixed_5d', 'Mixed_5e', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c', 'Mixed_6d', 'Mixed_6e', 'Mixed_6f', 'Mixed_6g', 'Mixed_6h', 'Mixed_7a', 'Mixed_7b', 'Mixed_7c', 'Mixed_7d'] - Returns: - logits: the logits outputs of the model. - end_points: the set of end_points from the inception model. + Args: + inputs: a 4-D tensor of size [batch_size, height, width, 3]. + final_endpoint: specifies the endpoint to construct the network up to. + aux_endpoint: for aux loss. - Raises: - ValueError: if final_endpoint is not set to one of the predefined values, + Returns: + the neural net + the last layer + the set of end_points from the inception model. """ end_points = {} net = ffnet.FeedForwardNet() - def add_and_check_final(name, lyr): - end_points[name] = lyr - return name == final_endpoint + def final_aux_check(block_name): + if block_name == final_endpoint: + return True + if block_name == aux_endpoint: + aux = aux_endpoint + '-aux' + end_points[aux] = net.add(Split(aux, 2)) + return False + # 299 x 299 x 3 - net.add(Conv2D('%s/Conv2d_1a_3x3' % name, 32, 3, 2, border_mode='VALID', use_bias=False, input_sample_shape=sample_shape)) - net.add(BatchNormalization('%s/Conv2d_1a_3x3/BatchNorm' % name)) - net.add(Activation('%s/Conv2d_1a_3x3/relu' % name)) + blk = name + 'Conv2d_1a_3x3' + net.add(Conv2D(blk, 32, 3, 2, border_mode='VALID', use_bias=False, + input_sample_shape=sample_shape)) + net.add(BatchNormalization('%s/BatchNorm' % blk)) + end_points[blk] = net.add(Activation('%s/relu' % blk)) + if final_aux_check(blk): + return net, end_points + # 149 x 149 x 32 - conv2d(net, '%s/Conv2d_2a_3x3' % name, 32, 3, padding='VALID') + blk = name + 'Conv2d_2a_3x3' + end_points[blk] = conv2d(net, '%s/Conv2d_2a_3x3' % blk, 32, 3, + border_mode='VALID') + if final_aux_check(blk): + return net, end_points + # 147 x 147 x 32 - conv2d(net, '%s/Conv2d_2b_3x3' % name, 64, 3) + blk = name + 'Conv2d_2b_3x3' + end_points[blk] = conv2d(net, '%s/Conv2d_2b_3x3' % blk, 64, 3) + if final_aux_check(blk): + return net, end_points + # 147 x 147 x 64 - s = net.add(Split('%s/Mixed_3a/Split' % name, 2)) - br0 = net.add(MaxPooling2D('%s/Mixed_3a/Branch_0/MaxPool_0a_3x3' % name, 3, 2, border_mode='VALID'), s) - br1 = conv2d(net, '%s/Mixed_3a/Branch_1/Conv2d_0a_3x3' % name, 96, 3, 2, padding='VALID', src=s) - net.add(Concat('%s/Mixed_3a/Concat' % name, 1), [br0, br1]) + blk = name + '/Mixed_3a' + s = net.add(Split('%s/Split' % blk, 2)) + br0 = net.add(MaxPooling2D('%s/Branch_0/MaxPool_0a_3x3' % blk, 3, + 2, border_mode='VALID'), s) + br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_3x3' % blk, 96, 3, 2, + border_mode='VALID', src=s) + end_points[blk] = net.add(Concat('%s/Concat' % blk, 1), [br0, br1]) + if final_aux_check(blk): + return net, end_points # 73 x 73 x 160 - s = net.add(Split('%s/Mixed_4a/Split' % name, 2)) - conv2d(net, '%s/Mixed_4a/Branch_0/Conv2d_0a_1x1' % name, 64, 1, src=s) - br0 = conv2d(net, '%s/Mixed_4a/Branch_0/Conv2d_1a_3x3' % name, 96, 3, padding='VALID') - conv2d(net, '%s/Mixed_4a/Branch_1/Conv2d_0a_1x1' % name, 64, 1, src=s) - conv2d(net, '%s/Mixed_4a/Branch_1/Conv2d_0b_1x7' % name, 64, (1, 7)) - conv2d(net, '%s/Mixed_4a/Branch_1/Conv2d_0c_7x1' % name, 64, (7, 1)) - br1 = conv2d(net, '%s/Mixed_4a/Branch_1/Conv2d_1a_3x3' % name, 96, 3, padding='VALID') - net.add(Concat('%s/Mixed_4a/Concat' % name, 1), [br0, br1]) - - # 71 x 71 x 192 - s = net.add(Split('%s/Mixed_5a/Split' % name, 2)) - br0 = conv2d(net, '%s/Mixed_5a/Branch_0/Conv2d_1a_3x3' % name, 192, 3, 2, padding='VALID', src=s) - br1 = net.add(MaxPooling2D('%s/Mixed_5a/Branch_1/MaxPool_1a_3x3' % name, 3, 2, border_mode='VALID'), s) - net.add(Concat('%s/Mixed_5a/Concat' % name, 1), [br0, br1]) + blk = name + '/Mixed_4a' + s = net.add(Split('%s/Split' % blk, 2)) + br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, 64, 1, src=s) + br0 = conv2d(net, '%s/Branch_0/Conv2d_1a_3x3' % blk, 96, 3, + border_mode='VALID') + br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, 64, 1, src=s) + br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % blk, 64, (1, 7)) + br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % blk, 64, (7, 1)) + br1 = conv2d(net, '%s/Branch_1/Conv2d_1a_3x3' % blk, 96, 3, + border_mode='VALID') + end_points[blk] = net.add(Concat('%s/Concat' % blk, 1), [br0, br1]) + if final_aux_check(blk): + return net, end_points + + # 71 x 71 x 192 + blk = name + '/Mixed_5a' + s = net.add(Split('%s/Split' % blk, 2)) + br0 = conv2d(net, '%s/Branch_0/Conv2d_1a_3x3' % blk, 192, 3, 2, + border_mode='VALID', src=s) + br1 = net.add(MaxPooling2D('%s/Branch_1/MaxPool_1a_3x3' % blk, 3, + 2, border_mode='VALID'), s) + end_points[blk] = net.add(Concat('%s/Concat' % blk, 1), [br0, br1]) + if final_aux_check(blk): + return net, end_points # 35 x 35 x 384 # 4 x Inception-A blocks for idx in range(4): - block_scope = name + '/Mixed_5' + chr(ord('b') + idx) - lyr = block_inception_a(block_scope, net) - if add_and_check_final(block_scope, lyr): return net, lyr, end_points + blk = name + '/Mixed_5' + chr(ord('b') + idx) + end_points[blk] = block_inception_a(blk, net) + if final_aux_check(blk): + return net, end_points # 35 x 35 x 384 # Reduction-A block - block_reduction_a(name + '/Mixed_6a', net) + blk = name + '/Mixed_6a' + end_points[blk] = block_reduction_a(blk, net) + if final_aux_check(blk): + return net, end_points[blk], end_points # 17 x 17 x 1024 # 7 x Inception-B blocks for idx in range(7): - block_scope = name + '/Mixed_6' + chr(ord('b') + idx) - lyr = block_inception_b(block_scope, net) - if add_and_check_final(block_scope, lyr): return net, lyr, end_points - if block_scope == aux_name: - end_points[aux_name] = net.add(Split('%s/Split' % block_scope, 2)) + blk = name + '/Mixed_6' + chr(ord('b') + idx) + end_points[blk] = block_inception_b(blk, net) + if final_aux_check(blk): + return net, end_points # 17 x 17 x 1024 # Reduction-B block - block_reduction_b(name + '/Mixed_7a', net) + blk = name + '/Mixed_7a' + end_points[blk] = block_reduction_b(blk, net) + if final_aux_check(blk): + return net, end_points # 8 x 8 x 1536 # 3 x Inception-C blocks for idx in range(3): - block_scope = name + '/Mixed_7' + chr(ord('b') + idx) - lyr = block_inception_c(block_scope, net) - if add_and_check_final(block_scope, lyr): return net, lyr, end_points - if block_scope == aux_name: - end_points[aux_name] = net.add(Split('%s/Split' % block_scope, 2)) - return net, lyr, end_points + blk = name + '/Mixed_7' + chr(ord('b') + idx) + end_points[blk] = block_inception_c(blk, net) + if final_aux_check(blk): + return net, end_points + + return net, end_points -def create_net(num_classes=1001, sample_shape=(3, 299, 299), is_training=True, dropout_keep_prob=0.8, create_aux_logits=True): +def create_net(num_classes=1001, sample_shape=(3, 299, 299), is_training=True, + dropout_keep_prob=0.8, final_endpoint='InceptionV4/Mixed_7d', + aux_endpoint='InceptionV4/Mixed_6e'): """Creates the Inception V4 model. Args: num_classes: number of predicted classes. is_training: whether is training or not. dropout_keep_prob: float, the fraction to keep before final layer. - reuse: whether or not the network and its variables should be reused. To be - able to reuse 'scope' must be given. - create_aux_logits: Whether to include the auxiliary logits. + final_endpoint, aux_endpoint: refer to inception_v4_base() Returns: logits: the logits outputs of the model. @@ -233,29 +285,33 @@ def create_net(num_classes=1001, sample_shape=(3, 299, 299), is_training=True, d """ end_points = {} name = 'InceptionV4' - if is_training and create_aux_logits: - aux_name = name + '/Mixed_6h' - else: - aux_name = None - net, last_layer, end_points = inception_v4_base(name, sample_shape, aux_name=aux_name) + net, end_points = inception_v4_base(name, sample_shape, + final_endpoint=final_endpoint, aux_endpoint=aux_endpoint) # Auxiliary Head logits - if aux_name is not None: + if aux_endpoint is not None: # 17 x 17 x 1024 - aux_logits = end_points[aux_name] - net.add(AvgPooling2D('%s/AuxLogits/AvgPool_1a_5x5' % name, 5, stride=3, border_mode='VALID'), aux_logits) - t = conv2d(net, '%s/AuxLogits/Conv2d_1b_1x1' % name, 128, 1) - conv2d(net, '%s/AuxLogits/Conv2d_2a' % name, 768, t.get_output_sample_shape()[1:3], padding='VALID') - net.add(Flatten('%s/AuxLogits/flat' % name)) - end_points['AuxLogits'] = net.add(Dense('%s/AuxLogits/Aux_logits' % name, num_classes)) + aux_logits = end_points[aux_endpoint + '-aux'] + blk = 'AuxLogits' + net.add(AvgPooling2D('%s/AvgPool_1a_5x5' % blk, 5, stride=3, + border_mode='VALID'), aux_logits) + t = conv2d(net, '%s/Conv2d_1b_1x1' % blk, 128, 1) + conv2d(net, '%s/Conv2d_2a' % blk, 768, + t.get_output_sample_shape()[1:3], border_mode='VALID') + net.add(Flatten('%s/flat' % blk)) + end_points[blk] = net.add(Dense('%s/Aux_logits' % blk, num_classes)) # Final pooling and prediction # 8 x 8 x 1536 - net.add(AvgPooling2D('%s/Logits/AvgPool_1a' % name, last_layer.get_output_sample_shape()[1:3], border_mode='VALID'), last_layer) + blk = 'Logits' + last_layer = end_points[final_endpoint] + net.add(AvgPooling2D('%s/AvgPool_1a' % blk, + last_layer.get_output_sample_shape()[1:3], border_mode='VALID'), + last_layer) # 1 x 1 x 1536 - net.add(Dropout('%s/Logits/Dropout_1b' % name, 1 - dropout_keep_prob)) - net.add(Flatten('%s/Logits/PreLogitsFlatten' % name)) + net.add(Dropout('%s/Dropout_1b' % blk, 1 - dropout_keep_prob)) + net.add(Flatten('%s/PreLogitsFlatten' % blk)) # 1536 - end_points['Logits'] = net.add(Dense('%s/Logits/Logits' % name, num_classes)) + end_points[blk] = net.add(Dense('%s/Logits' % blk, num_classes)) return net, end_points http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/40124db7/tool/conda/meta.yaml ---------------------------------------------------------------------- diff --git a/tool/conda/meta.yaml b/tool/conda/meta.yaml index ee3aa7a..f0b6dd0 100644 --- a/tool/conda/meta.yaml +++ b/tool/conda/meta.yaml @@ -15,26 +15,26 @@ build: requirements: build: - - python 2.7* - - numpy 1.12.0 - swig 3.0.2 - openblas 0.2.18 - - protobuf 3.0.0 + - protobuf 3.2.0 - glog 0.3.4 - libgfortran 3.0.0 # [osx] - gcc 4.8.5 # [linux] + - python 2.7* + - numpy 1.12.0 run: + - openblas 0.2.18 + - protobuf 3.2.0 + - glog 0.3.4 + - libgfortran 3.0.0 # [osx] + - libgcc 4.8.5 # [linux] - python 2.7* - numpy >=1.12.0 - - protobuf >=3.0.0 - - glog >=0.3.4 - - openblas >=0.2.18 - flask >=0.10.1 - flask-cors >=3.0.2 - pillow >=2.3.0 - - libgfortran >=3.0.0 # [osx] - - libgcc 4.8.5 # [linux] test: source_files:
