CIFAR-10 文件下载函数

函数挺有意思的,可能将来写项目会用到。

import tensorflow as tf
import os
import sys
from six.moves import urllib
import tarfile

FLAGS = tf.app.flags.FLAGS
FLAGS.data_dir = 'cifar10_data/'

DATA_URL = 'http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz'


# Download and extract the tarball from Alex's website.
def maybe_download_and_extract():
    dest_directory = FLAGS.data_dir
    if not os.path.exists(dest_directory):
        os.makedirs(dest_directory)

    filename = DATA_URL.split('/')[-1]  # 文件名
    filepath = os.path.join(dest_directory, filename)

    if not os.path.exists(filepath):
        # 文件下载函数
        def _progress(count, block_size, total_size):
            # %.1f%% -> 实数后面输出1个 %
            sys.stdout.write('\r>> Downloading %s %.1f%%' % (filename,
                                                             float(count * block_size) / float(total_size) * 100.0))
            sys.stdout.flush()  # 更新stdout

        filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress)
        print()
        statinfo = os.stat(filepath)
        print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')

    # 提取 bin 文件路径
    extracted_dir_path = os.path.join(dest_directory, 'cifar-10-batches-bin')

    if not os.path.exists(extracted_dir_path):
        tarfile.open(filepath, 'r:gz').extractall(dest_directory)  # Read from and write to tar format archives


if __name__ == '__main__':
    maybe_download_and_extract()
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,834评论 25 709
  • 今天是一周的开始,同样,也是阳历一年的开始。真快啊,又一年过去了。小时候觉得大人说一年一年过的快 是假的,自己也成...
    苏小乾阅读 222评论 0 0
  • 宇宙这是为毛啊 不知道从什么时候开始就在不停的,不停的联想这一些不属于我的东西,或者说不应属于哪个年龄的我。 小时...
    嘿艮阅读 300评论 0 0
  • 今日。立冬。 在冬天来临之际,梦到醒不来的梦。 梦,真的,不知和解。哪里有解。 我过得很好。 我真的一切都好。 只...
    我清醒的痛苦阅读 220评论 0 0
  • 我们如何才能有效的向别人传达出我们的观念,让别人印象深刻? 如何才能让别人听了讲话后,能准确执行我们的想法意图?要...
    晨风er阅读 610评论 0 1