遥感图像地物分类流程

遥感图像地物分类流程

1. 制作标签

使用arcgis pro或者arcgis或者envi,画标签,保存为tiff格式

2. 处理标签数据

用python gdal库安装 osgdal库,如果安装失败就需要下载 对应库得 .whl去安装,网站具体搞忘了,可以百度

或者rasterio库

2.1 读入tif数据
def readTif(fileName):
    """
    dataset包含了tif文件得属性比如
    波段数
    高
    宽
    数据
    """
    dataset = rasterio.open(fileName)
    if dataset == None:
        print(fileName + "文件无法打开")
        return None
    # print(dataset.width)
   
    return dataset

2.2 处理数据
import csv
# 提取栅格图像信息,制作数据
ori_dataset = readTif(orgin_path)
label_dataset = readTif(sample_path)

width = ori_dataset.width # 宽
height = ori_dataset.height # 高

bands = ori_dataset.count # 波段数
# ori_data = for k in range(bands)

label_matri = label_dataset.read(1) #读出标签的矩阵
data_matri = ori_dataset.read() #原始图像的矩阵

count = np.count_nonzero(label_matri) #非零就是标签, 有多少非零的就代表样本像素是多少
print(count)
train_data = np.zeros((count, 8), dtype=data_matri.dtype) # 新建一个count*8的numpy数组,第8维度是原始图像的某一像素点对应的标签,0~6代表这一个像素点对应的7ge波段,landsata影像
nonzero_indices = np.nonzero(label_matri) #非零索引, 返回的是
"""
(row:array([ 30,  31,  31, ..., 390, 390, 390], dtype=int64), col:array([166, 165, 166, ..., 186, 187, 188], dtype=int64))
"""
print(nonzero_indices)
# 写入数据csv, 提取训练数据
# 将 train_data 写入 CSV 文件
csv_file = open(csv_filename, mode='w', newline='')
csv_writer = csv.writer(csv_file)
# 写入 CSV 文件的标题行,包括 Label 和 LabelName
csv_writer.writerow(csv_head_name)
    
for i in range(count):
    print(i)
    row, col = nonzero_indices[0][i], nonzero_indices[1][i]
    train_data[i, :7] = data_matri[:, row, col]
    train_data[i, 7] = label_matri[row, col]
    label = int(train_data[i, 7])
    row_data = train_data[i]
    row_data = np.append(row_data, labels_name[label])  # 在数据行中添加 LabelName
    csv_writer.writerow(row_data)
        
print(f"已将数据写入 CSV 文件: {csv_filename}")
csv_file.close()
2.3 数据格式

生成的数据格式如下

Band1,Band2,Band3,Band4,Band5,Band6,Band7,Label,LabelName
812,774,969,1111,1152,1146,1069,2,building
801,755,846,1016,1177,1411,1472,2,building
794,748,949,1179,1202,1399,1383,2,building
605,567,691,877,1537,1880,2070,2,building
602,556,768,994,1506,1625,1607,2,building
613,570,768,1045,1394,1483,1460,2,building
465,408,562,772,963,1035,990,2,building
549,484,648,828,969,1096,1028,2,building

3. 训练

from sklearn.ensemble import RandomForestClassifier
from sklearn import model_selection
import pickle

X = train_data[:, :7]
Y = train_data[:, 7]
# print(X.shape)
# print(Y.shape)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, Y, test_size=0.1, random_state=42, stratify=Y)
print(y_train)
# 3.用100个树来创建随机森林模型,训练随机森林
classifier = RandomForestClassifier(n_estimators=100,
                               bootstrap = True,
                               max_features = 'sqrt')
classifier.fit(X_train, y_train)


#  4.计算随机森林的准确率
print("训练集:",classifier.score(X_train,y_train))
print("测试集:",classifier.score(X_test,y_test))

pred_test_y = classifier.predict(X_test)
cfm = CFM(5, labels_name)
cfm.update(pred_test_y, y_test)
acc, comment_numpy = cfm.get_cfm()
print(comment_numpy)
cfm.plot()


file = open(model_path, "wb")
#将模型写入文件:
pickle.dump(classifier, file)
#最后关闭文件:
file.close()

4. 使用模型预测

pred_dataset = readTif(pred_path)
pred_width = pred_dataset.width
pred_height = pred_dataset.height
pred_bands = pred_dataset.count
pred_geotrans = pred_dataset.transform
pred_crs = pred_dataset.crs

print(pred_geotrans)
print(pred_crs)


file = open(model_path, "rb")
# 把模型从文件中读取出来
rf_model = pickle.load(file)
# 关闭文件
file.close()

pred_martix = pred_dataset.read()
data = np.zeros((pred_martix.shape[0], pred_martix.shape[1] * pred_martix.shape[2]))

# print(pred_martix.shape)
# print(pred_martix[0])
for i in range(pred_martix.shape[0]):
    # 第i个波段一维数组
    data[i] = pred_martix[i].flatten()
# 转换下维度
pred_x = data.swapaxes(0, 1)

pred_y = rf_model.predict(pred_x)
# print(pred_y, pred_y.shape)

# 将标签还原为图像的二维矩阵
pred_image = pred_y.reshape(pred_martix.shape[1], pred_martix.shape[2])
height_, width_ = pred_image.shape
tif_data = np.zeros((height_, width_, 3), dtype=np.int64)
for label, color in color_mapping.items():
    tif_data[pred_image == label] = color

tif_data = np.transpose(tif_data, (2, 0, 1))

im_bands, im_height, im_width = tif_data.shape
driver = gdal.GetDriverByName("GTiff")
dataset = driver.Create(pred_result_tif_path, im_width, im_height, im_bands, gdal.GDT_Byte)
for i in range(im_bands):
    dataset.GetRasterBand(i + 1).WriteArray(tif_data[i])
# if dataset != None:
#     #将栅格数据和地理坐标系统关联起来
#     dataset.SetProjection(pred_crs)  # 写入投影
#     dataset.SetGeoTransform(pred_geotrans)  # 写入仿射变换参数
    
dataset = None

5. other

import numpy as np
import matplotlib.pyplot as plt
from prettytable import PrettyTable

class CFM:
    """
    混淆矩阵类
    返回精度和混淆举证
    """
    def __init__(self, num_classes: int, labels: list):
        self.matrix = np.zeros((num_classes, num_classes))
        self.num_classes = num_classes
        self.labels = labels

    def plot(self):
        matrix = self.matrix
        print(matrix)
        plt.imshow(matrix, cmap=plt.cm.Blues)

        # 设置x轴坐标label
        plt.xticks(range(self.num_classes), self.labels, rotation=45)
        # 设置y轴坐标label
        plt.yticks(range(self.num_classes), self.labels)
        # 显示colorbar
        plt.colorbar()
        plt.xlabel('True Labels')
        plt.ylabel('Predicted Labels')
        plt.title('Confusion matrix')

        # 在图中标注数量/概率信息
        thresh = matrix.max() / 2
        for x in range(self.num_classes):
            for y in range(self.num_classes):
                # 注意这里的matrix[y, x]不是matrix[x, y]
                info = int(matrix[y, x])
                plt.text(x, y, info,
                         verticalalignment='center',
                         horizontalalignment='center',
                         color="white" if info > thresh else "black")
        plt.tight_layout()
        plt.show()

    def update(self, preds, labels):
        """_summary_

        Args:
            preds (_type_): _description_
            labels (_type_): _description_

        preds:预测值
        labels:真实值
        confusion martix
               label0 label1 label2 label3
        pred0
        pred1
        pred2
        pred3
        """
        for p, t in zip(preds, labels):
            self.matrix[p, t] += 1
        print("confusion matrix", self.matrix)
    
    def get_cfm(self):

        """
        Accuarcy: 正确样本占总样本数量的比例
        Percision: 精度Precision
        Recall: 召回率
        Specificaity: 特异性
        """
        sum_true = 0
        for i in range(self.num_classes):
            sum_true += self.matrix[i, i]
        acc = sum_true / np.sum(self.matrix)
        print("the model accuracy is ", acc)
        comment_labels = ["categeory", "Precision", "Recall", "Specificity"]
        tabel = PrettyTable()
        tabel.field_names = comment_labels
        comment_numpy = np.zeros((self.num_classes, 3))
        for i in range(self.num_classes):
        # 第i个分类的精确率, 召回率, 特异度
            TP = self.matrix[i, i]
            FP = np.sum(self.matrix[i, :]) - TP
            FN = np.sum(self.matrix[:, i]) - TP
            TN = np.sum(self.matrix) - TP - FN - FP
            # 保留三位小数, 如果 TP + FN 不等于零,就计算并将结果四舍五入到小数点后三位;否则,率设置为0。
            Precision = round(TP / (TP + FP), 3) if TP + FP != 0 else 0.
            Recall = round(TP / (TP + FN), 3) if TP + FN != 0 else 0.
            Specificity = round(TN / (TN + FP), 3) if TN + FP != 0 else 0.
            tabel.add_row([self.labels[i], Precision, Recall, Specificity])
            comment_numpy[i] = [Precision, Recall, Specificity]
        print(tabel)
        return acc, comment_numpy
    
if __name__ == "__main__":
    cfm = CFM(2, ["cat", "dog"])
    actual = [1, 0, 1, 1, 0, 1, 0, 0, 1, 0]
    predicted = [1, 0, 1, 0, 0, 1, 1, 1, 1, 0]
    cfm.update(predicted, actual)
    acc, comment_numpy = cfm.get_cfm()
    print(comment_numpy)
    cfm.plot()

变量名代表得含义

sample_path = "../sample/sample.tif" #标签图
orgin_path = "../datasets/landsat.tif" #原始图
pred_path = "../datasets/landsat.tif" #需要预测的图
txt_Path = "./result/label_data.txt" #无
labels_name = ["", "tudi", "building", "veg", "water"] # 样本名字,分类的类别
csv_filename = '../result/train_data.csv' # 生成训练数据的存放路径
csv_head_name = ['Band1', 'Band2', 'Band3', 'Band4', 'Band5', 'Band6', 'Band7', 'Label', "LabelName"] # 存放格式
model_path = "../model/myrnf.pickle" # 最终保存的模型路径
pred_result_tif_path = "../result/pred_landsat.tif" # 用训练的模型保存的路径
color_mapping = {
    1: (255, 255, 0),
    2: (255, 0, 0),
    3: (0, 255, 0),
    4: (0, 0, 255)
}
# 颜色映射从2D标签映射到3D
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: PyTorch是一个基于Python开发的机器学习框架,它拥有丰富的工具和功能,适用于各种任务,包括遥感图像地物分类遥感图像地物分类是指通过对遥感图像进行分析和识别,将不同地物分类为不同的类别,如建筑、道路、植被等。 在PyTorch中实现遥感图像地物分类可以遵循以下步骤: 1. 数据准备:首先,需要准备用于训练和评估的遥感图像数据集。可以从公开数据集中获取,或者根据实际需要收集和整理数据集。 2. 数据加载:使用PyTorch的数据加载器,将图像数据集加载到内存中,并对数据集进行预处理,如裁剪、缩放和标准化等。 3. 模型设计:选择适合遥感图像分类的模型架构,如卷积神经网络(CNN)。可以使用PyTorch提供的模型库,如ResNet、VGG等,也可以自定义模型。 4. 模型训练:将加载的图像数据集输入到模型中,通过定义损失函数和优化器,使用PyTorch提供的自动求导功能,进行模型训练。可以根据需要设置训练的迭代次数、学习率等超参数,并周期性地评估模型的性能。 5. 模型评估:训练完成后,使用测试集对模型进行评估,计算分类精度、查准率、查全率等指标,评估模型的性能。 6. 模型应用:经过训练和评估后,可以使用该模型对新的遥感图像进行分类预测。将新的图像输入到模型中,经过前向传播计算,得到图像的预测类别。 总而言之,通过PyTorch实现遥感图像地物分类可以借助其强大的机器学习功能和便捷的开发环境,快速高效地完成图像分类任务。同时,PyTorch还提供了丰富的工具和库,方便用户进行模型设计、训练和评估,并具有良好的可扩展性和灵活性,满足不同用户的需求。 ### 回答2: PyTorch是一个常用的深度学习框架,它提供了丰富的功能和工具,可以用于遥感图像地物分类任务的实现。在知乎上,关于PyTorch实现遥感图像地物分类的问题,可能会有一些相关的回答。 首先,我们需要准备好用于训练的遥感图像数据集。可以使用公开的遥感图像数据集,或者是自己收集的数据集。数据集应包含不同类别的地物图像样本,并且要进行适当的标注。 接下来,我们可以使用PyTorch的数据处理工具,如`torchvision`来加载和预处理图像数据。可以使用`torch.utils.data.Dataset`构建一个自定义的数据集类,根据需要对图像进行预处理操作,如缩放、裁剪、归一化等。 然后,我们可以使用PyTorch搭建一个卷积神经网络(CNN)模型,用于图像分类任务。可以根据具体的需求选择不同的网络结构,如ResNet、VGG等。可以使用`torch.nn`模块来构建自定义的网络模型,包括卷积层、池化层、全连接层等。 在模型搭建完成后,我们需要定义损失函数和优化器来进行训练。常用的损失函数有交叉熵损失函数(CrossEntropyLoss),可以通过`torch.nn.CrossEntropyLoss`来定义。优化器可以选择Adam、SGD等,可以使用`torch.optim`模块来构建。 接着,我们可以编写训练循环,使用训练数据来迭代训练模型。可以使用`torch.utils.data.DataLoader`来创建一个数据迭代器,方便获取批量的数据样本。然后,依次将数据输入到模型中,计算损失函数,并通过优化器来更新模型参数。 在训练过程中,可以使用一些技巧来提高模型性能,如数据增强、学习率调整等。可以通过`torchvision.transforms`来实现数据增强操作,如随机裁剪、随机旋转等。可以使用学习率调整器(Learning Rate Scheduler)来动态调整学习率,如StepLR、ReduceLROnPlateau等。 最后,在训练完成后,我们可以使用测试数据对模型进行评估。可以使用测试数据集来验证模型的泛化能力,并计算评估指标,如准确率、召回率等。 总之,使用PyTorch实现遥感图像地物分类是一个相对复杂的任务,但通过合理的数据处理、模型搭建和优化方法,可以有效实现。知乎上也有很多关于这一问题的讨论和分享,可以帮助我们更好地理解和实践相关内容。 ### 回答3: pytorch是一个常用的深度学习框架,可以用于遥感图像地物分类任务的实现。在pytorch中,可以利用卷积神经网络(CNN)进行图像分类任务。 首先,需要准备好遥感图像数据集。数据集应包含标注好的遥感图像样本,以及每个样本对应的地物分类标签。接下来,可以利用pytorch的数据加载工具,如torchvision库中的datasets模块,将数据集按照一定的比例划分为训练集、验证集和测试集。 然后,可以利用pytorch的模型类来定义一个卷积神经网络模型。模型的结构可以根据具体任务进行设计,一般建议包含多个卷积层、池化层和全连接层。可以根据需要,使用不同的卷积核大小、步幅和激活函数等。 在模型定义好后,可以利用pytorch的优化器类定义一个优化器,如Adam优化器。优化器可以控制模型的权重更新方式,在训练过程中调整学习率和动量等超参数。 接下来,可以利用pytorch的训练循环来训练模型。训练循环包括多个迭代的训练阶段,每个阶段包括前向传播、计算损失、反向传播和模型权重更新等步骤。可以利用pytorch的损失函数类定义一个损失函数,如交叉熵损失函数。在训练过程中,通过最小化损失函数来优化模型的权重。 在训练结束后,可以利用验证集来评估模型的性能,并根据需要进行调参和优化。最后,可以利用测试集对训练好的模型进行评估,并根据评估结果进行后续的地物分类任务。 总之,pytorch可以提供一个灵活、高效的深度学习框架,用于实现遥感图像地物分类任务。通过合理设计模型结构、选择合适的优化器和损失函数,以及使用训练循环和数据加载工具等功能,可以实现高准确率的地物分类模型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值