1.下载数据
下载地址:http://data.csail.mit.edu/places/ADEchallenge/ADEChallengeData2016.zip
from urllib import request
#回调函数,当连接上服务器、以及相应的数据块传输完毕的时候会触发该回调。我们可以利用这个回调函数来显示当前的下载进度。
def _progress(count, block_size, total_size):
#sys.stdout.write等价于print
sys.stdout.write(
'\r>> Downloading %s %.1f%%' % (filename, float(count * block_size) / float(total_size) * 100.0))
sys.stdout.flush()
#下载指定url中的文件
filepath, _ = urllib.request.urlretrieve(url_name, filepath, reporthook=_progress)
2.解压压缩包
1.解压zip文件
import zipfile
#压缩包解压
with zipfile.ZipFile(filepath) as zf:
zip_dir = zf.namelist()[0]
zf.extractall(dir_path)
2.解压tar文件
import tarfile
tarfile.open(filepath, 'r:gz').extractall(dir_path)
3.生成训练的数据
3.1生成文件名列表
image——训练数据的地址,annotation——目标图像的地址,filename——文件名
{'training':[{'image': f, 'annotation': annotation_file, 'filename': filename},....
...,{'image': f, 'annotation': annotation_file, 'filename': filename}],
'validation':[{'image': f, 'annotation': annotation_file, 'filename': filename},...
....,{'image': f, 'annotation': annotation_file, 'filename': filename}]
}
3.2读取图像
import scipy.misc as misc
image = misc.imread(filename)
3.3生成batch数据
images[start:end], annotations[start:end]
3.3.1从images,annotations列表中从头截取一个batch_size的数据,start=0,end=batch_size
3.3.2继续获取后边batch_size的数据 ,start=end,end=end+batch_size
3.3.3当end超出images的大小后,打乱images后执行3.3.1