吹爆!遥感高光谱分类(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

相关推荐
过期动态9 分钟前
详解Python面向对象程序设计
开发语言·python·pycharm·django
小码贾10 分钟前
评估 机器学习 回归模型 的性能和准确度
人工智能·机器学习·回归·scikit-learn·性能评估
不是AI11 分钟前
【持续更新】【NLP项目】【自然语言处理】智能聊天机器人——“有问必答”【Chatbot】第2章、《模式一:问候模式》
人工智能·自然语言处理·机器人
YRr YRr28 分钟前
深度学习:Transformer 详解
人工智能·深度学习·transformer
兜里有糖请分享39 分钟前
Python中序列化/反序列化JSON格式的数据
爬虫·python
萧鼎1 小时前
Python中的TensorFlow与Keras:深度学习模型构建与训练
python·深度学习·tensorflow
UCloud_TShare1 小时前
融合虚拟化与容器技术,打造灵活又安全的AI算力服务
人工智能·安全
决战春招1 小时前
人工智能之人脸识别(人脸采集人脸识别)
人工智能·opencv·学习·计算机视觉
阿乾之铭2 小时前
通过Django 与 PostgreSQL 进行WEB开发详细流程
python·postgresql·django