繁体   English   中英

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

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

我无法弄清楚如何将我的数据提供给 CNN。 数据已从拟合文件中提取为 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 

这是火车数据:

打印(数据火车)

[[ 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]]

和火车数据形状:

打印(data_train.shape)

(2384, 40000)

通道数为1

拟合 model 时应自动检索批量大小

model.fit(data_train,labels_train,epochs=10,validation_data=(data_test,labels_test),batch_size=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,200,200,1) 形状的数据转换为数据集并对其进行批处理。 它应该可以解决您的尺寸问题。

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

您使用test_model变量而不是 model 创建model 这只是拼写错误。

暂无
暂无

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

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