2

My Sequential CNN model is trained on 39 classes as a multi-class classifier. As for predictions, it returns a one-hot encoded array like [0,0,...1,0,...] whereas I want something like [0.012,0.022,0.067,...,0.997,0.0004,...]

Is there a way to get this? if not what exactly should I make to get these?

The reason I want it this way is to verify how close are other classes, so if one says 0.98 and others say 0.96 then I am doing something wrong, data isn't enough, etc..

Thank you :)

My model is basically a keras.model resnet50 with following configs :

model = keras.applications.resnet.ResNet50(include_top=False, weights=None, input_tensor=None, input_shape=(64,64,1), pooling='avg', classes=39)

x = model.output
x = Dropout(0.7)(x)
num_classes = 39
predictions = Dense(num_classes, activation= 'softmax')(x)
model = Model(inputs = model.input, outputs = predictions)

optimizer = keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False)
model.compile(optimizer, loss='categorical_crossentropy', metrics=['categorical_accuracy'], loss_weights=None, sample_weight_mode=None, weighted_metrics=None, target_tensors=None)

Sample input :

import cv2
img = cv2.imread(IMAGE_PATH, 0)
img = cv2.resize(img, (64,64))
img = np.reshape(img, (1,64,64,1))
predicted_class_indices = np.argmax(model.predict(img, verbose = 1))

Sample output:

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

Desired output (numbers are hypothetical):

array([[0.022, 0.353, 0.0535, 0.52, 0212., 0.822, 0.532, 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0.]], dtype=float32)
dhrumil barot
  • 448
  • 2
  • 5
  • 17

1 Answers1

0

One way to do so is to remove the last activation layer (related issue).

You can do so by using model.layers[-1].activation=None.

However, the softmax shouldn't output a one-hot vector but the prob distribution, you might want to check how your training is doing.

Raphael Meudec
  • 686
  • 5
  • 10
  • I do think that there's some issue with the data. However, how can I check whether training is done "right" or not. Why did you mention checking training. Please elaborate. Thank you – dhrumil barot Sep 23 '19 at 15:21
  • What I meant is that your model is predicting with 100% confidence a specific class, so you might want to check what you are training on (mislabelled data?). – Raphael Meudec Sep 24 '19 at 07:55
  • I'm using .flow_from_directory() function to feed the data. Each sub directory has name of label and contains files relevant to that label only – dhrumil barot Sep 24 '19 at 08:33