1 读取文件
import urllib
import os
import sys
import tarfile
import glob
import pickle
import numpy as np
import cv2
def download_and_uncompress_tarball(tarball_url, dataset_dir):
"""Downloads the `tarball_url` and uncompresses it locally.
Args:
tarball_url: The URL of a tarball file.
dataset_dir: The directory where the temporary files are stored.
"""
filename = tarball_url.split('/')[-1]
filepath = os.path.join(dataset_dir, filename)
def _progress(count, block_size, total_size):
sys.stdout.write('\r>> Downloading %s %.1f%%' % (
filename, float(count * block_size) / float(total_size) * 100.0))
sys.stdout.flush()
filepath, _ = urllib.request.urlretrieve(tarball_url, filepath, _progress)
print()
statinfo = os.stat(filepath)
print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
tarfile.open(filepath, 'r:gz').extractall(dataset_dir)
classification = ['airplane',
'automobile',
'bird',
'cat',
'deer',
'dog',
'frog',
'horse',
'ship',
'truck']
def unpickle(file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
DATA_URL = 'http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz'
DATA_DIR = 'data'
folders = r'E:\zhuomian\tf_read_write\data_manager/data/cifar-10-batches-py'
trfiles = glob.glob(folders + "/data_batch*")
data = []
labels = []
for file in trfiles:
dt = unpickle(file)
print(dt)
data += list(dt[b"data"])
labels += list(dt[b"labels"])
imgs = np.reshape(data, [-1, 3, 32, 32])
for i in range(imgs.shape[0]):
im_data = imgs[i, ...]
im_data = np.transpose(im_data, [1, 2, 0])
im_data = cv2.cvtColor(im_data, cv2.COLOR_RGB2BGR)
f = "{}/{}".format(r"E:\zhuomian\tf_read_write\data_manager/data/image/train", classification[labels[i]])
if not os.path.exists(f):
os.mkdir(f)
cv2.imwrite("{}/{}.jpg".format(f, str(i)), im_data)
2.图片写入
import tensorflow as tf
import cv2
import numpy as np
classification = ['airplane',
'automobile',
'bird',
'cat',
'deer',
'dog',
'frog',
'horse',
'ship',
'truck']
import glob
idx = 0
im_data = []
im_labels = []
for path in classification:
path = r"E:\zhuomian\tf_read_write\data_manager/data/image/train" + path
im_list = glob.glob(path + "/*")
im_label = [idx for i in range(im_list.__len__())]
idx += 1
im_data += im_list
im_labels += im_label
print(im_labels)
print(im_data)
3 然后遇到了tensorflow安不上
- 首先pip说找不到tensorflow的包
- 然后我去查了下是自己python是32位,然后就要换64位的
- 我发现自己用的是Anaconda32位
- 我就去安装Anaconda64位,更改环境变量
- 以为可以开心安装,但是download的速度太慢了
- 我就去找了豆瓣源的安装tensorflow
pip install -i https://pypi.doubanio.com/simple/ tensorflow
Found existing installation: wrapt 1.10.11
ERROR: Cannot uninstall 'wrapt'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
pip install -U --ignore-installed wrapt enum34 simplejson netaddr