0
votes

Let's say I have a vector of image tensors with each image tensor having the dimensions of [frames, height, width, num_channels] and I want to take that vector and convert it to one larger tensor of [num_tracks(size of vector), frames, height, width, num_channels]. What's the easiest way to do this with the tensorflow::Tensor api? This is for constructing an input tensor for a graph, and not in the graph execution itself.

Thanks!

1

1 Answers

1
votes

You can create a new Tensor with desired shape, and just fill it out by iterating for all dims in for loops (to access individual item use operator() of Eigen's TensorMap which you can get by tensor<DataType,DIMS> on Tensor):

tensorflow::Tensor concat(const std::vector<tensorflow::Tensor>& in){
    int frames = in[0].dim_size(0);
    int height = in[0].dim_size(1);
    int width = in[0].dim_size(2);
    int num_channels = in[0].dim_size(3);
    
    int num_tracks = in.size();
    
    tensorflow::Tensor res(DT_FLOAT,tensorflow::TensorShape{num_tracks,frames,height,width,num_channels});
    auto& resMap = res.tensor<float,5>();
    
    for (int nt = 0; nt < num_tracks; ++nt) {
        auto& inFrame = in[nt];
        auto& inMap = inFrame.tensor<float,4>(); // Eigen's TensorMap which has operator()(Indices...)
        
        for (int f = 0; f < frames; ++f) {
            for (int r = 0; r < height; ++r) {
                for (int c = 0; c < width; ++c) {
                    for (int ch = 0; ch < num_channels; ++ch) {
                        resMap(nt,f,r,c,ch) = inMap(f,r,c,ch);
                    }
                }
            }
        }
    }
    return res;
}