简体   繁体   English

如何将数据提供给 keras.layer Conv2D 以及如何更改输入形状?

[英]how to feed data to keras.layer Conv2D and how to change input shape?

I'm having trouble figuring out how to feed my data to a CNN.我无法弄清楚如何将我的数据提供给 CNN。 Data has been extracted from fits files as numpy array of 200x200 representing grayscale images数据已从拟合文件中提取为 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   

    This is what model summary puts out:  
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    input_1 (InputLayer)         [(None, 200, 200, 1)]     0         
    _________________________________________________________________
    conv2d (Conv2D)              (None, 198, 198, 64)      640       
    _________________________________________________________________
    conv2d_1 (Conv2D)            (None, 196, 196, 64)      36928     
    _________________________________________________________________
    max_pooling2d (MaxPooling2D) (None, 98, 98, 64)        0         
    _________________________________________________________________
    conv2d_2 (Conv2D)            (None, 96, 96, 128)       73856     
    _________________________________________________________________
    conv2d_3 (Conv2D)            (None, 94, 94, 128)       147584    
    _________________________________________________________________
    max_pooling2d_1 (MaxPooling2 (None, 47, 47, 128)       0         
    _________________________________________________________________
    conv2d_4 (Conv2D)            (None, 45, 45, 256)       295168    
    _________________________________________________________________
    conv2d_5 (Conv2D)            (None, 43, 43, 256)       590080    
    _________________________________________________________________
    conv2d_6 (Conv2D)            (None, 41, 41, 256)       590080    
    _________________________________________________________________
    max_pooling2d_2 (MaxPooling2 (None, 20, 20, 256)       0         
    _________________________________________________________________
    flatten (Flatten)            (None, 102400)            0         
    _________________________________________________________________
    dense (Dense)                (None, 100)               10240100  
    _________________________________________________________________
    dense_1 (Dense)              (None, 50)                5050      
    _________________________________________________________________
    dense_2 (Dense)              (None, 2)                 102       
    =================================================================
    Total params: 11,979,588
    Trainable params: 11,979,588
    
    Non-trainable params: 0 

this is the train data:这是火车数据:

print(data_train)打印(数据火车)

[[ 773  794 1009 ... 1057 1059 1011],  
    
[1847 1890 1897 ... 1968 2116 2365],  
    
[ 670  643  642 ...  633  647  650],  
...,  
[   0    0    0 ...  457  435  429],  
[ 879  848  853 ...  830  858  821],  
[2030 2002 2097 ...    0    0    0]]

and the train data shape:和火车数据形状:

print(data_train.shape)打印(data_train.shape)

(2384, 40000)

The number of channels is 1通道数为1

Batch size should be retrieved automatically when fitting the model拟合 model 时应自动检索批量大小

model.fit(data_train, labels_train, epochs=10, validation_data=(data_test, labels_test), batch_size=32 ) model.fit(data_train,labels_train,epochs=10,validation_data=(data_test,labels_test),batch_size=32)

The error put out is输出的错误是

 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.

Both input shape has been defined and fit() called.两个输入形状都已定义并调用 fit()。 So I assume the shape is wrong.所以我认为形状是错误的。 When I try to reshape data before feeding当我尝试在喂食之前重塑数据时

 data_train.reshape((200, 200, 1))

this error is displayed:显示此错误:

ValueError: cannot reshape array of size 95360000 into shape (200,200,1)

I tried我试过了

data_train.reshape((-1,200, 200, 1))

and while there are no reshape errors, printing before and after changes absolutely nothing to the shape.虽然没有重塑错误,但之前和之后的打印完全不会改变形状。 How should I go about feeding my data?我应该如何提供我的数据?

You can use the following command to reshape to a 200x200x1 array.您可以使用以下命令重塑为 200x200x1 数组。

data = data.reshape(-1,200,200,1)

You can also transform your (n_samples,200,200,1) shaped data into a dataset and batch it.您还可以将 (n_samples,200,200,1) 形状的数据转换为数据集并对其进行批处理。 It should fix your dimension problem.它应该可以解决您的尺寸问题。

You can do that by using the following command: tf.data.Dataset.from_tensor_slices((inputs,outputs)).batch(BATCHSIZE)您可以使用以下命令执行此操作: tf.data.Dataset.from_tensor_slices((inputs,outputs)).batch(BATCHSIZE)

You create a Model using the test_model Variable instead of model .您使用test_model变量而不是 model 创建model It is just typo error.这只是拼写错误。

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

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