I have successfully trained a simple model in Keras to classify images:
model = Sequential()
model.add(Convolution2D(32, 3, 3, border_mode='valid', input_shape=(img_channels, img_rows, img_cols),
activation='relu', name='conv1_1'))
model.add(Convolution2D(32, 3, 3, activation='relu', name='conv1_2'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(64, 3, 3, border_mode='valid', activation='relu', name='conv2_1'))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
I can also predict the image classes using
y_pred = model.predict_classes(img, 1, verbose=0)
However the output of y_pred
is always binary. This also seems to be the case when using predict_proba
and predict
. My outputs are in this form
[[ 1. 0. 0. 0.]]
[[ 0. 1. 0. 0.]]
This works OK, but I'd like to have a probability percent for each classification, for example
[[ 0.8 0.1 0.1 0.4]]
How do I get this in Keras?
Model Prediction. Prediction is the final step and our expected outcome of the model generation. Keras provides a method, predict to get the prediction of the trained model.
Input parameters that influence output in a Keras model. Optimizer. Optimizer/loss function used to minimize loss. Usage: One of two arguments required for compiling a Keras model: Set of Losses and Metrics. When a model is compiled, compile () includes required losses and metrics:
This is a simple Keras model which should work as a first iteration step. However, due to the small amount of data you provided us I cannot get any meaningful results after training. Thanks for contributing an answer to Data Science Stack Exchange! Please be sure to answer the question. Provide details and share your research! But avoid …
Finalize Model Before you can make predictions, you must train a final model. You may have trained models using k-fold cross validation or train/test splits of your data. This was done in order to give you an estimate of the skill of the model on out of sample data, e.g. new data.
Softmax might yield "one-hot" like output. Consider the following example:
# Input; Exponent; Softmax value
20 485165195 0.99994
9 8103 0.00002
5 148 0.00000
10 22026 0.00005
------------------------
# Sum 485195473 1
Since the exponential function grows very fast softmax
starts yielding one-hot like output starting from order of magnitude 1. In Keras implementation of the softmax
function the maximum value is subtracted from the input, but in the stated above case it won't make any difference.
Possible ways to fix this:
Make sure that input images are rescaled, so that pixels values are between 0
and 1
.
Add some regularizers to your model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With