Skip to content

Commit

Permalink
[Format] Convert all Python code w/o CI (apache#6448)
Browse files Browse the repository at this point in the history
* Add black setup

* Tweak pyproject.toml

* Fix syntax issues

* Fix

* Tweak

* Black all Python code
  • Loading branch information
jroesch authored Sep 11, 2020
1 parent 01460e0 commit f13fed5
Show file tree
Hide file tree
Showing 1,013 changed files with 60,654 additions and 43,142 deletions.
76 changes: 46 additions & 30 deletions apps/android_camera/models/prepare_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,52 +28,62 @@
from tvm.contrib import util, ndk, graph_runtime as runtime
from tvm.contrib.download import download_testdata, download

target = 'llvm -mtriple=arm64-linux-android'
target = "llvm -mtriple=arm64-linux-android"
target_host = None


def del_dir(target: Union[Path, str], only_if_empty: bool = False):
target = Path(target).expanduser()
assert target.is_dir()
for p in sorted(target.glob('**/*'), reverse=True):
for p in sorted(target.glob("**/*"), reverse=True):
if not p.exists():
continue
p.chmod(0o666)
if p.is_dir():
p.rmdir()
else:
if only_if_empty:
raise RuntimeError(f'{p.parent} is not empty!')
raise RuntimeError(f"{p.parent} is not empty!")
p.unlink()
target.rmdir()


def get_model(model_name, batch_size=1):
if model_name == 'resnet18_v1':
if model_name == "resnet18_v1":
import mxnet as mx
from mxnet import gluon
from mxnet.gluon.model_zoo import vision

gluon_model = vision.get_model(model_name, pretrained=True)
img_size = 224
data_shape = (batch_size, 3, img_size, img_size)
net, params = relay.frontend.from_mxnet(gluon_model, {"data": data_shape})
return (net, params)
elif model_name == 'mobilenet_v2':
elif model_name == "mobilenet_v2":
import keras
from keras.applications.mobilenet_v2 import MobileNetV2

keras.backend.clear_session() # Destroys the current TF graph and creates a new one.
weights_url = ''.join(['https://github.com/JonathanCMitchell/',
'mobilenet_v2_keras/releases/download/v1.1/',
'mobilenet_v2_weights_tf_dim_ordering_tf_kernels_0.5_224.h5'])
weights_file = 'mobilenet_v2_weights.h5'
weights_path = download_testdata(weights_url, weights_file, module='keras')
keras_mobilenet_v2 = MobileNetV2(alpha=0.5, include_top=True, weights=None,
input_shape=(224, 224, 3), classes=1000)
weights_url = "".join(
[
"https://github.com/JonathanCMitchell/",
"mobilenet_v2_keras/releases/download/v1.1/",
"mobilenet_v2_weights_tf_dim_ordering_tf_kernels_0.5_224.h5",
]
)
weights_file = "mobilenet_v2_weights.h5"
weights_path = download_testdata(weights_url, weights_file, module="keras")
keras_mobilenet_v2 = MobileNetV2(
alpha=0.5, include_top=True, weights=None, input_shape=(224, 224, 3), classes=1000
)
keras_mobilenet_v2.load_weights(weights_path)

img_size = 224
data_shape = (batch_size, 3, img_size, img_size)
mod, params = relay.frontend.from_keras(keras_mobilenet_v2, {'input_1': data_shape})
mod, params = relay.frontend.from_keras(keras_mobilenet_v2, {"input_1": data_shape})
return (mod, params)


def main(model_str, output_path):
if output_path.exists():
del_dir(output_path)
Expand All @@ -90,34 +100,40 @@ def main(model_str, output_path):
with tvm.transform.PassContext(opt_level=3):
graph, lib, params = relay.build(net, target, target_host=target_host, params=params)
print("dumping lib...")
lib.export_library(output_path_str + '/' + 'deploy_lib_cpu.so', ndk.create_shared)
lib.export_library(output_path_str + "/" + "deploy_lib_cpu.so", ndk.create_shared)
print("dumping graph...")
with open(output_path_str + '/' + 'deploy_graph.json', 'w') as f:
with open(output_path_str + "/" + "deploy_graph.json", "w") as f:
f.write(graph)
print("dumping params...")
with open(output_path_str + '/' + 'deploy_param.params', 'wb') as f:
with open(output_path_str + "/" + "deploy_param.params", "wb") as f:
f.write(relay.save_param_dict(params))
print("dumping labels...")
synset_url = ''.join(['https://gist.githubusercontent.com/zhreshold/',
'4d0b62f3d01426887599d4f7ede23ee5/raw/',
'596b27d23537e5a1b5751d2b0481ef172f58b539/',
'imagenet1000_clsid_to_human.txt'])
synset_path = output_path_str + '/image_net_labels'
download(synset_url, output_path_str + '/image_net_labels')
synset_url = "".join(
[
"https://gist.githubusercontent.com/zhreshold/",
"4d0b62f3d01426887599d4f7ede23ee5/raw/",
"596b27d23537e5a1b5751d2b0481ef172f58b539/",
"imagenet1000_clsid_to_human.txt",
]
)
synset_path = output_path_str + "/image_net_labels"
download(synset_url, output_path_str + "/image_net_labels")
with open(synset_path) as fi:
synset = eval(fi.read())
with open(output_path_str + '/image_net_labels.json', "w") as fo:
with open(output_path_str + "/image_net_labels.json", "w") as fo:
json.dump(synset, fo, indent=4)
os.remove(synset_path)

if __name__ == '__main__':
if environ.get('TVM_NDK_CC') is None:

if __name__ == "__main__":
if environ.get("TVM_NDK_CC") is None:
raise RuntimeError("Require environment variable TVM_NDK_CC")
models_path = Path().absolute().parent.joinpath('app/src/main/assets/models/')
models_path = Path().absolute().parent.joinpath("app/src/main/assets/models/")
if not models_path.exists():
models_path.mkdir()
models = {'mobilenet_v2': models_path.joinpath('mobilenet_v2'),
'resnet18_v1': models_path.joinpath('resnet18_v1')
}
models = {
"mobilenet_v2": models_path.joinpath("mobilenet_v2"),
"resnet18_v1": models_path.joinpath("resnet18_v1"),
}
for model, output_path in models.items():
main(model, output_path)
20 changes: 10 additions & 10 deletions apps/android_rpc/tests/android_rpc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@
# whether enable to execute test on Vulkan target
test_vulkan = False


def test_rpc_module():
# graph
n = tvm.runtime.convert(1024)
A = te.placeholder((n,), name='A')
B = te.compute(A.shape, lambda *i: A(*i) + 1.0, name='B')
A = te.placeholder((n,), name="A")
B = te.compute(A.shape, lambda *i: A(*i) + 1.0, name="B")
a_np = np.random.uniform(size=1024).astype(A.dtype)
temp = util.tempdir()

# Establish remote connection with target hardware
tracker = rpc.connect_tracker(tracker_host, tracker_port)
remote = tracker.request(key, priority=0,
session_timeout=60)
remote = tracker.request(key, priority=0, session_timeout=60)

# Compile the Graph for CPU target
s = te.create_schedule(B.op)
Expand All @@ -67,15 +67,15 @@ def test_rpc_module():
f.export_library(path_dso_cpu, ndk.create_shared)

# Execute the portable graph on cpu target
print('Run CPU test ...')
print("Run CPU test ...")
ctx = remote.cpu(0)
remote.upload(path_dso_cpu)
f2 = remote.load_module("cpu_lib.so")
a = tvm.nd.array(a_np, ctx)
b = tvm.nd.array(np.zeros(1024, dtype=A.dtype), ctx)
time_f = f2.time_evaluator(f2.entry_name, ctx, number=10)
cost = time_f(a, b).mean
print('%g secs/op\n' % cost)
print("%g secs/op\n" % cost)
np.testing.assert_equal(b.asnumpy(), a.asnumpy() + 1)

# Compile the Graph for OpenCL target
Expand All @@ -90,15 +90,15 @@ def test_rpc_module():
path_dso_cl = temp.relpath("dev_lib_cl.so")
f.export_library(path_dso_cl, ndk.create_shared)

print('Run GPU(OpenCL Flavor) test ...')
print("Run GPU(OpenCL Flavor) test ...")
ctx = remote.cl(0)
remote.upload(path_dso_cl)
f1 = remote.load_module("dev_lib_cl.so")
a = tvm.nd.array(a_np, ctx)
b = tvm.nd.array(np.zeros(1024, dtype=A.dtype), ctx)
time_f = f1.time_evaluator(f1.entry_name, ctx, number=10)
cost = time_f(a, b).mean
print('%g secs/op\n' % cost)
print("%g secs/op\n" % cost)
np.testing.assert_equal(b.asnumpy(), a.asnumpy() + 1)

# Compile the Graph for Vulkan target
Expand All @@ -113,15 +113,15 @@ def test_rpc_module():
path_dso_vulkan = temp.relpath("dev_lib_vulkan.so")
f.export_library(path_dso_vulkan, ndk.create_shared)

print('Run GPU(Vulkan Flavor) test ...')
print("Run GPU(Vulkan Flavor) test ...")
ctx = remote.vulkan(0)
remote.upload(path_dso_vulkan)
f1 = remote.load_module("dev_lib_vulkan.so")
a = tvm.nd.array(a_np, ctx)
b = tvm.nd.array(np.zeros(1024, dtype=A.dtype), ctx)
time_f = f1.time_evaluator(f1.entry_name, ctx, number=10)
cost = time_f(a, b).mean
print('%g secs/op\n' % cost)
print("%g secs/op\n" % cost)
np.testing.assert_equal(b.asnumpy(), a.asnumpy() + 1)


Expand Down
54 changes: 35 additions & 19 deletions apps/benchmark/arm_cpu_imagenet_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ def evaluate_network(network, target, target_host, repeat):

print_progress("%-20s building..." % network)
with tvm.transform.PassContext(opt_level=3):
graph, lib, params = relay.build(
net, target=target, target_host=target_host, params=params)
graph, lib, params = relay.build(net, target=target, target_host=target_host, params=params)

tmp = tempdir()
if 'android' in str(target):
if "android" in str(target):
from tvm.contrib import ndk

filename = "%s.so" % network
lib.export_library(tmp.relpath(filename), ndk.create_shared)
else:
Expand All @@ -60,38 +60,55 @@ def evaluate_network(network, target, target_host, repeat):
rlib = remote.load_module(filename)
module = runtime.create(graph, rlib, ctx)
data_tvm = tvm.nd.array((np.random.uniform(size=input_shape)).astype(dtype))
module.set_input('data', data_tvm)
module.set_input("data", data_tvm)
module.set_input(**params)

# evaluate
print_progress("%-20s evaluating..." % network)
ftimer = module.module.time_evaluator("run", ctx, number=1, repeat=repeat)
prof_res = np.array(ftimer().results) * 1000 # multiply 1000 for converting to millisecond
print("%-20s %-19s (%s)" % (network, "%.2f ms" % np.mean(prof_res), "%.2f ms" % np.std(prof_res)))
print(
"%-20s %-19s (%s)" % (network, "%.2f ms" % np.mean(prof_res), "%.2f ms" % np.std(prof_res))
)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--network", type=str, choices=
['resnet-18', 'resnet-34', 'resnet-50',
'vgg-16', 'vgg-19', 'densenet-121', 'inception_v3',
'mobilenet', 'squeezenet_v1.0', 'squeezenet_v1.1'],
help='The name of neural network')
parser.add_argument("--model", type=str, choices=
['rk3399', 'mate10', 'mate10pro', 'p20', 'p20pro',
'pixel2', 'rasp3b', 'pynq'], default='rk3399',
help="The model of the test device. If your device is not listed in "
"the choices list, pick the most similar one as argument.")
parser.add_argument("--host", type=str, default='localhost')
parser.add_argument(
"--network",
type=str,
choices=[
"resnet-18",
"resnet-34",
"resnet-50",
"vgg-16",
"vgg-19",
"densenet-121",
"inception_v3",
"mobilenet",
"squeezenet_v1.0",
"squeezenet_v1.1",
],
help="The name of neural network",
)
parser.add_argument(
"--model",
type=str,
choices=["rk3399", "mate10", "mate10pro", "p20", "p20pro", "pixel2", "rasp3b", "pynq"],
default="rk3399",
help="The model of the test device. If your device is not listed in "
"the choices list, pick the most similar one as argument.",
)
parser.add_argument("--host", type=str, default="localhost")
parser.add_argument("--port", type=int, default=9190)
parser.add_argument("--rpc-key", type=str, required=True)
parser.add_argument("--repeat", type=int, default=10)
args = parser.parse_args()

dtype = 'float32'
dtype = "float32"

if args.network is None:
networks = ['squeezenet_v1.1', 'mobilenet', 'resnet-18', 'vgg-16']
networks = ["squeezenet_v1.1", "mobilenet", "resnet-18", "vgg-16"]
else:
networks = [args.network]

Expand All @@ -103,4 +120,3 @@ def evaluate_network(network, target, target_host, repeat):
print("--------------------------------------------------")
for network in networks:
evaluate_network(network, target, target_host, args.repeat)

Loading

0 comments on commit f13fed5

Please sign in to comment.