libtorch操作
带stride数组与tensor之间的转化
int16_t src0[64 * 64] = { 0 };
int16_t * Psrc0 = src0;
int stride0 = 64;
for (int i = 0; i < 64 * 64; ++i)
src0[i] = i % 64;
torch::Tensor Tsrc0 = torch::from_blob(Psrc0, { 1, 64 }, at::kShort);
Psrc0 += stride0;
for (int j = 1; j < 64; ++j)
{
Tsrc0 = torch::cat({Tsrc0, torch::from_blob(Psrc0, { 1, 64 }, at::kShort) }, 0);
Psrc0 += stride0;
}
int16_t src1[64 * 64] = { 0 };
int16_t * Psrc1 = src1;
int stride1 = 64;
for (int i = 0; i < 64 * 64; ++i)
src1[i] = (i % 64) << 1;
torch::Tensor Tsrc1 = torch::from_blob(Psrc1, { 1, 64 }, at::kShort);
Psrc1 += stride1;
for (int j = 1; j < 64; ++j)
{
Tsrc1 = torch::cat({ Tsrc1, torch::from_blob(Psrc1, { 1, 64 }, at::kShort) }, 0);
Psrc1 += stride1;
}
// 转换为float类型
torch::Tensor curLuma = torch::cat({ Tsrc0.unsqueeze(0), Tsrc1.unsqueeze(0) }, 0).toType(c10::ScalarType(6));
std::cout << curLuma << std::endl;
std::cout << curLuma.sizes() << std::endl;
分片操作
https://pytorch.org/cppdocs/notes/tensor_indexing.html
int tempArr[9] = { 0 };
for (int i = 0; i < 9; ++i)
{
tempArr[i] = i + 1;
}
torch::Tensor TArr = torch::from_blob(tempArr, { 3, 3 }, at::kInt);
std::cout << TArr << std::endl;
std::cout << TArr.index({ torch::indexing::Slice(0, 2), torch::indexing::Slice(0, 2) }) << std::endl;