How to get the probability percentage in keras predicting model CNN
Asked Answered
E

3

7

Here, I am getting the data as [0 1 0 0] or [0 0 0 1],--- I get it that it is telling me that [0 1 0 0] is label2,[0 0 0 1] is label4, [1 0 0 0] is label1, [0 0 1 0] is label3.

import pickle
from keras.preprocessing.sequence import pad_sequences

MAX_SEQUENCE_LENGTH = 1000
MAX_NB_WORDS = 20000

with open ('textsdata', 'rb') as fp:
    texts = pickle.load(fp)

tokenizer = Tokenizer(num_words=MAX_NB_WORDS)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)
inputquery = ["Play some music will ya"]
sequences = tokenizer.texts_to_sequences(inputquery)
model = load_model('my_model.h5')
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['acc'])
print("sequences", sequences)

data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH)
classes = model.predict(data)
y_classes = classes.argmax(axis=-1)
print(y_classes)

I need it in percentages like it is confident that it is label1 as 0.67 The value before softmax or the value of that which is confident enough to tell it is label1 or label2 or label3 or label4--

I need the percentage of it being any one of them or all of those percentages like...

If a input is given output be like

Class1 - 0.87

Class2 - 0.3

Class3 - 0.5

Class4 - 0.5 How can I get this kind of output rather than just [1 0 0 0] What should i add next to the code above please do tell

Eustace answered 10/10, 2018 at 5:33 Comment(3)
just print your classes variable, that contains uncalibrated probabilities.Wonderland
right now i am having only two classes 0 or 1Eustace
"just print your classes variable, that contains uncalibrated probabilities" -- How do i do it is the question to be exactEustace
D
0
from keras.models import load_model

from keras.preprocessing import image

model=load_model("/blah/blah/blah")

img = image.load_img(path, color_mode = "grayscale", target_size=(128, 128, 1))

y = image.img_to_array(img)

y = np.expand_dims(y, axis=0)

images = np.vstack([y])

classes = model.predict(images/255.0, batch_size=8, verbose=0)
Dumbwaiter answered 13/3, 2019 at 6:21 Comment(1)
You can force the keras model to display the probability by dividing the image array by 255.0.Dumbwaiter
C
0

There is method named predict_proba that returns probabilities of individual class instead of class prediction. This can be used as

probabilities = model.predict_proba(data)

Find more information in this blog.

Capsaicin answered 13/3, 2019 at 6:35 Comment(1)
According to "keras.io/models/model/" there is no such function!Drag
I
0

predict returns a list containing the predictions. you can use this

results = model.predict(data)
for result in results:
    print(str(result))

this will return

0.99
0.87
0.75

or if you have the classes in another list, which you should.

res = model.predict(data)
results = [[i,r] for i,r in enumerate(res)]
results.sort(key=lambda x: x[1], reverse=True)
for r in results:
    print(classes[r[0]], str(r[1])))

this returns

("classA", 0.99)
("classB", 0.95)
Insecurity answered 7/12, 2020 at 2:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.