ValueError:检查目标时出错:预期conv2d_19具有形状(None,320,320,1)但得到形状为(18,320,320,2)的数组

时间:2019-09-04 15:03:48

标签: python tensorflow keras conv-neural-network

我尝试使用常规分割(不是多级)在喀拉拉邦训练U-Net模型,但是出现以下错误:

  

ValueError:检查目标时出错:预期conv2d_19具有形状(无,320、320、1)但形状为(18、320、320、2)的数组

我知道这与输入Y_train有关,我与keras.utils.to_categorical函数有关,但是to_categorical(Y_train)和to_categorical(Y_train,2)返回此错误,而to_categorical(Y_train,1)返回IndexError:索引1超出了轴1的大小1。

我什至试图通过将Y_train重塑为(None,320,320,1)来更改它,但是它返回了

  

'ValueError:您正在传递形状为(18、320、320、1)的目标数组,同时将categorical_crossentropy用作损耗。 categorical_crossentropy期望目标是形状(样本,类)的二进制矩阵(1s和0s)。如果目标是整数类,则可以通过以下方式将它们转换为预期的格式:   从keras.utils导入to_categorical   y_binary = to_categorical(y_int)   另外,您也可以使用损失函数sparse_categorical_crossentropy,它确实需要整数目标。'

使用训练功能尝试拟合模型时,会返回错误。

    def preprocess_images(X_train, Y_train, X_test, Y_test):

        X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2], 1)
        X_test = X_test.reshape(X_test.shape[0], X_train.shape[1], X_train.shape[2], 1)

        X_train = X_train.astype("float32")
        X_test = X_test.astype("float32")
        X_train /= 255
        X_test /= 255

        Y_train /= 255
        Y_test /= 255
        #Y_train = Y_train.reshape(Y_train.shape[0], Y_train.shape[1], Y_train.shape[2], 1)
        #Y_test = Y_test.reshape(Y_test.shape[0], Y_train.shape[1], Y_train.shape[2], 1)
        Y_train = keras.utils.to_categorical(Y_train, 1)
        Y_test = keras.utils.to_categorical(Y_test, 1)

        return ((X_train, Y_train), (X_test, Y_test))


    def CNNs_layers(nb_conv_pool = 1, filters = 32, kernel_size = (3, 3), activation = 'relu', pool_size = (2 ,2), dropout_rate = 0.25, input_shape = (28, 28, 1), data_format = 'channels_last'):


        inputs = tf.keras.layers.Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
        s = tf.keras.layers.Lambda(lambda x: x / 255)(inputs)

        c1 = tf.keras.layers.Conv2D(16, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(s)
        c1 = tf.keras.layers.Dropout(0.1)(c1)
        c1 = tf.keras.layers.Conv2D(16, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(c1)
        p1 = tf.keras.layers.MaxPooling2D((2, 2))(c1)

        c2 = tf.keras.layers.Conv2D(32, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(p1)
        c2 = tf.keras.layers.Dropout(0.1)(c2)
        c2 = tf.keras.layers.Conv2D(32, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(c2)
        p2 = tf.keras.layers.MaxPooling2D((2, 2))(c2)

        c3 = tf.keras.layers.Conv2D(64, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(p2)
        c3 = tf.keras.layers.Dropout(0.2)(c3)
        c3 = tf.keras.layers.Conv2D(64, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(c3)

        p3 = tf.keras.layers.MaxPooling2D((2, 2))(c3)

        c4 = tf.keras.layers.Conv2D(128, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(p3)
        c4 = tf.keras.layers.Dropout(0.2)(c4)
        c4 = tf.keras.layers.Conv2D(128, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(c4)
        p4 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(c4)

        c5 = tf.keras.layers.Conv2D(256, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(p4)
        c5 = tf.keras.layers.Dropout(0.3)(c5)
        c5 = tf.keras.layers.Conv2D(256, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(c5)

        u6 = tf.keras.layers.Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(c5)
        u6 = tf.keras.layers.concatenate([u6, c4])
        c6 = tf.keras.layers.Conv2D(128, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(u6)
        c6 = tf.keras.layers.Dropout(0.2)(c6)
        c6 = tf.keras.layers.Conv2D(128, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(c6)

        u7 = tf.keras.layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(c6)
        u7 = tf.keras.layers.concatenate([u7, c3])
        c7 = tf.keras.layers.Conv2D(64, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(u7)
        c7 = tf.keras.layers.Dropout(0.2)(c7)
        c7 = tf.keras.layers.Conv2D(64, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(c7)

        u8 = tf.keras.layers.Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(c7)
        u8 = tf.keras.layers.concatenate([u8, c2])
        c8 = tf.keras.layers.Conv2D(32, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(u8)
        c8 = tf.keras.layers.Dropout(0.1)(c8)
        c8 = tf.keras.layers.Conv2D(32, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(c8)

        u9 = tf.keras.layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same')(c8)
        u9 = tf.keras.layers.concatenate([u9, c1], axis=3)
        c9 = tf.keras.layers.Conv2D(16, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(u9)
        c9 = tf.keras.layers.Dropout(0.1)(c9)
        c9 = tf.keras.layers.Conv2D(16, (3, 3), activation=tf.keras.activations.elu, kernel_initializer='he_normal',
                                padding='same')(c9)

        outputs = tf.keras.layers.Conv2D(1, (1, 1), activation='sigmoid')(c9)

        model = tf.keras.Model(inputs=[inputs], outputs=[outputs])
         model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
        model.summary()

        return (model)


    def training(model, X_train, Y_train, loss = 'categorical_crossentropy', optimizer='adam', metrics=['accuracy'], batch_size = 32, nb_epoch = 100, verbose = 2, validation_split = 0.3):
        model.compile(loss = loss, optimizer = optimizer, metrics = metrics)
        history = model.fit(X_train, Y_train, batch_size = batch_size, epochs = nb_epoch, verbose = verbose, validation_split = validation_split)
        return (history)

1 个答案:

答案 0 :(得分:0)

假设标签是一次性编码的,则应将输出层更改为:

outputs = tf.keras.layers.Conv2D(2, (1, 1), activation='softmax')(c9)

损失函数为categorical_crossentropy。如果您想使用当前模型并且不做任何修改就损失,那么您必须对标签进行一次热编码,因为binary_crossentropy直接获取0-1个二进制标签而无需进行一次热编码