3

I am a newbie in machine learning. Actually, I used my unet code for image segmentation using one input image slice (192x912) and one output mask image (192x192)

My Unet code is contained several CNN layer and I usually used one input image (192x912) and one its corresponding mask binary image for training. Code related with above explanation is as below.

def get_unet():
    inputs = Input((img_rows, img_cols, 1))
    conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
    conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    drop1 = Dropout(0.2)(pool1)

    conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(drop1)
    conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
    drop2 = Dropout(0.2)(pool2)
    '''''''
    return model

model.fit(imgs_train, imgs_mask_train, batch_size=32, epochs=100, verbose=2, shuffle=True, validation_split=0.1, callbacks=[model_checkpoint])

it works well. But, now, I would like to use multi input image when I train my network. So, I add another train data and edit my code like below.

def get_unet():
        inputs = Input((img_rows, img_cols, 1))
        conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
        conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
        pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
        drop1 = Dropout(0.2)(pool1)

        conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(drop1)
        conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
        pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
        drop2 = Dropout(0.2)(pool2)
        '''''''
        return model

    model.fit([imgs_train, imgs_other_train], imgs_mask_train, batch_size=32, epochs=100, verbose=2, shuffle=True, validation_split=0.1, callbacks=[model_checkpoint])

but when I run the train code, I got error message as below. "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 1 array(s), but instead got the following list of 2 arrays: "

I think my U net needs to be changed for multi input, but I don't know where I have to change. Please help and give me any comments. Thanks.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Tom
  • 531
  • 4
  • 7
  • 19

1 Answers1

1

This is actually rather easy. All you need to do would adjust your input size I believe.

inputs = Input((img_rows, img_cols, size)) would have worked. Or you could have used concat like:

inputs = []
for _ in range(num_inputs):
    inputs.append(Input((self.input_height, self.input_width, self.input_features)))
x = concatenate(inputs)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)

You can check something similar I implemented here

Aniket Chowdhury
  • 332
  • 3
  • 13