I trained a model to categorize pictures in 7 different types. my Model can only do a specific prediction (numpy.ndarray in my case), but I am interested to have a prediction which is more like a probability (For example 90% class1 and 80% class2 ...etc). Where is the part of my code which I should change now? how l get correct Probability value using the train model for each class
import tensorflow as tf
from tensorflow.keras.layers import Input, Lambda, Dense,Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.inception_v3 import preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img
from tensorflow.keras.models import Sequential
import numpy as np
from glob import glob
from google.colab import drive
from google.colab import drive
drive.mount('/content/drive')
IMAGE_SIZE = [244,244]
train_path = '/content/drive/MyDrive/Programs/Datasets/Train'
test_path = '/content/drive/MyDrive/Programs/Datasets/Test'
folders = glob('/content/drive/MyDrive/Programs/Datasets/Train/*')
7 categories
['/content/drive/MyDrive/Programs/Datasets/Train/Circle', '/content/drive/MyDrive/Programs/Datasets/Train/Grapes', '/content/drive/MyDrive/Programs/Datasets/Train/Sun', '/content/drive/MyDrive/Programs/Datasets/Train/Tree', '/content/drive/MyDrive/Programs/Datasets/Train/Square', '/content/drive/MyDrive/Programs/Datasets/Train/Triangle', '/content/drive/MyDrive/Programs/Datasets/Train/Leaf', '/content/drive/MyDrive/Programs/Datasets/Train/Pencil']
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(30, (4, 4), activation='relu', input_shape=(224, 224, 3)),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(60, (2, 2), activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(20, activation='relu'),
tf.keras.layers.Dense(len(folders), activation='softmax')])
model.summary()
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory(train_path,
target_size = (224,224),
batch_size = 16,
class_mode = 'categorical')
test_set = test_datagen.flow_from_directory(test_path,
target_size = (224, 224),
batch_size = 16,
class_mode = 'categorical')
model.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
#Traning
r = model.fit_generator(
training_set,
validation_data=test_set,
epochs=5,
steps_per_epoch=len(training_set),
validation_steps=len(test_set)
)
from tensorflow.keras.models import load_model
model.save('my_model.h5')
```
Model prediction Part
```
y_pred = model.predict(test_set)
import numpy as np
y_pred = np.argmax(y_pred, axis=1)
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
model=load_model('my_model.h5')
img=image.load_img('/content/drive/MyDrive/Programs/Datasets/circle604.jpg',target_size=
(224,224))
x=image.img_to_array(img)
x=x/255
import numpy as np
x=np.expand_dims(x,axis=0)
img_data=preprocess_input(x)
img_data.shape
model.predict(img_data)
```
out put of model.predict(img_data)
array([[6.1735226e-09, 5.3491673e-11, 1.6549424e-09, 9.9484622e-01,
5.1531033e-03, 7.3390618e-07, 2.1824545e-16, 4.2561878e-11]],
dtype=float32)
```
# Predict with test data
predictions = model.predict(img_data)
# getting the highet probable digit
predicted_value = np.argmax(model.predict(img_data))
print("The set of predicted values")
print(model.predict(img_data))
print("\nPredicted Class : ", predicted_value)
print("Probability of the Class being ", predicted_value, " is : ",
max(model.predict(img_data)), "\n")
print(type(model.predict(img_data)))
```
#I want get class name and Probability vale for prediction
#But output results is
The set of predicted values
[[6.1735226e-09 5.3491673e-11 1.6549424e-09 9.9484622e-01 5.1531033e-03
7.3390618e-07 2.1824545e-16 4.2561878e-11]]
Predicted Class : 3
Probability of the Class being 3 is : [6.1735226e-09 5.3491673e-11 1.6549424e-09
9.9484622e-01 5.1531033e-03
7.3390618e-07 2.1824545e-16 4.2561878e-11]
<class 'numpy.ndarray'>
CodePudding user response:
predictions
hold the "probability" for each class. The argmax
bit is selecting the class with maximum "probability"
CodePudding user response:
First of all there are 8 categories. Secondly the output of the prediction has shape (1,8) which is technically a list of a single list(a row of column data) so by passing in model.predict(img_data)
you are going to get back the row.
What you need to do is max(model.predict(img_data)[0])
to get the highest value.
To get the class name, that has to do with the method of encoding used on your labels.
Also, if you want, as you stated, your probabilities of each class to be in this form, 90% class1 and 80% class2 ...etc, you should use sigmoid
instead of softmax
as your activation function in the output layer.