kaushalyap
kaushalyap

Reputation: 13597

Tensorflow Prediction always zero

I am Tensorflow newbie. I have model generated using convNetKerasLarge.py and saved as tflite model.

I am trying to test this saved model as follows

import tensorflow as tf
import numpy as np
import glob
from skimage.transform import resize
from skimage import io

# out of previously used training and test set
start = 4001
# no of images
row_count = 1
end = start + row_count

n_image_rows = 106
n_image_cols = 106

np_val_images = np.zeros(shape=(1, 1))
np_val_labels = np.zeros(shape=(1, 1))


def prepare_validation_set():
    global np_val_images
    global np_val_labels

    positive_samples = glob.glob('datasets/drunk_resize_frontal_faces/pos/*')[start:end]
    # negative_samples = glob.glob('datasets/drunk_resize_frontal_faces/neg/*')[start:end]
    # negative_samples = random.sample(negative_samples, len(positive_samples))

    val_images = []
    val_labels = []

    for i in range(len(positive_samples)):
        val_images.append(resize(io.imread(positive_samples[i]), (n_image_rows, n_image_cols)))
        val_labels.append(1)
    # for i in range(len(negative_samples)):
    #    val_images.append(resize(io.imread(negative_samples[i]), (n_image_rows, n_image_cols)))
    #    val_labels.append(0)
    np_val_images = np.array(val_images)
    np_val_labels = np.array(val_labels)


def run_tflite_model(tflite_file, index):

    prepare_validation_set()

    # Initialize the interpreter
    interpreter = tf.lite.Interpreter(model_path=str(tflite_file))
    interpreter.allocate_tensors()

    input_details = interpreter.get_input_details()[0]
    output_details = interpreter.get_output_details()[0]

    test_image = np_val_images[index]

    test_image = np.expand_dims(test_image, axis=0).astype(input_details["dtype"])
    interpreter.set_tensor(input_details["index"], test_image)
    interpreter.invoke()
    output = interpreter.get_tensor(output_details["index"])[0]
    print(output_details)

    prediction = output.argmax()
    print(prediction)


if __name__ == '__main__':

    test_image_index = 1
    tflite_model_file = "models/converted/model.tflite"
    run_tflite_model(tflite_model_file, 0)

If I run this I am getting prediction as 0 even though label should be 1 since I am inputing a positive image. (FYI: Test loss: 0.08881912380456924 Test accuracy: 0.9729166626930237 with 10 epochs). I am confident that there a mistake in my code which causes this please help me find it.

Upvotes: 0

Views: 478

Answers (1)

Lescurel
Lescurel

Reputation: 11631

The script you linked normalize the data before the training by subtracting the mean (here 0.5) and dividing by the standard deviation (here 1):

mean = np.array([0.5,0.5,0.5])
std = np.array([1,1,1])
X_train = X_train.astype('float')
X_test = X_test.astype('float')
for i in range(3):
    X_train[:,:,:,i] = (X_train[:,:,:,i]- mean[i]) / std[i]
    X_test[:,:,:,i] = (X_test[:,:,:,i]- mean[i]) / std[i]

If you don't repeat the same operations before doing a prediction with the model, the input you are passing to the model will not have the same characteristics as the that you trained with.

You could fix it by subtracting the mean (0.5) to the image when preparing the data, i.e:

    np_val_images = np.array(val_images) - 0.5

Upvotes: 1

Related Questions