如何通过TensorFlow C API识别tensorflow.contrib.resampler算子并执行冻结图?
Resampler op from tf.contrib.resampler? Problem Description
I have a TensorFlow frozen graph (.pb format) that uses the tensorflow.contrib.resampler op, and I need to load and execute it in a C application using c_api.h.
In Python, I can successfully load and run the graph by running this code first:
import tensorflow as tf tf.contrib.resampler
But when using the C API, I can't find a way to achieve the same effect, and I get this runtime error:
Failed to process frame with No OpKernel was registered to support Op 'Resampler' with these attrs. Registered devices: [CPU,GPU], Registered kernels: <no registered kernels>
How can I make TensorFlow recognize this op via the C API?
Solution
This issue happens because the TensorFlow C API doesn't automatically load ops from the contrib module by default. In Python, importing tf.contrib.resampler triggers the op's registration logic, but this doesn't happen automatically in C. Here are two workable solutions:
Method 1: Compile a custom TensorFlow C library with the resampler op included
The resampler op from contrib isn't included in the precompiled TensorFlow C libraries, so you'll need to build TensorFlow from source to include it:
- Clone the TensorFlow source repository and switch to the exact branch matching the TensorFlow version used to generate your frozen graph (version mismatch will cause compatibility issues).
- During the build configuration (using the
configurescript), ensure that options related tocontribops are enabled. For TensorFlow 1.x (sincetf.contribwas mostly deprecated in 2.x), you can also check the BUILD files to make sure theresamplertarget is included in the C library build. - Compile the source to generate
libtensorflow.so(Linux/macOS) ortensorflow.dll(Windows), then replace the precompiled TensorFlow library in your C project with this custom build.
Method 2: Manually register the resampler op via a dynamic library
If rebuilding the entire TensorFlow library feels overkill, you can compile just the resampler op code into a dynamic library and register it in your C app:
- Extract the relevant
resamplercode from the TensorFlow source: locate files in thetensorflow/contrib/resamplerdirectory, likeresampler_ops.ccandresampler_ops.h. - Create a wrapper file (e.g.,
resampler_register.cc) to expose the op registration function:#include "tensorflow/contrib/resampler/resampler_ops.h" #include "tensorflow/core/framework/op_kernel.h" extern "C" void RegisterResamplerOps() { // Trigger registration of the resampler ops tensorflow::RegisterResamplerOps(); } - Compile this wrapper along with the
resamplersource files into a dynamic library (e.g.,libresampler_ops.so), making sure to link against TensorFlow's headers and libraries during compilation. - In your C application, load this dynamic library, call the registration function, then load your frozen graph as usual:
#include <dlfcn.h> #include "tensorflow/c/c_api.h" int main() { // Load the resampler ops dynamic library void* resampler_lib = dlopen("./libresampler_ops.so", RTLD_NOW); if (!resampler_lib) { fprintf(stderr, "Failed to load libresampler_ops.so: %s\n", dlerror()); return 1; } // Get the registration function void (*RegisterResamplerOps)() = (void (*)())dlsym(resampler_lib, "RegisterResamplerOps"); if (!RegisterResamplerOps) { fprintf(stderr, "Failed to find RegisterResamplerOps: %s\n", dlerror()); dlclose(resampler_lib); return 1; } // Register the resampler ops RegisterResamplerOps(); // Proceed to load and run your .pb graph TF_Graph* graph = TF_NewGraph(); TF_Status* status = TF_NewStatus(); // ... Add your graph loading and execution code here ... // Clean up resources dlclose(resampler_lib); TF_DeleteGraph(graph); TF_DeleteStatus(status); return 0; }
Key Notes
- Version Matching: Always ensure the TensorFlow version of your custom build/dynamic library matches exactly the version used to create the frozen graph. Mismatched versions will lead to op incompatibility or registration failures.
- GPU Support: If your frozen graph was built for GPU, make sure to enable GPU support during compilation, and ensure your C application can load the required CUDA libraries at runtime.
内容的提问来源于stack exchange,提问作者The Fiddler

