forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NNVM] Add symbol squeezenet (apache#1436)
- Loading branch information
1 parent
6b8d0c0
commit c19cf6f
Showing
10 changed files
with
235 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# 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. | ||
|
||
# coding: utf-8 | ||
# pylint: disable=unused-argument | ||
|
||
""" | ||
Symbol of SqueezeNet | ||
Reference: | ||
Iandola, Forrest N., et al. | ||
"Squeezenet: Alexnet-level accuracy with 50x fewer parameters and< 0.5 mb model size." (2016). | ||
""" | ||
|
||
from .. import symbol as sym | ||
from . utils import create_workload | ||
|
||
# Helpers | ||
def _make_fire(net, squeeze_channels, expand1x1_channels, expand3x3_channels): | ||
net = _make_fire_conv(net, squeeze_channels, 1, 0) | ||
|
||
left = _make_fire_conv(net, expand1x1_channels, 1, 0) | ||
right = _make_fire_conv(net, expand3x3_channels, 3, 1) | ||
# NOTE : Assume NCHW layout here | ||
net = sym.concatenate(left, right, axis=1) | ||
|
||
return net | ||
|
||
def _make_fire_conv(net, channels, kernel_size, padding=0): | ||
net = sym.conv2d(net, channels=channels, kernel_size=(kernel_size, kernel_size), | ||
padding=(padding, padding)) | ||
net = sym.relu(net) | ||
return net | ||
|
||
# Net | ||
def get_symbol(num_classes, version, **kwargs): | ||
"""Get symbol of SqueezeNet | ||
Parameters | ||
---------- | ||
num_classes: int | ||
The number of classification results | ||
version : str, optional | ||
"1.0" or "1.1" of SqueezeNet | ||
""" | ||
assert version in ['1.0', '1.1'], ("Unsupported SqueezeNet version {version}:" | ||
"1.0 or 1.1 expected".format(version=version)) | ||
net = sym.Variable("data") | ||
if version == '1.0': | ||
net = sym.conv2d(net, channels=96, kernel_size=(7, 7), strides=(2, 2), padding=(3, 3)) | ||
net = sym.relu(net) | ||
net = sym.max_pool2d(net, pool_size=(3, 3), strides=(2, 2)) | ||
net = _make_fire(net, 16, 64, 64) | ||
net = _make_fire(net, 16, 64, 64) | ||
net = _make_fire(net, 32, 128, 128) | ||
net = sym.max_pool2d(net, pool_size=(3, 3), strides=(2, 2)) | ||
net = _make_fire(net, 32, 128, 128) | ||
net = _make_fire(net, 48, 192, 192) | ||
net = _make_fire(net, 48, 192, 192) | ||
net = _make_fire(net, 64, 256, 256) | ||
net = sym.max_pool2d(net, pool_size=(3, 3), strides=(2, 2)) | ||
net = _make_fire(net, 64, 256, 256) | ||
else: | ||
net = sym.conv2d(net, channels=64, kernel_size=(3, 3), strides=(2, 2), padding=(1, 1)) | ||
net = sym.relu(net) | ||
net = sym.max_pool2d(net, pool_size=(3, 3), strides=(2, 2)) | ||
net = _make_fire(net, 16, 64, 64) | ||
net = _make_fire(net, 16, 64, 64) | ||
net = sym.max_pool2d(net, pool_size=(3, 3), strides=(2, 2)) | ||
net = _make_fire(net, 32, 128, 128) | ||
net = _make_fire(net, 32, 128, 128) | ||
net = sym.max_pool2d(net, pool_size=(3, 3), strides=(2, 2)) | ||
net = _make_fire(net, 48, 192, 192) | ||
net = _make_fire(net, 48, 192, 192) | ||
net = _make_fire(net, 64, 256, 256) | ||
net = _make_fire(net, 64, 256, 256) | ||
net = sym.dropout(net, rate=0.5) | ||
net = sym.conv2d(net, channels=num_classes, kernel_size=(1, 1)) | ||
net = sym.relu(net) | ||
net = sym.global_avg_pool2d(net) | ||
net = sym.flatten(net) | ||
return sym.softmax(net) | ||
|
||
def get_workload(batch_size=1, num_classes=1000, version='1.0', | ||
image_shape=(3, 224, 224), dtype="float32", **kwargs): | ||
"""Get benchmark workload for resnet | ||
Parameters | ||
---------- | ||
batch_size : int | ||
The batch size used in the model | ||
num_classes : int, optional | ||
Number of classes | ||
version : str, optional | ||
"1.0" or "1.1" of SqueezeNet | ||
image_shape : tuple, optional | ||
The input image shape | ||
dtype : str, optional | ||
The data type | ||
kwargs : dict | ||
Extra arguments | ||
Returns | ||
------- | ||
net : nnvm.Symbol | ||
The computational graph | ||
params : dict of str to NDArray | ||
The parameters. | ||
""" | ||
net = get_symbol(num_classes=num_classes, version=version, **kwargs) | ||
return create_workload(net, batch_size, image_shape, dtype) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
""" | ||
Symbol of SqueezeNet | ||
Reference: | ||
Iandola, Forrest N., et al. | ||
"Squeezenet: Alexnet-level accuracy with 50x fewer parameters and< 0.5 mb model size." (2016). | ||
""" | ||
|
||
import mxnet as mx | ||
|
||
# Helpers | ||
def _make_fire(net, squeeze_channels, expand1x1_channels, expand3x3_channels): | ||
net = _make_fire_conv(net, squeeze_channels, 1, 0) | ||
|
||
left = _make_fire_conv(net, expand1x1_channels, 1, 0) | ||
right = _make_fire_conv(net, expand3x3_channels, 3, 1) | ||
# NOTE : Assume NCHW layout here | ||
net = mx.sym.concat(left, right, dim=1) | ||
|
||
return net | ||
|
||
def _make_fire_conv(net, channels, kernel_size, padding=0): | ||
net = mx.sym.Convolution(net, num_filter=channels, kernel=(kernel_size, kernel_size), | ||
pad=(padding, padding)) | ||
net = mx.sym.Activation(net, act_type='relu') | ||
return net | ||
|
||
# Net | ||
def get_symbol(num_classes=1000, version='1.0', **kwargs): | ||
"""Get symbol of SqueezeNet | ||
Parameters | ||
---------- | ||
num_classes: int | ||
The number of classification results | ||
version : str, optional | ||
"1.0" or "1.1" of SqueezeNet | ||
""" | ||
assert version in ['1.0', '1.1'], ("Unsupported SqueezeNet version {version}:" | ||
"1.0 or 1.1 expected".format(version=version)) | ||
net = mx.sym.Variable("data") | ||
if version == '1.0': | ||
net = mx.sym.Convolution(net, num_filter=96, kernel=(7, 7), stride=(2, 2), pad=(3, 3)) | ||
net = mx.sym.Activation(net, act_type='relu') | ||
net = mx.sym.Pooling(data=net, kernel=(3, 3), pool_type='max', stride=(2, 2)) | ||
net = _make_fire(net, 16, 64, 64) | ||
net = _make_fire(net, 16, 64, 64) | ||
net = _make_fire(net, 32, 128, 128) | ||
net = mx.sym.Pooling(data=net, kernel=(3, 3), pool_type='max', stride=(2, 2)) | ||
net = _make_fire(net, 32, 128, 128) | ||
net = _make_fire(net, 48, 192, 192) | ||
net = _make_fire(net, 48, 192, 192) | ||
net = _make_fire(net, 64, 256, 256) | ||
net = mx.sym.Pooling(data=net, kernel=(3, 3), pool_type='max', stride=(2, 2)) | ||
net = _make_fire(net, 64, 256, 256) | ||
else: | ||
net = mx.sym.Convolution(net, num_filter=64, kernel=(3, 3), stride=(2, 2), pad=(1, 1)) | ||
net = mx.sym.Activation(net, act_type='relu') | ||
net = mx.sym.Pooling(data=net, kernel=(3, 3), pool_type='max', stride=(2, 2)) | ||
net = _make_fire(net, 16, 64, 64) | ||
net = _make_fire(net, 16, 64, 64) | ||
net = mx.sym.Pooling(data=net, kernel=(3, 3), pool_type='max', stride=(2, 2)) | ||
net = _make_fire(net, 32, 128, 128) | ||
net = _make_fire(net, 32, 128, 128) | ||
net = mx.sym.Pooling(data=net, kernel=(3, 3), pool_type='max', stride=(2, 2)) | ||
net = _make_fire(net, 48, 192, 192) | ||
net = _make_fire(net, 48, 192, 192) | ||
net = _make_fire(net, 64, 256, 256) | ||
net = _make_fire(net, 64, 256, 256) | ||
net = mx.sym.Dropout(net, p=0.5) | ||
net = mx.sym.Convolution(net, num_filter=num_classes, kernel=(1, 1)) | ||
net = mx.sym.Activation(net, act_type='relu') | ||
net = mx.sym.Pooling(data=net, global_pool=True, kernel=(13, 13), pool_type='avg') | ||
net = mx.sym.flatten(net) | ||
return mx.sym.softmax(net) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters