# Input numpy ndarray instead of images in a CNN

**URL:** <https://discuss.pytorch.org/t/input-numpy-ndarray-instead-of-images-in-a-cnn/18797>\
**Category:** Uncategorized\
**Created:** [May 28, 2018, 4:47pm UTC](https://discuss.pytorch.org/t/input-numpy-ndarray-instead-of-images-in-a-cnn/18797 "2018-05-28T16:47:41Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![jain26iitd](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@jain26iitd](https://discuss.pytorch.org/u/jain26iitd)\
**Post date:** [November 21, 2018, 12:13pm UTC](https://discuss.pytorch.org/t/input-numpy-ndarray-instead-of-images-in-a-cnn/18797/3 "2018-11-21T12:13:04Z")

</div>

@ptrblck Thanks for the code.  
I used the code as below to create a custom dataloader.

```auto
# Creating custom Dataset classes

class MyDataset(Dataset):
    def __init__ (self, data, target, transform=None):
        self.data = torch.from_numpy(data).float()
        self.target = torch.from_numpy(target).long()
        self.transform = transform
        
    def __getitem__ (self, index):
        x = self.data[index]
        y = self.target[index]
        
        if self.transform:
            x = self.transform(x)
            
        return x, y
    
    def __len__ (self):
        return len(self.data)

```

```auto
numpy_data = np.random.randn(100,3,224,224) # 10 samples, image size = 224 x 224 x 3
numpy_target = np.random.randint(0,5,size=(100))

dataset = MyDataset(numpy_data, numpy_target)
loader = DataLoader(dataset, batch_size=1, shuffle=True, num_workers=2, pin_memory=False) # Running on CPU

```

Till this part everything is fine.  
The below mentioned part of the code is taking too much time to run.  
Any help on this??

```auto
for batch_idx, (data, target) in enumerate(loader):
    print('Batch idx {}, data shape {}, target shape {}'.format(batch_idx, data.shape, target.shape))

```

---

_[View the full topic](https://discuss.pytorch.org/t/input-numpy-ndarray-instead-of-images-in-a-cnn/18797)._
