[Python] Keras(FIT_GENERATOR)- Error, when checking target: expected activation_1 to have 3...

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    I've been working on a segmentation problem for many days and after finally finding out how to properly read the dataset, I ran into this problem:


    ValueError: Error when checking target: expected activation_1(Softmax) to have 3 dimensions, but got array with shape (32, 416, 608, 3)​

    I used the functional API, since I took the FCNN architecture from [here](https://github.com/divamgupta/image-segmentation-keras/blob/master/Models/FCN32.py).


    It is slightly modified and adapted in accordance with my task(IMAGE_ORDERING = "channels_last"(TensorFlow backend)). Can anyone please help me? Massive thanks in advance. The architecture below is for FCNN, which I try to implement for the purpose of the segmentation. Here is the architecture(after calling model.summary()):

    1. [​IMG]

    2. [​IMG]


    1. The specific error is: [​IMG]


    2. "Importing the dataset" function: [​IMG]


    3. "Fit_Generator method calling": [​IMG]

      img_input = Input(shape=(input_height,input_width,3))

      #Block 1
      x = Convolution2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1', data_format=IMAGE_ORDERING)(img_input)
      x = BatchNormalization()(x)
      x = Convolution2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2', data_format=IMAGE_ORDERING)(x)
      x = BatchNormalization()(x)
      x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool', data_format=IMAGE_ORDERING)(x)
      f1 = x
      # Block 2
      x = Convolution2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1', data_format=IMAGE_ORDERING)(x)
      x = BatchNormalization()(x)
      x = Convolution2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2', data_format=IMAGE_ORDERING)(x)
      x = BatchNormalization()(x)
      x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool', data_format=IMAGE_ORDERING )(x)
      f2 = x

      # Block 3
      x = Convolution2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1', data_format=IMAGE_ORDERING)(x)
      x = BatchNormalization()(x)
      x = Convolution2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2', data_format=IMAGE_ORDERING)(x)
      x = BatchNormalization()(x)
      x = Convolution2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3', data_format=IMAGE_ORDERING)(x)
      x = BatchNormalization()(x)
      x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool', data_format=IMAGE_ORDERING )(x)
      f3 = x

      # Block 4
      x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1', data_format=IMAGE_ORDERING)(x)
      x = BatchNormalization()(x)
      x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2',data_format=IMAGE_ORDERING)(x)
      x = BatchNormalization()(x)
      x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3',data_format=IMAGE_ORDERING)(x)
      x = BatchNormalization()(x)
      x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool', data_format=IMAGE_ORDERING)(x)
      f4 = x

      # Block 5
      x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1', data_format=IMAGE_ORDERING)(x)
      x = BatchNormalization()(x)
      x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2',data_format=IMAGE_ORDERING)(x)
      x = BatchNormalization()(x)
      x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3', data_format=IMAGE_ORDERING)(x)
      x = BatchNormalization()(x)
      x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool', data_format=IMAGE_ORDERING)(x)
      f5 = x

      x = (Convolution2D(4096,(7,7) , activation='relu' , padding='same', data_format=IMAGE_ORDERING))(x)
      x = Dropout(0.5)(x)
      x = (Convolution2D(4096,(1,1) , activation='relu' , padding='same',data_format=IMAGE_ORDERING))(x)
      x = Dropout(0.5)(x)

      #First parameter = number of classes+1 (de la background)
      x = (Convolution2D(20,(1,1) ,kernel_initializer='he_normal' ,data_format=IMAGE_ORDERING))(x)
      x = Convolution2DTranspose(20,kernel_size=(64,64), strides=(32,32),use_bias=False,data_format=IMAGE_ORDERING)(x)
      o_shape = Model(img_input,x).output_shape

      outputHeight = o_shape[1]
      print('Output Height is:', outputHeight)
      outputWidth = o_shape[2]
      print('Output Width is:', outputWidth)
      #https://keras.io/layers/core/#reshape
      x = (Reshape((20,outputHeight*outputWidth)))(x)
      #https://keras.io/layers/core/#permute
      x = (Permute((2, 1)))(x)
      print("Output shape before softmax is", o_shape)
      x = (Activation('softmax'))(x)
      print("Output shape after softmax is", o_shape)
      model = Model(inputs = img_input,outputs = x)
      model.outputWidth = outputWidth
      model.outputHeight = outputHeight
      model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics =['accuracy'])

    Continue reading...

Compartilhe esta Página