import numpy as npimport tensorflow as tf# Load TFLite model and allocate tensors.interpreter = tf.lite.Interpreter(model_path="tutorial1.tflite")interpreter.allocate_tensors()
Sample the tflite model to a surface
defsample(): input_details = interpreter.get_input_details() output_details = interpreter.get_output_details()# Set the 'latent' input tensor. input_shape = input_details[0]['shape'] latent = (np.random.random_sample(input_shape)-0.5) *2.0 input_data = np.array(latent, dtype=np.float32) interpreter.set_tensor(input_details[0]['index'], input_data) interpreter.invoke()# Get the output image and transform it for display result = interpreter.get_tensor(output_details[0]['index']) result = np.reshape(result, [256,256,3]) result = (result +1.0) *127.5 result = pygame.surfarray.make_surface(result) result = pygame.transform.rotate(result, -90)return result