Convert c10::Scalar to int

Hi, I have an output tensor come from my network.

void LPRNet<SpTDatum>::PostProcess(std::vector<SpTDatum>& the_datas, torch::Tensor & model_outputs)
		model_outputs =;//make the variable is in CPU

		for (int i = 0; i < the_datas.size(); ++i)
		{ //each batch
			for (int j = 0; j < the_datas[i]->results->size(); ++j)
			{ //each detected LP
				if (the_datas[i]->results->at(j).HaveLp)
					//process the tensor data
					std::string LpLabel = "";
					int last_char = -1;
					int current_char = -1;
					for (int k = 0; k < model_outputs[the_datas[i]->results->at(j).QueueNumber].size(0); ++k)
						//each character
						current_char = model_outputs[the_datas[i]->results->at(j).QueueNumber][k].item<int>();
						if ((last_char == current_char) || (mBlankIdx == current_char)) {
							last_char = current_char;
						LpLabel += mDisplayChar[current_char];//convert the index value to character value
						last_char = current_char;
					the_datas[i]->results->at(j).LpLabel = LpLabel;
		//program end here!

This program works fine and is compiled in the MSVC. However when I use GCC 7.5 to build, i got this error message

[  1%] Building CXX object LPDR/CMakeFiles/lpdr.dir/src/LPDR/lprNet.cpp.o
/media/ion/ioNdrive/lpdr_beta/LPDR/src/LPDR/lprNet.cpp: In member function ‘void LPDR::LPRNet<SpTDatum>::PostProcess(std::vector<_Tp>&, at::Tensor&)’:
/media/ion/ioNdrive/lpdr_beta/LPDR/src/LPDR/lprNet.cpp:183:86: error: expected primary-expression before ‘int’
       current_char = model_outputs[the_datas[i]->results->at(j).QueueNumber][k].item<int>();
/media/ion/ioNdrive/lpdr_beta/LPDR/src/LPDR/lprNet.cpp:183:86: error: expected ‘;’ before ‘int’
LPDR/CMakeFiles/lpdr.dir/build.make:285: recipe for target 'LPDR/CMakeFiles/lpdr.dir/src/LPDR/lprNet.cpp.o' failed
make[2]: *** [LPDR/CMakeFiles/lpdr.dir/src/LPDR/lprNet.cpp.o] Error 1
CMakeFiles/Makefile2:205: recipe for target 'LPDR/CMakeFiles/lpdr.dir/all' failed
make[1]: *** [LPDR/CMakeFiles/lpdr.dir/all] Error 2
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2

What is the other options to convert c10::Scalar to int(or other data type such as double, float, etc)
best regards,

@ptrblck would you help me, please?

Could you check what is returned by model_outputs[the_datas[i]->results->at(j).QueueNumber][k] as I’m currently unsure what’s raising:
error: expected primary-expression before ‘int’.

It is a tensor (CPULongType).
A single tensor of an tensor output (*model_outputs *).
model_outputs have dimensions [batch x num_detection x 15].
I want to convert it into int.
I sensed a GCC version problem.
I have solved the problem. In GCC we can not do this

current_char = model_outputs[the_datas[i]->results->at(j).QueueNumber][k].item<int>();

We must do this

at::Tensor tmp = model_outputs[the_datas[i]->results->at(j).QueueNumber][k]
current_char =tmp.item<int>();

@ptrblck Thank you very much for your time.

1 Like

Thanks for identifying this bug.