简体   繁体   English

Keras tensorflow 修改 model NN 到 CNN

[英]Keras tensorflow modify model NN to CNN

I'm trying to rewrite a Neural Network model which used to classify satellite images, I want to use some conv layers in that model,like #keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding = 'same',input_shape=(1,nBands)), but I can't get the input_shape parameter right, can anyone help me?我正在尝试重写用于对卫星图像进行分类的神经网络 model,我想在 model 中使用一些卷积层,例如#keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding = 'same',input_shape=(1,nBands)),但我无法正确获取input_shape参数,谁能帮帮我?

the previous NN model is this:之前的NN model是这样的:

# 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)

The input shape must be the shape of the images.输入形状必须是图像的形状。 for exemple if you train your model with 48x48 black and white images the input shape will be (48,48, 1) or (1, 48, 48) if the chanel value is placed before the height and width.例如,如果您使用 48x48 黑白图像训练 model 输入形状将是 (48,48, 1) 或 (1, 48, 48) 如果通道值放在高度和宽度之前。

Great question, and I think you'll find CNNs perform much better than training NNs on flattened images.很好的问题,我认为你会发现 CNN 比在扁平图像上训练 NN 的性能要好得多。 For the input shape in Conv2D layers, the shape can be given in one of two forms:对于Conv2D层中的输入形状,可以在两个 forms 之一中给出形状:

  1. "Channels last" (Keras uses this by default): (image height, image width, 6) , where 6 refers to the number of channels ( 3 for RGB, 1 for grayscale). “Channels last”(Keras 默认使用这个): (image height, image width, 6) ,其中6表示通道数(RGB 为3 ,灰度为1 )。

  2. "Channels first" (you can select this option by setting data_option="channels_first" when you define the Conv2D layer): (6, image height, image width) . “Channels first”(可以在定义Conv2D层时通过设置data_option="channels_first" select这个选项):( (6, image height, image width)

On a more intuitive note, you can think of the input size as being the size of the image you are giving to the network, which is (image height, image width, number of bands) .更直观地说,您可以将输入大小视为您提供给网络的图像的大小,即(image height, image width, number of bands) I found this documentation from Keras helpful as well: https://keras.io/api/layers/convolution_layers/convolution2d/ .我发现来自 Keras 的文档也很有帮助: https://keras.io/api/layers/convolution_layers/convolution2d/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM