tensorflow下载mnist数据到当前路径下的MNIST_data目录
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
网络异常,报错如下
WARNING:tensorflow:From /home/leon.lai/miniconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py:252: _internal_retry..wrap..wrapped_fn (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please use urllib or similar directly.
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
~/miniconda3/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
1317 h.request(req.get_method(), req.selector, req.data, headers,
-> 1318 encode_chunked=req.has_header('Transfer-encoding'))
1319 except OSError as err: # timeout error
~/miniconda3/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
1238 """Send a complete request to the server."""
-> 1239 self._send_request(method, url, body, headers, encode_chunked)
1240
~/miniconda3/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
1284 body = _encode(body, 'body')
-> 1285 self.endheaders(body, encode_chunked=encode_chunked)
1286
~/miniconda3/lib/python3.6/http/client.py in endheaders(self, message_body, encode_chunked)
1233 raise CannotSendHeader()
-> 1234 self._send_output(message_body, encode_chunked=encode_chunked)
1235
~/miniconda3/lib/python3.6/http/client.py in _send_output(self, message_body, encode_chunked)
1025 del self._buffer[:]
-> 1026 self.send(msg)
1027
~/miniconda3/lib/python3.6/http/client.py in send(self, data)
963 if self.auto_open:
--> 964 self.connect()
965 else:
~/miniconda3/lib/python3.6/http/client.py in connect(self)
1391
-> 1392 super().connect()
1393
~/miniconda3/lib/python3.6/http/client.py in connect(self)
935 self.sock = self._create_connection(
--> 936 (self.host,self.port), self.timeout, self.source_address)
937 self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
~/miniconda3/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
721 if err is not None:
--> 722 raise err
723 else:
~/miniconda3/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
712 sock.bind(source_address)
--> 713 sock.connect(sa)
714 return sock
OSError: [Errno 101] Network is unreachable
During handling of the above exception, another exception occurred:
URLError Traceback (most recent call last)
in ()
1 from tensorflow.examples.tutorials.mnist import input_data
----> 2 mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
~/miniconda3/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs)
304 'in a future version' if date is None else ('after %s' % date),
305 instructions)
--> 306 return func(*args, **kwargs)
307 return tf_decorator.make_decorator(
308 func, new_func, 'deprecated',
~/miniconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py in read_data_sets(train_dir, fake_data, one_hot, dtype, reshape, validation_size, seed, source_url)
259
260 local_file = base.maybe_download(TRAIN_IMAGES, train_dir,
--> 261 source_url + TRAIN_IMAGES)
262 with gfile.Open(local_file, 'rb') as f:
263 train_images = extract_images(f)
~/miniconda3/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs)
304 'in a future version' if date is None else ('after %s' % date),
305 instructions)
--> 306 return func(*args, **kwargs)
307 return tf_decorator.make_decorator(
308 func, new_func, 'deprecated',
~/miniconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py in maybe_download(filename, work_directory, source_url)
250 filepath = os.path.join(work_directory, filename)
251 if not gfile.Exists(filepath):
--> 252 temp_file_name, _ = urlretrieve_with_retry(source_url)
253 gfile.Copy(temp_file_name, filepath)
254 with gfile.GFile(filepath) as f:
~/miniconda3/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs)
304 'in a future version' if date is None else ('after %s' % date),
305 instructions)
--> 306 return func(*args, **kwargs)
307 return tf_decorator.make_decorator(
308 func, new_func, 'deprecated',
~/miniconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py in wrapped_fn(*args, **kwargs)
203 for delay in delays():
204 try:
--> 205 return fn(*args, **kwargs)
206 except Exception as e: # pylint: disable=broad-except
207 if is_retriable is None:
~/miniconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py in urlretrieve_with_retry(url, filename)
231 @_internal_retry(initial_delay=1.0, max_delay=16.0, is_retriable=_is_retriable)
232 def urlretrieve_with_retry(url, filename=None):
--> 233 return urllib.request.urlretrieve(url, filename)
234
235
~/miniconda3/lib/python3.6/urllib/request.py in urlretrieve(url, filename, reporthook, data)
246 url_type, path = splittype(url)
247
--> 248 with contextlib.closing(urlopen(url, data)) as fp:
249 headers = fp.info()
250
~/miniconda3/lib/python3.6/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
221 else:
222 opener = _opener
--> 223 return opener.open(url, data, timeout)
224
225 def install_opener(opener):
~/miniconda3/lib/python3.6/urllib/request.py in open(self, fullurl, data, timeout)
524 req = meth(req)
525
--> 526 response = self._open(req, data)
527
528 # post-process response
~/miniconda3/lib/python3.6/urllib/request.py in _open(self, req, data)
542 protocol = req.type
543 result = self._call_chain(self.handle_open, protocol, protocol +
--> 544 '_open', req)
545 if result:
546 return result
~/miniconda3/lib/python3.6/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
502 for handler in handlers:
503 func = getattr(handler, meth_name)
--> 504 result = func(*args)
505 if result is not None:
506 return result
~/miniconda3/lib/python3.6/urllib/request.py in https_open(self, req)
1359 def https_open(self, req):
1360 return self.do_open(http.client.HTTPSConnection, req,
-> 1361 context=self._context, check_hostname=self._check_hostname)
1362
1363 https_request = AbstractHTTPHandler.do_request_
~/miniconda3/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
1318 encode_chunked=req.has_header('Transfer-encoding'))
1319 except OSError as err: # timeout error
-> 1320 raise URLError(err)
1321 r = h.getresponse()
1322 except:
URLError:
原因:input_data.read_data_sets访问https://storage.googleapis.com/cvdf-datasets/mnist/被限制,需要修改read_data_sets函数,将source_url设置成http://yann.lecun.com/exdb/mnist/,修改后代码如下
@deprecated(None, 'Please use alternatives such as official/mnist/dataset.py'
' from tensorflow/models.')
def read_data_sets(train_dir,
fake_data=False,
one_hot=False,
dtype=dtypes.float32,
reshape=True,
236,20 73%
dtype=dtypes.float32,
reshape=True,
validation_size=5000,
seed=None,
source_url=DEFAULT_SOURCE_URL):
if fake_data:
def fake():
return DataSet(
[], [], fake_data=True, one_hot=one_hot, dtype=dtype, seed=seed)
train = fake()
validation = fake()
test = fake()
return base.Datasets(train=train, validation=validation, test=test)
if not source_url: # empty string check
source_url = DEFAULT_SOURCE_URL
print(source_url)
# 修改source_url
source_url = 'http://yann.lecun.com/exdb/mnist/'
TRAIN_IMAGES = 'train-images-idx3-ubyte.gz'
TRAIN_LABELS = 'train-labels-idx1-ubyte.gz'
TEST_IMAGES = 't10k-images-idx3-ubyte.gz'
TEST_LABELS = 't10k-labels-idx1-ubyte.gz'
local_file = base.maybe_download(TRAIN_IMAGES, train_dir,
source_url + TRAIN_IMAGES)
with gfile.Open(local_file, 'rb') as f:
train_images = extract_images(f)
local_file = base.maybe_download(TRAIN_LABELS, train_dir,
source_url + TRAIN_LABELS)
with gfile.Open(local_file, 'rb') as f:
train_labels = extract_labels(f, one_hot=one_hot)
local_file = base.maybe_download(TEST_IMAGES, train_dir,
source_url + TEST_IMAGES)
with gfile.Open(local_file, 'rb') as f:
test_images = extract_images(f)
local_file = base.maybe_download(TEST_LABELS, train_dir,
source_url + TEST_LABELS)
with gfile.Open(local_file, 'rb') as f:
test_labels = extract_labels(f, one_hot=one_hot)
if not 0 <= validation_size <= len(train_images):
raise ValueError('Validation size should be between 0 and {}. Received: {}.'
.format(len(train_images), validation_size))
validation_images = train_images[:validation_size]
validation_labels = train_labels[:validation_size]
train_images = train_images[validation_size:]
train_labels = train_labels[validation_size:]
options = dict(dtype=dtype, reshape=reshape, seed=seed)
235,20 95%
options = dict(dtype=dtype, reshape=reshape, seed=seed)
train = DataSet(train_images, train_labels, **options)
validation = DataSet(validation_images, validation_labels, **options)
test = DataSet(test_images, test_labels, **options)
return base.Datasets(train=train, validation=validation, test=test)
重新运行,正常!
查看MNIST_data下的新下载文件
$ ll MNIST_data
total 11336
-rw-r--r-- 1 leon.lai users 1648877 Jan 14 14:33 t10k-images-idx3-ubyte.gz
-rw-r--r-- 1 leon.lai users 4542 Jan 14 14:33 t10k-labels-idx1-ubyte.gz
-rw-r--r-- 1 leon.lai users 9912422 Jan 14 14:33 train-images-idx3-ubyte.gz
-rw-r--r-- 1 leon.lai users 28881 Jan 14 14:33 train-labels-idx1-ubyte.gz