This is my data loader for one model
import torch
import numpy as np
import glob
import scipy
import scipy.io as sio
from ModelResNet import *
from Preper import *
from torch.utils.data import Dataset, DataLoader
class ImageDataset:
def init(self,alldata,  transform=None):
#self.root_dir = root_dir
#load data from mat files
self.alldata=alldata
alldata_olivier = A
alldata_non_olivier = Y
    #a list of label 

    for i in range(5027) : #number of samples 'non olive'
        labels.append(0)
    for i in range(4990) : #number of samples 'olive
        labels.append(1)
    
    #shuffle data using sklearn

    self.numdata = 10017
    self.transform = transform
    
def __len__(self):
    return self.numdata
def __getitem__(self, idx):
    label=labels[idx]

    #newidx = self.shuffle[idx]
    image = self.alldata[idx]
    label=np.asarray(label)
    #transform data from numpy to torch tensor
    imageTensor =np.asarray(alldata)# 
    imageTensor =torch.from_numpy(imageTensor)
    #plt.imshow(imageTensor[:,:,0])
    labelTensor =np.asarray(labels)# torch.from_numpy(label)
    labelTensor =torch.from_numpy(labelTensor)
    #print(imageTensor) 
    return imageTensor , labelTensor

if name == ‘main’:
k= ImageDataset(z)
k.getitem(60)
import sklearn.model_selection as model_selection
X_train, X_test, y_train, y_test = model_selection.train_test_split(alldata, labels, train_size=0.8,test_size=0.2)
class trainData():
def __init__(self, X_data, y_data):
    self.X_data = X_data
    self.y_data = y_data
    
def __getitem__(self, index):
    return self.X_data[index], self.y_data[index]
    
def __len__ (self):
    return len(self.X_data)

train_data = trainData(torch.FloatTensor(X_train),
torch.FloatTensor(y_train),
)
class testData():
def __init__(self, X_data):
    self.X_data = X_data
    
def __getitem__(self, index):
    return self.X_data[index]
  
def __len__ (self):
    return len(self.X_data)

train_loader = DataLoader(dataset=train_data, batch_size=2, shuffle=True)
net.to(device)
device=torch.device(“cuda:0” if torch.cuda.is_available () else “cpu”)
test_data = testData(torch.FloatTensor(X_test))
EPOCHS =21
LEARNING_RATE = 0.001
def binary_acc(y_pred, y_testt):
y_pred_tag = torch.round(y_pred)
correct_results_sum = (y_pred_tag[:,0]  == y_testt).sum().float()
acc = correct_results_sum/y_testt.shape[0]
acc = torch.round(acc * 100)
return acc
net.train()
for e in range(1, EPOCHS+1):
epoch_loss = 0
epoch_acc = 0
for X_batch, y_batch in train_loader:
    X_batch =np.asarray(X_batch)
    X_batch =torch.from_numpy(X_batch)
    optimizer.zero_grad()
    
    X_batch= X_batch.permute(0,3,2,1).float()
    y_batch =np.asarray(y_batch) 
    y_batch =torch.from_numpy(y_batch)
    X_batch, y_batch =X_batch.to(device) , y_batch.to(device)
    y_pred = net(X_batch) 
    
    acc = binary_acc(y_pred, y_batch)
    
    loss = loss_fn(y_pred[:,0], y_batch.float())  
        
    loss.backward()
    
    optimizer.step()
    
    epoch_loss += loss.item()
    epoch_acc += acc.item()
    
print('debut training : ')   
print(f'Epoch {e+0:03}: | Loss: {epoch_loss/len(train_loader):.5f} | Acc: {epoch_acc/len(train_loader):.3f}')