2

I want to concatenate several tensorflow::Tensor in one to use the session only once, I have a process that retrieve a tensor for each image filling the data with this code suggestion import-opencv-mat-into-c-tensorflow-without-copying

But I have i issues trying to merge all this tensors, and documentation for function concat is not clear about how to do it, by this moment my code looks like:

std::vector<tensorflow::Tensor> inputTensorVector;
for(cv::Mat image: images){
    inputTensorVector.push_back(create_vector(image));
}

tensorflow::Tensor input(tensorflow::DT_FLOAT, tensorflow::TensorShape({ inputTensorVector.size(), 100, 100, 3 }));

create_vector is a function that creates a Tensor of shape (1,100,100,3) from the image that is passed as argument, the question is how to fill input tensor with each tensor from inputTensorVector?

Dinl
  • 153
  • 6

1 Answers1

0

the input tensor for the function ConvertImagesToTensor needs to be

Tensor input_tensor(DT_FLOAT,TensorShape({(long)images.size(), 160, 160, 3}));

the line is if your input is float values

images[i].convertTo(images[i], CV_32FC1)

 void ConvertImagesToTensor(vector<Mat>& images, Tensor& input_tensor)
{
auto input_tensor_mapped = input_tensor.tensor<float, 4>();
for(int i = 0 ; i < images.size(); ++i)
{
    int height = images[i].rows, width = images[i].cols, depth = images[i].channels();
    
    images[i].convertTo(images[i], CV_32FC1);
    const float * source_data = (float*) images[i].data;

    for (int y = 0; y < height; ++y)
    {
        const float* source_row = source_data + (y * width * depth);
        for (int x = 0; x < width; ++x)
        {
            const float* source_pixel = source_row + (x * depth);
            for (int c = 0; c < depth; ++c)
            {
                const float* source_value = source_pixel + c;
                input_tensor_mapped(i, y, x, c) = *source_value;
            }
        }
    }
}

}

alon
  • 220
  • 1
  • 16