Tensorflow 2.4.0 on linux Compile and run the 1 - GPU C + + interface pb model

Posted by freewholly on Wed, 15 Dec 2021 22:27:54 +0100

All error reports and Solutions

cmake .. Error reporting set

stay

make error collection

stay

./ your_demo process error collection

1. Segment error (core dumped)

Cause: the program has cross-border access
1) Memory access out of bounds (array out of bounds)
2) Multithreading is unsafe
3) Stack overflow (using large local variables is easy to cause stack overflow, because local variables are allocated on the stack)

Check the specific causes of the error: configure the operating system to generate the core file, and check the core file with gdb
Analyze the bug with the core file after the program crash

Final cause and solution:
Upgrade to TF2 4.1+cuda11. This error is reported after 1 because main CPP finally outputs the output confidence. The output array here is out of bounds. The output [0] below is out of bounds because the session does not run successfully at all,
(add STD:: cout < < status_run. Tostring() < < "\ n" after session - > run);, It is judged that the session did not run successfully, so the outputs are naturally an empty array, and the outputs[0] is accessed beyond the boundary.)
The reason why the session did not run successfully is described in the next problem.

	//Forward operation, the network carries out practical reasoning
	tensorflow::Status status_run = session->Run(inputs, { out_put_nodes }, {}, &outputs);

	//Gain confidence
	auto confidence_vector = outputs[0].tensor<float, 2>();
	for (int ProposalNum = 0; ProposalNum < maxbatchSize; ProposalNum++) {

		float confidence_float1 = confidence_vector(ProposalNum, 1);
		cout << "the confidence is:" << confidence_float1 << endl;
	}
	return 0;

2. Invalid argument:Tensor ShuffleOutputPb:0,specified in either feed_devices or fetch_devices was not found in the Graph

Reason: main The tensor variable ShuffleOutputPb:0 in CPP is different from the tensor name set in python to generate pb model. Modify main Variable name in CPP.
Then run again and finally status_run.ok() is True, but the following error is reported:

3. No algorithm worked

① main. Bundle in CPP session->Run(inputs,{out_put_nodes},{},&outputs); Input is written directly here instead of {{input_name, input}}
Because in this project, the input has been set in advance_ Name and resized_tensor push_back into the inputs tensor.
②OP_REQUIRES failed at conv_ops.cc:Not found: No algorithm worked!

Reason: when querying the issue of the official github of tensorflow, the error may be that the OOM memory is exceeded. You can watch -n 0.5 NVIDIA SMI at the same time at runtime. You can see that the error will be reported when the memory usage reaches the maximum capacity.

python solution

c + + Solution: put session_ options_ config-> ... These three lines of settings are moved to the front of the NewSession definition statement (these three lines in the original code are useless after the NewSession definition).

If the memory usage of GPU is not restricted before the session definition, the related statements of memory restriction shall be added:

	tensorflow::SessionOptions session_options;
	tensorflow::ConfigProto* session_options_config = &session_options.config;

    // Memory limit
	session_options_config->set_allow_soft_placement(true);
	session_options_config->mutable_gpu_options()->set_per_process_gpu_memory_fraction(0.33);
	session_options_config->mutable_gpu_options()->set_allow_growth(true);

	//Create a new session and configure parameters
	status = NewSession(session_options, &session);//Create a new Session session
	status_load = tensorflow::ReadBinaryProto(tensorflow::Env::Default(), "model.pb", &graphdef); //Read the graph model from pb file;
	status_create = session->Create(graphdef); //Import the model into the Session;

Topics: C++ Linux TensorFlow Deep Learning