You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@singa.apache.org by wa...@apache.org on 2017/07/13 07:01:07 UTC
[1/6] incubator-singa git commit: SINGA-326 - Add Inception V4 for
ImageNet classification
Repository: incubator-singa
Updated Branches:
refs/heads/master 63c6ae17c -> a39ed5ce5
SINGA-326 - Add Inception V4 for ImageNet classification
adding inceptionv4 following https://github.com/tensorflow/models/blob/master/slim/nets/inception_v4.py
convert params from tensorflow to pickle dictionary
updated the padding configuration following Tensorflow for 'SAME' and 'VALID'
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/2cdc1725
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/2cdc1725
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/2cdc1725
Branch: refs/heads/master
Commit: 2cdc1725d7e8b1ec075994e46076f7a7a87e5795
Parents: 334c27d
Author: wangwei <wa...@comp.nus.edu.sg>
Authored: Tue Jun 27 17:28:37 2017 +0800
Committer: wangwei <wa...@comp.nus.edu.sg>
Committed: Thu Jun 29 15:27:34 2017 +0800
----------------------------------------------------------------------
examples/imagenet/googlenet/serve.py | 4 +-
examples/imagenet/inceptionv4/README.md | 43 +++++
examples/imagenet/inceptionv4/convert.py | 117 ++++++++++++
examples/imagenet/inceptionv4/model.py | 263 ++++++++++++++++++++++++++
examples/imagenet/inceptionv4/serve.py | 121 ++++++++++++
python/singa/image_tool.py | 8 +-
python/singa/layer.py | 71 ++++++-
7 files changed, 611 insertions(+), 16 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/2cdc1725/examples/imagenet/googlenet/serve.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/googlenet/serve.py b/examples/imagenet/googlenet/serve.py
index 57e005d..aee890d 100644
--- a/examples/imagenet/googlenet/serve.py
+++ b/examples/imagenet/googlenet/serve.py
@@ -139,7 +139,7 @@ def create_net(shape, weight_path='bvlc_googlenet.pickle'):
# prob=net.add(Softmax('softmax'))
net.load(weight_path, use_pickle=True)
- print 'total num of params %d' % (len(net.param_names()))
+ print('total num of params %d' % (len(net.param_names())))
# SINGA and Caffe have different layout for the weight matrix of the dense
# layer
for key, val in zip(net.param_names(), net.param_values()):
@@ -153,7 +153,7 @@ def create_net(shape, weight_path='bvlc_googlenet.pickle'):
def serve(agent, use_cpu, parameter_file, topk=5):
if use_cpu:
- print 'running with cpu'
+ print('running with cpu')
dev = device.get_default_device()
layer.engine = 'singacpp'
else:
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/2cdc1725/examples/imagenet/inceptionv4/README.md
----------------------------------------------------------------------
diff --git a/examples/imagenet/inceptionv4/README.md b/examples/imagenet/inceptionv4/README.md
new file mode 100644
index 0000000..f129edc
--- /dev/null
+++ b/examples/imagenet/inceptionv4/README.md
@@ -0,0 +1,43 @@
+---
+name: Inception V4 on ImageNet
+SINGA version: 1.1.1
+SINGA commit:
+parameter_url: https://s3-ap-southeast-1.amazonaws.com/dlfile/inception_v4.tar.gz
+parameter_sha1: 5fdd6f5d8af8fd10e7321d9b38bb87ef14e80d56
+license: https://github.com/tensorflow/models/tree/master/slim
+---
+
+# Image Classification using Inception V4
+
+In this example, we convert Inception V4 trained on Tensorflow to SINGA for image classification.
+
+## Instructions
+
+* Download the parameter checkpoint file
+
+ $ wget
+ $ tar xvf inception_v4.tar.gz
+
+* Download [synset_word.txt](https://github.com/BVLC/caffe/blob/master/data/ilsvrc12/get_ilsvrc_aux.sh) file.
+
+* Run the program
+
+ # use cpu
+ $ python serve.py -C &
+ # use gpu
+ $ python serve.py &
+
+* Submit images for classification
+
+ $ curl -i -F image=@image1.jpg http://localhost:9999/api
+ $ curl -i -F image=@image2.jpg http://localhost:9999/api
+ $ curl -i -F image=@image3.jpg http://localhost:9999/api
+
+image1.jpg, image2.jpg and image3.jpg should be downloaded before executing the above commands.
+
+## Details
+
+We first extract the parameter values from [Tensorflow's checkpoint file](http://download.tensorflow.org/models/inception_v4_2016_09_09.tar.gz) into a pickle version.
+After downloading and decompressing the checkpoint file, run the following script
+
+ $ python convert.py --file_name=inception_v4.ckpt
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/2cdc1725/examples/imagenet/inceptionv4/convert.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/inceptionv4/convert.py b/examples/imagenet/inceptionv4/convert.py
new file mode 100644
index 0000000..e3f5adc
--- /dev/null
+++ b/examples/imagenet/inceptionv4/convert.py
@@ -0,0 +1,117 @@
+# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ==============================================================================
+"""A simple script for inspect checkpoint files."""
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+import argparse
+import sys
+import cPickle as pickle
+import os
+
+import numpy as np
+from tensorflow.python import pywrap_tensorflow
+from tensorflow.python.platform import app
+import model
+
+
+FLAGS = None
+
+
+def rename(name, suffix):
+ p = name.rfind('/')
+ if p == -1:
+ print('Bad name=%s' % name)
+ return name[0:p+1] + suffix
+
+
+def convert(file_name):
+ net, _ = model.create_net()
+ params = {'SINGA_VERSION': 1101}
+ try:
+ reader = pywrap_tensorflow.NewCheckpointReader(file_name)
+ for pname, pval in zip(net.param_names(), net.param_values()):
+ if 'weight' in pname:
+ val = reader.get_tensor(rename(pname, 'weights'))
+ if 'Conv' in pname:
+ val = val.transpose((3, 2, 0, 1))
+ val = val.reshape((val.shape[0], -1))
+ elif 'bias' in pname:
+ val = reader.get_tensor(rename(pname, 'biases'))
+ elif 'mean' in pname:
+ val = reader.get_tensor(rename(pname, 'moving_mean'))
+ elif 'var' in pname:
+ val = reader.get_tensor(rename(pname, 'moving_variance'))
+ elif 'beta' in pname:
+ val= reader.get_tensor(pname)
+ elif 'gamma' in pname:
+ 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)
+ params[pname] = val.astype(np.float32)
+ print('converting:', pname, pval.shape)
+ var_to_shape_map = reader.get_variable_to_shape_map()
+ for key in var_to_shape_map:
+ if 'weights' in key:
+ key = rename(key, 'weight')
+ elif 'biases' in key:
+ key = rename(key, 'bias')
+ elif 'moving_mean' in key:
+ key = rename(key, 'mean')
+ elif 'moving_variance' in key:
+ key = rename(key, 'var')
+ if key not in params:
+ print('key=%s not in the net' % key)
+ '''
+ for key in var_to_shape_map:
+ print("tensor_name: ", key, var_to_shape_map[key])
+ '''
+ with open(os.path.splitext(file_name)[0] + '.pickle', 'wb') as fd:
+ pickle.dump(params, fd)
+ except Exception as e: # pylint: disable=broad-except
+ print(str(e))
+ if "corrupted compressed block contents" in str(e):
+ print("It's likely that your checkpoint file has been compressed "
+ "with SNAPPY.")
+ if ("Data loss" in str(e) and
+ (any([e in file_name for e in [".index", ".meta", ".data"]]))):
+ proposed_file = ".".join(file_name.split(".")[0:-1])
+ v2_file_error_template = """
+ It's likely that this is a V2 checkpoint and you need to provide the filename
+ *prefix*. Try removing the '.' and extension. Try:
+ inspect checkpoint --file_name = {}"""
+ print(v2_file_error_template.format(proposed_file))
+
+
+
+def main(unused_argv):
+ if not FLAGS.file_name:
+ print("Usage: convert.py --file_name=checkpoint_file_name ")
+ sys.exit(1)
+ else:
+ convert(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.")
+ 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/2cdc1725/examples/imagenet/inceptionv4/model.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/inceptionv4/model.py b/examples/imagenet/inceptionv4/model.py
new file mode 100644
index 0000000..baab522
--- /dev/null
+++ b/examples/imagenet/inceptionv4/model.py
@@ -0,0 +1,263 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =============================================================================
+
+
+"""
+http://arxiv.org/abs/1602.07261.
+
+ Inception-v4, Inception-ResNet and the Impact of Residual Connections
+ on Learning
+ Christian Szegedy, Sergey Ioffe, Vincent Vanhoucke, Alex Alemi
+
+Refer to
+https://github.com/tensorflow/models/blob/master/slim/nets/inception_v4.py
+"""
+
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+from singa.layer import Conv2D, Activation, MaxPooling2D, AvgPooling2D,\
+ Split, Concat, Dropout, Flatten, Dense, BatchNormalization
+
+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)
+ net.add(BatchNormalization('%s/BatchNorm' % name))
+ return net.add(Activation(name+'/relu'))
+
+
+def block_inception_a(name, 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):
+ """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):
+ """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):
+ """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):
+ """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):
+ """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',
+ '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.
+
+ Raises:
+ ValueError: if final_endpoint is not set to one of the predefined values,
+ """
+ end_points = {}
+ net = ffnet.FeedForwardNet()
+ def add_and_check_final(name, lyr):
+ end_points[name] = lyr
+ return name == final_endpoint
+
+ # 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))
+ # 149 x 149 x 32
+ conv2d(net, '%s/Conv2d_2a_3x3' % name, 32, 3, padding='VALID')
+ # 147 x 147 x 32
+ conv2d(net, '%s/Conv2d_2b_3x3' % name, 64, 3)
+ # 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])
+
+ # 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])
+
+ # 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
+
+ # 35 x 35 x 384
+ # Reduction-A block
+ block_reduction_a(name + '/Mixed_6a', net)
+
+ # 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))
+
+ # 17 x 17 x 1024
+ # Reduction-B block
+ block_reduction_b(name + '/Mixed_7a', net)
+
+ # 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
+
+
+def create_net(num_classes=1001, sample_shape=(3, 299, 299), is_training=True, dropout_keep_prob=0.8, create_aux_logits=True):
+ """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.
+
+ Returns:
+ logits: the logits outputs of the model.
+ end_points: the set of end_points from the inception model.
+ """
+ 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)
+ # Auxiliary Head logits
+ if aux_name 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))
+
+ # 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)
+ # 1 x 1 x 1536
+ net.add(Dropout('%s/Logits/Dropout_1b' % name, 1 - dropout_keep_prob))
+ net.add(Flatten('%s/Logits/PreLogitsFlatten' % name))
+ # 1536
+ end_points['Logits'] = net.add(Dense('%s/Logits/Logits' % name, num_classes))
+ return net, end_points
+
+
+if __name__ == '__main__':
+ net, _ = create_net()
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/2cdc1725/examples/imagenet/inceptionv4/serve.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/inceptionv4/serve.py b/examples/imagenet/inceptionv4/serve.py
new file mode 100644
index 0000000..9ba099a
--- /dev/null
+++ b/examples/imagenet/inceptionv4/serve.py
@@ -0,0 +1,121 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =============================================================================
+import model
+
+from singa import device
+from singa import tensor
+from singa import image_tool
+from singa import layer
+from rafiki.agent import Agent, MsgType
+
+import sys
+import time
+import traceback
+from argparse import ArgumentParser
+import numpy as np
+
+
+def serve(agent, use_cpu, parameter_file, topk=5):
+ if use_cpu:
+ print('running with cpu')
+ dev = device.get_default_device()
+ layer.engine = 'singacpp'
+ else:
+ print("runing with gpu")
+ dev = device.create_cuda_gpu()
+ agent = agent
+
+ print('Start intialization............')
+ net, _ = model.create_net(is_training=False)
+ net.load(parameter_file, use_pickle=True)
+ net.to_device(dev)
+ print('End intialization............')
+
+ labels = np.loadtxt('synset_words.txt', str, delimiter='\t').tolist()
+ labels.insert(0, 'empty background')
+ while True:
+ key, val = agent.pull()
+ if key is None:
+ time.sleep(0.1)
+ continue
+ msg_type = MsgType.parse(key)
+ if msg_type.is_request():
+ try:
+ response = ""
+ ratio = 0.875
+ img = image_tool.load_img(val['image'])
+ height, width = img.size[0], img.size[1]
+ print(img.size)
+ crop_h, crop_w = int(height * ratio), int(width * ratio)
+ img = np.array(image_tool.crop(img, (crop_h, crop_w), 'center').resize((299, 299))).astype(np.float32) / float(255)
+ img -= 0.5
+ img *= 2
+ # img[:,:,[0,1,2]] = img[:,:,[2,1,0]]
+ img = img.transpose((2, 0, 1))
+ images = np.expand_dims(img, axis=0)
+ x = tensor.from_numpy(images.astype(np.float32))
+ x.to_device(dev)
+ y = net.predict(x)
+ prob = np.average(tensor.to_numpy(y), 0)
+ # sort and reverse
+ idx = np.argsort(-prob)[0:topk]
+ for i in idx:
+ response += "%s:%s<br/>" % (labels[i], prob[i])
+ except:
+ traceback.print_exc()
+ response = "Sorry, system error during prediction."
+ agent.push(MsgType.kResponse, response)
+ elif MsgType.kCommandStop.equal(msg_type):
+ print('get stop command')
+ agent.push(MsgType.kStatus, "success")
+ break
+ else:
+ print('get unsupported message %s' % str(msg_type))
+ agent.push(MsgType.kStatus, "Unknown command")
+ break
+ # while loop
+ print("server stop")
+
+
+def main():
+ try:
+ # Setup argument parser
+ parser = ArgumentParser(description="InceptionV4 for image classification")
+ parser.add_argument("-p", "--port", default=9999, help="listen port")
+ parser.add_argument("-C", "--use_cpu", action="store_true")
+ parser.add_argument("--parameter_file", default="inception_v4.pickle",
+ help="relative path")
+
+ # Process arguments
+ args = parser.parse_args()
+ port = args.port
+
+ # start to train
+ agent = Agent(port)
+ serve(agent, args.use_cpu, args.parameter_file)
+ agent.stop()
+
+ except SystemExit:
+ return
+ except:
+ traceback.print_exc()
+ sys.stderr.write(" for help use --help \n\n")
+ return 2
+
+
+if __name__ == '__main__':
+ main()
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/2cdc1725/python/singa/image_tool.py
----------------------------------------------------------------------
diff --git a/python/singa/image_tool.py b/python/singa/image_tool.py
index bbdd32e..7c9caeb 100644
--- a/python/singa/image_tool.py
+++ b/python/singa/image_tool.py
@@ -53,11 +53,11 @@ def crop(img, patch, position):
and center.
'''
if img.size[0] < patch[0]:
- raise Exception(
- 'img size[0] %d is smaller than patch[0]: %d' % (img[0], patch[0]))
+ raise Exception('img size[0] %d is smaller than patch[0]: %d'
+ % (img.size[0], patch[0]))
if img.size[1] < patch[1]:
- raise Exception(
- 'img size[1] %d is smaller than patch[1]: %d' % (img[1], patch[1]))
+ raise Exception('img size[1] %d is smaller than patch[1]: %d'
+ % (img.size[1], patch[1]))
if position == 'left_top':
left, upper = 0, 0
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/2cdc1725/python/singa/layer.py
----------------------------------------------------------------------
diff --git a/python/singa/layer.py b/python/singa/layer.py
index 4fe9983..026a766 100644
--- a/python/singa/layer.py
+++ b/python/singa/layer.py
@@ -340,7 +340,10 @@ class Conv2D(Layer):
conf.num_output = nb_kernels
conf.prefer = cudnn_prefer
conf.workspace_byte_limit = workspace_byte_limit
- conf = _set_kernel_stride_pad(conf, kernel, stride, border_mode, pad)
+ self.kernel = kernel
+ self.stride = stride
+ self.pad = pad
+ self.border_mode = border_mode
conf.bias_term = use_bias
# TODO(wangwei) enable data format for cpp code
# conf.data_format = data_format
@@ -365,6 +368,21 @@ class Conv2D(Layer):
if input_sample_shape is not None:
self.setup(input_sample_shape)
+ def setup(self, in_shape):
+ '''Set up the kernel, stride and padding; then call the C++ setup
+ function to create params and set some meta data.
+
+ Args:
+ in_shapes is a tuple of int for the input sample shape
+ '''
+ if self.has_setup:
+ return
+ _set_kernel_stride_pad(self.conf.convolution_conf, self.kernel,
+ self.stride, self.border_mode, self.pad,
+ in_shape)
+ self.layer.Setup(list(in_shape), self.conf.SerializeToString())
+ self.has_setup = True
+
class Conv1D(Conv2D):
"""Construct a layer for 1D convolution.
@@ -417,13 +435,30 @@ class Pooling2D(Layer):
assert data_format == 'NCHW', 'Not supported data format: %s ' \
'only "NCHW" is enabled currently' % (data_format)
conf = self.conf.pooling_conf
- conf = _set_kernel_stride_pad(conf, kernel, stride, border_mode, pad)
conf.pool = mode
+ self.kernel = kernel
+ self.stride = stride
+ self.pad = pad
+ self.border_mode = border_mode
_check_engine(engine, ['cudnn', 'singacpp', 'singacl'])
self.layer = _create_layer(engine, 'Pooling')
if input_sample_shape is not None:
self.setup(input_sample_shape)
+ def setup(self, in_shape):
+ '''Set up the kernel, stride and padding; then call the C++ setup
+ function to create params and set some meta data.
+
+ Args:
+ in_shapes is a tuple of int for the input sample shape
+ '''
+ if self.has_setup:
+ return
+ _set_kernel_stride_pad(self.conf.pooling_conf, self.kernel, self.stride,
+ self.border_mode, self.pad, in_shape)
+ self.layer.Setup(list(in_shape), self.conf.SerializeToString())
+ self.has_setup = True
+
class MaxPooling2D(Pooling2D):
@@ -1144,8 +1179,20 @@ def _create_layer(eng, layer):
return singa_wrap.CreateLayer(layer_type.lower())
-def _set_kernel_stride_pad(conf, kernel, stride, border_mode, pad):
- """Private function called by Convolution2D and Pooling2D."""
+def _set_kernel_stride_pad(conf, kernel, stride, border_mode, pad, in_shape):
+ """Private function called by Convolution2D and Pooling2D.
+
+ PyTorch:
+ http://pytorch.org/docs/nn.html#pooling-layers
+ floor for both conv and pooling
+ Caffe:
+ https://github.com/BVLC/caffe/issues/1318#issuecomment-59594323
+ floor for conv and ceil for pooling
+ Tensorflow: https://www.tensorflow.org/api_guides/python/nn#Convolution
+ SAME outsize = ceil(insize/stride),
+ pad_h_w = max((outsize-1)*stride+k-insize, 0)
+ VALID same as pytorch
+ """
if isinstance(kernel, tuple):
conf.kernel_h = kernel[0]
conf.kernel_w = kernel[1]
@@ -1162,16 +1209,20 @@ def _set_kernel_stride_pad(conf, kernel, stride, border_mode, pad):
if pad is None:
# TODO(wangwei) check the border mode
if mode == 'same':
- assert conf.kernel_h % 2 == 1 and conf.kernel_w % 2 == 1, \
- 'Must use odd kernel for mode="same", kernel is (%d, %d)' % (
- conf.kernel_h, conf.kernel_w)
- pad = (conf.kernel_h / 2, conf.kernel_w / 2)
+ out_h = in_shape[1] / conf.stride_h
+ out_w = in_shape[2] / conf.stride_w
+ ph = max((out_h - 1) * conf.stride_h + conf.kernel_h - in_shape[1],
+ 0)
+ pw = max((out_w - 1) * conf.stride_w + conf.kernel_w - in_shape[2],
+ 0)
+ assert ph % 2 == 0 and pw % 2 == 0, 'ph=%d and pw=%d are not even' \
+ % (ph, pw)
+ pad = (ph / 2, pw / 2)
elif mode == 'valid':
pad = (0, 0)
else:
assert False, ('Unsupported border_mode: %s. '
- 'Please use {"valid", "same"}' % border_mode)
- assert isinstance(pad, tuple), 'pad should be a tuple'
+ 'Please use {"VALID", "SAME"}' % border_mode)
if isinstance(pad, tuple):
conf.pad_h = pad[0]
conf.pad_w = pad[1]
[4/6] incubator-singa git commit: 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.
Posted by wa...@apache.org.
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 <wa...@comp.nus.edu.sg>
Authored: Fri Jul 7 12:26:20 2017 +0800
Committer: wang wei <wa...@comp.nus.edu.sg>
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:
[2/6] incubator-singa git commit: add inception v3
Posted by wa...@apache.org.
add inception v3
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/fc4d1ccc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/fc4d1ccc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/fc4d1ccc
Branch: refs/heads/master
Commit: fc4d1ccc8033d70dd813bddb5c041d61d3ee388a
Parents: 2cdc172
Author: wang wei <wa...@comp.nus.edu.sg>
Authored: Thu Jul 6 23:23:08 2017 +0800
Committer: wang wei <wa...@comp.nus.edu.sg>
Committed: Thu Jul 6 23:23:08 2017 +0800
----------------------------------------------------------------------
examples/cifar10/vgg.py | 1 +
examples/imagenet/inception/README.md | 43 +++++
examples/imagenet/inception/convert.py | 117 ++++++++++++
examples/imagenet/inception/model.py | 263 ++++++++++++++++++++++++++
examples/imagenet/inception/serve.py | 121 ++++++++++++
examples/imagenet/inceptionv4/README.md | 43 -----
examples/imagenet/inceptionv4/convert.py | 117 ------------
examples/imagenet/inceptionv4/model.py | 263 --------------------------
examples/imagenet/inceptionv4/serve.py | 121 ------------
9 files changed, 545 insertions(+), 544 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/fc4d1ccc/examples/cifar10/vgg.py
----------------------------------------------------------------------
diff --git a/examples/cifar10/vgg.py b/examples/cifar10/vgg.py
index 89c6fe8..ce0c210 100644
--- a/examples/cifar10/vgg.py
+++ b/examples/cifar10/vgg.py
@@ -28,6 +28,7 @@ from singa import metric
from singa import loss
from singa import net as ffnet
+ffnet.verbose=True
def ConvBnReLU(net, name, nb_filers, sample_shape=None):
net.add(layer.Conv2D(name + '_1', nb_filers, 3, 1, pad=1,
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/fc4d1ccc/examples/imagenet/inception/README.md
----------------------------------------------------------------------
diff --git a/examples/imagenet/inception/README.md b/examples/imagenet/inception/README.md
new file mode 100644
index 0000000..f129edc
--- /dev/null
+++ b/examples/imagenet/inception/README.md
@@ -0,0 +1,43 @@
+---
+name: Inception V4 on ImageNet
+SINGA version: 1.1.1
+SINGA commit:
+parameter_url: https://s3-ap-southeast-1.amazonaws.com/dlfile/inception_v4.tar.gz
+parameter_sha1: 5fdd6f5d8af8fd10e7321d9b38bb87ef14e80d56
+license: https://github.com/tensorflow/models/tree/master/slim
+---
+
+# Image Classification using Inception V4
+
+In this example, we convert Inception V4 trained on Tensorflow to SINGA for image classification.
+
+## Instructions
+
+* Download the parameter checkpoint file
+
+ $ wget
+ $ tar xvf inception_v4.tar.gz
+
+* Download [synset_word.txt](https://github.com/BVLC/caffe/blob/master/data/ilsvrc12/get_ilsvrc_aux.sh) file.
+
+* Run the program
+
+ # use cpu
+ $ python serve.py -C &
+ # use gpu
+ $ python serve.py &
+
+* Submit images for classification
+
+ $ curl -i -F image=@image1.jpg http://localhost:9999/api
+ $ curl -i -F image=@image2.jpg http://localhost:9999/api
+ $ curl -i -F image=@image3.jpg http://localhost:9999/api
+
+image1.jpg, image2.jpg and image3.jpg should be downloaded before executing the above commands.
+
+## Details
+
+We first extract the parameter values from [Tensorflow's checkpoint file](http://download.tensorflow.org/models/inception_v4_2016_09_09.tar.gz) into a pickle version.
+After downloading and decompressing the checkpoint file, run the following script
+
+ $ python convert.py --file_name=inception_v4.ckpt
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/fc4d1ccc/examples/imagenet/inception/convert.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/inception/convert.py b/examples/imagenet/inception/convert.py
new file mode 100644
index 0000000..e3f5adc
--- /dev/null
+++ b/examples/imagenet/inception/convert.py
@@ -0,0 +1,117 @@
+# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ==============================================================================
+"""A simple script for inspect checkpoint files."""
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+import argparse
+import sys
+import cPickle as pickle
+import os
+
+import numpy as np
+from tensorflow.python import pywrap_tensorflow
+from tensorflow.python.platform import app
+import model
+
+
+FLAGS = None
+
+
+def rename(name, suffix):
+ p = name.rfind('/')
+ if p == -1:
+ print('Bad name=%s' % name)
+ return name[0:p+1] + suffix
+
+
+def convert(file_name):
+ net, _ = model.create_net()
+ params = {'SINGA_VERSION': 1101}
+ try:
+ reader = pywrap_tensorflow.NewCheckpointReader(file_name)
+ for pname, pval in zip(net.param_names(), net.param_values()):
+ if 'weight' in pname:
+ val = reader.get_tensor(rename(pname, 'weights'))
+ if 'Conv' in pname:
+ val = val.transpose((3, 2, 0, 1))
+ val = val.reshape((val.shape[0], -1))
+ elif 'bias' in pname:
+ val = reader.get_tensor(rename(pname, 'biases'))
+ elif 'mean' in pname:
+ val = reader.get_tensor(rename(pname, 'moving_mean'))
+ elif 'var' in pname:
+ val = reader.get_tensor(rename(pname, 'moving_variance'))
+ elif 'beta' in pname:
+ val= reader.get_tensor(pname)
+ elif 'gamma' in pname:
+ 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)
+ params[pname] = val.astype(np.float32)
+ print('converting:', pname, pval.shape)
+ var_to_shape_map = reader.get_variable_to_shape_map()
+ for key in var_to_shape_map:
+ if 'weights' in key:
+ key = rename(key, 'weight')
+ elif 'biases' in key:
+ key = rename(key, 'bias')
+ elif 'moving_mean' in key:
+ key = rename(key, 'mean')
+ elif 'moving_variance' in key:
+ key = rename(key, 'var')
+ if key not in params:
+ print('key=%s not in the net' % key)
+ '''
+ for key in var_to_shape_map:
+ print("tensor_name: ", key, var_to_shape_map[key])
+ '''
+ with open(os.path.splitext(file_name)[0] + '.pickle', 'wb') as fd:
+ pickle.dump(params, fd)
+ except Exception as e: # pylint: disable=broad-except
+ print(str(e))
+ if "corrupted compressed block contents" in str(e):
+ print("It's likely that your checkpoint file has been compressed "
+ "with SNAPPY.")
+ if ("Data loss" in str(e) and
+ (any([e in file_name for e in [".index", ".meta", ".data"]]))):
+ proposed_file = ".".join(file_name.split(".")[0:-1])
+ v2_file_error_template = """
+ It's likely that this is a V2 checkpoint and you need to provide the filename
+ *prefix*. Try removing the '.' and extension. Try:
+ inspect checkpoint --file_name = {}"""
+ print(v2_file_error_template.format(proposed_file))
+
+
+
+def main(unused_argv):
+ if not FLAGS.file_name:
+ print("Usage: convert.py --file_name=checkpoint_file_name ")
+ sys.exit(1)
+ else:
+ convert(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.")
+ 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/fc4d1ccc/examples/imagenet/inception/model.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/inception/model.py b/examples/imagenet/inception/model.py
new file mode 100644
index 0000000..baab522
--- /dev/null
+++ b/examples/imagenet/inception/model.py
@@ -0,0 +1,263 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =============================================================================
+
+
+"""
+http://arxiv.org/abs/1602.07261.
+
+ Inception-v4, Inception-ResNet and the Impact of Residual Connections
+ on Learning
+ Christian Szegedy, Sergey Ioffe, Vincent Vanhoucke, Alex Alemi
+
+Refer to
+https://github.com/tensorflow/models/blob/master/slim/nets/inception_v4.py
+"""
+
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+from singa.layer import Conv2D, Activation, MaxPooling2D, AvgPooling2D,\
+ Split, Concat, Dropout, Flatten, Dense, BatchNormalization
+
+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)
+ net.add(BatchNormalization('%s/BatchNorm' % name))
+ return net.add(Activation(name+'/relu'))
+
+
+def block_inception_a(name, 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):
+ """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):
+ """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):
+ """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):
+ """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):
+ """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',
+ '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.
+
+ Raises:
+ ValueError: if final_endpoint is not set to one of the predefined values,
+ """
+ end_points = {}
+ net = ffnet.FeedForwardNet()
+ def add_and_check_final(name, lyr):
+ end_points[name] = lyr
+ return name == final_endpoint
+
+ # 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))
+ # 149 x 149 x 32
+ conv2d(net, '%s/Conv2d_2a_3x3' % name, 32, 3, padding='VALID')
+ # 147 x 147 x 32
+ conv2d(net, '%s/Conv2d_2b_3x3' % name, 64, 3)
+ # 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])
+
+ # 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])
+
+ # 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
+
+ # 35 x 35 x 384
+ # Reduction-A block
+ block_reduction_a(name + '/Mixed_6a', net)
+
+ # 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))
+
+ # 17 x 17 x 1024
+ # Reduction-B block
+ block_reduction_b(name + '/Mixed_7a', net)
+
+ # 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
+
+
+def create_net(num_classes=1001, sample_shape=(3, 299, 299), is_training=True, dropout_keep_prob=0.8, create_aux_logits=True):
+ """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.
+
+ Returns:
+ logits: the logits outputs of the model.
+ end_points: the set of end_points from the inception model.
+ """
+ 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)
+ # Auxiliary Head logits
+ if aux_name 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))
+
+ # 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)
+ # 1 x 1 x 1536
+ net.add(Dropout('%s/Logits/Dropout_1b' % name, 1 - dropout_keep_prob))
+ net.add(Flatten('%s/Logits/PreLogitsFlatten' % name))
+ # 1536
+ end_points['Logits'] = net.add(Dense('%s/Logits/Logits' % name, num_classes))
+ return net, end_points
+
+
+if __name__ == '__main__':
+ net, _ = create_net()
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/fc4d1ccc/examples/imagenet/inception/serve.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/inception/serve.py b/examples/imagenet/inception/serve.py
new file mode 100644
index 0000000..9ba099a
--- /dev/null
+++ b/examples/imagenet/inception/serve.py
@@ -0,0 +1,121 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =============================================================================
+import model
+
+from singa import device
+from singa import tensor
+from singa import image_tool
+from singa import layer
+from rafiki.agent import Agent, MsgType
+
+import sys
+import time
+import traceback
+from argparse import ArgumentParser
+import numpy as np
+
+
+def serve(agent, use_cpu, parameter_file, topk=5):
+ if use_cpu:
+ print('running with cpu')
+ dev = device.get_default_device()
+ layer.engine = 'singacpp'
+ else:
+ print("runing with gpu")
+ dev = device.create_cuda_gpu()
+ agent = agent
+
+ print('Start intialization............')
+ net, _ = model.create_net(is_training=False)
+ net.load(parameter_file, use_pickle=True)
+ net.to_device(dev)
+ print('End intialization............')
+
+ labels = np.loadtxt('synset_words.txt', str, delimiter='\t').tolist()
+ labels.insert(0, 'empty background')
+ while True:
+ key, val = agent.pull()
+ if key is None:
+ time.sleep(0.1)
+ continue
+ msg_type = MsgType.parse(key)
+ if msg_type.is_request():
+ try:
+ response = ""
+ ratio = 0.875
+ img = image_tool.load_img(val['image'])
+ height, width = img.size[0], img.size[1]
+ print(img.size)
+ crop_h, crop_w = int(height * ratio), int(width * ratio)
+ img = np.array(image_tool.crop(img, (crop_h, crop_w), 'center').resize((299, 299))).astype(np.float32) / float(255)
+ img -= 0.5
+ img *= 2
+ # img[:,:,[0,1,2]] = img[:,:,[2,1,0]]
+ img = img.transpose((2, 0, 1))
+ images = np.expand_dims(img, axis=0)
+ x = tensor.from_numpy(images.astype(np.float32))
+ x.to_device(dev)
+ y = net.predict(x)
+ prob = np.average(tensor.to_numpy(y), 0)
+ # sort and reverse
+ idx = np.argsort(-prob)[0:topk]
+ for i in idx:
+ response += "%s:%s<br/>" % (labels[i], prob[i])
+ except:
+ traceback.print_exc()
+ response = "Sorry, system error during prediction."
+ agent.push(MsgType.kResponse, response)
+ elif MsgType.kCommandStop.equal(msg_type):
+ print('get stop command')
+ agent.push(MsgType.kStatus, "success")
+ break
+ else:
+ print('get unsupported message %s' % str(msg_type))
+ agent.push(MsgType.kStatus, "Unknown command")
+ break
+ # while loop
+ print("server stop")
+
+
+def main():
+ try:
+ # Setup argument parser
+ parser = ArgumentParser(description="InceptionV4 for image classification")
+ parser.add_argument("-p", "--port", default=9999, help="listen port")
+ parser.add_argument("-C", "--use_cpu", action="store_true")
+ parser.add_argument("--parameter_file", default="inception_v4.pickle",
+ help="relative path")
+
+ # Process arguments
+ args = parser.parse_args()
+ port = args.port
+
+ # start to train
+ agent = Agent(port)
+ serve(agent, args.use_cpu, args.parameter_file)
+ agent.stop()
+
+ except SystemExit:
+ return
+ except:
+ traceback.print_exc()
+ sys.stderr.write(" for help use --help \n\n")
+ return 2
+
+
+if __name__ == '__main__':
+ main()
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/fc4d1ccc/examples/imagenet/inceptionv4/README.md
----------------------------------------------------------------------
diff --git a/examples/imagenet/inceptionv4/README.md b/examples/imagenet/inceptionv4/README.md
deleted file mode 100644
index f129edc..0000000
--- a/examples/imagenet/inceptionv4/README.md
+++ /dev/null
@@ -1,43 +0,0 @@
----
-name: Inception V4 on ImageNet
-SINGA version: 1.1.1
-SINGA commit:
-parameter_url: https://s3-ap-southeast-1.amazonaws.com/dlfile/inception_v4.tar.gz
-parameter_sha1: 5fdd6f5d8af8fd10e7321d9b38bb87ef14e80d56
-license: https://github.com/tensorflow/models/tree/master/slim
----
-
-# Image Classification using Inception V4
-
-In this example, we convert Inception V4 trained on Tensorflow to SINGA for image classification.
-
-## Instructions
-
-* Download the parameter checkpoint file
-
- $ wget
- $ tar xvf inception_v4.tar.gz
-
-* Download [synset_word.txt](https://github.com/BVLC/caffe/blob/master/data/ilsvrc12/get_ilsvrc_aux.sh) file.
-
-* Run the program
-
- # use cpu
- $ python serve.py -C &
- # use gpu
- $ python serve.py &
-
-* Submit images for classification
-
- $ curl -i -F image=@image1.jpg http://localhost:9999/api
- $ curl -i -F image=@image2.jpg http://localhost:9999/api
- $ curl -i -F image=@image3.jpg http://localhost:9999/api
-
-image1.jpg, image2.jpg and image3.jpg should be downloaded before executing the above commands.
-
-## Details
-
-We first extract the parameter values from [Tensorflow's checkpoint file](http://download.tensorflow.org/models/inception_v4_2016_09_09.tar.gz) into a pickle version.
-After downloading and decompressing the checkpoint file, run the following script
-
- $ python convert.py --file_name=inception_v4.ckpt
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/fc4d1ccc/examples/imagenet/inceptionv4/convert.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/inceptionv4/convert.py b/examples/imagenet/inceptionv4/convert.py
deleted file mode 100644
index e3f5adc..0000000
--- a/examples/imagenet/inceptionv4/convert.py
+++ /dev/null
@@ -1,117 +0,0 @@
-# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-# ==============================================================================
-"""A simple script for inspect checkpoint files."""
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-
-import argparse
-import sys
-import cPickle as pickle
-import os
-
-import numpy as np
-from tensorflow.python import pywrap_tensorflow
-from tensorflow.python.platform import app
-import model
-
-
-FLAGS = None
-
-
-def rename(name, suffix):
- p = name.rfind('/')
- if p == -1:
- print('Bad name=%s' % name)
- return name[0:p+1] + suffix
-
-
-def convert(file_name):
- net, _ = model.create_net()
- params = {'SINGA_VERSION': 1101}
- try:
- reader = pywrap_tensorflow.NewCheckpointReader(file_name)
- for pname, pval in zip(net.param_names(), net.param_values()):
- if 'weight' in pname:
- val = reader.get_tensor(rename(pname, 'weights'))
- if 'Conv' in pname:
- val = val.transpose((3, 2, 0, 1))
- val = val.reshape((val.shape[0], -1))
- elif 'bias' in pname:
- val = reader.get_tensor(rename(pname, 'biases'))
- elif 'mean' in pname:
- val = reader.get_tensor(rename(pname, 'moving_mean'))
- elif 'var' in pname:
- val = reader.get_tensor(rename(pname, 'moving_variance'))
- elif 'beta' in pname:
- val= reader.get_tensor(pname)
- elif 'gamma' in pname:
- 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)
- params[pname] = val.astype(np.float32)
- print('converting:', pname, pval.shape)
- var_to_shape_map = reader.get_variable_to_shape_map()
- for key in var_to_shape_map:
- if 'weights' in key:
- key = rename(key, 'weight')
- elif 'biases' in key:
- key = rename(key, 'bias')
- elif 'moving_mean' in key:
- key = rename(key, 'mean')
- elif 'moving_variance' in key:
- key = rename(key, 'var')
- if key not in params:
- print('key=%s not in the net' % key)
- '''
- for key in var_to_shape_map:
- print("tensor_name: ", key, var_to_shape_map[key])
- '''
- with open(os.path.splitext(file_name)[0] + '.pickle', 'wb') as fd:
- pickle.dump(params, fd)
- except Exception as e: # pylint: disable=broad-except
- print(str(e))
- if "corrupted compressed block contents" in str(e):
- print("It's likely that your checkpoint file has been compressed "
- "with SNAPPY.")
- if ("Data loss" in str(e) and
- (any([e in file_name for e in [".index", ".meta", ".data"]]))):
- proposed_file = ".".join(file_name.split(".")[0:-1])
- v2_file_error_template = """
- It's likely that this is a V2 checkpoint and you need to provide the filename
- *prefix*. Try removing the '.' and extension. Try:
- inspect checkpoint --file_name = {}"""
- print(v2_file_error_template.format(proposed_file))
-
-
-
-def main(unused_argv):
- if not FLAGS.file_name:
- print("Usage: convert.py --file_name=checkpoint_file_name ")
- sys.exit(1)
- else:
- convert(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.")
- 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/fc4d1ccc/examples/imagenet/inceptionv4/model.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/inceptionv4/model.py b/examples/imagenet/inceptionv4/model.py
deleted file mode 100644
index baab522..0000000
--- a/examples/imagenet/inceptionv4/model.py
+++ /dev/null
@@ -1,263 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-# =============================================================================
-
-
-"""
-http://arxiv.org/abs/1602.07261.
-
- Inception-v4, Inception-ResNet and the Impact of Residual Connections
- on Learning
- Christian Szegedy, Sergey Ioffe, Vincent Vanhoucke, Alex Alemi
-
-Refer to
-https://github.com/tensorflow/models/blob/master/slim/nets/inception_v4.py
-"""
-
-
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-
-from singa.layer import Conv2D, Activation, MaxPooling2D, AvgPooling2D,\
- Split, Concat, Dropout, Flatten, Dense, BatchNormalization
-
-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)
- net.add(BatchNormalization('%s/BatchNorm' % name))
- return net.add(Activation(name+'/relu'))
-
-
-def block_inception_a(name, 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):
- """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):
- """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):
- """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):
- """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):
- """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',
- '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.
-
- Raises:
- ValueError: if final_endpoint is not set to one of the predefined values,
- """
- end_points = {}
- net = ffnet.FeedForwardNet()
- def add_and_check_final(name, lyr):
- end_points[name] = lyr
- return name == final_endpoint
-
- # 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))
- # 149 x 149 x 32
- conv2d(net, '%s/Conv2d_2a_3x3' % name, 32, 3, padding='VALID')
- # 147 x 147 x 32
- conv2d(net, '%s/Conv2d_2b_3x3' % name, 64, 3)
- # 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])
-
- # 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])
-
- # 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
-
- # 35 x 35 x 384
- # Reduction-A block
- block_reduction_a(name + '/Mixed_6a', net)
-
- # 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))
-
- # 17 x 17 x 1024
- # Reduction-B block
- block_reduction_b(name + '/Mixed_7a', net)
-
- # 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
-
-
-def create_net(num_classes=1001, sample_shape=(3, 299, 299), is_training=True, dropout_keep_prob=0.8, create_aux_logits=True):
- """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.
-
- Returns:
- logits: the logits outputs of the model.
- end_points: the set of end_points from the inception model.
- """
- 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)
- # Auxiliary Head logits
- if aux_name 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))
-
- # 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)
- # 1 x 1 x 1536
- net.add(Dropout('%s/Logits/Dropout_1b' % name, 1 - dropout_keep_prob))
- net.add(Flatten('%s/Logits/PreLogitsFlatten' % name))
- # 1536
- end_points['Logits'] = net.add(Dense('%s/Logits/Logits' % name, num_classes))
- return net, end_points
-
-
-if __name__ == '__main__':
- net, _ = create_net()
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/fc4d1ccc/examples/imagenet/inceptionv4/serve.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/inceptionv4/serve.py b/examples/imagenet/inceptionv4/serve.py
deleted file mode 100644
index 9ba099a..0000000
--- a/examples/imagenet/inceptionv4/serve.py
+++ /dev/null
@@ -1,121 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-# =============================================================================
-import model
-
-from singa import device
-from singa import tensor
-from singa import image_tool
-from singa import layer
-from rafiki.agent import Agent, MsgType
-
-import sys
-import time
-import traceback
-from argparse import ArgumentParser
-import numpy as np
-
-
-def serve(agent, use_cpu, parameter_file, topk=5):
- if use_cpu:
- print('running with cpu')
- dev = device.get_default_device()
- layer.engine = 'singacpp'
- else:
- print("runing with gpu")
- dev = device.create_cuda_gpu()
- agent = agent
-
- print('Start intialization............')
- net, _ = model.create_net(is_training=False)
- net.load(parameter_file, use_pickle=True)
- net.to_device(dev)
- print('End intialization............')
-
- labels = np.loadtxt('synset_words.txt', str, delimiter='\t').tolist()
- labels.insert(0, 'empty background')
- while True:
- key, val = agent.pull()
- if key is None:
- time.sleep(0.1)
- continue
- msg_type = MsgType.parse(key)
- if msg_type.is_request():
- try:
- response = ""
- ratio = 0.875
- img = image_tool.load_img(val['image'])
- height, width = img.size[0], img.size[1]
- print(img.size)
- crop_h, crop_w = int(height * ratio), int(width * ratio)
- img = np.array(image_tool.crop(img, (crop_h, crop_w), 'center').resize((299, 299))).astype(np.float32) / float(255)
- img -= 0.5
- img *= 2
- # img[:,:,[0,1,2]] = img[:,:,[2,1,0]]
- img = img.transpose((2, 0, 1))
- images = np.expand_dims(img, axis=0)
- x = tensor.from_numpy(images.astype(np.float32))
- x.to_device(dev)
- y = net.predict(x)
- prob = np.average(tensor.to_numpy(y), 0)
- # sort and reverse
- idx = np.argsort(-prob)[0:topk]
- for i in idx:
- response += "%s:%s<br/>" % (labels[i], prob[i])
- except:
- traceback.print_exc()
- response = "Sorry, system error during prediction."
- agent.push(MsgType.kResponse, response)
- elif MsgType.kCommandStop.equal(msg_type):
- print('get stop command')
- agent.push(MsgType.kStatus, "success")
- break
- else:
- print('get unsupported message %s' % str(msg_type))
- agent.push(MsgType.kStatus, "Unknown command")
- break
- # while loop
- print("server stop")
-
-
-def main():
- try:
- # Setup argument parser
- parser = ArgumentParser(description="InceptionV4 for image classification")
- parser.add_argument("-p", "--port", default=9999, help="listen port")
- parser.add_argument("-C", "--use_cpu", action="store_true")
- parser.add_argument("--parameter_file", default="inception_v4.pickle",
- help="relative path")
-
- # Process arguments
- args = parser.parse_args()
- port = args.port
-
- # start to train
- agent = Agent(port)
- serve(agent, args.use_cpu, args.parameter_file)
- agent.stop()
-
- except SystemExit:
- return
- except:
- traceback.print_exc()
- sys.stderr.write(" for help use --help \n\n")
- return 2
-
-
-if __name__ == '__main__':
- main()
[6/6] incubator-singa git commit: SINGA-326 - Add Inception V4 for
ImageNet classification
Posted by wa...@apache.org.
SINGA-326 - Add Inception V4 for ImageNet classification
Conflicts:
tool/conda/meta.yaml
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/a39ed5ce
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/a39ed5ce
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/a39ed5ce
Branch: refs/heads/master
Commit: a39ed5ce5571cfb1b7381bec2965076a97678116
Parents: 63c6ae1 3f39cfa
Author: Wei Wang <wa...@comp.nus.edu.sg>
Authored: Thu Jul 13 15:00:02 2017 +0800
Committer: Wei Wang <wa...@comp.nus.edu.sg>
Committed: Thu Jul 13 15:00:02 2017 +0800
----------------------------------------------------------------------
examples/cifar10/vgg.py | 1 +
examples/imagenet/googlenet/serve.py | 4 +-
examples/imagenet/inception/README.md | 43 +++
examples/imagenet/inception/convert.py | 120 ++++++++
examples/imagenet/inception/inception_v3.py | 371 +++++++++++++++++++++++
examples/imagenet/inception/inception_v4.py | 322 ++++++++++++++++++++
examples/imagenet/inception/serve.py | 121 ++++++++
python/singa/image_tool.py | 8 +-
python/singa/layer.py | 71 ++++-
tool/conda/meta.yaml | 26 +-
10 files changed, 1054 insertions(+), 33 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/a39ed5ce/python/singa/image_tool.py
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/a39ed5ce/tool/conda/meta.yaml
----------------------------------------------------------------------
diff --cc tool/conda/meta.yaml
index 4d27ce7,f0b6dd0..f562ecc
--- a/tool/conda/meta.yaml
+++ b/tool/conda/meta.yaml
@@@ -30,20 -35,15 +35,7 @@@ requirements
- 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:
- - test/python/*.py
- requires:
- - unittest-xml-reporting
- test:
- - python run.py
-test:
- source_files:
- - test/python/*.py
- requires:
- - unittest-xml-reporting
- test:
- - python run.py
-
about:
home: http://singa.apache.org/
license: Apache V2
[5/6] incubator-singa git commit: convert param for both v3 and v4
Posted by wa...@apache.org.
convert param for both v3 and v4
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/3f39cfa2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/3f39cfa2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/3f39cfa2
Branch: refs/heads/master
Commit: 3f39cfa246b477329ce349b8d2bcf10e5353bf37
Parents: 40124db
Author: wangwei <wa...@comp.nus.edu.sg>
Authored: Fri Jul 7 13:20:03 2017 +0800
Committer: wangwei <wa...@comp.nus.edu.sg>
Committed: Fri Jul 7 13:20:03 2017 +0800
----------------------------------------------------------------------
examples/imagenet/inception/inception_v3.py | 456 +++++++++++++----------
examples/imagenet/inception/inception_v4.py | 87 ++---
2 files changed, 305 insertions(+), 238 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/3f39cfa2/examples/imagenet/inception/inception_v3.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/inception/inception_v3.py b/examples/imagenet/inception/inception_v3.py
index dc7b24b..421343b 100644
--- a/examples/imagenet/inception/inception_v3.py
+++ b/examples/imagenet/inception/inception_v3.py
@@ -28,52 +28,63 @@ from __future__ import division
from __future__ import print_function
from singa.layer import Conv2D, Activation, MaxPooling2D, AvgPooling2D,\
- Split, Concat, Dropout, Flatten, Dense, BatchNormalization
+ Split, Concat, Dropout, Flatten, BatchNormalization
from singa import net as ffnet
ffnet.verbose = True
+
def conv2d(net, name, nb_filter, k, s=1, border_mode='SAME', src=None):
if type(k) is list:
k = (k[0], k[1])
- net.add(Conv2D(name, nb_filter, k, s, border_mode=border_mode, use_bias=False), src)
+ 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 inception_v3_base(name, sample_shape, final_endpoint='Mixed_6e', aux_name=None, depth_multiplier=1, min_depth=16):
+def inception_v3_base(name, sample_shape, final_endpoint, aux_endpoint,
+ depth_multiplier=1, min_depth=16):
"""Creates the Inception V3 network up to the given final endpoint.
Args:
- inputs: a 4-D tensor of size [batch_size, height, width, 3].
+ sample_shape: input image sample shape, 3d tuple
final_endpoint: specifies the endpoint to construct the network up to.
+ aux_endpoint: for aux loss.
Returns:
logits: the logits outputs of the model.
end_points: the set of end_points from the inception model.
Raises:
- ValueError: if final_endpoint is not set to one of the predefined values,
+ ValueError: if final_endpoint is not set to one of the predefined values
"""
- endpoints = {}
- def final_aux_check(block_name, net):
+ V3 = 'InceptionV3'
+ end_points = {}
+ net = ffnet.FeedForwardNet()
+
+ def final_aux_check(block_name):
if block_name == final_endpoint:
- return net, endpoints[block_name], endpoints
- if block_name == aux_name:
- endpoints[aux_name + '-aux'] = net.add(Split('%s-aux' % aux_name, 2))
+ return True
+ if block_name == aux_endpoint:
+ aux = aux_endpoint + '-aux'
+ end_points[aux] = net.add(Split(aux, 2))
+ return False
- net = ffnet.FeedForwardNet()
- depth = lambda d: max(int(d * depth_multiplier), min_depth)
- V3 = 'InceptionV3'
+ def depth(d):
+ return max(int(d * depth_multiplier), min_depth)
- name = V3 + '/Conv2d_1a_3x3'
+ blk = V3 + '/Conv2d_1a_3x3'
# 299 x 299 x 3
- net.add(Conv2D(name, depth(32), 3, 2, border_mode='VALID', use_bias=False, input_sample_shape=sample_shape))
- net.add(BatchNormalization(name + '/BatchNorm'))
- net.add(Activation(name + '/relu'))
- # 149 x 149 x 32
+ net.add(Conv2D(blk, depth(32), 3, 2, border_mode='VALID', use_bias=False,
+ input_sample_shape=sample_shape))
+ net.add(BatchNormalization(blk + '/BatchNorm'))
+ end_points[blk] = net.add(Activation(blk + '/relu'))
+ if final_aux_check(blk):
+ return net, end_points
+ # 149 x 149 x 32
conv2d(net, '%s/Conv2d_2a_3x3' % V3, depth(32), 3, border_mode='VALID')
# 147 x 147 x 32
conv2d(net, '%s/Conv2d_2b_3x3' % V3, depth(64), 3)
@@ -85,221 +96,274 @@ def inception_v3_base(name, sample_shape, final_endpoint='Mixed_6e', aux_name=No
conv2d(net, '%s/Conv2d_4a_3x3' % V3, depth(192), 3, border_mode='VALID')
# 71 x 71 x 192.
net.add(MaxPooling2D('%s/MaxPool_5a_3x3' % V3, 3, 2, border_mode='VALID'))
- # 35 x 35 x 192.
+ # 35 x 35 x 192.
+ blk = V3 + '/Mixed_5b'
+ s = net.add(Split('%s/Split' % blk, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, depth(64), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, depth(48), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_5x5' % blk, depth(64), 5)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % blk, depth(64), 1, src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % blk, depth(96), 3)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_3x3' % blk, depth(96), 3)
+ net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % blk, 3, 1), s)
+ br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % blk, depth(32), 1)
+ end_points[blk] = net.add(Concat('%s/Concat' % blk, 1),
+ [br0, br1, br2, br3])
+ if final_aux_check(blk):
+ return net, end_points
- m5b = V3 + '/Mixed_5b'
- s = net.add(Split('%s/Split' % m5b, 4))
- br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m5b, depth(64), 1, src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m5b, depth(48), 1, src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_5x5' % m5b, depth(64), 5)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m5b, depth(64), 1, src=s)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % m5b, depth(96), 3)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_3x3' % m5b, depth(96), 3)
- net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m5b, 3, 1), s)
- br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m5b, depth(32), 1)
- endpoints[m5b] =net.add(Concat('%s/Concat' % m5b, 1), [br0, br1, br2, br3])
- final_aux_check(m5b, net)
# mixed_1: 35 x 35 x 288.
- m5c = V3 + '/Mixed_5c'
- s = net.add(Split('%s/Split' % m5c, 4))
- br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m5c, depth(64), 1, src=s)
- br1 = conv2d(net, '%s/Branch_1/Con2d_0b_1x1' % m5c, depth(48), 1, src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv_1_0c_5x5' % m5c, depth(64), 5)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m5c, depth(64), 1, src=s)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % m5c, depth(96), 3)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_3x3' % m5c, depth(96), 3)
- br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m5c, 3, 1), src=s)
- br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m5c, depth(64), 1)
- endpoints[m5c] = net.add(Concat('%s/Concat' % m5c, 1), [br0, br1, br2, br3])
- final_aux_check(m5c, net)
+ blk = V3 + '/Mixed_5c'
+ s = net.add(Split('%s/Split' % blk, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, depth(64), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x1' % blk, depth(48), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv_1_0c_5x5' % blk, depth(64), 5)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % blk, depth(64), 1, src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % blk, depth(96), 3)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_3x3' % blk, depth(96), 3)
+ br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % blk, 3, 1), src=s)
+ br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % blk, depth(64), 1)
+ end_points[blk] = net.add(Concat('%s/Concat' % blk, 1),
+ [br0, br1, br2, br3])
+ if final_aux_check(blk):
+ return net, end_points
# mixed_2: 35 x 35 x 288.
- m5d = V3 + '/Mixed_5d'
- s = net.add(Split('%s/Split' % m5d, 4))
- br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m5d, depth(64), 1, src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m5d, depth(48), 1, src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_5x5' % m5d, depth(64), 5)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m5d, depth(64), 1, src=s)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % m5d, depth(96), 3)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_3x3' % m5d, depth(96), 3)
- br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m5d, 3, 1), s)
- br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m5d, depth(64), 1)
- endpoints[m5d] =net.add(Concat('%s/Concat' % m5d, 1), [br0, br1, br2, br3])
- final_aux_check(m5d, net)
+ blk = V3 + '/Mixed_5d'
+ s = net.add(Split('%s/Split' % blk, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, depth(64), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, depth(48), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_5x5' % blk, depth(64), 5)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % blk, depth(64), 1, src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % blk, depth(96), 3)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_3x3' % blk, depth(96), 3)
+ br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % blk, 3, 1), s)
+ br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % blk, depth(64), 1)
+ end_points[blk] = net.add(Concat('%s/Concat' % blk, 1),
+ [br0, br1, br2, br3])
+ if final_aux_check(blk):
+ return net, end_points
# mixed_3: 17 x 17 x 768.
- m6a = V3 + '/Mixed_6a'
- s = net.add(Split('%s/Split' % m6a, 3))
- br0 = conv2d(net, '%s/Branch_0/Conv2d_1a_1x1' % m6a, depth(384), 3, 2, border_mode='VALID', src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m6a, depth(64), 1, src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_3x3' % m6a, depth(96), 3)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_1a_1x1' % m6a, depth(96), 3, 2, border_mode='VALID')
- br2 = net.add(MaxPooling2D('%s/Branch_2/MaxPool_1a_3x3' % m6a, 3, 2, border_mode='VALID'), s)
- endpoints[m6a] = net.add(Concat('%s/Concat' % m6a, 1), [br0, br1, br2])
- final_aux_check(m6a, net)
+ blk = V3 + '/Mixed_6a'
+ s = net.add(Split('%s/Split' % blk, 3))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_1a_1x1' % blk, depth(384), 3, 2,
+ border_mode='VALID', src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, depth(64), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_3x3' % blk, depth(96), 3)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_1a_1x1' % blk, depth(96), 3, 2,
+ border_mode='VALID')
+ br2 = net.add(MaxPooling2D('%s/Branch_2/MaxPool_1a_3x3' % blk, 3, 2,
+ border_mode='VALID'), s)
+ end_points[blk] = net.add(Concat('%s/Concat' % blk, 1), [br0, br1, br2])
+ if final_aux_check(blk):
+ return net, end_points
# mixed4: 17 x 17 x 768.
- m6b = V3 + '/Mixed_6b'
- s = net.add(Split('%s/Split' % m6b, 4))
- br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m6b, depth(192), 1, src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m6b, depth(128), 1, src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % m6b, depth(128), [1, 7])
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % m6b, depth(192), [7, 1])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m6b, depth(128), [1, 1], src=s)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_7x1' % m6b, depth(128), [7, 1])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x7' % m6b, depth(128), [1, 7])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0d_7x1' % m6b, depth(128), [7, 1])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0e_1x7' % m6b, depth(192), [1, 7])
- br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m6b, 3, 1), s)
- br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m6b, depth(192), [1, 1])
- endpoints[m6b] = net.add(Concat('%s/Concat' % m6b, 1), [br0, br1, br2, br3])
- final_aux_check(m6b, net)
+ blk = V3 + '/Mixed_6b'
+ s = net.add(Split('%s/Split' % blk, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, depth(192), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, depth(128), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % blk, depth(128), [1, 7])
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % blk, depth(192), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % blk, depth(128), [1, 1],
+ src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_7x1' % blk, depth(128), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x7' % blk, depth(128), [1, 7])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0d_7x1' % blk, depth(128), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0e_1x7' % blk, depth(192), [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, depth(192), [1, 1])
+ end_points[blk] = net.add(Concat('%s/Concat' % blk, 1),
+ [br0, br1, br2, br3])
+ if final_aux_check(blk):
+ return net, end_points
# mixed_5: 17 x 17 x 768.
- m6c = V3 + '/Mixed_6c'
- s = net.add(Split('%s/Split' % m6c, 4))
- br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m6c, depth(192), [1, 1], src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m6c, depth(160), [1, 1], src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % m6c, depth(160), [1, 7])
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % m6c, depth(192), [7, 1])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m6c, depth(160), [1, 1], src=s)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_7x1' % m6c, depth(160), [7, 1])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x7' % m6c, depth(160), [1, 7])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0d_7x1' % m6c, depth(160), [7, 1])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0e_1x7' % m6c, depth(192), [1, 7])
- br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m6c, 3, 1), s)
- br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m6c, depth(192), [1, 1])
- endpoints[m6c] = net.add(Concat('%s/Concat' % m6c, 1), [br0, br1, br2, br3])
- final_aux_check(m6c, net)
+ blk = V3 + '/Mixed_6c'
+ s = net.add(Split('%s/Split' % blk, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, depth(192), [1, 1],
+ src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, depth(160), [1, 1],
+ src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % blk, depth(160), [1, 7])
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % blk, depth(192), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % blk, depth(160), [1, 1],
+ src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_7x1' % blk, depth(160), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x7' % blk, depth(160), [1, 7])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0d_7x1' % blk, depth(160), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0e_1x7' % blk, depth(192), [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, depth(192), [1, 1])
+ end_points[blk] = net.add(Concat('%s/Concat' % blk, 1),
+ [br0, br1, br2, br3])
+ if final_aux_check(blk):
+ return net, end_points
# mixed_6: 17 x 17 x 768.
- m6d = V3 + '/Mixed_6d'
- s = net.add(Split('%s/Split' % m6d, 4))
- br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m6d, depth(192), [1, 1], src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m6d, depth(160), [1, 1], src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % m6d, depth(160), [1, 7])
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % m6d, depth(192), [7, 1])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m6d, depth(160), [1, 1], src=s)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_7x1' % m6d, depth(160), [7, 1])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x7' % m6d, depth(160), [1, 7])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0d_7x1' % m6d, depth(160), [7, 1])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0e_1x7' % m6d, depth(192), [1, 7])
- br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m6d, 3, 1), s)
- br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m6d, depth(192), [1, 1])
- endpoints[m6d] = net.add(Concat('%s/Concat' % m6d, 1), [br0, br1, br2, br3])
- final_aux_check(m6d, net)
-
- m6e = V3 + '/Mixed_6e'
- s = net.add(Split('%s/Split' % m6e, 4))
- br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m6e, depth(192), [1, 1], src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m6e, depth(192), [1, 1], src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % m6e, depth(192), [1, 7])
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % m6e, depth(192), [7, 1])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m6e, depth(192), [1, 1], src=s)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_7x1' % m6e, depth(192), [7, 1])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x7' % m6e, depth(192), [1, 7])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0d_7x1' % m6e, depth(192), [7, 1])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0e_1x7' % m6e, depth(192), [1, 7])
- br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m6d, 3, 1), s)
- br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m6d, depth(192), [1, 1])
- endpoints[m6e] = net.add(Concat('%s/Concat' % m6d, 1), [br0, br1, br2, br3])
- final_aux_check(m6e, net)
+ blk = V3 + '/Mixed_6d'
+ s = net.add(Split('%s/Split' % blk, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, depth(192), [1, 1],
+ src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, depth(160), [1, 1],
+ src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % blk, depth(160), [1, 7])
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % blk, depth(192), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % blk, depth(160), [1, 1],
+ src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_7x1' % blk, depth(160), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x7' % blk, depth(160), [1, 7])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0d_7x1' % blk, depth(160), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0e_1x7' % blk, depth(192), [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, depth(192), [1, 1])
+ end_points[blk] = net.add(Concat('%s/Concat' % blk, 1),
+ [br0, br1, br2, br3])
+ if final_aux_check(blk):
+ return net, end_points
+
+ blk = V3 + '/Mixed_6e'
+ s = net.add(Split('%s/Split' % blk, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, depth(192), [1, 1],
+ src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, depth(192), [1, 1],
+ src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % blk, depth(192), [1, 7])
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % blk, depth(192), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % blk, depth(192), [1, 1],
+ src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_7x1' % blk, depth(192), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x7' % blk, depth(192), [1, 7])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0d_7x1' % blk, depth(192), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0e_1x7' % blk, depth(192), [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, depth(192), [1, 1])
+ end_points[blk] = net.add(Concat('%s/Concat' % blk, 1),
+ [br0, br1, br2, br3])
+ if final_aux_check(blk):
+ return net, end_points
# mixed_8: 8 x 8 x 1280.
- m7a = V3 + '/Mixed_7a'
- s = net.add(Split('%s/Split' % m7a, 3))
- br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m7a, depth(192), [1, 1], src=s)
- br0 = conv2d(net, '%s/Branch_0/Conv2d_1a_3x3' % m7a, depth(320), [3, 3], 2, border_mode='VALID')
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m7a, depth(192), [1, 1], src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % m7a, depth(192), [1, 7])
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % m7a, depth(192), [7, 1])
- br1 = conv2d(net, '%s/Branch_1/Conv2d_1a_3x3' % m7a, depth(192), [3, 3], 2, border_mode='VALID')
- br2 = net.add(MaxPooling2D('%s/Branch_2/MaxPool_1a_3x3' % m7a, 3, 2, border_mode='VALID'), s)
- endpoints[m7a] = net.add(Concat('%s/Concat' % m7a, 1), [br0, br1, br2])
- final_aux_check(m7a, net)
+ blk = V3 + '/Mixed_7a'
+ s = net.add(Split('%s/Split' % blk, 3))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, depth(192), [1, 1],
+ src=s)
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_1a_3x3' % blk, depth(320), [3, 3], 2,
+ border_mode='VALID')
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, depth(192), [1, 1],
+ src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % blk, depth(192), [1, 7])
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % blk, depth(192), [7, 1])
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_1a_3x3' % blk, depth(192), [3, 3], 2,
+ border_mode='VALID')
+ br2 = net.add(MaxPooling2D('%s/Branch_2/MaxPool_1a_3x3' % blk, 3, 2,
+ border_mode='VALID'), s)
+ end_points[blk] = net.add(Concat('%s/Concat' % blk, 1), [br0, br1, br2])
+ if final_aux_check(blk):
+ return net, end_points
# mixed_9: 8 x 8 x 2048.
- m7b = V3 + '/Mixed_7b'
- s = net.add(Split('%s/Split' % m7b, 4))
- br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m7b, depth(320), [1, 1], src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m7b, depth(384), [1, 1], src=s)
- s1 = net.add(Split('%s/Branch_1/Split1' % m7b, 2))
- br11 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x3' % m7b, depth(384), [1, 3], src=s1)
- br12 = conv2d(net, '%s/Branch_1/Conv2d_0b_3x1' % m7b, depth(384), [3, 1], src=s1)
- br1 = net.add(Concat('%s/Branch_1/Concat1' % m7b, 1), [br11, br12])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m7b, depth(448), [1, 1], src=s)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % m7b, depth(384), [3, 3])
- s2 = net.add(Split('%s/Branch_2/Split2' % m7b, 2))
- br21 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x3' % m7b, depth(384), [1, 3], src=s2)
- br22 = conv2d(net, '%s/Branch_2/Conv2d_0d_3x1' % m7b, depth(384), [3, 1], src=s2)
- br2 = net.add(Concat('%s/Branch_2/Concat2' % m7b, 1), [br21, br22])
- br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m7b, 3, 1), src=s)
- br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m7b, depth(192), [1, 1])
- endpoints[m7b] = net.add(Concat('%s/Concat' % m7b, 1), [br0, br1, br2, br3])
- final_aux_check(m7b, net)
+ blk = V3 + '/Mixed_7b'
+ s = net.add(Split('%s/Split' % blk, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, depth(320), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, depth(384), 1, src=s)
+ s1 = net.add(Split('%s/Branch_1/Split1' % blk, 2))
+ br11 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x3' % blk, depth(384), [1, 3],
+ src=s1)
+ br12 = conv2d(net, '%s/Branch_1/Conv2d_0b_3x1' % blk, depth(384), [3, 1],
+ src=s1)
+ br1 = net.add(Concat('%s/Branch_1/Concat1' % blk, 1), [br11, br12])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % blk, depth(448), 1, src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % blk, depth(384), 3)
+ s2 = net.add(Split('%s/Branch_2/Split2' % blk, 2))
+ br21 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x3' % blk, depth(384), [1, 3],
+ src=s2)
+ br22 = conv2d(net, '%s/Branch_2/Conv2d_0d_3x1' % blk, depth(384), [3, 1],
+ src=s2)
+ br2 = net.add(Concat('%s/Branch_2/Concat2' % blk, 1), [br21, br22])
+ br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % blk, 3, 1), src=s)
+ br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % blk, depth(192), [1, 1])
+ end_points[blk] = net.add(Concat('%s/Concat' % blk, 1),
+ [br0, br1, br2, br3])
+ if final_aux_check(blk):
+ return net, end_points
# mixed_10: 8 x 8 x 2048.
- m7c = V3 + '/Mixed_7c'
- s = net.add(Split('%s/Split' % m7c, 4))
- br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m7c, depth(320), [1, 1], src=s)
- br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m7c, depth(384), [1, 1], src=s)
- s1 = net.add(Split('%s/Branch_1/Split1' % m7c, 2))
- br11 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x3' % m7c, depth(384), [1, 3], src=s1)
- br12 = conv2d(net, '%s/Branch_1/Conv2d_0b_3x1' % m7c, depth(384), [3, 1], src=s1)
- br1 = net.add(Concat('%s/Branch_1/Concat1' % m7c, 1), [br11, br12])
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m7c, depth(448), [1, 1], src=s)
- br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % m7c, depth(384), [3, 3])
- s2 = net.add(Split('%s/Branch_2/Split2' % m7c, 2))
- br21 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x3' % m7c, depth(384), [1, 3], src=s2)
- br22 = conv2d(net, '%s/Branch_2/Conv2d_0d_3x1' % m7c, depth(384), [3, 1], src=s2)
- br2 = net.add(Concat('%s/Branch_2/Concat2' % m7c, 1), [br21, br22])
- br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m7c, 3, 1), src=s)
- br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m7c, depth(192), [1, 1])
- endpoints[m7c] = net.add(Concat('%s/Concat' % m7c, 1), [br0, br1, br2, br3])
- final_aux_check(m7c, net)
- return net, endpoints[m7c], endpoints
-
-
-def create_net(num_classes=1001, sample_shape=(3, 299, 299), is_training=True, dropout_keep_prob=0.8, create_aux_logits=True):
+ blk = V3 + '/Mixed_7c'
+ s = net.add(Split('%s/Split' % blk, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % blk, depth(320), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % blk, depth(384), 1, src=s)
+ s1 = net.add(Split('%s/Branch_1/Split1' % blk, 2))
+ br11 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x3' % blk, depth(384), [1, 3],
+ src=s1)
+ br12 = conv2d(net, '%s/Branch_1/Conv2d_0c_3x1' % blk, depth(384), [3, 1],
+ src=s1)
+ br1 = net.add(Concat('%s/Branch_1/Concat1' % blk, 1), [br11, br12])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % blk, depth(448), [1, 1],
+ src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % blk, depth(384), [3, 3])
+ s2 = net.add(Split('%s/Branch_2/Split2' % blk, 2))
+ br21 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x3' % blk, depth(384), [1, 3],
+ src=s2)
+ br22 = conv2d(net, '%s/Branch_2/Conv2d_0d_3x1' % blk, depth(384), [3, 1],
+ src=s2)
+ br2 = net.add(Concat('%s/Branch_2/Concat2' % blk, 1), [br21, br22])
+ br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % blk, 3, 1), src=s)
+ br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % blk, depth(192), [1, 1])
+ end_points[blk] = net.add(Concat('%s/Concat' % blk, 1),
+ [br0, br1, br2, br3])
+ assert final_endpoint == blk, \
+ 'final_enpoint = %s is not in the net' % final_endpoint
+ return net, end_points
+
+
+def create_net(num_classes=1001, sample_shape=(3, 299, 299),
+ final_endpoint='InceptionV3/Mixed_7c',
+ aux_endpoint='InceptionV3/Mixed_6e',
+ dropout_keep_prob=0.8):
"""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: 'InceptionV3/Mixed_7d',
+ aux_endpoint:
Returns:
logits: the logits outputs of the model.
end_points: the set of end_points from the inception model.
"""
name = 'InceptionV3'
- if is_training and create_aux_logits:
- aux_name = name + '/Mixed_6e'
- else:
- aux_name = None
- net, last_layer, end_points = inception_v3_base(name, sample_shape, aux_name=aux_name)
+ net, end_points = inception_v3_base(name, sample_shape, final_endpoint,
+ aux_endpoint)
# Auxiliary Head logits
- if aux_name is not None:
+ if aux_endpoint is not None:
# 8 x 8 x 1280
- aux_logits = end_points[aux_name + '-aux']
- 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], border_mode='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 = name + '/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)
+ s = t.get_output_sample_shape()[1:3]
+ conv2d(net, '%s/Conv2d_2a_%dx%d' % (blk, s[0], s[1]), 768, s,
+ border_mode='VALID')
+ net.add(Conv2D('%s/Conv2d_2b_1x1' % blk, num_classes, 1))
+ net.add(Flatten('%s/flat' % blk))
# Final pooling and prediction
# 8 x 8 x 2048
- net.add(AvgPooling2D('%s/Logits/AvgPool_1a' % name, last_layer.get_output_sample_shape()[1:3], 1, border_mode='VALID'), last_layer)
+ blk = name + '/Logits'
+ last_layer = end_points[final_endpoint]
+ net.add(AvgPooling2D('%s/AvgPool_1a' % blk,
+ last_layer.get_output_sample_shape()[1:3], 1,
+ border_mode='VALID'), last_layer)
# 1 x 1 x 2048
- 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(Conv2D('%s/Conv2d_1c_1x1' % blk, num_classes, 1))
+ end_points[blk] = net.add(Flatten('%s/flat' % blk))
# 2048
- end_points['Logits'] = net.add(Dense('%s/Logits/Logits' % name, num_classes))
return net, end_points
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/3f39cfa2/examples/imagenet/inception/inception_v4.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/inception/inception_v4.py b/examples/imagenet/inception/inception_v4.py
index b641706..9c5883f 100644
--- a/examples/imagenet/inception/inception_v4.py
+++ b/examples/imagenet/inception/inception_v4.py
@@ -39,9 +39,10 @@ from singa import net as ffnet
ffnet.verbose = True
+
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)
+ use_bias=False), src)
net.add(BatchNormalization('%s/BatchNorm' % name))
return net.add(Activation(name+'/relu'))
@@ -66,13 +67,13 @@ def block_reduction_a(blk, net):
# By default use stride=1 and SAME padding
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)
+ 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')
+ border_mode='VALID')
br2 = net.add(MaxPooling2D('%s/Branch_2/MaxPool_1a_3x3' % blk, 3, 2,
- border_mode='VALID'), s)
+ border_mode='VALID'), s)
return net.add(Concat('%s/Concat' % blk, 1), [br0, br1, br2])
@@ -97,17 +98,17 @@ def block_inception_b(blk, net):
def block_reduction_b(blk, net):
"""Builds Reduction-B block for Inception v4 network."""
# By default use stride=1 and SAME padding
- s = net.add(Split('%s/Split' % blk , 3))
+ 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')
+ 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')
+ border_mode='VALID')
br2 = net.add(MaxPooling2D('%s/Branch_2/MaxPool_1a_3x3' % blk, 3, 2,
- border_mode='VALID'), s)
+ border_mode='VALID'), s)
return net.add(Concat('%s/Concat' % blk, 1), [br0, br1, br2])
@@ -123,9 +124,9 @@ def block_inception_c(blk, net):
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 = 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)
@@ -136,8 +137,8 @@ def block_inception_c(blk, net):
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'):
+def inception_v4_base(sample_shape, final_endpoint='Inception/Mixed_7d',
+ aux_endpoint='Inception/Mixed_6e'):
"""Creates the Inception V4 network up to the given final endpoint.
Endpoint name list: 'InceptionV4/' +
@@ -148,17 +149,18 @@ def inception_v4_base(name, sample_shape, final_endpoint='Inception/Mixed_7d',
'Mixed_7d']
Args:
- inputs: a 4-D tensor of size [batch_size, height, width, 3].
+ sample_shape: input image sample shape, 3d tuple
final_endpoint: specifies the endpoint to construct the network up to.
aux_endpoint: for aux loss.
Returns:
the neural net
- the last layer
the set of end_points from the inception model.
"""
+ name = 'InceptionV4'
end_points = {}
net = ffnet.FeedForwardNet()
+
def final_aux_check(block_name):
if block_name == final_endpoint:
return True
@@ -167,36 +169,34 @@ def inception_v4_base(name, sample_shape, final_endpoint='Inception/Mixed_7d',
end_points[aux] = net.add(Split(aux, 2))
return False
-
# 299 x 299 x 3
- blk = name + 'Conv2d_1a_3x3'
+ blk = name + '/Conv2d_1a_3x3'
net.add(Conv2D(blk, 32, 3, 2, border_mode='VALID', use_bias=False,
- input_sample_shape=sample_shape))
+ 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
- blk = name + 'Conv2d_2a_3x3'
- end_points[blk] = conv2d(net, '%s/Conv2d_2a_3x3' % blk, 32, 3,
- border_mode='VALID')
+ blk = name + '/Conv2d_2a_3x3'
+ end_points[blk] = conv2d(net, blk, 32, 3, border_mode='VALID')
if final_aux_check(blk):
return net, end_points
# 147 x 147 x 32
- blk = name + 'Conv2d_2b_3x3'
- end_points[blk] = conv2d(net, '%s/Conv2d_2b_3x3' % blk, 64, 3)
+ blk = name + '/Conv2d_2b_3x3'
+ end_points[blk] = conv2d(net, blk, 64, 3)
if final_aux_check(blk):
return net, end_points
# 147 x 147 x 64
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)
+ 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)
+ 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
@@ -206,12 +206,12 @@ def inception_v4_base(name, sample_shape, final_endpoint='Inception/Mixed_7d',
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')
+ 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')
+ border_mode='VALID')
end_points[blk] = net.add(Concat('%s/Concat' % blk, 1), [br0, br1])
if final_aux_check(blk):
return net, end_points
@@ -220,9 +220,9 @@ def inception_v4_base(name, sample_shape, final_endpoint='Inception/Mixed_7d',
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)
+ 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
@@ -265,12 +265,13 @@ def inception_v4_base(name, sample_shape, final_endpoint='Inception/Mixed_7d',
if final_aux_check(blk):
return net, end_points
- return net, end_points
+ assert final_endpoint == blk, \
+ 'final_enpoint = %s is not in the net' % final_endpoint
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'):
+ dropout_keep_prob=0.8, final_endpoint='InceptionV4/Mixed_7d',
+ aux_endpoint='InceptionV4/Mixed_6e'):
"""Creates the Inception V4 model.
Args:
@@ -285,28 +286,30 @@ def create_net(num_classes=1001, sample_shape=(3, 299, 299), is_training=True,
"""
end_points = {}
name = 'InceptionV4'
- net, end_points = inception_v4_base(name, sample_shape,
- final_endpoint=final_endpoint, aux_endpoint=aux_endpoint)
+ net, end_points = inception_v4_base(sample_shape,
+ final_endpoint=final_endpoint,
+ aux_endpoint=aux_endpoint)
# Auxiliary Head logits
if aux_endpoint is not None:
# 17 x 17 x 1024
aux_logits = end_points[aux_endpoint + '-aux']
- blk = 'AuxLogits'
+ blk = name + '/AuxLogits'
net.add(AvgPooling2D('%s/AvgPool_1a_5x5' % blk, 5, stride=3,
- border_mode='VALID'), aux_logits)
+ 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')
+ 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
- blk = 'Logits'
+ blk = name + '/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)
+ last_layer.get_output_sample_shape()[1:3],
+ border_mode='VALID'),
+ last_layer)
# 1 x 1 x 1536
net.add(Dropout('%s/Dropout_1b' % blk, 1 - dropout_keep_prob))
net.add(Flatten('%s/PreLogitsFlatten' % blk))
[3/6] incubator-singa git commit: add inception v3
Posted by wa...@apache.org.
add inception v3
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/59b0167b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/59b0167b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/59b0167b
Branch: refs/heads/master
Commit: 59b0167b404fdd24bd6d06de5893bbcf9a93a4e1
Parents: fc4d1cc
Author: wang wei <wa...@comp.nus.edu.sg>
Authored: Thu Jul 6 23:24:16 2017 +0800
Committer: wang wei <wa...@comp.nus.edu.sg>
Committed: Thu Jul 6 23:24:16 2017 +0800
----------------------------------------------------------------------
examples/imagenet/inception/inception_v3.py | 307 +++++++++++++++++++++++
examples/imagenet/inception/inception_v4.py | 263 +++++++++++++++++++
examples/imagenet/inception/model.py | 263 -------------------
3 files changed, 570 insertions(+), 263 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/59b0167b/examples/imagenet/inception/inception_v3.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/inception/inception_v3.py b/examples/imagenet/inception/inception_v3.py
new file mode 100644
index 0000000..dc7b24b
--- /dev/null
+++ b/examples/imagenet/inception/inception_v3.py
@@ -0,0 +1,307 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =============================================================================
+
+
+"""
+
+Refer to
+https://github.com/tensorflow/models/blob/master/slim/nets/inception_v3.py
+"""
+
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+from singa.layer import Conv2D, Activation, MaxPooling2D, AvgPooling2D,\
+ Split, Concat, Dropout, Flatten, Dense, BatchNormalization
+
+from singa import net as ffnet
+
+ffnet.verbose = True
+
+def conv2d(net, name, nb_filter, k, s=1, border_mode='SAME', src=None):
+ if type(k) is list:
+ k = (k[0], k[1])
+ 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 inception_v3_base(name, sample_shape, final_endpoint='Mixed_6e', aux_name=None, depth_multiplier=1, min_depth=16):
+ """Creates the Inception V3 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.
+
+ Returns:
+ logits: the logits outputs of the model.
+ end_points: the set of end_points from the inception model.
+
+ Raises:
+ ValueError: if final_endpoint is not set to one of the predefined values,
+ """
+ endpoints = {}
+ def final_aux_check(block_name, net):
+ if block_name == final_endpoint:
+ return net, endpoints[block_name], endpoints
+ if block_name == aux_name:
+ endpoints[aux_name + '-aux'] = net.add(Split('%s-aux' % aux_name, 2))
+
+ net = ffnet.FeedForwardNet()
+ depth = lambda d: max(int(d * depth_multiplier), min_depth)
+ V3 = 'InceptionV3'
+
+ name = V3 + '/Conv2d_1a_3x3'
+ # 299 x 299 x 3
+ net.add(Conv2D(name, depth(32), 3, 2, border_mode='VALID', use_bias=False, input_sample_shape=sample_shape))
+ net.add(BatchNormalization(name + '/BatchNorm'))
+ net.add(Activation(name + '/relu'))
+ # 149 x 149 x 32
+
+ conv2d(net, '%s/Conv2d_2a_3x3' % V3, depth(32), 3, border_mode='VALID')
+ # 147 x 147 x 32
+ conv2d(net, '%s/Conv2d_2b_3x3' % V3, depth(64), 3)
+ # 147 x 147 x 64
+ net.add(MaxPooling2D('%s/MaxPool_3a_3x3' % V3, 3, 2, border_mode='VALID'))
+ # 73 x 73 x 64
+ conv2d(net, '%s/Conv2d_3b_1x1' % V3, depth(80), 1, border_mode='VALID')
+ # 73 x 73 x 80.
+ conv2d(net, '%s/Conv2d_4a_3x3' % V3, depth(192), 3, border_mode='VALID')
+ # 71 x 71 x 192.
+ net.add(MaxPooling2D('%s/MaxPool_5a_3x3' % V3, 3, 2, border_mode='VALID'))
+ # 35 x 35 x 192.
+
+
+ m5b = V3 + '/Mixed_5b'
+ s = net.add(Split('%s/Split' % m5b, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m5b, depth(64), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m5b, depth(48), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_5x5' % m5b, depth(64), 5)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m5b, depth(64), 1, src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % m5b, depth(96), 3)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_3x3' % m5b, depth(96), 3)
+ net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m5b, 3, 1), s)
+ br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m5b, depth(32), 1)
+ endpoints[m5b] =net.add(Concat('%s/Concat' % m5b, 1), [br0, br1, br2, br3])
+ final_aux_check(m5b, net)
+ # mixed_1: 35 x 35 x 288.
+ m5c = V3 + '/Mixed_5c'
+ s = net.add(Split('%s/Split' % m5c, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m5c, depth(64), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Con2d_0b_1x1' % m5c, depth(48), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv_1_0c_5x5' % m5c, depth(64), 5)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m5c, depth(64), 1, src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % m5c, depth(96), 3)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_3x3' % m5c, depth(96), 3)
+ br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m5c, 3, 1), src=s)
+ br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m5c, depth(64), 1)
+ endpoints[m5c] = net.add(Concat('%s/Concat' % m5c, 1), [br0, br1, br2, br3])
+ final_aux_check(m5c, net)
+
+ # mixed_2: 35 x 35 x 288.
+ m5d = V3 + '/Mixed_5d'
+ s = net.add(Split('%s/Split' % m5d, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m5d, depth(64), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m5d, depth(48), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_5x5' % m5d, depth(64), 5)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m5d, depth(64), 1, src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % m5d, depth(96), 3)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_3x3' % m5d, depth(96), 3)
+ br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m5d, 3, 1), s)
+ br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m5d, depth(64), 1)
+ endpoints[m5d] =net.add(Concat('%s/Concat' % m5d, 1), [br0, br1, br2, br3])
+ final_aux_check(m5d, net)
+
+ # mixed_3: 17 x 17 x 768.
+ m6a = V3 + '/Mixed_6a'
+ s = net.add(Split('%s/Split' % m6a, 3))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_1a_1x1' % m6a, depth(384), 3, 2, border_mode='VALID', src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m6a, depth(64), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_3x3' % m6a, depth(96), 3)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_1a_1x1' % m6a, depth(96), 3, 2, border_mode='VALID')
+ br2 = net.add(MaxPooling2D('%s/Branch_2/MaxPool_1a_3x3' % m6a, 3, 2, border_mode='VALID'), s)
+ endpoints[m6a] = net.add(Concat('%s/Concat' % m6a, 1), [br0, br1, br2])
+ final_aux_check(m6a, net)
+
+ # mixed4: 17 x 17 x 768.
+ m6b = V3 + '/Mixed_6b'
+ s = net.add(Split('%s/Split' % m6b, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m6b, depth(192), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m6b, depth(128), 1, src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % m6b, depth(128), [1, 7])
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % m6b, depth(192), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m6b, depth(128), [1, 1], src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_7x1' % m6b, depth(128), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x7' % m6b, depth(128), [1, 7])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0d_7x1' % m6b, depth(128), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0e_1x7' % m6b, depth(192), [1, 7])
+ br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m6b, 3, 1), s)
+ br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m6b, depth(192), [1, 1])
+ endpoints[m6b] = net.add(Concat('%s/Concat' % m6b, 1), [br0, br1, br2, br3])
+ final_aux_check(m6b, net)
+
+ # mixed_5: 17 x 17 x 768.
+ m6c = V3 + '/Mixed_6c'
+ s = net.add(Split('%s/Split' % m6c, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m6c, depth(192), [1, 1], src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m6c, depth(160), [1, 1], src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % m6c, depth(160), [1, 7])
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % m6c, depth(192), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m6c, depth(160), [1, 1], src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_7x1' % m6c, depth(160), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x7' % m6c, depth(160), [1, 7])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0d_7x1' % m6c, depth(160), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0e_1x7' % m6c, depth(192), [1, 7])
+ br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m6c, 3, 1), s)
+ br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m6c, depth(192), [1, 1])
+ endpoints[m6c] = net.add(Concat('%s/Concat' % m6c, 1), [br0, br1, br2, br3])
+ final_aux_check(m6c, net)
+
+ # mixed_6: 17 x 17 x 768.
+ m6d = V3 + '/Mixed_6d'
+ s = net.add(Split('%s/Split' % m6d, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m6d, depth(192), [1, 1], src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m6d, depth(160), [1, 1], src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % m6d, depth(160), [1, 7])
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % m6d, depth(192), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m6d, depth(160), [1, 1], src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_7x1' % m6d, depth(160), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x7' % m6d, depth(160), [1, 7])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0d_7x1' % m6d, depth(160), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0e_1x7' % m6d, depth(192), [1, 7])
+ br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m6d, 3, 1), s)
+ br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m6d, depth(192), [1, 1])
+ endpoints[m6d] = net.add(Concat('%s/Concat' % m6d, 1), [br0, br1, br2, br3])
+ final_aux_check(m6d, net)
+
+ m6e = V3 + '/Mixed_6e'
+ s = net.add(Split('%s/Split' % m6e, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m6e, depth(192), [1, 1], src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m6e, depth(192), [1, 1], src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % m6e, depth(192), [1, 7])
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % m6e, depth(192), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m6e, depth(192), [1, 1], src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_7x1' % m6e, depth(192), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x7' % m6e, depth(192), [1, 7])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0d_7x1' % m6e, depth(192), [7, 1])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0e_1x7' % m6e, depth(192), [1, 7])
+ br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m6d, 3, 1), s)
+ br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m6d, depth(192), [1, 1])
+ endpoints[m6e] = net.add(Concat('%s/Concat' % m6d, 1), [br0, br1, br2, br3])
+ final_aux_check(m6e, net)
+
+ # mixed_8: 8 x 8 x 1280.
+ m7a = V3 + '/Mixed_7a'
+ s = net.add(Split('%s/Split' % m7a, 3))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m7a, depth(192), [1, 1], src=s)
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_1a_3x3' % m7a, depth(320), [3, 3], 2, border_mode='VALID')
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m7a, depth(192), [1, 1], src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x7' % m7a, depth(192), [1, 7])
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0c_7x1' % m7a, depth(192), [7, 1])
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_1a_3x3' % m7a, depth(192), [3, 3], 2, border_mode='VALID')
+ br2 = net.add(MaxPooling2D('%s/Branch_2/MaxPool_1a_3x3' % m7a, 3, 2, border_mode='VALID'), s)
+ endpoints[m7a] = net.add(Concat('%s/Concat' % m7a, 1), [br0, br1, br2])
+ final_aux_check(m7a, net)
+
+ # mixed_9: 8 x 8 x 2048.
+ m7b = V3 + '/Mixed_7b'
+ s = net.add(Split('%s/Split' % m7b, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m7b, depth(320), [1, 1], src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m7b, depth(384), [1, 1], src=s)
+ s1 = net.add(Split('%s/Branch_1/Split1' % m7b, 2))
+ br11 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x3' % m7b, depth(384), [1, 3], src=s1)
+ br12 = conv2d(net, '%s/Branch_1/Conv2d_0b_3x1' % m7b, depth(384), [3, 1], src=s1)
+ br1 = net.add(Concat('%s/Branch_1/Concat1' % m7b, 1), [br11, br12])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m7b, depth(448), [1, 1], src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % m7b, depth(384), [3, 3])
+ s2 = net.add(Split('%s/Branch_2/Split2' % m7b, 2))
+ br21 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x3' % m7b, depth(384), [1, 3], src=s2)
+ br22 = conv2d(net, '%s/Branch_2/Conv2d_0d_3x1' % m7b, depth(384), [3, 1], src=s2)
+ br2 = net.add(Concat('%s/Branch_2/Concat2' % m7b, 1), [br21, br22])
+ br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m7b, 3, 1), src=s)
+ br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m7b, depth(192), [1, 1])
+ endpoints[m7b] = net.add(Concat('%s/Concat' % m7b, 1), [br0, br1, br2, br3])
+ final_aux_check(m7b, net)
+
+ # mixed_10: 8 x 8 x 2048.
+ m7c = V3 + '/Mixed_7c'
+ s = net.add(Split('%s/Split' % m7c, 4))
+ br0 = conv2d(net, '%s/Branch_0/Conv2d_0a_1x1' % m7c, depth(320), [1, 1], src=s)
+ br1 = conv2d(net, '%s/Branch_1/Conv2d_0a_1x1' % m7c, depth(384), [1, 1], src=s)
+ s1 = net.add(Split('%s/Branch_1/Split1' % m7c, 2))
+ br11 = conv2d(net, '%s/Branch_1/Conv2d_0b_1x3' % m7c, depth(384), [1, 3], src=s1)
+ br12 = conv2d(net, '%s/Branch_1/Conv2d_0b_3x1' % m7c, depth(384), [3, 1], src=s1)
+ br1 = net.add(Concat('%s/Branch_1/Concat1' % m7c, 1), [br11, br12])
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0a_1x1' % m7c, depth(448), [1, 1], src=s)
+ br2 = conv2d(net, '%s/Branch_2/Conv2d_0b_3x3' % m7c, depth(384), [3, 3])
+ s2 = net.add(Split('%s/Branch_2/Split2' % m7c, 2))
+ br21 = conv2d(net, '%s/Branch_2/Conv2d_0c_1x3' % m7c, depth(384), [1, 3], src=s2)
+ br22 = conv2d(net, '%s/Branch_2/Conv2d_0d_3x1' % m7c, depth(384), [3, 1], src=s2)
+ br2 = net.add(Concat('%s/Branch_2/Concat2' % m7c, 1), [br21, br22])
+ br3 = net.add(AvgPooling2D('%s/Branch_3/AvgPool_0a_3x3' % m7c, 3, 1), src=s)
+ br3 = conv2d(net, '%s/Branch_3/Conv2d_0b_1x1' % m7c, depth(192), [1, 1])
+ endpoints[m7c] = net.add(Concat('%s/Concat' % m7c, 1), [br0, br1, br2, br3])
+ final_aux_check(m7c, net)
+ return net, endpoints[m7c], endpoints
+
+
+def create_net(num_classes=1001, sample_shape=(3, 299, 299), is_training=True, dropout_keep_prob=0.8, create_aux_logits=True):
+ """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.
+
+ Returns:
+ logits: the logits outputs of the model.
+ end_points: the set of end_points from the inception model.
+ """
+ name = 'InceptionV3'
+ if is_training and create_aux_logits:
+ aux_name = name + '/Mixed_6e'
+ else:
+ aux_name = None
+ net, last_layer, end_points = inception_v3_base(name, sample_shape, aux_name=aux_name)
+ # Auxiliary Head logits
+ if aux_name is not None:
+ # 8 x 8 x 1280
+ aux_logits = end_points[aux_name + '-aux']
+ 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], border_mode='VALID')
+ net.add(Flatten('%s/AuxLogits/flat' % name))
+ end_points['AuxLogits'] = net.add(Dense('%s/AuxLogits/Aux_logits' % name, num_classes))
+
+ # Final pooling and prediction
+ # 8 x 8 x 2048
+ net.add(AvgPooling2D('%s/Logits/AvgPool_1a' % name, last_layer.get_output_sample_shape()[1:3], 1, border_mode='VALID'), last_layer)
+ # 1 x 1 x 2048
+ net.add(Dropout('%s/Logits/Dropout_1b' % name, 1 - dropout_keep_prob))
+ net.add(Flatten('%s/Logits/PreLogitsFlatten' % name))
+ # 2048
+ end_points['Logits'] = net.add(Dense('%s/Logits/Logits' % name, num_classes))
+ return net, end_points
+
+
+if __name__ == '__main__':
+ net, _ = create_net()
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/59b0167b/examples/imagenet/inception/inception_v4.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/inception/inception_v4.py b/examples/imagenet/inception/inception_v4.py
new file mode 100644
index 0000000..baab522
--- /dev/null
+++ b/examples/imagenet/inception/inception_v4.py
@@ -0,0 +1,263 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =============================================================================
+
+
+"""
+http://arxiv.org/abs/1602.07261.
+
+ Inception-v4, Inception-ResNet and the Impact of Residual Connections
+ on Learning
+ Christian Szegedy, Sergey Ioffe, Vincent Vanhoucke, Alex Alemi
+
+Refer to
+https://github.com/tensorflow/models/blob/master/slim/nets/inception_v4.py
+"""
+
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+from singa.layer import Conv2D, Activation, MaxPooling2D, AvgPooling2D,\
+ Split, Concat, Dropout, Flatten, Dense, BatchNormalization
+
+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)
+ net.add(BatchNormalization('%s/BatchNorm' % name))
+ return net.add(Activation(name+'/relu'))
+
+
+def block_inception_a(name, 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):
+ """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):
+ """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):
+ """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):
+ """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):
+ """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',
+ '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.
+
+ Raises:
+ ValueError: if final_endpoint is not set to one of the predefined values,
+ """
+ end_points = {}
+ net = ffnet.FeedForwardNet()
+ def add_and_check_final(name, lyr):
+ end_points[name] = lyr
+ return name == final_endpoint
+
+ # 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))
+ # 149 x 149 x 32
+ conv2d(net, '%s/Conv2d_2a_3x3' % name, 32, 3, padding='VALID')
+ # 147 x 147 x 32
+ conv2d(net, '%s/Conv2d_2b_3x3' % name, 64, 3)
+ # 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])
+
+ # 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])
+
+ # 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
+
+ # 35 x 35 x 384
+ # Reduction-A block
+ block_reduction_a(name + '/Mixed_6a', net)
+
+ # 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))
+
+ # 17 x 17 x 1024
+ # Reduction-B block
+ block_reduction_b(name + '/Mixed_7a', net)
+
+ # 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
+
+
+def create_net(num_classes=1001, sample_shape=(3, 299, 299), is_training=True, dropout_keep_prob=0.8, create_aux_logits=True):
+ """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.
+
+ Returns:
+ logits: the logits outputs of the model.
+ end_points: the set of end_points from the inception model.
+ """
+ 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)
+ # Auxiliary Head logits
+ if aux_name 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))
+
+ # 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)
+ # 1 x 1 x 1536
+ net.add(Dropout('%s/Logits/Dropout_1b' % name, 1 - dropout_keep_prob))
+ net.add(Flatten('%s/Logits/PreLogitsFlatten' % name))
+ # 1536
+ end_points['Logits'] = net.add(Dense('%s/Logits/Logits' % name, num_classes))
+ return net, end_points
+
+
+if __name__ == '__main__':
+ net, _ = create_net()
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/59b0167b/examples/imagenet/inception/model.py
----------------------------------------------------------------------
diff --git a/examples/imagenet/inception/model.py b/examples/imagenet/inception/model.py
deleted file mode 100644
index baab522..0000000
--- a/examples/imagenet/inception/model.py
+++ /dev/null
@@ -1,263 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-# =============================================================================
-
-
-"""
-http://arxiv.org/abs/1602.07261.
-
- Inception-v4, Inception-ResNet and the Impact of Residual Connections
- on Learning
- Christian Szegedy, Sergey Ioffe, Vincent Vanhoucke, Alex Alemi
-
-Refer to
-https://github.com/tensorflow/models/blob/master/slim/nets/inception_v4.py
-"""
-
-
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-
-from singa.layer import Conv2D, Activation, MaxPooling2D, AvgPooling2D,\
- Split, Concat, Dropout, Flatten, Dense, BatchNormalization
-
-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)
- net.add(BatchNormalization('%s/BatchNorm' % name))
- return net.add(Activation(name+'/relu'))
-
-
-def block_inception_a(name, 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):
- """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):
- """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):
- """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):
- """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):
- """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',
- '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.
-
- Raises:
- ValueError: if final_endpoint is not set to one of the predefined values,
- """
- end_points = {}
- net = ffnet.FeedForwardNet()
- def add_and_check_final(name, lyr):
- end_points[name] = lyr
- return name == final_endpoint
-
- # 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))
- # 149 x 149 x 32
- conv2d(net, '%s/Conv2d_2a_3x3' % name, 32, 3, padding='VALID')
- # 147 x 147 x 32
- conv2d(net, '%s/Conv2d_2b_3x3' % name, 64, 3)
- # 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])
-
- # 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])
-
- # 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
-
- # 35 x 35 x 384
- # Reduction-A block
- block_reduction_a(name + '/Mixed_6a', net)
-
- # 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))
-
- # 17 x 17 x 1024
- # Reduction-B block
- block_reduction_b(name + '/Mixed_7a', net)
-
- # 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
-
-
-def create_net(num_classes=1001, sample_shape=(3, 299, 299), is_training=True, dropout_keep_prob=0.8, create_aux_logits=True):
- """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.
-
- Returns:
- logits: the logits outputs of the model.
- end_points: the set of end_points from the inception model.
- """
- 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)
- # Auxiliary Head logits
- if aux_name 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))
-
- # 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)
- # 1 x 1 x 1536
- net.add(Dropout('%s/Logits/Dropout_1b' % name, 1 - dropout_keep_prob))
- net.add(Flatten('%s/Logits/PreLogitsFlatten' % name))
- # 1536
- end_points['Logits'] = net.add(Dense('%s/Logits/Logits' % name, num_classes))
- return net, end_points
-
-
-if __name__ == '__main__':
- net, _ = create_net()