CIFAR10图像生成与NN分类

1. CIFAR10的数据集描述

       整个CIFAR10数据集包括5个batch,一个test,整个数据集文件如图1所示。其中数据集以字典形式存放,包括数据集的名称、标签、数据矩阵、图片文件名称四个数据。标签范围从0到9变化,数据矩阵大小为10000*(3*32*32),后面的数字表示图像的大小为32*32的彩色图像。具体键名称为'batch_label', 'labels', 'data', 'filenames'。

                                     

                                                                 图1 CIFAR10数据集

CIFAR10的下载链接

2. CIFAR10图像生成

(1) 通过图像生成程序得到CIFAR的数据集图像,如图2所示。

Class Name

Class Num

Images

airplane

0

扫描二维码关注公众号,回复: 6077214 查看本文章

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)

运行结果表明,图像的分类准确率非常的低。

猜你喜欢

转载自blog.csdn.net/YMilton/article/details/89181190