简体   繁体   中英

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. Data has been extracted from fits files as numpy array of 200x200 representing grayscale images

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)

(2384, 40000)

The number of channels is 1

Batch size should be retrieved automatically when fitting the model

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. 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.

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

You create a Model using the test_model Variable instead of model . It is just typo error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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