Skip to content

Instantly share code, notes, and snippets.

@nudles
Created February 9, 2017 02:22
Show Gist options
  • Save nudles/c86c163f47bfbdf38c9b9bf21f926c63 to your computer and use it in GitHub Desktop.
Save nudles/c86c163f47bfbdf38c9b9bf21f926c63 to your computer and use it in GitHub Desktop.
# 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.
# =============================================================================
''' This model is created following the structure from
https://github.com/soumith/convnet-benchmarks/blob/master/caffe/imagenet_winners/vgg_a.prototxt
'''
from timeit import timeit as timer
import argparse
from singa import device
from singa import tensor
from singa import optimizer
from singa import layer
from singa import loss
from singa import metric
from singa import net as ffnet
# ffnet.verbose=True
def create_net(input_shape, use_cpu=False, use_ocl=False):
if use_cpu:
layer.engine = 'singacpp'
if use_ocl:
layer.engine = 'singacl'
net = ffnet.FeedForwardNet(loss.SoftmaxCrossEntropy(), metric.Accuracy())
net.add(layer.Conv2D("conv1/3x3_s1", 64, 3, 1, pad=1,
input_sample_shape=input_shape))
net.add(layer.Activation("conv1/relu"))
net.add(layer.MaxPooling2D("pool1/2x2_s2", 2, 2, border_mode='valid'))
net.add(layer.Conv2D("conv2/3x3_s1", 128, 3, 1, pad=1))
net.add(layer.Activation("conv2/relu"))
net.add(layer.MaxPooling2D("pool2/2x2_s2", 2, 2, border_mode='valid'))
net.add(layer.Conv2D("conv3/3x3_s1", 256, 3, 1, pad=1))
net.add(layer.Activation("conv3/relu"))
# No pooling layer here.
net.add(layer.Conv2D("conv4/3x3_s1", 256, 3, 1, pad=1))
net.add(layer.Activation("conv4/relu"))
net.add(layer.MaxPooling2D("pool3/2x2_s2", 2, 2, border_mode='valid'))
net.add(layer.Conv2D("conv5/3x3_s1", 512, 3, 1, pad=1))
net.add(layer.Activation("conv5/relu"))
# No pooling layer here.
net.add(layer.Conv2D("conv6/3x3_s1", 512, 3, 1, pad=1))
net.add(layer.Activation("conv6/relu"))
net.add(layer.MaxPooling2D("pool4/2x2_s2", 2, 2, border_mode='valid'))
net.add(layer.Conv2D("conv7/3x3_s1", 512, 3, 1, pad=1))
net.add(layer.Activation("conv7/relu"))
# No pooling layer here.
net.add(layer.Conv2D("conv8/3x3_s1", 512, 3, 1, pad=1))
net.add(layer.Activation("conv8/relu"))
net.add(layer.MaxPooling2D("pool5/2x2_s2", 2, 2, border_mode='valid'))
net.add(layer.Flatten('flat'))
net.add(layer.Dense("fc6", 4096))
net.add(layer.Dense("fc7", 4096))
net.add(layer.Dense("fc8", 1000))
for (val, spec) in zip(net.param_values(), net.param_specs()):
if len(val.shape) > 1:
val.gaussian(0, 0.01)
else:
val.set_value(0)
print spec.name, spec.filler.type, val.l1()
return net
def train(net, dev, num_iter=10, batch_size=128, input_shape=(3, 244, 244)):
'''Train the net for multiple iterations to measure the efficiency.
Including timer per iteration, forward, backward, parameter update and
timer for each layer.'''
tx = tensor.Tensor((batch_size,) + input_shape, dev)
ty = tensor.Tensor((batch_size,), dev)
tx.gaussian(1.0, 0.5)
ty.set_value(0.0)
opt = optimizer.SGD(momentum=0.9)
t0 = timer()
for b in range(num_iter):
print b
grads, (l, a) = net.train(tx, ty)
for (s, p, g) in zip(net.param_names(), net.param_values(), grads):
opt.apply_with_lr(0, 0.01, g, p, str(s), b)
print "Total iterations = %d" % num_iter
print "Average training time per iteration = %.4f" % (timer() - t0) / num_iter
if __name__ == '__main__':
use_cpu = False
use_opencl = True
if use_cpu:
dev = device.get_default_device()
else:
dev = device.create_opencl_device()
input_shape = (3, 244, 244,)
net = create_net(input_shape, use_cpu, use_opencl)
net.to_device(dev)
train(net, dev, input_shape=input_shape)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment