吹爆!遥感高光谱分类(Python)

目录

一、数据集下载

二、安装包

三、数据处理

四、模型训练

五、模型推理

六、踩坑记录


一、数据集下载

Hyperspectral Remote Sensing Scenes - Grupo de Inteligencia Computacional (GIC) (ehu.eus)

Installing SPy --- Spectral Python 0.21 documentation

二、安装包

Spectral Python (SPy)是一个用于处理高光谱图像数据的纯Python模块。它具有读取、显示、操作和分类高光谱图像的功能。

Spectral安装:

官网链接:

Installing SPy --- Spectral Python 0.21 documentation

安装命令:

pip install spectral   

三、数据处理

加载数据、统计元素个数、光谱图显示、重构需要用到的类、标准化数据并存储

import matplotlib.pyplot as plt  
import numpy as np
from scipy.io import loadmat
import spectral
import cv2
import pandas as pd
from sklearn import preprocessing

print("OpenCV version:", cv2.__version__)
print("Spectral version:", spectral.__version__)

input_image = loadmat(r'C:\xxxxxxxxxxxxxxxxxxxxxxx/KSC.mat')['KSC']  #数据
output_image = loadmat(r'C:\xxxxxxxxxxxxxxxxxxxxxx/KSC_gt.mat')['KSC_gt']#标签

dict_k = {}
for i in range(output_image.shape[0]):
    for j in range(output_image.shape[1]):
        #if output_image[i][j] in [m for m in range(1,17)]:
        if output_image[i][j] in [1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13]:
            if output_image[i][j] not in dict_k:
                dict_k[output_image[i][j]]=0
            dict_k[output_image[i][j]] +=1
            
print (dict_k)
#print (reduce(lambda x,y:x+y,dict_k.values()))


ksc_color =np.array([[255,255,255],
     [184,40,99],
     [74,77,145],
     [35,102,193],
     [238,110,105],
     [117,249,76],
     [114,251,253],
     [126,196,59],
     [234,65,247],
     [141,79,77],
     [183,40,99],
     [0,39,245],
     [90,196,111],
        ])

ground_truth = spectral.imshow(classes = output_image.astype(int),figsize =(9,9),colors=ksc_color)

cv2.imshow('1',output_image)  #没有实质性的作用,解决spectral.imshow闪退问题
cv2.waitKey(0)


# 除掉 0 这个非分类的类,把所有需要分类的元素提取出来
need_label = np.zeros([output_image.shape[0],output_image.shape[1]])
for i in range(output_image.shape[0]):
    for j in range(output_image.shape[1]):
        if output_image[i][j] != 0:
            need_label[i][j] = output_image[i][j]
        
            
new_datawithlabel_list = []
for i in range(output_image.shape[0]):
    for j in range(output_image.shape[1]):
        if need_label[i][j] != 0:
            c2l = list(input_image[i][j])
            c2l.append(need_label[i][j])
            new_datawithlabel_list.append(c2l)

new_datawithlabel_array = np.array(new_datawithlabel_list)  
data_D = preprocessing.StandardScaler().fit_transform(new_datawithlabel_array[:,:-1])
data_L = new_datawithlabel_array[:,-1]

new = np.column_stack((data_D,data_L))
new_ = pd.DataFrame(new)
new_.to_csv(r'C:xxxxxxxx/KSC.csv',header=False,index=False)# 将结果存档后续处理

四、模型训练

import joblib
from sklearn.model_selection import KFold
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.svm import SVC
from sklearn import metrics
from sklearn import preprocessing
import pandas as pd


# 导入数据集切割训练与测试数据
data = pd.read_csv(r'C:xxxxxxxxxxxxx/KSC.csv',header=None)
data = data.values
data_D = data[:,:-1]
data_L = data[:,-1]
data_train, data_test, label_train, label_test = train_test_split(data_D,data_L,test_size=0.5)


# 模型训练与拟合
clf = SVC(kernel='rbf',gamma=0.125,C=16)
clf.fit(data_train,label_train)
pred = clf.predict(data_test)
accuracy = metrics.accuracy_score(label_test, pred)*100
print (accuracy)


# 存储结果学习模型,方便之后的调用
joblib.dump(clf, "KSC_MODEL.m")

五、模型推理

import matplotlib.pyplot as plt  
import numpy as np
from scipy.io import loadmat
import spectral
import joblib
from sklearn import metrics
import cv2

# KSC
input_image = loadmat(r'C:\xxxxxxxxxxx/KSC.mat')['KSC']
output_image = loadmat(r'C:\xxxxxxxxxx/KSC_gt.mat')['KSC_gt']


testdata = np.genfromtxt(r'C:\xxxxxxxx/KSC.csv',delimiter=',')
data_test = testdata[:,:-1]
label_test = testdata[:,-1]

clf = joblib.load("KSC_MODEL.m")

predict_label = clf.predict(data_test)
accuracy = metrics.accuracy_score(label_test, predict_label)*100

print (accuracy) # 97.1022836308


# 将预测的结果匹配到图像中
new_show = np.zeros((output_image.shape[0],output_image.shape[1]))
k = 0
for i in range(output_image.shape[0]):
    for j in range(output_image.shape[1]):
        if output_image[i][j] != 0 :
            new_show[i][j] = predict_label[k]
            k +=1 
            

# 展示地物
ground_truth = spectral.imshow(classes = output_image.astype(int),figsize =(9,9))
ground_predict = spectral.imshow(classes = new_show.astype(int), figsize =(9,9))

cv2.imshow('1',output_image)
cv2.waitKey(0)

六、踩坑记录

(1)问题描述:spectral.imshow(img)时,图像一闪而过 ,并且spectral好像没有类似CV2waitKey方法。所以无法暂停。

C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\spectral\graphics\spypylab.py:796: UserWarning: Failed to create RectangleSelector object. Interactive pixel class labeling will be unavailable.

warnings.warn(msg)

解决方法:借助CV2的waitKey

在ground_truth = spectral.imshow(classes = output_image.astype(int),figsize =(9,9),colors=ksc_color)下加入cv图像显示

cv2.imshow('1',output_image)

cv2.waitKey(0)

(2)问题描述:AttributeError: module 'spectral' has no attribute 'preprocessing'

解决方法:

导入该模块

from sklearn import preprocessing

(3)问题描述:AttributeError: 'DataFrame' object has no attribute 'as_matrix'

解决方法:as_matrix()属性已被淘汰,所以DataFrame对象没有as_matrix属性

解决方法:将 as_matrix() 改为 values

示例如下:

将:

data = data.as_matrix()

改为:

data = data.values

相关推荐
ZOMI酱6 分钟前
【AI系统】模型转换基本介绍
人工智能
CodeDevMaster18 分钟前
LangChain之检索增强生成RAG
人工智能·python·llm
凡解1 小时前
[自动化测试:实践01]:2:(4-1 )元素定位(selenium)在实际场景中的应用2
自动化测试·python·selenium·测试工具
卖个几把萌1 小时前
【04】Selenium+Python 手动添加Cookie免登录(实例)
python·selenium·测试工具
数学人学c语言1 小时前
yolov11剪枝
pytorch·python·深度学习
今天又是学习1 小时前
深度学习5
人工智能·深度学习
海阔天空_20131 小时前
浏览器自动化库playwright简介
网络·python·自动化
兔子的洋葱圈1 小时前
Python的3D可视化库 - vedo (3)visual子模块 点对象的可视化控制
python·数据可视化
新加坡内哥谈技术1 小时前
RAG架构类型
大数据·人工智能·语言模型·chatgpt
努力的小好2 小时前
【python】数据可视化之图像处理
图像处理·python·信息可视化