How to make dataset and dataloader from wavelet transformed image(npy.file)?

davidlee: <p>Hello teachers.<br>
I’m a starter for PyTorch and i’m thinking of building up CNN model for fault detection of vibration signals.</p>
<p>Before making pytorch dataset and dataloader, i created wavelet transformed image from signal.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/6/6641d56479b32295a79a77e8a04f2d2ff66cb4d7.png" data-download-href="https://discuss.pytorch.org/uploads/default/6641d56479b32295a79a77e8a04f2d2ff66cb4d7" title="1"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/6/6641d56479b32295a79a77e8a04f2d2ff66cb4d7_2_597x499.png" alt="1" data-base62-sha1="eABKu1HpzXRe0iS5zsuCDNf5gWz" width="597" height="499" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/6/6641d56479b32295a79a77e8a04f2d2ff66cb4d7_2_597x499.png, https://discuss.pytorch.org/uploads/default/original/2X/6/6641d56479b32295a79a77e8a04f2d2ff66cb4d7.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/6/6641d56479b32295a79a77e8a04f2d2ff66cb4d7.png 2x" data-dominant-color="E4EDED"><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">1</span><span class="informations">849×710 45.6 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>
<ul>
<li>type : numpy array</li>
<li>dimension : (100,50,50), 100 images with 50x50</li>
<li>value : around -0.01~0.01</li>
</ul>
<p>after loading npy.file with np.load function,<br>
I don’t know how to make my Dataset Class for Dataloader,<br>
because it is not regular image.</p>
<p>Please help me <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=12" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>
<p>Have a good day!</p>

ptrblck: <p>You don’t have to use regular images in your <code>Dataset</code> so your custom numpy arrays should be fine.<br>
Here is a small dummy example:</p>
<pre><code class="lang-python">class MyDataset(Dataset):
    def __init__(self, data):
        self.data = torch.from_numpy(data).float()
        
    def __getitem__(self, index):
        x = self.data[index]
        return x
    
    def __len__(self):
        return len(self.data)
</code></pre>
<p>I’m not sure, what your targets are, so you might want to pass them additionally to the <code>Dataset</code> or calculate them in <code>__getitem__</code>.</p>

davidlee: <p>Hi Ptrblck,<br>
I really appreciate with your answer but there is still a problem.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/e/ebbd4efc0221cafad03d141350c55dacf89a78d0.png" data-download-href="https://discuss.pytorch.org/uploads/default/ebbd4efc0221cafad03d141350c55dacf89a78d0" title="3"><img src="https://discuss.pytorch.org/uploads/default/original/2X/e/ebbd4efc0221cafad03d141350c55dacf89a78d0.png" alt="3" data-base62-sha1="xDrNqv1WlxOTpcNBU96vEIZPK7K" width="677" height="500" data-dominant-color="F3F4F4"><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">3</span><span class="informations">1015×749 16.4 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>Error says : object of type ‘type’ has no len()</p>
<ul>
<li>The thing that i wanna do with this is, inputting images with 50x50 size and<br>
building Convolutional AutoEncoder model with unsupervised learning for anomaly detection.</li>
<li>I’m wondering i need to resize (100,30,30) into (100,1,30,30)… because input dimension is comprised of (batchsize, channel, height, width) in every the PyTorch Tutorials.</li>
</ul>
<p>Thanks again!!</p>

tom: <p>You want to pass an instance of the dataset to the dataloader constructor, not the type.</p>
<p>Best regards</p>
<p>Thomas</p>

davidlee: <p>Thank you very much, tom,</p>
<p>but im afraid that i still don’t know how to modify the code for passing an instance for this case…</p>
<p>Regards,</p>
<p>David</p>

davidlee: <p>i solved with dataset=MyDataset()</p>
<p>Thank you very much <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>