5

I load a model I trained. This is the last layer from training:

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(3))
model.add(Activation('sigmoid'))

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

After that I try to make a prediction to a random image. So I load the model:

#load the model we created
json_file = open('/path/to/model_3.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weight into model
loaded_model.load_weights("/path/to/model_3.h5")
print("\nModel successfully loaded from disk! ")


# Predicting images
img =image.load_img('/path/to/image.jpeg', target_size=(224, 224))
x = image.img_to_array(img)
x *= (255.0/x.max())
image = np.expand_dims(x, axis = 0)
image = preprocess(image)
preds = loaded_model.predict_proba(image)
pred_classes = np.argmax(preds)
print(preds)
print(pred_classes)

As an output I get this:

[[6.0599333e-26 0.0000000e+00 1.0000000e+00]]
2

Which basically it is like I get [0 0 1] like predict_classes. Though I would like to get probabilities. So I am searching for an output like [0.75 0.1 0.15]. Any ideas?

1

1 Answer 1

5

If you want's probabilities as output of the network you just need to use softmax activation function instead of sigmoid

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(3))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
8
  • 1
    I changed to softmax now i get [1 0 0
    – Ioan Kats
    Commented Feb 5, 2018 at 16:04
  • which is a distribution, that just means that the network is sure its the first one. There are other functions like "log-softmax" which give less confident probabilities as output but i think softmax si the only one provided by keras Commented Feb 5, 2018 at 16:10
  • Ok i see! But is it normal for every foto i tried to get combinations of this ? ([0 1 0]). I mean the network is so sure about all the photos?
    – Ioan Kats
    Commented Feb 5, 2018 at 16:28
  • For example i trained the model to pictures of birds and cats and the third class is a class which has mixed photos. That is the probability that an image wont be not a bird nor a cat.When i try to classify an image with a cat and a person, i should get fro example [ 0,45 0,45 0,1] for classes [cat, random, bird]. But i get just [1, 0 ,0].Thats why i am telling i dont get probabilities!
    – Ioan Kats
    Commented Feb 5, 2018 at 16:35
  • softmax tend to generate vector where only one is high and all the rest are low, but if you get always two zeros and a one it's a bit strange. in the case of a cat + human being 1 0 0 it makes sense because if your network is learning to classify between cat and bird if it sees a cat it's likely to say it's a cat for sure, the network has no idea that there is a human in the picture. But make sure you are not overfitting, that may be another reason why the output is always one hot Commented Feb 5, 2018 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.