解决MNIST数据无法通过网络加载问题

在做深度学习的过程中,MNIST是我们初学者常用到的数据集
通常在很多示例代码中仅通过

1
2
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

这两句代码即可加载
但是在很多情况下由于网络被墙原因(PS:需要到https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz下载数据集)或者Python sll模块并不支持HTTPS
错误代码如下:

1
2
3
4
5
6
7
8
Traceback (most recent call last):
File "D:/workplace/python/tf2_learn/tf2_1.py", line 20, in <module>
(x_train, y_train), (x_test, y_test) = mnist.load_data()
File "D:\Application\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\keras\datasets\mnist.py", line 49, in load_data
file_hash='8a61469f7ea1b51cbae51d4f78837e45')
File "D:\Application\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\keras\utils\data_utils.py", line 255, in get_file
raise Exception(error_msg.format(origin, e.errno, e.reason))
Exception: URL fetch failure on https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz: None -- unknown url type: https

以上问题可通过本地导入的方式解决:
1.手动下载数据集
MNIST下载地址
2.放到你的python项目里面,路径随意
3.在代码中做如下配置:

1
2
3
4
5
6
7
8
#配置你MNIST所在路径
path = "MNIST_data/mnist.npz"

#加载mnist数据
f = np.load(path)
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
f.close()