Open gerbenvv opened 3 years ago
Sorry for the late reply. Took some time to get a proper env to try it out. The results are quite different for yours though.
The calculation error doesn't seem to occur, as seen in the output True 0.0 True 0.0 True 0.0
The performance is very interesting. The first run is always super long in my env. So I changed the code to do the initial run and count next 100 times for the average.
# don't count the first run which seem to take super long to warm up
start = time.perf_counter()
output_f = concrete_f(inputs)
end = time.perf_counter()
print("warm-up time...")
print(end - start) # 0.06361602060496807
print("start performance tests...")
count=100
for i in range(count):
total_time = 0 if i == 0 else total_time
start = time.perf_counter()
output_f = concrete_f(inputs)
end = time.perf_counter()
time_taken = end - start
total_time += time_taken
print("average time for tf.nn.conv2d is {}".format(total_time/count) )
for i in range(count):
total_time = 0 if i == 0 else total_time
start = time.perf_counter()
output_g = concrete_g(inputs)
end = time.perf_counter()
time_taken = end - start
total_time += time_taken
print("average time for tf.nn.convolution is {}".format(total_time/count) )
Here is what I got: warm-up time... 1.4917952585965395 start performance tests... average time for tf.nn.conv2d is 0.09312625492922962 average time for tf.nn.convolution is 0.09329927522689103
Seems conv2d performs slightly better but not sure significantly enough for a major concern.
The different results are likely caused by different system setup. Here is mine. Python version: 3.8.5 ONNX version: 1.8.1 ONNX-TF version: 1.8.0 Tensorflow version: 2.4.1 tf2onnx version: 1.8.5 GPU: Tesla P100
Describe the bug
There is a calculation error and performance hit when exporting certain dilated 2d convolutions from tensorflow to ONNX and then evaluating that ONNX in tensorflow again.
It seems the problem stems from a difference in calculation between
tf.nn.conv2d
andtf.nn.convolution
. These give different results on the same hardware (the error is small, but increases in bigger networks). Another thing is thattf.nn.convolution
seems to be less performant thantf.nn.conv2d
. Right nowtf.nn.convolution
is used in the conversion from ONNX -> TF, however, I believe using the specific functions (tf.nn.conv1/2/3d
) is more appropriate as they give no calculation error when going TF -> ONNX -> TF and are faster.In addtion to that, when using
tf.nn.convolution
the ONNX contains a lot more ops than when usingtf.nn.conv2d
. However, this is a problem of tensorflow-onnx. It's good to be aware of this however.To reproduce
ONNX model file
If you want it, I can give it, or you can save it from my reproduce.
Python, ONNX, ONNX-TF, Tensorflow version
This section can be obtained by running
get_version.py
from util folder.Additional context
GPU used: GTX 1080 ti
Additional speed test of the convolution functions