0

I have 3 labels and the task is to label images. One image can have multiple labels. I built my CNN but I'm having issues with the last Dense layer:

model = Sequential()
model.add(Conv2D(32, (3,3), padding='same', activation='relu', input_shape=(32,32,3)))
model.add(Conv2D(32, (3,3), padding='same', activation='relu'))
model.add(MaxPooling2D((2,2)))
model.add(Dropout(.25))
model.add(Flatten())
model.add(Dense(3, activation='sigmoid'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)

model.compile(
    optimizer=sgd,
    loss="binary_crossentropy",
)
model.fit(x_train, y_train, epochs=10)

I one hot encoded my y_train, which has a y_train.shape of (120, 3, 3)

y_train[0]:

array([[1., 0., 0.],
       [1., 0., 0.],
       [1., 0., 0.]], dtype=float32)

x_train.shape:

(120, 32, 32, 3)

The error that I'm getting is that:

 binary_crossentropy
        return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)

    ValueError: `logits` and `labels` must have the same shape, received ((32, 3) vs (32, 3, 3)).

In my last Dense layer, I'm not sure what number to put, as I want to keep activation sigmoid, which I understand is better for multi-label classification.

Claudiu Creanga
  • 8,031
  • 10
  • 71
  • 110
  • @KDecker hmm, ok. multiple stackoverlow posts suggested sigmoid with binary_crossentropy: https://stackoverflow.com/a/44165755/3711219 I will check your suggestion to see if I get better results, Thanks! But I would still want to know what I'm doing wrong here. – Claudiu Creanga Nov 23 '22 at 14:46
  • Your labels are being encoded incorrectly. – AloneTogether Nov 23 '22 at 14:55
  • Why does `y_train` have a shape of `(120, 3, 3)`, and not `(120, 3)` ? – AndrzejO Nov 23 '22 at 14:55
  • @AndrzejO Because I have 3 labels. and after one hot encoding label1 is [1,0,0], label2 is [0,1,0] and label 3 is [0,0,1] – Claudiu Creanga Nov 23 '22 at 15:08
  • Are you sure you are not confusing multi-class and multi-label classification? https://stats.stackexchange.com/questions/11859/what-is-the-difference-between-multiclass-and-multilabel-problem – AloneTogether Nov 23 '22 at 15:21
  • @AloneTogether ok so I have multi label classification and I should not hot-encode my labels.. Thanks. Now it works, but it does not converge. Should look into it more. – Claudiu Creanga Nov 23 '22 at 15:25
  • You can..but then you need a different loss function and output layer. – AloneTogether Nov 23 '22 at 15:26
  • 2
    The shape of `y_train` should match the shape of the model output (except for the batch dimension). It's what you want your model to output. If an image contains two labels – for example `(1, 0, 0)` and `(0, 0, 1)` – you want the model output to be `(1, 0, 1)`. So that's what your y_train should look like – AndrzejO Nov 23 '22 at 16:42

0 Answers0