def download_model(self):
     """Download model from url."""
     cache_dir = Test.cache_dir
     if not os.path.exists(cache_dir):
         os.makedirs(cache_dir)
     url = self.url
     if url.startswith(r'module://'):
         return self.download_from_module()
     k = url.rfind('/')
     fname = self.url[k + 1:]
     dir_name = fname + "_dir"
     ftype = None
     if url.endswith(".tar.gz") or url.endswith(".tgz"):
         ftype = 'tgz'
         dir_name = fname.replace(".tar.gz", "").replace(".tgz", "")
     elif url.endswith('.zip'):
         ftype = 'zip'
         dir_name = fname.replace(".zip", "")
     dir_name = os.path.join(cache_dir, dir_name)
     os.makedirs(dir_name, exist_ok=True)
     fpath = os.path.join(dir_name, fname)
     if not os.path.exists(fpath):
         utils.get_url(url, fpath)
     model_path = os.path.join(dir_name, self.local)
     if not os.path.exists(model_path) or self.local == ".":
         if ftype == 'tgz':
             tar = tarfile.open(fpath)
             tar.extractall(dir_name)
             tar.close()
         elif ftype == 'zip':
             zip_ref = zipfile.ZipFile(fpath, 'r')
             zip_ref.extractall(dir_name)
             zip_ref.close()
     return fpath, dir_name
示例#2
0
def download_tflite(url, dest, verbose=True):
    """
    Downloads a model from tfhub.
    The function assumes the format is `.tflite`.
    """
    if not os.path.exists(dest):
        os.makedirs(dest)
    fpath = os.path.join(dest, "model.tflite")
    if not os.path.exists(fpath):
        from tf2onnx import utils
        if verbose:
            print("Download %r." % fpath)
        utils.get_url(url, fpath)
    return fpath
def download_model(url, dest, verbose=True):
    """
    Downloads a model from tfhub and unzips it.
    The function assumes the format is `.tar.gz`.
    """
    if not os.path.exists(dest):
        os.makedirs(dest)
    fpath = os.path.join(dest, "model.tar.gz")
    if not os.path.exists(fpath):
        if verbose:
            print("Download %r." % fpath)
        utils.get_url(url, fpath)
    tname = os.path.join(dest, "model_path")
    if not os.path.exists(tname):
        if verbose:
            print("Untar %r." % tname)
        tar = tarfile.open(fpath)
        tar.extractall(tname)
        tar.close()
    return fpath, tname