Keras 中语义分割的两个并行模型

数据挖掘 Python 深度学习 喀拉斯 图像分类 计算机视觉
2022-02-14 06:55:49

我想在 Keras 中为图像语义分割建立两个并行模型。

    input1 = Input(shape=(480,480,3))
    input2 = Input(shape=(480,480,1))

    c1_1 = Conv2D(filters=64, kernel_size=(3,3), activation='relu',  padding='same')(input1)
    c1_1 = MaxPool2D(strides=(2,2))(c1_1)

    c2_1 = Conv2D(filters=16, kernel_size=(3,3), activation='relu',  padding='same')(input2)
    c2_1 = MaxPool2D(strides=(2,2))(c2_1)

   (...)

    # Merge to models:
    c = concatenate([c1_n, c2_n], axis=3)
    c = UpSampling2D(size=(2,2))(c)
    c = Conv2D(filters=512, kernel_size=(3,3), activation='relu', padding='same')(c)

   (...)

   output_layer = Conv2D(6, kernel_size=(1,1), activation='softmax')(c)

   model = Model([input1, input2], output_layer)

   model.compile(optimizer=Adam(2e-4), loss='categorical_crossentropy', metrics=['categorical_accuracy'])


   def my_generator(x_train, y_train, batch_size):
     data_generator = ImageDataGenerator(
        (...).flow(x_train, x_train, batch_size, seed=42)
     mask_generator = ImageDataGenerator(
        (...).flow(y_train, y_train, batch_size, seed=42)

     while True:
        x_batch, _ = data_generator.next()
        y_batch, _ = mask_generator.next()
        yield [x_batch[:,:,:,:3], x_batch[:,:,:,3]], y_batch

     #X = [72, 480, 480, 4]
     #Y = [72, 480, 480, 5]
     model.fit_generator(my_generator(X, Y, 7),
                       steps_per_epoch = 60,
                       validation_data = (X_test, Y_test),
                       epochs=150, verbose=2)

但我有一个错误:

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[[ 0.33891213,  0.37238494,  0.33054393,  0.        ],
     [ 0.34728033,  0.38493724,  0.33472803,  0.        ],
     [ 0.35146444,  0.39330544,  0.35983264,  0.        ],
     ....

你能帮我解决我的错误或告诉我这个假设是否正确吗?

1个回答

这可能是laye,但如果其他人被卡住,请查看此链接

基本上,您的模型需要此处定义的两个输入:

model = Model([input1, input2], output_layer)

因此,您将需要传递具有与您在此处定义的相同形状的两个输入的列表:

input1 = Input(shape=(480,480,3))
input2 = Input(shape=(480,480,1))