首页 > 编程语言 > 详细

各种下载深度学习数据集方法(In python)

时间:2019-07-15 22:19:16      阅读:398      评论:0      收藏:0      [点我收藏+]

一、使用urllib下载网络数据集

 1 # -*- coding:utf-8 -*-
 2 __author__ = Leo.Z
 3 
 4 import sys
 5 import os
 6 
 7 # 给定url下载文件
 8 def download_from_url(url, dir=‘‘):
 9     _file_name = url.split(/)[-1]
10     _file_path = os.path.join(dir, _file_name)
11 
12     # 打印下载进度
13     def _progress(count, block_size, total_size):
14         sys.stdout.write(\r>> Downloading %s %.1f%% %
15                          (_file_name, float(count * block_size) / float(total_size) * 100.0))
16         sys.stdout.flush()
17 
18     # 如果不存在dir,则创建文件夹
19     if not os.path.exists(dir):
20         print("Dir is not exsit,Create it..")
21         os.makedirs(dir)
22 
23     if not os.path.exists(_file_path):
24         print("Start downloading..")
25         # 开始下载文件
26         import urllib
27         urllib.request.urlretrieve(url, _file_path, _progress)
28     else:
29         print("File already exists..")
30 
31     return _file_path
32 
33 # 使用tarfile解压缩
34 def extract(filepath, dest_dir):
35     if os.path.exists(filepath) and not os.path.exists(dest_dir):
36         import tarfile
37         tarfile.open(filepath, r:gz).extractall(dest_dir)
38 
39 
40 if __name__ == __main__:
41     FILE_URL = http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz
42     FILE_DIR = cifar10_dir/
43 
44     loaded_file_path = download_from_url(FILE_URL, FILE_DIR)
45     extract(loaded_file_path)

 

各种下载深度学习数据集方法(In python)

原文:https://www.cnblogs.com/leokale-zz/p/11191906.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!