I am trying to convert a model I downloaded to TorchScript.
loaded_model = keras.models.load_model("model.hdf5")
loaded_model.predict(X) #this works, where X is a numpy array
torch.jit.trace(loaded_model.predict, tf.convert_to_tensor(X)) #returns the error RuntimeError: Tracer cannot infer type of (<tf.Tensor: shape=(60, 6), dtype=float16, numpy=... dtype=float16)>)
:Only tensors and (possibly nested) tuples of tensors, lists, or dictsare supported as inputs or outputs of traced functions, but instead got value of type EagerTensor.
I then disable eager execution with tf.compat.v1.disable_eager_execution()
, but get the following error:
OperatorNotAllowedInGraphError: Iterating over a symbolic `tf.Tensor` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.
I do not have access to decorate this function myself, since it’s just a model I downloaded but do not have access to the code. Is there a way to make trace work here?