class ImageDataset:
def __init__(self,alldata,  transform=None):

  

    #self.root_dir = root_dir

    #load data from mat files

    self.alldata=alldata

    #glob.glob(self.root_dir+"/*.mat")

    alldata_olivier = A

    alldata_non_olivier = Y

    #transform data to unique list of data

    

    #a list of label 

    #à) construire à partir [0 ou 1] selon self.alldata

    

    for i in range(373) : #number of samples 'non olive'

        labels.append(0)

    for i in range(1958) : #number of samples 'olive

        labels.append(1)

    

    #shuffle data using sklearn

            

    self.numdata = 2331

    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)

    print("label de l'image : " ,labelTensor)

    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, random_state=101)
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)

from torchvision import transforms
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)

test_data = testData(torch.FloatTensor(X_test))
EPOCHS = 20
LEARNING_RATE = 0.001
def binary_acc(y_pred, y_test):
y_pred_tag = torch.round(torch.sigmoid(y_pred))

correct_results_sum = (y_pred_tag == y_test).sum().float()

acc = correct_results_sum/y_test.size(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_data:

    optimizer.zero_grad()

    X_batch=np.transpose(X_batch)

    X_batch.unsqueeze_(0)

    y_pred = net(X_batch)

    y_tensor = torch.tensor(y_batch, dtype=torch.long)

    loss = loss_fn(y_pred,  y_tensor.unsqueeze(-1))

    

    acc = binary_acc(y_pred, y_batch.unsqueeze(0))

    

    loss.backward()

    optimizer.step()

    epoch_loss += loss.item()

    epoch_acc += acc.item()

    

print(f'Epoch {e+0:03}: | Loss: {epoch_loss/len(train_data):.5f} | Acc: {epoch_acc/len(train_data):.3f}')