Converting own custom dataset into MNIST numpy form

Does anyone have an idea how can I convert my own custom dataset in the form of an MNIST dataset for the below code? I am stuck at this point like MNIST has its own train.target.numpy,test.targets.numpy. How should I do it for my own custom dataset.

I have around 16000 images that are in .png form, so converting all of them into NumPy array will result in out of memory or the system is killing my script.

batchsize=128
train = dsets.MNIST(root='../data/',train=True,download=True)
test=dsets.MNIST(root='../data/',train=False,transform=transforms.Compose([transforms.ToTensor(),]))
indices= np.random.choice(len(train.targets.numpy()), 2000, replace=False)
indices_test= np.random.choice(len(test.targets.numpy()), 100, replace=False)

Any suggestion are welcome?