简体   繁体   English

Tensorflow keras Conv1d input_shape问题,谁能帮帮我?

[英]Tensorflow keras Conv1d input_shape problem, can anyone help me?

I'm trying to rewrite a keras model which used to classify satellite images,the model is a NN model and I want to rewrite it to CNN, I found the model from here . I'm trying to rewrite a keras model which used to classify satellite images,the model is a NN model and I want to rewrite it to CNN, I found the model from here .
the previous NN model is this:之前的NN model是这样的:

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(1, nBands)),
    keras.layers.Dense(14, activation='relu'),
    keras.layers.Dense(2, activation='softmax')])

the original image shape is 6, 2054, 2044 , after reshape to two-dimensional array, it has become (2519025, 6) ,According to the article, the reason for reshape is:原图形状为6, 2054, 2044 ,reshape为二维数组后,变成了(2519025, 6) ,根据文章,reshape的原因是:

We will now change the shape of the arrays to a two-dimensional array, which is expected by the majority of ML algorithms, where each row represents a pixel.我们现在将 arrays 的形状更改为二维数组,这是大多数 ML 算法所期望的,其中每一行代表一个像素。

then it has been reshaped again,to (2519025, 1, 6)然后它再次被重塑为(2519025, 1, 6)
I use Conv1D as a conv layer like this我使用Conv1D作为这样的转换层

model = keras.Sequential([
    keras.layers.Conv1D(filters=64, kernel_size=(3), activation='relu', padding = 'same',input_shape=(2519025,  6)),
    keras.layers.Dense(128, activation='relu',kernel_initializer='glorot_normal'),
    keras.layers.Dense(2, activation='softmax')])

I call the model like this: model.fit(xTrain, yTrain, epochs=2,batch_size=10)我这样称呼 model: model.fit(xTrain, yTrain, epochs=2,batch_size=10)

the shapes of xTrain and yTrain are (2519025, 1, 6) (1679351, 1, 6) xTrain 和 yTrain 的形状是(2519025, 1, 6) (1679351, 1, 6)

I got this Waring:我得到了这个华林:

ARNING:tensorflow:Model was constructed with shape (None, 2519025, 6) for input Tensor("conv1d_input:0", shape=(None, 2519025, 6), dtype=float32), but it was called on an input with incompatible shape (None, 1, 6). ARNING:tensorflow:Model 是用形状 (None, 2519025, 6) 构造的,用于输入 Tensor("conv1d_input:0", shape=(None, 2519025, 6), 但它被称为 on=anfloat32)形状(无、1、6)。

what is the correct input_shape for the model or how can I change that NN model to use CNN? model 的正确input_shape是什么,或者如何更改 NN model 以使用 CNN?

As the warning says, the network expects the input to be in the shape of (None, 2519025, 6) where None is the batch size, but your xTrain and yTrain are in the shape of (2519025, 1, 6) (1679351, 1, 6).正如警告所说,网络期望输入的形状为 (None, 2519025, 6) 其中 None 是批量大小,但您的 xTrain 和 yTrain 的形状为 (2519025, 1, 6) (1679351, 1、6)。 You can try the following to make your input shape to match the network input shapes:您可以尝试以下方法使您的输入形状与网络输入形状相匹配:

xTrain = xTrain.reshape(2519025, 6)

However, If (2519025, 6) is the size of single input data, then your xTrain must be size of (#samples, 2519025, 6).但是,如果 (2519025, 6) 是单个输入数据的大小,那么您的 xTrain 必须是 (#samples, 2519025, 6) 的大小。 Also, both networks are classifiers with two classes, but you mentioned that your yTrain is (1679351, 1, 6), which has to be (#samples, 2).此外,两个网络都是具有两个类的分类器,但您提到您的 yTrain 是 (1679351, 1, 6),它必须是 (#samples, 2)。 You are going to get a separate error for that after fixing your input issue.修复输入问题后,您将收到一个单独的错误。

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

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