How to convert .mat file struct into pytorch Tensors

mamoona_nisar: <p>Hi ,I have 3064 images dataset into single .mat (matlab) file.<br>
The file has structures format.<br>
How can I access the structure using pytorch.I have Loaded in Python the .mat file but not sure how to access data from it.<br>
Following are the items  in cjdata structure<br>
cjdata.image<br>
cjdata.id<br>
cjdata.tumorborder</p>

ptrblck: <p>I guess you’ve opened the file using <code>scipy.io</code>?<br>
If so, you should get a <code>dict</code> with all the members and can just load these arrays as numpy arrays.<br>
Once you have the numpy data, you can transform them to <code>torch.Tensors</code> using <code>torch.from_numpy()</code>.<br>
Have a look at some examples from the <a href="https://docs.scipy.org/doc/scipy/reference/tutorial/io.html" rel="nofollow noopener">scipy docs</a> on how to load the data.</p>

mamoona_nisar: <p>Thanks ,your reply is helpful .Before moving to numpy- then tensor.<br>
I need little help more please.<br>
this is how is combined all .mat structure in one mat.See so you can get idea for my problem.</p>
<pre><code class="lang-auto">% % Making single mat file for all images detail
% for i=1:3064                     
%     fname = strcat('data/figshareBrainTumorDataPublic/',num2str(i),'.mat')
%     load(fname)
%     label(i) = cjdata.label;
%     PID(i) = {cjdata.PID};
%     if (size(cjdata.image,1) ~= 512) 
%         image(i,:,:) = imresize(cjdata.image, [512, 512]);
%         tumorMask(i,:,:) = imresize(cjdata.tumorMask, [512, 512]);
%     else
%         image(i,:,:) = cjdata.image;
%         tumorMask(i,:,:) = cjdata.tumorMask;
%     end
%     clear cjdata fname
% end
% save('data/figshareBrainTumorDataPublicNewSingle/data.mat', 'label', 'PID', 'image', 'tumorMask');
</code></pre>
<p>The code in Python so far is :</p>
<pre><code class="lang-auto">import scipy.io as io
fname='./data.mat'
data= io.loadmat(fname)
fname=data['label','PID','image','tumorBorder','tumorMask'] 
</code></pre>
<p>‘image’ has all 3064 images.How can I access one image from it :fname=data[‘image’]   or selective images.like 4 image.Thankyou for helping me<br>
Please suggest best way to make the data ready for pytorch CNN.I m beginner at this.    <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=6" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

ptrblck: <p>Is <code>data['image']</code> returning a numpy array?<br>
If so, you could just slice it using e.g. <code>data['image'][:4]</code>, if you want to get the first 4 images.<br>
You could try to write your custom <code>Dataset</code>, load all data in <code>__init__</code>, and get a single sample in <code>__getitem__</code>. Here is some dummy example:</p>
<pre><code class="lang-python">class MyDataset(Dataset):
    def __init__(self, mat_path):
        data = io.loadmat(mat_path)
        self.images = torch.from_numpy(data['images'])
        self.targets = torch.from_numpy(data['tumorMask'])

    def __getitem__(self, index):
        x = self.images[index]
        y = self.targets[index]
        return x, y

    def __len__(self):
        return len(self.images)
</code></pre>
<p>Also, have a look at the <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html#dataset-class" rel="nofollow noopener">Data loading tutorial</a> to see how the <code>Dataset</code> and <code>DataLoader</code> play together.</p>

mamoona_nisar: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/8/89ec090288aedcf91a9050f352a3201e8ed76f99.png" data-download-href="https://discuss.pytorch.org/uploads/default/89ec090288aedcf91a9050f352a3201e8ed76f99" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/8/89ec090288aedcf91a9050f352a3201e8ed76f99_2_690x163.png" alt="image" data-base62-sha1="jG77dV0U4BdF7j6El9RVrxD7aBz" width="690" height="163" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/8/89ec090288aedcf91a9050f352a3201e8ed76f99_2_690x163.png, https://discuss.pytorch.org/uploads/default/optimized/2X/8/89ec090288aedcf91a9050f352a3201e8ed76f99_2_1035x244.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/8/89ec090288aedcf91a9050f352a3201e8ed76f99.png 2x" data-dominant-color="F2F5F3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1336×317 23.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>This is what i  m getting .</p>

ptrblck: <p>Is <code>imshow</code> showing something?<br>
Based on the variable explorer it looks like your first images are all zero.<br>
Also, the images seem to be single channel images. Is this correct?</p>

mamoona_nisar: <p>Imshow is not showing anything.yes you are right.Your command data[‘image’][:4] return (4,512,512) and does not displays images.<br>
But <code>fname=data['image']</code><br>
returns long arrays.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/2X/8/83b2cb56c5457400b8447c462de9a75883aa1b7d.png" alt="image" data-base62-sha1="iN3C2Og1fZI3a0c1iCRXT71wN1b" width="443" height="367"></p>

ptrblck: <p>Are you expecting this image format? In a previous post you’ve mentioned <code>data['image']</code> should contain 3064 images. Now your <code>fname</code> seems to have 1207 images. Maybe the data is being stored/loaded in the wrong format.</p>

mamoona_nisar: <p>I don’t know why the it has 1207 images.I have stored through loop with i value 3064.<br>
Why this happening I don’t know.This is how i stored in one mat file:</p>
<pre><code class="lang-auto">% % Making single mat file for all images detail
for i=1:3064
    fname = strcat('data/figshareBrainTumorDataPublic/',num2str(i),'.mat')
    load(fname)
    label(i) = cjdata.label;
    PID(i) = {cjdata.PID};
    image(i,:,:) = cjdata.image;
     tumorBorder(i,:) = cjdata.tumorBorder;
     tumorMask(i,:,:) = cjdata.tumorMask;
end
% save('data/figshareBrainTumorDataPublicNewSingle/data.mat', 'label', 'PID', 'image', 'tumorMask');
 save('data/figshareBrainTumorDataPublicNewSingle/data.mat', 'label', 'PID', 'image', 'tumorBorder', 'tumorMask');

</code></pre>

mamoona_nisar: <p>By the way I have the other method running byside.saved 3064 images as png from mat files.I have saved 3 labeled folders (images stored  as PNG) .But found RuntimeError: Found 0 files in subfolders.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/2/2d59b6fb364af62d57b4ac8f590e9550bc1437bd.png" data-download-href="https://discuss.pytorch.org/uploads/default/2d59b6fb364af62d57b4ac8f590e9550bc1437bd" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/2X/2/2d59b6fb364af62d57b4ac8f590e9550bc1437bd.png" alt="image" data-base62-sha1="6tbHlE9H55NPBkMDudn99DSeOFT" width="690" height="347" data-dominant-color="F2F3F2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1339×675 56.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div> .<br>
"each subfolders  in "train “have png images.”</p>

saba: <p>Hi Ptrblck</p>
<p>I want to save data as mat file in the pytorch which command is the right one.  I can load mat  file but now I need to save my tensor or numpy array as mat file.<br>
I used this one &gt;&gt;&gt; scipy.io.savemat(‘images1.mat’, images1.numpy())<br>
images1 are my input patches. but it dose not work.</p>
<p>I appreciate your reply.</p>

ptrblck: <p>The command looks alright.<br>
What kind of error do you get?</p>

saba: <p>Thank you I solve it I should have used</p>
<p>io.savemat(os.path.join(root_dirDurringTraining11+’/’,“images”+str(i)+".mat"), {“images11”: images1.numpy()})</p>

hahaha_lts: <p>Hi, I got sing channel images from ‘.mat’. And I write my custom Dataset, but got some <strong>errors.RuntimeError: output with shape [1, 21, 21] doesn’t match the broadcast shape [3, 21, 21]</strong><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/1/f/1f0ae95bc8935bb35712c39426085f7cd6ae372d.png" alt="error" data-base62-sha1="4qCb7WE6hBpqqe76hM5L6BxYW73" width="598" height="498"><br>
my images array shape(1596, 21, 21)</p>

hahaha_lts: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> sorry if I disturb you</p>

ptrblck: <p>I guess <code>transforms.Normalize</code> raises this error, since you are using a single-channel tensor, while <code>Normalize</code> uses 3 values for the <code>mean</code> and <code>stddev</code>.<br>
If you are dealing with grayscale images, you could pass <code>tuples</code> containing a single element to <code>Normalize</code>:</p>
<pre><code class="lang-python">transforms.Normalize((0.5,), (0.5,))
</code></pre>

hahaha_lts: <p>thank you sir！ sovled it</p>