Python 如何将数据馈送到keras.layer Conv2D以及如何更改输入形状?

Python 如何将数据馈送到keras.layer Conv2D以及如何更改输入形状?,python,tensorflow,keras,Python,Tensorflow,Keras,我很难弄清楚如何将我的数据传送给CNN。 数据已经从fits文件中提取出来,作为代表灰度图像的200x200的numpy数组 def create_vgg16(): img_height = 200 img_width = 200 model = Sequential() inputs = Input(shape=(200, 200, 1)) y = Conv2D(64, (3, 3), activation='relu')(inputs) y

我很难弄清楚如何将我的数据传送给CNN。 数据已经从fits文件中提取出来,作为代表灰度图像的200x200的numpy数组

def create_vgg16():
    img_height = 200
    img_width = 200

    model = Sequential()
    inputs = Input(shape=(200,  200, 1))
    y = Conv2D(64, (3, 3), activation='relu')(inputs)
    y = Conv2D(64, (3, 3), activation='relu')(y)
    y = MaxPooling2D(2, 2)(y)
    y = Conv2D(128, (3, 3), activation='relu')(y)
    y = Conv2D(128, (3, 3), activation='relu')(y)
    y = MaxPooling2D(2, 2)(y)
    y = Conv2D(256, (3, 3), activation='relu')(y)
    y = Conv2D(256, (3, 3), activation='relu')(y)
    y = Conv2D(256, (3, 3), activation='relu')(y)
    y = MaxPooling2D(2, 2)(y)
    y = Flatten()(y)
    y = Dense(100, activation='relu')(y)
    y = Dense(50, activation='relu')(y)
    predictions = Dense(2, activation='softmax')(y)
    test_model = Model(inputs=inputs, outputs=predictions)
    test_model.compile(Adam(lr=.0001), loss='categorical_crossentropy',
                       metrics=['accuracy'])
    test_model.summary()

    model.compile(loss=tf.losses.MeanSquaredError(),
                  optimizer=tf.optimizers.Adagrad(),
                  metrics=tf.keras.metrics.binary_accuracy)

    return model   
这是列车数据:

打印(数据列车)

以及列车数据形状:

打印(数据列形状)

频道数为1

拟合模型时应自动检索批次大小

模型拟合(数据拟合), 火车, 纪元=10, 验证数据=(数据测试、标签测试), 批量大小=32 )

发出的错误是

 ValueError: This model has not yet been built. Build the model first by calling `build()` or calling `fit()` with some data, or specify an `input_shape` argument in the first layer(s) for automatic build.
已定义输入形状并调用fit()。所以我认为形状是错误的。 当我尝试在馈送前重塑数据时

 data_train.reshape((200, 200, 1))
将显示此错误:

ValueError: cannot reshape array of size 95360000 into shape (200,200,1)
我试过了

data_train.reshape((-1,200, 200, 1))
虽然没有整形错误,但印刷前后的形状绝对没有变化。
我应该如何输入数据?

您可以使用以下命令来重塑200x200x1阵列的形状

data = data.reshape(-1,200,200,1)
您还可以将(n_samples,200200,1)形状的数据转换为数据集并对其进行批处理。它应该可以解决您的尺寸问题


您可以使用以下命令执行此操作:tf.data.Dataset.from_tensor_slices((输入,输出)).batch(BATCHSIZE)

您可以使用
test_Model
变量而不是
Model
创建模型。这只是打字错误

ValueError: cannot reshape array of size 95360000 into shape (200,200,1)
data_train.reshape((-1,200, 200, 1))
data = data.reshape(-1,200,200,1)