1. CIFAR10的數據集描述
整個CIFAR10數據集包括5個batch,一個test,整個數據集文件如圖1所示。其中數據集以字典形式存放,包括數據集的名稱、標籤、數據矩陣、圖片文件名稱四個數據。標籤範圍從0到9變化,數據矩陣大小爲10000*(3*32*32),後面的數字表示圖像的大小爲32*32的彩色圖像。具體鍵名稱爲'batch_label', 'labels', 'data', 'filenames'。
圖1 CIFAR10數據集
2. CIFAR10圖像生成
(1) 通過圖像生成程序得到CIFAR的數據集圖像,如圖2所示。
Class Name |
Class Num |
Images |
airplane |
0 |
|
automobile |
1 |
|
bird |
2 |
|
cat |
3 |
|
deer |
4 |
|
dog |
5 |
|
frog |
6 |
|
horse |
7 |
|
Ship |
8 |
|
truck |
9 |
圖2 data_batch_1數據集生成的圖像展示
(2) 生成文件目錄如圖3所示:
airplane automobile bird
圖3 data_batch_1生成的圖像目錄與內容
(3) 代碼內容如下:
import os
import pickle
import pylab as pl
import imageio
import numpy as np
def unpickle(file):
with open(file,'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
# 目錄的創建,存在不創建
def create_dir(filename):
if not os.path.exists(filename):
os.makedirs(filename)
# 保存CIFAR10的指定數據集爲圖片
def save_images(data_name):
path = 'cifar-10-batches-py'
dict = unpickle(path+'/'+data_name)
data = dict.get(b'data')
labels = dict.get(b'labels')
filenames = dict.get(b'filenames')
# make file dir
classification = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']
for i in range(10):
create_dir(data_name+'/'+classification[i])
for i in range(data.shape[0]):
img = data[i,:].reshape(3,32,32)
img = img.transpose(1,2,0)
imageio.imsave(data_name+'/'+classification[labels[i]]+'/'
+str(filenames[i],encoding='utf-8'),img)
save_images('data_batch_1')
# save_images('test_batch')
3. NN圖像分類
(1) NN算法python代碼如下:
# 最近鄰算法
class NearestNeighbor:
def __init__(self):
pass
def train(self, X, y):
'''X is N x D where each row is an example. Y is 1-dimesion of size N'''
self.Xtr = X
self.ytr = y
# 多個類別的預測
def predict(self, X):
'''X is N x D where each row is an example we wish to predict label for'''
num_test = X.shape[0]
# make sure that the output type matches the input type
Ypred = np.zeros(num_test, dtype=self.ytr.dtype)
# loop over all test rows
for i in range(num_test):
# find the nearest training image to i'th test image
# using the L1 distance
distances = np.sum(np.abs(self.Xtr - X[i,:]), axis=1)
min_index = np.argmin(distances) # get the index with smallest distance
Ypred[i] = self.ytr[min_index]
return Ypred
(2) 通過測試集的數據看分類準確率
# 圖像分類,最鄰近算法調用
top_num = 50 # 取倒數50條數據做檢測
path = 'cifar-10-batches-py'
train_data = unpickle(path+'/data_batch_5')
test_data = unpickle(path+'/test_batch')
nn = NearestNeighbor()
# print(np.array(train_data[b'labels']).reshape(-1,1))
nn.train(train_data[b'data'],np.array(train_data[b'labels']))
Ypred = nn.predict(test_data[b'data'][-top_num:,:])
accur = np.sum(np.array(Ypred)==np.array(test_data[b'labels'][-top_num:])) / len(Ypred)
print(accur)
運行結果表明,圖像的分類準確率非常的低。