Keras张量流将模型NN修改为CNN



我正在尝试重写一个用于对卫星图像进行分类的神经网络模型,我想在那个模型中使用一些conv层,比如#keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding = 'same',input_shape=(1,nBands)),但我无法正确获取input_shape参数,有人能帮我吗?

以前的NN模型是这样的:

# Print the shape of reshaped data
print(xTrain.shape, xTest.shape, featuresHyderabad.shape)
#(2519025, 1, 6) (1679351, 1, 6) (1391808, 1, 6)
# Define the parameters of the model
model = keras.Sequential([
#keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding = 'same',input_shape=(1,nBands)),
keras.layers.Flatten(input_shape=(1, nBands)), 
keras.layers.Dense(128, activation='relu',kernel_initializer='glorot_normal'),
keras.layers.Dense(2, activation='softmax')])
# Define the accuracy metrics and parameters
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
# Run the model
model.fit(xTrain, yTrain, epochs=2,batch_size=10)

输入形状必须是图像的形状。例如,如果您使用48x48黑白图像训练模型,则如果chanel值位于高度和宽度之前,则输入形状将为(48,48,1(或(1,48,48(。

好问题,我想你会发现神经网络比在平面图像上训练神经网络表现得更好。对于Conv2D层中的输入形状,该形状可以以两种形式之一给出:

  1. ";Channels last";(Keras默认使用此选项(:(image height, image width, 6),其中6表示通道数(3表示RGB,1表示灰度(。

  2. "频道优先";(定义Conv2D层时,可以通过设置data_option="channels_first"来选择此选项(:(6, image height, image width)

更直观地说,您可以将输入大小视为您提供给网络的图像的大小,即(image height, image width, number of bands)。我发现Keras的这份文档也很有用:https://keras.io/api/layers/convolution_layers/convolution2d/.

最新更新