Search code examples
pythontensorflowkerashdf5

How can I test a tensorflow model which I have trained on a real-life picture?


I have trained a model with Keras and Tensorflow and generated an .h5 file where the model is saved. Here is my code (I have only included the model and not the data processing snippet so that it will be more readable):

ts = 0.3 # Percentage of images that we want to use for testing. The rest is used for training.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=ts, random_state=42)

# Import of keras model and hidden layers for our convolutional network
from keras.models import Sequential
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers import Dense, Flatten

from tensorflow.python.client import device_lib
from keras import backend as K

# Construction of model
model = Sequential()
model.add(Conv2D(32, (5, 5), activation='relu', input_shape=(120, 320, 1))) 
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu')) 
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))

# Configures the model for training
model.compile(optimizer='adam', # Optimization routine, which tells the computer how to adjust the parameter values to minimize the loss function.
              loss='sparse_categorical_crossentropy', # Loss function, which tells us how bad our predictions are.
              metrics=['accuracy']) # List of metrics to be evaluated by the model during training and testing.

# Trains the model for a given number of epochs (iterations on a dataset) and validates it.
model.fit(X_train, y_train, epochs=5, batch_size=64, verbose=2, validation_data=(X_test, y_test))

# Save entire model to a HDF5 file
model.save('handrecognition_model.h5')

I have trained my model on this data set which contains photos of hand gestures. Now I have a photo which I have taken with laptop camera, lets say it is called "thumbs_up.jpg" and it contains a picture of me doing a thumbs up. I wanna put this picture on the model I trained to see if it will predict it correctly. I know I am probably missing something extremently basic here, but how can I do this? Should I use the .h5 file somehow? Sorry if my question is super basic/obvious I am just legit confused and dont know what to do. Thanks in advance


Solution

  • I think what you are looking for is the predict() function built in to tensorflow. You might also use the evaluate() function to evaluate your model on multiple test images. Here is the guide that explains how it is done: https://www.tensorflow.org/guide/keras/train_and_evaluate?hl=en

    # Evaluate the model on the test data using `evaluate`
    print("Evaluate on test data")
    results = model.evaluate(x_test, y_test, batch_size=128)
    print("test loss, test acc:", results)
    
    # Generate predictions (probabilities -- the output of the last layer)
    # on new data using `predict`
    print("Generate predictions for 3 samples")
    predictions = model.predict(x_test[:3])
    print("predictions shape:", predictions.shape)
    

    You don't even have to save your model. Just load the image from the path where it is saved.

    The predict function takes in a numpy array. You can use the cv2 library to read an image as a numpy array. This is the process I use:

    # read image
    image = cv2.imread(image_path)
    # resize image
    dimensions = (img_height, img_width)
    image = cv2.resize(image, dimensions)
    # change color channels form bgr to rgb
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    # normalise data
    image = (image/255).astype(np.float16)
    # add image to array
    image_data = np.concatenate(image)