Making predictions and determine class on binary tensorflow model

Viewed 133

I have implemented the following version of my ResNet50. I trained my model with my own data in another notebook so I just load the weights and compile the model. Now, I just want to make predictions on my new unseen data.

def resnet50F(im_size):

    resnet = ResNet50(input_shape=(im_size, im_size, 3), weights='imagenet', include_top = False)
    headModel = AvgPool2D(pool_size=(3,3))(resnet.output)
    headModel = Flatten(name='flatten')(headModel)
    headModel = Dense(256, activation='relu')(headModel)
    headModel = Dropout(0.5)(headModel)
    headModel = Dense(1, activation='sigmoid')(headModel)

    model = Model(inputs=resnet.input, outputs=headModel)

    model.trainable = True

    return model


resnet50 = resnet50F(im_size=224)
resnet50.load_weights(PATH_MODEL_WEIGHTS)
opt = optimizers.Adam(learning_rate=1e-6)
resnet50.compile(loss='binary_crossentropy', optimizer=opt, metrics=METRICS)
predictions = resnet50.predict(X)

However, when I print predictions I get the following output:

 [[4.22752373e-06]
 [2.81104029e-10]
 [3.21204737e-02]
 [5.09007333e-12]
 [6.25871266e-08]
 [3.95518853e-08]
 [3.76289577e-09]
 [1.04685043e-07]
 [4.40788448e-01]
 [4.18029167e-09]
 [1.68976447e-04]
 [4.83552366e-03]
 [5.67837298e-01]
 [1.92822833e-02]
 [1.86168763e-04]
 [3.30054699e-11]
 [1.55285016e-01]
 [1.40850764e-12]
 [4.75460291e-02]
 [2.36899691e-08]
 [1.91837142e-04]
 [2.70789745e-03]
 [2.28864295e-07]
 [1.04725331e-08]
 [3.17185315e-15]
 [1.86515141e-08]
 [9.09119472e-03]
 [2.67773657e-06]
 [6.43107248e-03]
 [1.06139310e-14]
 [3.12786847e-01]
 [1.47488710e-04]
 [7.75789477e-09]
 [2.05256441e-03]
 [5.19017190e-11]
 [6.54808059e-02]
 [9.27565736e-04]
 [6.90304815e-26]
 [8.59875661e-14]
 [2.54806340e-01]
 [1.05227390e-02]
 [4.43476923e-02]
 [3.65121141e-02]
 [4.71908916e-13]
 [1.16901109e-02]
 [2.83952375e-07]
 [6.87847793e-01]
 [6.25556211e-08]
 [2.92979064e-03]
 [1.00091375e-08]
 [7.29291560e-06]
 [7.43216195e-16]
 [1.16142066e-04]
 [6.63836045e-06]
 [4.89238771e-12]
 [3.75503966e-08]
 [7.99435584e-05]
 [5.35736717e-06]
 [2.15524092e-11]
 [1.89218114e-14]
 [4.04082388e-02]
 [1.11348586e-09]
 [1.72054302e-03]
 [2.21202258e-11]
 [2.13359108e-08]
 [2.09557402e-05]
 [1.01457292e-04]
 [9.81324539e-03]
 [9.62927871e-08]
 [4.38750768e-03]
 [7.26699904e-02]
 [6.57562000e-16]
 [4.28197110e-13]]

As I understand it, it's supposed to represent the probability of my model to belong to class 1. So there's either only one sample predicted as class 1 (5.67837298e-01) or I am missing something in my methodology.

3 Answers

First question is what is X? I assume X is an np array of images and each image is itself an np array. Now you have to make sure that what ever processing you did on the training images you do the same processing on the X images. For example if you trained on rgb images, the images in X must be rgb images. If you resized the training images you must resize the X images to the same size. If you scaled the pixel values for the training images you must rescale the pixels for the X images. try this code once you have insured of the above

preds=model.predict(X)
for p in preds:
    if P>=.5:
        klass=1
    else:
        klass=0
    print(klass)

Just print the decode_predictions

from keras.applications.resnet50 import  decode_predictions

print('Predicted:', decode_predictions(predictions,top=5 )[0])

To show the class

classes = np.argmax(predictions )

This is a full example Real Time Prediction using ResNet Mode

Yes, you missed training your dataset. You need to again train the dataset with your defined model:

resnet50.fit(train_generator, epochs=5, validation_data=validation_generator)

Output:

Epoch 1/5
1/1 [==============================] - 16s 16s/step - loss: 0.8554 - Acc: 0.5500 - val_loss: 0.7055 - val_Acc: 0.5000
Epoch 2/5
1/1 [==============================] - 0s 388ms/step - loss: 0.8787 - Acc: 0.6000 - val_loss: 0.7056 - val_Acc: 0.5000
Epoch 3/5
1/1 [==============================] - 0s 379ms/step - loss: 0.6170 - Acc: 0.6000 - val_loss: 0.7058 - val_Acc: 0.5000
Epoch 4/5
1/1 [==============================] - 0s 385ms/step - loss: 0.7834 - Acc: 0.6500 - val_loss: 0.7058 - val_Acc: 0.5000
Epoch 5/5
1/1 [==============================] - 0s 384ms/step - loss: 0.7088 - Acc: 0.5000 - val_loss: 0.7053 - val_Acc: 0.5000
<keras.callbacks.History at 0x7f59d00b3a90>

and then check the predictions.

predictions = resnet50.predict(test_gen) # I have used only 10 images for test_set
predictions

Output:

array([[0.5892666 ],
       [0.58739895],
       [0.580309  ],
       [0.5807614 ],
       [0.5811843 ],
       [0.5874025 ],
       [0.5829852 ],
       [0.58085686],
       [0.5907322 ],
       [0.587012  ]], dtype=float32)
Related