CIFAR-10数据集图像分类【PCA+基于最小错误率的贝叶斯决策】

简介: CIFAR-10数据集图像分类【PCA+基于最小错误率的贝叶斯决策】

CIFAR-10和CIFAR-100均是带有标签的数据集,都出自于规模更大的一个数据集,他有八千万张小图片。而本次实验采用CIFAR-10数据集,该数据集共有60000张彩色图像,这些图像是32*32,分为10个类,每类6000张图。


这里面有50000张用于训练,构成了5个训练批,每一批10000张图;另外10000用于测试,单独构成一批。测试批的数据里,取自10类中的每一类,每一类随机取1000张。抽剩下的就随机排列组成了训练批。注意一个训练批中的各类图像并不一定数量相同,总的来看训练批,每一类都有5000张图。


下面这幅图就是列举了10各类,每一类展示了随机的10张图片:


我的数据集一共有三个文件,分别是训练集train_data,测试集test_data以及标签名称labels_name,而标签名称中共有5个类,‘airplane‘, 'automobile‘, 'bird‘, 'cat‘, 'deer’.我现在准备对前三类‘airplane‘, ’automobile‘, ’bird‘,(即标签为1, 2, 3的数据 )进行分类。


经过之前大量测试,得到在累计方差贡献率为0.79时,基于最小错误率的贝叶斯决策用于图像分类最佳,以下为代码:


#CIFAR-10数据集:包含60000个32*32的彩色图像,共10类,每类6000个彩色图像。有50000个训练图像和10000个测试图像。
import scipy.io
train_data=scipy.io.loadmat("F:\\模式识别\\最小错误率的贝叶斯决策进行图像分类\\data\\train_data.mat")
print (type(train_data))
print (train_data.keys())
print (train_data.values())
print (len(train_data['Data']))
#单张图片的数据向量长度:32X32X3=3072
#内存占用量=3072*4*9968=116M  假定一个整数占用4个字节
print (len(train_data['Data'][0]))
print (train_data)
x = train_data['Data']
y = train_data['Label']
print (y)
print (len(y))
print (y.shape)
print (y.flatten().shape)
#labels_name:共5个标签,分别为airplane、automobile、bird、cat、deer
import scipy.io
labels_name=scipy.io.loadmat("F:\\模式识别\\最小错误率的贝叶斯决策进行图像分类\\data\\labels_name.mat")
print (type(labels_name))
print (labels_name)
print (len(labels_name))
#test_data:共5000个图像,5类,每类1000个图像
import scipy.io
test_data=scipy.io.loadmat("F:\\模式识别\\最小错误率的贝叶斯决策进行图像分类\\data\\test_data.mat")
print (test_data['Label'])
print (test_data['Data'])
print (len(test_data['Label']))
datatest = test_data['Data']
labeltest = test_data['Label']
print (datatest.shape)
print (labeltest.shape)
test_index=[]
for i in range(len(labeltest)):
    if labeltest[i]==1:
        test_index.append(i)
    elif labeltest[i]==2:
        test_index.append(i)
    elif labeltest[i]==3:
        test_index.append(i)
#print (test_index)
labeltest=test_data['Label'][:3000]
#print (labeltest)
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
print (x)
print (x.shape)
print (type(x))
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.decomposition import PCA
pca=PCA(n_components=0.79)
#训练模型
pca.fit(x)
x_new=pca.transform(x)
print("降维后各主成分的累计方差贡献率:",pca.explained_variance_ratio_)
print("降维后主成分的个数:",pca.n_components_)
print (x_new)
index_1=[]
index_2=[]
index_3=[]
index_num=[]
for i in range(len(y)):
    if y[i]==1:
        index_1.append(i)
    elif y[i]==2:
        index_2.append(i)
    elif y[i]==3:
        index_3.append(i)
index_num=[len(index_1),len(index_2),len(index_3)]
print(len(index_1))
print(len(index_2))
print(len(index_3))
print (index_num)
import numpy as np
class1_feature=[]
class2_feature=[]
class3_feature=[]
#index_1
for i in index_1:
    class1_feature.append(x_new[i])
print (len(class1_feature))
for i in index_2:
    class2_feature.append(x_new[i])
print (len(class2_feature))
for i in index_3:
    class3_feature.append(x_new[i])
print (len(class3_feature))
#计算第一类的类条件概率密度函数的参数
class1_feature=np.mat(class1_feature)
print (class1_feature.shape)
miu1=[]
sigma1=[]
for i in range(30):
    miu=class1_feature[:,i].sum()/len(index_1)
    miu1.append(miu)
    temp=class1_feature[:,i]-miu
    class1_feature[:,i]=temp
sigma1=(class1_feature.T*class1_feature)/len(index_1)
print (miu1)
print (sigma1)
print (sigma1.shape)
#计算第二类类条件概率密度函数的参数
class2_feature=np.mat(class2_feature)
miu2=[]
sigma2=[]
for i in range(30):
    miu=class2_feature[:,i].sum()/len(index_2)
    miu2.append(miu)
    temp=class2_feature[:,i]-miu
    class2_feature[:,i]=temp
sigma2=(class2_feature.T*class2_feature)/len(index_2)
print (miu2)
print (sigma2)
print (sigma2.shape)
#计算第三类类条件概率密度函数的参数
class3_feature=np.mat(class3_feature)
miu3=[]
sigma3=[]
for i in range(30):
    miu=class3_feature[:,i].sum()/len(index_3)
    miu3.append(miu)
    temp=class3_feature[:,i]-miu
    class3_feature[:,i]=temp
sigma3=(class3_feature.T*class3_feature)/len(index_3)
print (miu3)
print (sigma3)
print (sigma3.shape)
#计算三个类别的先验概率:
prior_index1=len(index_1)/len(y)
prior_index2=len(index_2)/len(y)
prior_index3=len(index_3)/len(y)
print (prior_index1)
print (prior_index2)
print (prior_index3)
import math
#降维
x_test = pca.transform(datatest)
print (x_test)
print (x_test.shape)
print (x_test[0])
#print ((np.mat(x_test[0]-miu1))*sigma1.I*(np.mat(x_test[0]-miu1).T))
#print (((np.mat(x_test[0]-miu1))*sigma1.I*(np.mat(x_test[0]-miu1).T))[0,0])
predict_label=[]
for i in range(3000):
    g1=-0.5*((np.mat(x_test[i]-miu1))*sigma1.I*(np.mat(x_test[i]-miu1).T))[0,0]-0.5*math.log(np.linalg.det(sigma1))+math.log(prior_index1)
    g2=-0.5*((np.mat(x_test[i]-miu2))*sigma2.I*(np.mat(x_test[i]-miu2).T))[0,0]-0.5*math.log(np.linalg.det(sigma2))+math.log(prior_index2)
    g3=-0.5*((np.mat(x_test[i]-miu3))*sigma3.I*(np.mat(x_test[i]-miu3).T))[0,0]-0.5*math.log(np.linalg.det(sigma3))+math.log(prior_index3)
    if g1>g2:
        max=1
        if g1>g3:
            max=1
        else:
            max=3
    else:
        max=2
        if g2>g3:
            max=2
        else:
            max=3
    predict_label.append(max)
from sklearn.metrics import accuracy_score
print (accuracy_score(predict_label,labeltest))


可以看到分类结果的准确率高达73%,这一数值在贝叶斯决策用于图像分类中已经是极值了。



相关文章
|
数据可视化 PyTorch 算法框架/工具
使用PyTorch搭建VGG模型进行图像风格迁移实战(附源码和数据集)
使用PyTorch搭建VGG模型进行图像风格迁移实战(附源码和数据集)
1645 1
|
机器学习/深度学习 Web App开发 人工智能
Colaboratory:手把手教你使用Google免费的云端IDE进行深度学习(免费的GPU加速)的详细攻略
Colaboratory:手把手教你使用Google免费的云端IDE进行深度学习(免费的GPU加速)的详细攻略
Colaboratory:手把手教你使用Google免费的云端IDE进行深度学习(免费的GPU加速)的详细攻略
|
7月前
|
人工智能 弹性计算 运维
2026阿里云最新优惠活动解析:云服务器抢购与低价特惠、企业补贴、百炼优惠券及AI产品免费试用
2026年阿里云推出多层次优惠活动,覆盖全场景上云需求。新客可享轻量应用服务器38元/年,ECS经济型e实例99元/年、通用算力型u1实例199元/年,且续费同价,长期成本可控。企业用户可申请迁云补贴和出海扶持优惠券,降低全球化部署成本。AI开发者可参与百炼"先用后返"活动最高省200元,另有超30款AI产品及7000万大模型tokens免费试用,助力零成本创新。建议用户先领券再购买,善用组合优惠与免费试用,高效降低上云与AI应用门槛。
|
人工智能 数据可视化 数据挖掘
从传统软件到SaaS:为什么更多企业选择订阅制服务?
本文详细介绍了SaaS的概念、优势及其在现代工作中的重要性。SaaS是一种通过互联网提供云计算服务,用户无需安装和维护本地软件,只需通过网络访问软件即可。SaaS通过自动更新和维护、订阅制收费模式等方式降低成本,提供更便捷的服务。
6381 4
从传统软件到SaaS:为什么更多企业选择订阅制服务?
|
机器学习/深度学习 人工智能 达摩院
MVGenMaster:复旦联合阿里等实验室推出的多视图扩散模型
MVGenMaster是由复旦大学、阿里巴巴达摩院和湖潘实验室联合推出的多视图扩散模型,专注于新视角合成(NVS)任务。该模型通过整合3D先验信息,显著提升了NVS的泛化和3D一致性,并能从单一图像生成多达100个新视图。此外,研究团队还推出了包含160万场景的大型多视图图像数据集MvD-1M,以支持模型的训练和优化。
658 27
MVGenMaster:复旦联合阿里等实验室推出的多视图扩散模型
|
前端开发 Java 应用服务中间件
《深入理解Spring》 Spring Boot——约定优于配置的革命者
Spring Boot基于“约定优于配置”理念,通过自动配置、起步依赖、嵌入式容器和Actuator四大特性,简化Spring应用的开发与部署,提升效率,降低门槛,成为现代Java开发的事实标准。
|
机器学习/深度学习 PyTorch 算法框架/工具
pytorch中nn.ReLU()和F.relu()有什么区别?
pytorch中nn.ReLU()和F.relu()有什么区别?
1360 0
|
机器学习/深度学习 PyTorch TensorFlow
Python实现深度学习学习率指数衰减的方法与参数介绍
学习率指数衰减提供了一种高效的动态调整学习率的手段,帮助模型在不同训练阶段以不同的学习速度优化,有利于提升模型性能和训练效率。通过合理设置衰减策略中的参数,可以有效地控制学习率的衰减过程,实现更加精确的模型训练调优。
1054 0
|
监控 安全 网络安全
网络安全中的网络隔离与访问控制技术
【7月更文挑战第5天】网络隔离与访问控制技术是网络安全领域的重要组成部分。通过合理的网络隔离和严格的访问控制策略,可以有效保护网络资产免受未经授权的访问和攻击。随着技术的不断发展,我们需要不断探索和创新,以应对日益复杂的网络安全威胁。
1602 2