cifar10 c语言,Python3读取深度学习CIFAR-10数据集出现的若干问题解决
今天在看網(wǎng)上的視頻學(xué)習(xí)深度學(xué)習(xí)的時(shí)候,用到了CIFAR-10數(shù)據(jù)集。當(dāng)我興高采烈的運(yùn)行代碼時(shí),卻發(fā)現(xiàn)了一些錯(cuò)誤:
# -*- coding: utf-8 -*-
import pickle as p
import numpy as np import os def load_CIFAR_batch(filename): """ 載入cifar數(shù)據(jù)集的一個(gè)batch """ with open(filename, 'r') as f: datadict = p.load(f) X = datadict['data'] Y = datadict['labels'] X = X.reshape(10000, 3, 32, 32).transpose(0, 2, 3, 1).astype("float") Y = np.array(Y) return X, Y def load_CIFAR10(ROOT): """ 載入cifar全部數(shù)據(jù) """ xs = [] ys = [] for b in range(1, 6): f = os.path.join(ROOT, 'data_batch_%d' % (b,)) X, Y = load_CIFAR_batch(f) xs.append(X) ys.append(Y) Xtr = np.concatenate(xs) Ytr = np.concatenate(ys) del X, Y Xte, Yte = load_CIFAR_batch(os.path.join(ROOT, 'test_batch')) return Xtr, Ytr, Xte, Yte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
錯(cuò)誤代碼如下:
'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence
1
于是乎開(kāi)始各種搜索問(wèn)題,問(wèn)大佬,網(wǎng)上的答案都是類似:
然而并沒(méi)有解決問(wèn)題!還是錯(cuò)誤的!(我大概搜索了一下午吧,都是上面的答案)
哇,就當(dāng)我很絕望的時(shí)候,我終于發(fā)現(xiàn)了一個(gè)新奇的答案,抱著試一試的態(tài)度,嘗試了一下:
def load_CIFAR_batch(filename):
""" 載入cifar數(shù)據(jù)集的一個(gè)batch """ with open(filename, 'rb') as f: datadict = p.load(f, encoding='latin1') X = datadict['data'] Y = datadict['labels'] X = X.reshape(10000, 3, 32, 32).transpose(0, 2, 3, 1).astype("float") Y = np.array(Y) return X, Y
1
2
3
4
5
6
7
8
9
10
竟然成功了,這里沒(méi)有報(bào)錯(cuò)了!欣喜之余,我就很好奇,encoding=’latin1’到底是啥玩意呢,以前沒(méi)有見(jiàn)過(guò)啊?于是,我搜索了一下,了解到:
Latin1是ISO-8859-1的別名,有些環(huán)境下寫作Latin-1。ISO-8859-1編碼是單字節(jié)編碼,向下兼容ASCII,其編碼范圍是0x00-0xFF,0x00-0x7F之間完全和ASCII一致,0x80-0x9F之間是控制字符,0xA0-0xFF之間是文字符號(hào)。
因?yàn)镮SO-8859-1編碼范圍使用了單字節(jié)內(nèi)的所有空間,在支持ISO-8859-1的系統(tǒng)中傳輸和存儲(chǔ)其他任何編碼的字節(jié)流都不會(huì)被拋棄。換言之,把其他任何編碼的字節(jié)流當(dāng)作ISO-8859-1編碼看待都沒(méi)有問(wèn)題。這是個(gè)很重要的特性,MySQL數(shù)據(jù)庫(kù)默認(rèn)編碼是Latin1就是利用了這個(gè)特性。ASCII編碼是一個(gè)7位的容器,ISO-8859-1編碼是一個(gè)8位的容器。
還沒(méi)等我高興起來(lái),運(yùn)行后,又發(fā)現(xiàn)了一個(gè)問(wèn)題:
memory error
1
什么鬼?內(nèi)存錯(cuò)誤!哇,原來(lái)是數(shù)據(jù)大小的問(wèn)題。
X = X.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float")
1
這告訴我們每批數(shù)據(jù)都是10000 * 3 * 32 * 32,相當(dāng)于超過(guò)3000萬(wàn)個(gè)浮點(diǎn)數(shù)。 float數(shù)據(jù)類型實(shí)際上與float64相同,意味著每個(gè)數(shù)字大小占8個(gè)字節(jié)。這意味著每個(gè)批次占用至少240 MB。你加載6這些(5訓(xùn)練+ 1測(cè)試)在總產(chǎn)量接近1.4 GB的數(shù)據(jù)。
for b in range(1,2):
f = os.path.join(ROOT, 'data_batch_%d' % (b,)) X, Y = load_CIFAR_batch(f) xs.append(X) ys.append(Y)
1
2
3
4
5
所以如有可能,如上代碼所示只能一次運(yùn)行一批。
到此為止,錯(cuò)誤基本搞定,下面貼出正確代碼:
# -*- coding: utf-8 -*-
import pickle as p
import numpy as np import os def load_CIFAR_batch(filename): """ 載入cifar數(shù)據(jù)集的一個(gè)batch """ with open(filename, 'rb') as f: datadict = p.load(f, encoding='latin1') X = datadict['data'] Y = datadict['labels'] X = X.reshape(10000, 3, 32, 32).transpose(0, 2, 3, 1).astype("float") Y = np.array(Y) return X, Y def load_CIFAR10(ROOT): """ 載入cifar全部數(shù)據(jù) """ xs = [] ys = [] for b in range(1, 2): f = os.path.join(ROOT, 'data_batch_%d' % (b,)) X, Y = load_CIFAR_batch(f) xs.append(X) #將所有batch整合起來(lái) ys.append(Y) Xtr = np.concatenate(xs) #使變成行向量,最終Xtr的尺寸為(50000,32,32,3) Ytr = np.concatenate(ys) del X, Y Xte, Yte = load_CIFAR_batch(os.path.join(ROOT, 'test_batch')) return Xtr, Ytr, Xte, Yte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import numpy as np
from julyedu.data_utils import load_CIFAR10
import matplotlib.pyplot as plt plt.rcParams['figure.figsize'] = (10.0, 8.0) plt.rcParams['image.interpolation'] = 'nearest' plt.rcParams['image.cmap'] = 'gray' # 載入CIFAR-10數(shù)據(jù)集 cifar10_dir = 'julyedu/datasets/cifar-10-batches-py' X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir) # 看看數(shù)據(jù)集中的一些樣本:每個(gè)類別展示一些 print('Training data shape: ', X_train.shape) print('Training labels shape: ', y_train.shape) print('Test data shape: ', X_test.shape) print('Test labels shape: ', y_test.shape)
順便看一下CIFAR-10數(shù)據(jù)組成:
總結(jié)
以上是生活随笔為你收集整理的cifar10 c语言,Python3读取深度学习CIFAR-10数据集出现的若干问题解决的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 关于linux系统中无法识别某一命令问题
- 下一篇: Python标准异常总结