检索错误层 conv2d_3 需要 1 个输入,但它收到 3 个输入张量

时间:2021-07-14 11:05:24

标签: python tensorflow machine-learning keras

我有以下代码试图创建 resnet32 架构并训练模型。该代码包含 Resnet32 的模型架构,用于抛出错误的输入形状,表示 ValueError:层 conv2d_3 需要 1 个输入,但它收到了 3 个输入张量。

from keras.datasets import cifar10
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
X_train = X_train.astype('float32') 
X_test = X_test.astype('float32') 
X_train = X_train / 255.0 
X_test = X_test / 255.0
y_train = tf.keras.utils.to_categorical(y_train) 
y_test = tf.keras.utils.to_categorical(y_test) 
num_classes = y_test.shape[1]

Resn​​et 架构

def building_block(X, filter_size, filters, stride=1):   
    X_shortcut = X
    # Reshape shortcut for later adding if dimensions change
    if stride > 1:
        X_shortcut = Conv2D(filters, (1, 1), strides=stride, padding='same')(X_shortcut)
        X_shortcut = BatchNormalization(axis=3)(X_shortcut) 
    X = Conv2D(filters, kernel_size = filter_size, strides=stride, padding='same')(X)
    X = BatchNormalization(axis=3)(X)
    X = Activation('relu')(X)
    X = Conv2D(filters, kernel_size = filter_size, strides=(1, 1), padding='same')(X)
    X = BatchNormalization(axis=3)(X)
    X = add([X, X_shortcut])  # Add shortcut value to main path
    X = Activation('relu')(X)
    return X
def create_model(input_shape, classes, name):
    # Define the input
    X_input = input_shape
    # Stage 1
    X = Conv2D(filters=16, kernel_size=3, strides=(1, 1), padding='same')(X_input)
    X = BatchNormalization(axis=3)(X)
    X = Activation('relu')(X)

    # Stage 2
    X = building_block(X, filter_size=3, filters=16, stride=1)
    X = building_block(X, filter_size=3, filters=16, stride=1)
    X = building_block(X, filter_size=3, filters=16, stride=1)
    X = building_block(X, filter_size=3, filters=16, stride=1)
    X = building_block(X, filter_size=3, filters=16, stride=1)

    # Stage 3
    X = building_block(X, filter_size=3, filters=32, stride=2)  # dimensions change (stride=2)
    X = building_block(X, filter_size=3, filters=32, stride=1)
    X = building_block(X, filter_size=3, filters=32, stride=1)
    X = building_block(X, filter_size=3, filters=32, stride=1)
    X = building_block(X, filter_size=3, filters=32, stride=1)

    # Stage 4
    X = building_block(X, filter_size=3, filters=64, stride=2)  # dimensions change (stride=2)
    X = building_block(X, filter_size=3, filters=64, stride=1)
    X = building_block(X, filter_size=3, filters=64, stride=1)
    X = building_block(X, filter_size=3, filters=64, stride=1)
    X = building_block(X, filter_size=3, filters=64, stride=1)

    # Average pooling and output layer
    X = GlobalAveragePooling2D()(X)
    X = Dense(classes, activation='softmax')(X)

    # Create model
    model = Model(inputs=X_input, outputs=X, name=name)
    return model

# Define optimizer and compile model
input_shape=X_train.shape[1:]
optimizer = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
ResNet32 = create_model(input_shape=input_shape, classes=2, name='ResNet32')
ResNet32.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics = ['accuracy'])
datagen = ImageDataGenerator(
        rotation_range=30,  # randomly rotate images in the range (degrees, 0 to 180)
        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
        horizontal_flip=True)  # randomly flip images
# Train model
results = ResNet32.fit_generator(datagen.flow(X_train, y_train,
                                 batch_size = 250),
                                 epochs = 100,
                                 steps_per_epoch=200,  # data_size/batch size
                                 validation_data=(X_test, y_test))

错误跟踪

<ipython-input-45-91e0f2fc304c> in <module>()
     71 input_shape=X_train.shape[1:]
     72 optimizer = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
---> 73 ResNet32 = create_model(input_shape=input_shape, classes=2, name='ResNet32')
     74 ResNet32.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics = ['accuracy'])
     75 

2 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    198                      str(len(input_spec)) + ' input(s), '
    199                      'but it received ' + str(len(inputs)) +
--> 200                      ' input tensors. Inputs received: ' + str(inputs))
    201   for input_index, (x, spec) in enumerate(zip(inputs, input_spec)):
    202     if spec is None:

ValueError: Layer conv2d_3 expects 1 input(s), but it received 3 input tensors. Inputs received: [<tf.Tensor: shape=(), dtype=int32, numpy=32>, <tf.Tensor: shape=(), dtype=int32, numpy=32>, <tf.Tensor: shape=(), dtype=int32, numpy=3>]

1 个答案:

答案 0 :(得分:0)

我通过定义模型的输入来解决这个问题,如下所示:

def create_model(input_shape, classes, name):
    # Define the input
    X_input = Input(input_shape)