简体   繁体   中英

model.predict() returns classes and not probabilities

Hello!

I am using Keras for first time. I trained and saved a model. (as a json file and its weights too) The model it is intended to classify an image among 3 classes. My compile method :

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

After that, I load the model and its weights and i try to make a prediction for a random image

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

How to i get a list with the probabilities? For example [75% 15% 10%] Currently i get as an output

[[5.571262e-21 0.000000e+00 1.000000e+00]]
2

This is the model summary print(loaded_model.summary()) Model successfully loaded from disk!

_________________________________________________________________
Layer (type)                 Output Shape              Param #  
=================================================================
conv2d_1 (Conv2D)            (None, 222, 222, 64)      1792      
_________________________________________________________________
activation_1 (Activation)    (None, 222, 222, 64)      0        
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 111, 111, 64)      0        
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 109, 109, 64)      36928    
_________________________________________________________________
activation_2 (Activation)    (None, 109, 109, 64)      0        
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 54, 54, 64)        0        
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 52, 52, 128)       73856    
_________________________________________________________________
activation_3 (Activation)    (None, 52, 52, 128)       0        
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 26, 26, 128)       0        
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 24, 24, 256)       295168    
_________________________________________________________________
activation_4 (Activation)    (None, 24, 24, 256)       0        
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 12, 12, 256)       0        
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 10, 10, 512)       1180160  
_________________________________________________________________
activation_5 (Activation)    (None, 10, 10, 512)       0        
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 5, 5, 512)         0        
_________________________________________________________________
flatten_1 (Flatten)          (None, 12800)             0        
_________________________________________________________________
dense_1 (Dense)              (None, 512)               6554112  
_________________________________________________________________
activation_6 (Activation)    (None, 512)               0        
_________________________________________________________________
dropout_1 (Dropout)          (None, 512)               0        
_________________________________________________________________
dense_2 (Dense)              (None, 3)                 1539      
_________________________________________________________________
activation_7 (Activation)    (None, 3)                 0        
=================================================================
Total params: 8,143,555
Trainable params: 8,143,555
Non-trainable params: 0

You already have the probabilities :)

Look at your list:

[[5.571262e-21 0.000000e+00 1.000000e+00]]

and the probabilities are:

0, 0, 1

Btw, it looks like the overfitting . Check this page: https://www.kdnuggets.com/2015/04/preventing-overfitting-neural-networks.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM