# how to convert a tensor with libtorch into a ITK image?

**URL:** https://discourse.itk.org/t/how-to-convert-a-tensor-with-libtorch-into-a-itk-image/5176
**Category:** Engineering
**Created:** [July 13, 2022, 9:46am UTC](https://discourse.itk.org/t/how-to-convert-a-tensor-with-libtorch-into-a-itk-image/5176 "2022-07-13T09:46:25Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Bugmaker](https://discourse.itk.org/user_avatar/discourse.itk.org/bugmaker/32/2500_2.png) [@Bugmaker](https://discourse.itk.org/u/Bugmaker)
#### Post date: [July 13, 2022, 9:46am UTC](https://discourse.itk.org/t/how-to-convert-a-tensor-with-libtorch-into-a-itk-image/5176/1 "2022-07-13T09:46:25Z")

</div>

I have created a tensor by libtorch , which it’s shape is 688,688,450, and I want to transfer this tensor into image.

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [July 13, 2022, 1:45pm UTC](https://discourse.itk.org/t/how-to-convert-a-tensor-with-libtorch-into-a-itk-image/5176/2 "2022-07-13T13:45:27Z")

</div>

Hello @Bugmaker,

1. Convert tensor to numpy array.  
If tensor is on cpu `np_arr = x.numpy()` otherwise `np_arr = x.to("cpu").numpy()`.
2. Convert numpy array to image.  
[ITK example](https://examples.itk.org/src/bridge/numpy/convertnumpyarraytoitkimage/convertnumpyarraytoitkimage?highlight=getimagefromarray), [SimpleITK Jupyter notebook](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/03_Image_Details.ipynb) section titled **Conversion between numpy and SimpleITK**.

---

<div class="post-metadata">

### Author: ![Bugmaker](https://discourse.itk.org/user_avatar/discourse.itk.org/bugmaker/32/2500_2.png) [@Bugmaker](https://discourse.itk.org/u/Bugmaker)
#### Post date: [July 14, 2022, 1:52am UTC](https://discourse.itk.org/t/how-to-convert-a-tensor-with-libtorch-into-a-itk-image/5176/3 "2022-07-14T01:52:24Z")

</div>

@zivy  
Maybe I didn`t make it clear, libtorch and ITK are C++ libraries, they do not have NumPy.

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [July 14, 2022, 12:34pm UTC](https://discourse.itk.org/t/how-to-convert-a-tensor-with-libtorch-into-a-itk-image/5176/4 "2022-07-14T12:34:17Z")

</div>

Hello @Bugmaker,

Thanks for the clarification. The relevant ITK filter is `ImportImageFilter`, see [C++ example here](https://examples.itk.org/src/core/common/convertarraytoimage/documentation).

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [July 14, 2022, 1:01pm UTC](https://discourse.itk.org/t/how-to-convert-a-tensor-with-libtorch-into-a-itk-image/5176/5 "2022-07-14T13:01:35Z")

</div>

This [StackOverflow question](https://stackoverflow.com/questions/61125616/how-to-use-itk-to-convert-png-into-tensor-for-pytorch) might be helpful. @Lee_Newberg did you do something with ITK C++ libTorch?

---

<div class="post-metadata">

### Author: ![Lee\_Newberg](https://discourse.itk.org/user_avatar/discourse.itk.org/lee_newberg/32/1461_2.png) [@Lee\_Newberg](https://discourse.itk.org/u/Lee_Newberg)
#### Post date: [July 14, 2022, 1:30pm UTC](https://discourse.itk.org/t/how-to-convert-a-tensor-with-libtorch-into-a-itk-image/5176/6 "2022-07-14T13:30:23Z")

</div>

I did some torch to C++ array stuff. If the torch tensor is on the GPU, the firs step would be to move it to the CPU. Then you can get a pointer to its first element. Something like

```cpp
cpuTensor = gpuTensor.to(torch::kCPU);
pFirst = cpuTensor.data_ptr();

```

Then you have to get that C++ array into an ITK image. ~~If there isn’t some direct way to do that already, maybe use `itk::ImportImageFilter::SetImportPointer(pFirst, ...)`?~~ Perhaps @dzenanz can help with that.

---

<div class="post-metadata">

### Author: ![Bugmaker](https://discourse.itk.org/user_avatar/discourse.itk.org/bugmaker/32/2500_2.png) [@Bugmaker](https://discourse.itk.org/u/Bugmaker)
#### Post date: [July 15, 2022, 2:36am UTC](https://discourse.itk.org/t/how-to-convert-a-tensor-with-libtorch-into-a-itk-image/5176/7 "2022-07-15T02:36:12Z")

</div>

hi @zivy  
I have tried that method, but it seems that the itk image data is not the same as tensor. The max value of tensor is 6 and the max value of the converted image is 7.3e-7.

```auto
    using PixelType = float;
	constexpr unsigned int Dimension = 3;
	using ImportFilterType = itk::ImportImageFilter<PixelType, Dimension>;
	ImportFilterType::Pointer importFilter = ImportFilterType::New();

	ImageTypeFloat::SizeType shape = img->GetLargestPossibleRegion().GetSize();
	std::cout << " shape value is : " << " " << shape[0] << " " << shape[1] << " " << shape[2] << std::endl;
	importFilter->SetOrigin(img->GetOrigin());
	importFilter->SetSpacing(img->GetSpacing());
	importFilter->SetDirection(img->GetDirection());
	ImportFilterType::IndexType start;
	ImportFilterType::SizeType size;
	size[0] = shape[0];
	size[1] = shape[1];
	size[2] = shape[2];
	//size[3] = data.sizes()[3];
	//start.fill(0);
	start[0] = 0;
	start[1] = 0;
	start[2] = 0;
	std::cout << " size value is : " << " " << size[0] << " " << size[1] << " " << size[2] << std::endl;
	ImportFilterType::RegionType region;
	region.SetIndex(start);
	region.SetSize(size);
	importFilter->SetRegion(region);
	//importfilter->setorigin(image->getorigin());
	//importfilter->setspacing(image->getspacing());
	//importfilter->setdirection(image->getdirection());
	const bool importimagefilterwillownthebuffer = false;
	importFilter->SetImportPointer(static_cast<float*>(data.data_ptr()),
		size[0] * size[1] * size[2],
		importimagefilterwillownthebuffer);
	importFilter->Update();
	ImageTypeFloat::Pointer res = importFilter->GetOutput();

```

**get max code:**

```auto
    using Calculator = itk::MinimumMaximumImageCalculator<ImageTypeFloat>;
	Calculator::Pointer calculator = Calculator::New();
	calculator->SetImage(res);
	calculator->ComputeMaximum();
	float mx = calculator->GetMaximum();
	std::cout << mx << std::endl;

```

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [July 15, 2022, 12:42pm UTC](https://discourse.itk.org/t/how-to-convert-a-tensor-with-libtorch-into-a-itk-image/5176/8 "2022-07-15T12:42:15Z")

</div>

Hello @Bugmaker,

First thing to check is that `array_from_tensor = static_cast<float*>(data.data_ptr())` contains the expected values. Possibly a memory issue, try to call `data.contiguous().data_ptr()` so that the memory is contiguous.

---

<div class="post-metadata">

### Author: ![Bugmaker](https://discourse.itk.org/user_avatar/discourse.itk.org/bugmaker/32/2500_2.png) [@Bugmaker](https://discourse.itk.org/u/Bugmaker)
#### Post date: [July 19, 2022, 9:40am UTC](https://discourse.itk.org/t/how-to-convert-a-tensor-with-libtorch-into-a-itk-image/5176/9 "2022-07-19T09:40:16Z")

</div>

hi @zivy,

I have used the method, but the result is still wrong. However, there is one thing confusing me that is the max value of ITK image is 1.4013e-45 which is the minimum value of the float number, and I also get the same value when I printed the tensor by deference such as \*(tensor.contiguous.data\_ptr)

---

<div class="post-metadata">

### Author: ![Lee\_Newberg](https://discourse.itk.org/user_avatar/discourse.itk.org/lee_newberg/32/1461_2.png) [@Lee\_Newberg](https://discourse.itk.org/u/Lee_Newberg)
#### Post date: [July 19, 2022, 12:44pm UTC](https://discourse.itk.org/t/how-to-convert-a-tensor-with-libtorch-into-a-itk-image/5176/10 "2022-07-19T12:44:42Z")

</div>

At the risk of stating the obvious, it looks like something that was created as a tensor of `int` is being treated as if it were a tensor of `float`. In particular, that small value `float(1.401298e-45)` that is being seen has a the same bit representation as `int(1)`.

---

<div class="post-metadata">

### Author: ![Bugmaker](https://discourse.itk.org/user_avatar/discourse.itk.org/bugmaker/32/2500_2.png) [@Bugmaker](https://discourse.itk.org/u/Bugmaker)
#### Post date: [August 1, 2022, 8:34am UTC](https://discourse.itk.org/t/how-to-convert-a-tensor-with-libtorch-into-a-itk-image/5176/11 "2022-08-01T08:34:29Z")

</div>

Sorry, i have not solved this problem. The whole picture becomes fragmented and chaotic, and the whole thing is shifted up. However, when i try to a method like in.set(tensor[i][j][p]), the image was right.
