Files
audio-driven-face-animation/src/utils/inference_example.py
T
Łukasz Zalewski 1f326bb5e9 Dev (#99)
* format files

* remove best metric score callback

* update readme
2021-03-15 17:24:56 +01:00

43 lines
1.2 KiB
Python

from PIL import Image
from src.models.mnist_model import LitModelMNIST
from src.transforms import mnist_transforms
def predict():
"""Example of inference with trained model.
It loads trained image classification model from checkpoint.
Then it loads example image and predicts its label.
"""
# ckpt can be also a URL!
CKPT_PATH = "last.ckpt"
# load model from checkpoint
# model __init__ parameters will be loaded from ckpt automatically
# you can also pass some parameter explicitly to override it
trained_model = LitModelMNIST.load_from_checkpoint(checkpoint_path=CKPT_PATH)
# print model hyperparameters
print(trained_model.hparams)
# switch to evaluation mode
trained_model.eval()
trained_model.freeze()
# load data
img = Image.open("data/example_img.png").convert("L") # convert to black and white
# img = Image.open("data/example_img.png").convert("RGB") # convert to RGB
# preprocess
img = mnist_transforms.mnist_test_transforms(img)
img = img.reshape((1, *img.size())) # reshape to form batch of size 1
# inference
output = trained_model(img)
print(output)
if __name__ == "__main__":
predict()