From 08ed77c5683f35ceeef67129e78bd2906c4cd28c Mon Sep 17 00:00:00 2001 From: Thierry Moreau Date: Thu, 21 Jun 2018 14:15:00 -0700 Subject: [PATCH 1/2] tvm contrib download utility --- .../tvm/contrib}/download.py | 48 ++++++++++--------- tutorials/nnvm/deploy_ssd.py | 2 +- tutorials/nnvm/from_darknet.py | 2 +- 3 files changed, 28 insertions(+), 24 deletions(-) rename {nnvm/python/nnvm/testing => python/tvm/contrib}/download.py (61%) diff --git a/nnvm/python/nnvm/testing/download.py b/python/tvm/contrib/download.py similarity index 61% rename from nnvm/python/nnvm/testing/download.py rename to python/tvm/contrib/download.py index 849c18bcf0f6..a7b852f5408a 100644 --- a/nnvm/python/nnvm/testing/download.py +++ b/python/tvm/contrib/download.py @@ -1,4 +1,3 @@ -# pylint: disable=invalid-name, no-member, import-error, no-name-in-module, global-variable-undefined, bare-except """Helper utility for downloading""" from __future__ import print_function from __future__ import absolute_import as _abs @@ -6,7 +5,6 @@ import os import sys import time -import urllib import requests if sys.version_info >= (3,): @@ -14,21 +12,6 @@ else: import urllib2 -def _download_progress(count, block_size, total_size): - """Show the download progress. - """ - global start_time - if count == 0: - start_time = time.time() - return - duration = time.time() - start_time - progress_size = int(count * block_size) - speed = int(progress_size / (1024 * duration)) - percent = int(count * block_size * 100 / total_size) - sys.stdout.write("\r...%d%%, %d MB, %d KB/s, %d seconds passed" % - (percent, progress_size / (1024 * 1024), speed, duration)) - sys.stdout.flush() - def download(url, path, overwrite=False, size_compare=False): """Downloads the file from the internet. Set the input options correctly to overwrite or do the size comparison @@ -62,8 +45,29 @@ def download(url, path, overwrite=False, size_compare=False): print('File {} exists, skip.'.format(path)) return print('Downloading from url {} to {}'.format(url, path)) - try: - urllib.request.urlretrieve(url, path, reporthook=_download_progress) - print('') - except: - urllib.urlretrieve(url, path, reporthook=_download_progress) + + # Stateful start time + start_time = time.time() + + def _download_progress(count, block_size, total_size): + #pylint: disable=unused-argument + """Show the download progress. + """ + if count == 0: + return + duration = time.time() - start_time + progress_size = int(count * block_size) + speed = int(progress_size / (1024 * duration)) + percent = min(int(count * block_size * 100 / block_size), 100) + sys.stdout.write("\r...%d%%, %d MB, %d KB/s, %d seconds passed" % + (percent, progress_size / (1024 * 1024), speed, duration)) + sys.stdout.flush() + + if sys.version_info >= (3,): + urllib2.urlretrieve(url, path, reporthook=_download_progress) + print("") + else: + f = urllib2.urlopen(url) + data = f.read() + with open(path, "wb") as code: + code.write(data) diff --git a/tutorials/nnvm/deploy_ssd.py b/tutorials/nnvm/deploy_ssd.py index b3b5072f28c9..58725a3c92d3 100644 --- a/tutorials/nnvm/deploy_ssd.py +++ b/tutorials/nnvm/deploy_ssd.py @@ -16,7 +16,7 @@ from nnvm import compiler from nnvm.frontend import from_mxnet -from nnvm.testing.download import download +from tvm.contrib.download import download from tvm.contrib import graph_runtime from mxnet.model import load_checkpoint diff --git a/tutorials/nnvm/from_darknet.py b/tutorials/nnvm/from_darknet.py index 2cd681b624ad..883026f2af98 100644 --- a/tutorials/nnvm/from_darknet.py +++ b/tutorials/nnvm/from_darknet.py @@ -24,7 +24,7 @@ import os from ctypes import * -from nnvm.testing.download import download +from tvm.contrib.download import download from nnvm.testing.darknet import __darknetffi__ ###################################################################### From 86091d376cc5cb6aa438e771aa0f35ed3e48323e Mon Sep 17 00:00:00 2001 From: Thierry Moreau Date: Thu, 21 Jun 2018 15:06:16 -0700 Subject: [PATCH 2/2] getting the progress computation right --- python/tvm/contrib/download.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/contrib/download.py b/python/tvm/contrib/download.py index a7b852f5408a..7f761f69b252 100644 --- a/python/tvm/contrib/download.py +++ b/python/tvm/contrib/download.py @@ -58,7 +58,7 @@ def _download_progress(count, block_size, total_size): duration = time.time() - start_time progress_size = int(count * block_size) speed = int(progress_size / (1024 * duration)) - percent = min(int(count * block_size * 100 / block_size), 100) + percent = min(int(count * block_size * 100 / total_size), 100) sys.stdout.write("\r...%d%%, %d MB, %d KB/s, %d seconds passed" % (percent, progress_size / (1024 * 1024), speed, duration)) sys.stdout.flush()