有公網(wǎng)ip 如何做一網(wǎng)站北京剛剛宣布比疫情更可怕的事情
前面的待補(bǔ)充
3.6 手寫數(shù)字識(shí)別
3.6.1 MNIST 數(shù)據(jù)集
本書提供了便利的 Python 腳本 mnist.py ,該腳本支持從下載 MNIST 數(shù)據(jù)集到將這些數(shù)據(jù)轉(zhuǎn)換成 NumPy 數(shù)組等處理(mnist.py 在 dataset 目錄下)。
使用 mnist.py 時(shí),當(dāng)前目錄必須是 ch01 、ch02 、ch03、…、ch08 目錄中的一個(gè)。使用 mnist.py 中的 load_mnist() 函數(shù),就可以按下述方式輕松讀入 MNIST 數(shù)據(jù)。
實(shí)際使用中是報(bào)錯(cuò)的,提示urllib.request獲取數(shù)據(jù)失敗,檢查代碼:
from dataset.mnist import load_mnist
使用的是dataset目錄下的mnist.py中的load_mnist函數(shù)
直接查看mnist.py源碼:
?
url_base = 'http://yann.lecun.com/exdb/mnist/'
key_file = {'train_img':'train-images-idx3-ubyte.gz','train_label':'train-labels-idx1-ubyte.gz','test_img':'t10k-images-idx3-ubyte.gz','test_label':'t10k-labels-idx1-ubyte.gz'
}dataset_dir = os.path.dirname(os.path.abspath(__file__))
save_file = dataset_dir + "/mnist.pkl"
上面的代碼是把從網(wǎng)站下載的4個(gè)文件放到mnist.pkl中保存。下載代碼如下:
def _download(file_name):file_path = dataset_dir + "/" + file_nameif os.path.exists(file_path):returnurllib.request.urlretrieve(url_base + file_name, file_path)
直接運(yùn)行,下載這一步報(bào)錯(cuò)了。從瀏覽器直接下載試試
MNIST handwritten digit database, Yann LeCun, Corinna Cortes and Chris Burges
下載被拒絕,網(wǎng)上搜了一下原因,然后就更換為:
GitHub - zalandoresearch/fashion-mnist: A MNIST-like fashion product database. Benchmark
下載OK了,但里面的圖明顯不是0-9的數(shù)字,庫(kù)不太對(duì),又找了一個(gè):
vision/torchvision/datasets/mnist.py at ddad38f3a84d4d87cbb389bc78e245920fe86f82 · pytorch/vision · GitHub
https://ossci-datasets.s3.amazonaws.com/mnist/
這個(gè)庫(kù)也不太對(duì),里面顯示的應(yīng)該也是各種襯衫、裙子的識(shí)別,估計(jì)和fashion-mnist是一樣的。
最終在github上找到了原始的0-9數(shù)字?jǐn)?shù)據(jù)集:
https://github.com/geektutu/tensorflow-tutorial-samples/blob/master/mnist/data_set/ ? ? ? ? ? ? ? ? ? ? ? ?
通過(guò)瀏覽器下載到dataset目錄,就可以了。
程序下載經(jīng)常會(huì)失敗,原因眾所周知,國(guó)內(nèi)訪問github總是時(shí)靈時(shí)不靈,用瀏覽器也要多刷幾次才行(罵一句萬(wàn)惡的墻,啥時(shí)候才能拆了接軌世界)。
插曲:如果是內(nèi)網(wǎng),python下載需要在程序中設(shè)置代理。
file_path = dataset_dir + "/" + file_nameif os.path.exists(file_path):returnprint("set proxy...")# 設(shè)置代理proxies = {'http': 'http://proxy.xxx:80', 'https': 'https://proxy.xxx:80'}proxy_handler = urllib.request.ProxyHandler(proxies)# 創(chuàng)建Openeropener = urllib.request.build_opener(proxy_handler)# 安裝Openerurllib.request.install_opener(opener)print("Downloading " + file_name + " ... ")urllib.request.urlretrieve(url_base + file_name, file_path)print("Done")
下載好了dataset就可以mnist_show.py了,提示還有錯(cuò)誤:
D:\python\test\ch03>py mnist_show.py
Traceback (most recent call last):
? File "mnist_show.py", line 5, in <module>
? ? from PIL import Image
ModuleNotFoundError: No module named 'PIL'
PIL 庫(kù)沒有?
py -m pip install Pillow --proxy http://proxy.xxx.cn:80 -i https://pypi.tuna.tsinghua.edu.cn/simple
Installing collected packages: Pillow
Successfully installed Pillow-9.5.0
成功安裝Pillow后問題解決。
mnist_show.py代碼,加了注釋
import sys,os
sys.path.append(os.pardir) # 為了導(dǎo)入父目錄的文件而進(jìn)行的設(shè)定,這里要用dataset目錄下的mnist.py文件
import numpy as np
from dataset.mnist import load_mnist # 導(dǎo)入mnist.py文件中的load_mnist函數(shù)
from PIL import Image # 這個(gè)包需要單獨(dú)安裝,py -m pip install Pillow --proxy http://proxy.xxx.cn:80 -i https://pypi.tuna.tsinghua.edu.cn/simple def img_show(img):pil_img = Image.fromarray(np.uint8(img)) # 把保存為 NumPy 數(shù)組的圖像數(shù)據(jù)轉(zhuǎn)換為 PIL 用的數(shù)據(jù)對(duì)象pil_img.show()# 第一次加載需要幾分鐘,load_mnist是從網(wǎng)上下載,如前所述,直接手工下載到dataset目錄就行了,不需要執(zhí)行下載,這樣就快了
# load_mnist 函數(shù)以“( 訓(xùn)練圖像, 訓(xùn)練標(biāo)簽 ),( 測(cè)試圖像, 測(cè)試標(biāo)簽 ) ”的形式返回讀入的 MNIST 數(shù)據(jù)
(x_train, t_train),(x_test, t_test) = load_mnist(flatten=True, # flatten=True 時(shí)讀入的圖像是以784個(gè)元素構(gòu)成的一維數(shù)組的形式保存的。因此,顯示圖像時(shí),需要把它變?yōu)樵瓉?lái)的 28像素 × 28 像素的形狀。normalize=False) # normalize 設(shè)置是否將輸入圖像正規(guī)化為 0.0~1.0 的值。如果將該參數(shù)設(shè)置為 False ,則輸入圖像的像素會(huì)保持原來(lái)的 0~255# 輸出各數(shù)據(jù)的形狀
print(x_train.shape) # (60000, 784)
print(t_train.shape) # (60000,)
print(x_test.shape) # (10000, 784)
print(t_test.shape) # (10000,)img = x_train[0]
label = t_train[0]
print(label)print(img.shape)
img = img.reshape(28,28) # 通過(guò) reshape() 方法的參數(shù)指定期望的形狀,更改 NumPy 數(shù)組的形狀。
print(img.shape)img_show(img)
運(yùn)行結(jié)果和書上是一樣的,標(biāo)簽是5,圖形也是5,側(cè)面證明和書上的數(shù)據(jù)集是一套。
D:\python\test\ch03>py mnist_show.py
(60000, 784)
(60000,)
(10000, 784)
(10000,)
5
(784,)
(28, 28)
圖形:
其他內(nèi)容待續(xù):