Then, I loaded this trace file in C++ and made prediction for a test image. Then I compared the result with the output in python. I observed that results are very different. I guess that the reason originates from that warning.
The tracer uses the example inputs you provide and records the operations. If a different input would result in a different operation, then this will not be captured by the tracer. Here it’s recording the arguments to the slice operation as constants (so it will always slice it with the values produced with the example inputs).
To get around this, put any code with data-dependent control flow inside a ScriptModule, and then call that in your traced code.
torch.jit.TracingCheckError: Tracing failed sanity checks!
Encountered an exception while running the Python function with test inputs.
Exception:
center_slice_helper() expected value of type Tensor for argument 'diff_y' in position 1, but instead got value of type int.
Value: 0
Declaration: center_slice_helper(Tensor layer, Tensor diff_y, Tensor diff_x, Tensor h_end, Tensor w_end) -> Tensor
I debugged the code and observed that x parameter of forward function has this type:
Our support for non-tensor types in tracing is pretty limited (see #14455), but your model looks like using script mode would work well. See something like: