0
点赞
收藏
分享

微信扫一扫

ansible自动化IT工具安装部署与使用验证

认真的老去 2023-06-30 阅读 64

文章目录

引言

近年来深度学习的技术在计算机视觉领域中大放异彩,使得对多光谱数据分类的研究迅速发展,结合2D-CNN,3D-CNN,注意力机制,PCA降维等方法均可使得对多光谱图像的分类精度得以提升。目前CNN网络大量用于传统的CV领域,而对于高光谱图像的分类仍比较缺乏,本文章基于CNN网络在高光谱图像中的分类做一个综述。根据CNN网络对高光谱图像特征提取方式的不同,分为基于谱特征,空间特征,和空谱特征的分类方法。

卷积神经网络(Convolutional Neural Networks, CNN)是深度学习中的一种网络结构,特别适合处理具有网格结构(如图像)的数据。因其在图像分类等任务上展现出的强大能力,近年来,CNN也被广泛应用于高光谱图像的分类任务。

高光谱图像概述:

在这里插入图片描述

高光谱图像与传统RGB图像区别

1. 基于光谱特征

基于光谱特征进行CNN分类利用了1D-CNN在光谱层进行卷积

下面是一个基于Python和深度学习库Keras的简单例子,展示如何使用一维卷积神经网络(1D-CNN)进行光谱特征提取和分类。

from keras.models import Sequential
from keras.layers import Conv1D, MaxPooling1D, Flatten, Dense

# 假设高光谱数据具有100个频道
n_channels = 100

# 创建一个序贯模型
model = Sequential()

# 添加一维卷积层,用于光谱特征提取
model.add(Conv1D(filters=32, kernel_size=3, activation='relu', input_shape=(n_channels, 1)))

# 添加最大池化层
model.add(MaxPooling1D(pool_size=2))

# 添加另一维卷积层
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))

# 再添加一个最大池化层
model.add(MaxPooling1D(pool_size=2))

# 将卷积的输出展平,以便连接全连接层
model.add(Flatten())

# 添加一个全连接层
model.add(Dense(128, activation='relu'))

# 假设有10个类别,添加一个输出层
model.add(Dense(10, activation='softmax'))

# 编译模型,设置优化器和损失函数
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# 输出模型的结构
model.summary()

# x_train和y_train分别是训练数据和标签,进行模型训练
# model.fit(x_train, y_train, epochs=10, batch_size=32)

2. 基于空间特征

目前已有研究证明,像素周围的空间信息对高光谱图像分类算法性能有至关重要的影响,在高光谱图像分类中,像素周围的空间信息起着重要的作用。这是因为在许多情况下,单个像素可能无法提供足够的信息来进行精确的分类,尤其是在存在噪声或者光谱混合的情况下。然而,当我们考虑到像素周围的空间环境时,我们就能得到更多的信息。这主要有以下几个原因:

空间特征是指图像中的像素及其邻近像素之间的关系。在高光谱图像中,空间特征通常表示物体的形状、纹理、大小以及这些物体在图像中的相对位置等信息。对于许多应用来说,尤其是那些涉及到地理、环境或生物学的应用,空间特征是至关重要的。

对于空间特征的提取,通常使用二维卷积神经网络(2D-CNN)。卷积神经网络可以在局部邻域上提取特征,从而捕获像素之间的空间关系。

在使用2D-CNN进行空间特征提取时,通常的做法是将每一个高光谱像素的光谱视为一个多通道的2D图像。然后,对这个2D图像应用一系列2D卷积层和池化层,提取出空间特征。

以下是一个基于Python和深度学习库Keras,利用2D-CNN网络提取特征并用PCA进行降维的例子。

import numpy as np
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

# 加载高光谱图像数据和标签,这里假设数据已经加载到变量data和labels中
# data shape: (num_samples, img_height, img_width, num_channels)
# labels shape: (num_samples, num_classes)

# 对数据进行预处理
scaler = StandardScaler()
data = scaler.fit_transform(data.reshape(data.shape[0], -1)).reshape(data.shape)

# 使用PCA进行降维
n_components = 50  # 可以根据需要调整
pca = PCA(n_components=n_components)
data_pca = pca.fit_transform(data.reshape(data.shape[0], -1))
data_pca = data_pca.reshape(data_pca.shape[0], int(np.sqrt(n_components)), int(np.sqrt(n_components)), 1)

# 划分训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(data_pca, labels, test_size=0.2, random_state=42)

# 创建一个CNN模型
model = Sequential()

# 添加卷积层
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(int(np.sqrt(n_components)), int(np.sqrt(n_components)), 1)))

# 添加池化层
model.add(MaxPooling2D(pool_size=(2, 2)))

# 添加另一卷积层
model.add(Conv2D(64, (3, 3), activation='relu'))

# 添加池化层
model.add(MaxPooling2D(pool_size=(2, 2)))

# 展平输出,以便添加全连接层
model.add(Flatten())

# 添加全连接层
model.add(Dense(128, activation='relu'))

# 添加输出层,假设有10个类别
model.add(Dense(10, activation='softmax'))

# 编译模型,设置优化器和损失函数
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# 训练模型
model.fit(x_train, y_train, epochs=10, batch_size=32)

# 测试模型
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

3. 基于空谱特征

近些年,关于分类算法的一个主要趋势是将高光谱图像的空间和光谱信息融合,这种方法已经将分类性能提升到新的高度,这里又可以细分为2中方法,一种是先分别提取空间信息和光谱信息,然后将两种信息融和得到新的空谱联合特征,最后把融合得到的特征送入分类器进行分类,第二种方法是直接利用3D-CNN实现对空间特征和光谱特征同时提取

3.1 空间特征和光谱特征的融合

我们先分别提取空间信息和光谱信息,然后将两种信息融和得到新的空谱联合特征,最后把融合得到的特征送入分类器进行分类,以下是一个基本的步骤:

下面是一个基于Keras库的详细的光谱和空间特征提取、融合及分类的示例代码。此处,我们使用1D卷积网络提取光谱特征,使用2D卷积网络提取空间特征,最后通过全连接层进行特征融合和分类。

from keras.models import Model
from keras.layers import Input, Conv1D, Conv2D, Flatten, Dense, Concatenate
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical

# 高光谱图像数据集(data)和对应的标签(labels)已经准备好
# 数据格式如下:(num_samples, img_height, img_width, num_channels)

# 数据预处理
data = data / np.max(data)  # 数据归一化

# 划分训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, random_state=42)
y_train, y_test = to_categorical(y_train), to_categorical(y_test)  # one-hot编码

# 提取光谱特征的1D-CNN
spectral_input = Input(shape=(img_height, img_width, num_channels))
spectral_conv = Conv1D(32, 5, activation='relu')(spectral_input)
spectral_conv = Flatten()(spectral_conv)
spectral_model = Model(inputs=spectral_input, outputs=spectral_conv)

# 提取空间特征的2D-CNN
spatial_input = Input(shape=(img_height, img_width, num_channels))
spatial_conv = Conv2D(32, (3, 3), activation='relu')(spatial_input)
spatial_conv = Flatten()(spatial_conv)
spatial_model = Model(inputs=spatial_input, outputs=spatial_conv)

# 融合特征并分类
combined = Concatenate()([spectral_model.output, spatial_model.output])
combined = Dense(128, activation='relu')(combined)
combined = Dense(num_classes, activation='softmax')(combined)  # 假设你有num_classes个类别

model = Model(inputs=[spectral_input, spatial_input], outputs=combined)

# 编译模型
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# 训练模型
model.fit([x_train, x_train], y_train, epochs=10, batch_size=32)

# 测试模型
score = model.evaluate([x_test, x_test], y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

3.2 基于3D-CNN分类

人们联想到视频序列的数据形式与高光谱图像的数据形式非常相似,并逐渐把用 3D-CNN 提取高光谱空谱联合信息作为新的特征提取方式,由此衍生出一系列基于 3D-CNN 的空谱联合分类方法
在这里插入图片描述

以下是一个将 2D-CNN 和 3D-CNN相结合,构建一个 3 层 3D-CNN、2 层 2D-CNN的模型,同时引入通道三维卷积 ( Depthwise3DCNN) 和通道二维卷积 ( Depthwise 2D-CNN) 以减少参数量,并配合 Adam 优化算法的代码示例:

import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, Conv3D, DepthwiseConv2D, MaxPooling2D, MaxPooling3D, Flatten, Dense
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split

# 定义参数
num_samples = 100
img_height, img_width = 64, 64
num_channels = 100
num_classes = 10

# 生成模拟数据
data = np.random.rand(num_samples, img_height, img_width, num_channels)
labels = np.random.randint(0, num_classes, num_samples)

# 数据预处理
data = data / np.max(data)  # 数据归一化
labels = to_categorical(labels)  # one-hot编码

# 划分训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, random_state=42)

# 建立模型
model = Sequential()

# 添加3层3D-CNN
model.add(Conv3D(32, (3, 3, 3), activation='relu', padding='same', input_shape=(img_height, img_width, num_channels, 1)))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))

model.add(Conv3D(64, (3, 3, 3), activation='relu', padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))

model.add(Conv3D(128, (3, 3, 3), activation='relu', padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))

# 转换为2D数据
model.add(Reshape((-1, num_channels // 8)))

# 添加2层2D-CNN和深度卷积
model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(DepthwiseConv2D((3, 3), depth_multiplier=1, activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(DepthwiseConv2D((3, 3), depth_multiplier=1, activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))

# 添加全连接层和输出层
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

# 编译模型
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# 训练模型
model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10, batch_size=32)

4. 总结

高光谱图像(HSI)由大量连续的光谱带组成,每个像素包含从不同波长反射的光的信息。这种丰富的光谱信息使得HSI在许多领域,如农业、环境科学和军事中具有广泛的应用。

在研究中,我们同样需要解决一些存在的问题:

举报

相关推荐

0 条评论