How to convert a memmap to tensor

wxystudio: <p>I use nibabel lib to read some 3D image, which are saved as ‘XX.nii’, After I read the image from file, the data type is &lt;class ‘numpy.memmap’&gt;, I want to use this image for 3D convolution, so I try to convert this data to tensor. How can I do with this problem?  Please help me, there is the code as follow</p>
<p>import nibabel as nib<br>
import matplotlib.pyplot as plt<br>
import pdb<br>
from torchvision import transforms as T<br>
import torch<br>
from torchvision.datasets import ImageFolder<br>
import numpy as np</p>
<p>def read_data(path):<br>
image_data = nib.load(path).get_data()<br>
return image_data</p>
<p>def show_img(ori_img):<br>
plt.imshow(ori_img[:,:,85], cmap = ‘gray’) <span class="hashtag">#channel_last</span><br>
plt.show()</p>
<p>path = ‘D:/wxydataset/forWang/sunjian/IOP-IXI006-p6.nii’<br>
data = read_data(path)<br>
show_img(data)</p>

tymokvo: <p>Check the <a href="https://het.as.utexas.edu/HET/Software/Numpy/reference/generated/numpy.memmap.html" rel="nofollow noopener">numpy docs</a>:</p>
<pre><code class="lang-auto">im_array = np.array(image_data) # image_data is the memmap

im_tensor = torch.from_numpy(im_array)
</code></pre>
<p>Something like this should work.</p>
<p>EDIT:</p>
<p>Looks like you can just call:</p>
<pre><code class="lang-auto">torch.from_numpy(image_data) 
</code></pre>

wxystudio: <aside class="quote no-group" data-username="tymokvo" data-post="2" data-topic="47671">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tymokvo/48/9527_2.png" class="avatar"> tymokvo:</div>
<blockquote>
<p>from_numpy</p>
</blockquote>
</aside>
<p>thx very much, it works</p>

vmoens: <p>Not to resuscitate this, but if someone ends up here being interested in a memmap tensor this is something that is provided by torchrl:</p><aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/rl/blob/main/torchrl/data/tensordict/memmap.py">
  <header class="source">

      <a href="https://github.com/pytorch/rl/blob/main/torchrl/data/tensordict/memmap.py" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/rl/blob/main/torchrl/data/tensordict/memmap.py" target="_blank" rel="noopener nofollow ugc">pytorch/rl/blob/main/torchrl/data/tensordict/memmap.py</a></h4>


      <pre><code class="lang-py"># Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from __future__ import annotations

import functools
import os
import tempfile
from typing import Any, Callable, List, Optional, Tuple, Union, Dict

import numpy as np
import torch

from torchrl._utils import prod
from torchrl.data.tensordict.utils import _getitem_batch_size
from torchrl.data.utils import (
    DEVICE_TYPING,
    INDEX_TYPING,
</code></pre>



  This file has been truncated. <a href="https://github.com/pytorch/rl/blob/main/torchrl/data/tensordict/memmap.py" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>