Histogram of accuracy and loss

adam_kossemtini: <p>i have this train and test code and i would like to see the loss and accuracy histogram but i don’t know how to do?<br>
import torch</p>
<p>import cv2</p>
<p>import numpy as np</p>
<p>import glob</p>
<p>import scipy</p>
<p>import <a href="http://scipy.io" rel="nofollow noopener">scipy.io</a> as sio</p>
<p>from torch.utils.data import Dataset, DataLoader</p>
<p>device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)</p>
<p>Y=Convolution_opMS(imgNonOlive,(16,16),(6,6))<span class="hashtag">#appel</span> a la fonction de la preparation de donnèes</p>
<p>Y=Y.tolist() <span class="hashtag">#list</span> of samples  ‘non olive’</p>
<p>A=Convolution_opMS(imgolive,(16,16),(8,8))<span class="hashtag">#appel</span> a la fonction de la preparation de donnèes</p>
<p>A=A.tolist()  <span class="hashtag">#list</span> of samples  ‘olive’</p>
<p><span class="hashtag">#len</span>(X)</p>
<p><span class="hashtag">#savemat</span>(“olivecoup.mat”,{“fo”:X})</p>
<p><span class="hashtag">#print</span>(‘number of data :’ ,len(X)+len(Y))</p>
<p>z=np.concatenate((A,Y))</p>
<p>z=z.tolist()  <span class="hashtag">#list</span> of samples  'alldata</p>
<p>labels=[]</p>
<p>alldata = z</p>
<p>net.eval()</p>
<p>class ImageDataset:</p>
<pre><code>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(1662) : #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 = 3620

    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
</code></pre>
<p>if <strong>name</strong> == ‘<strong>main</strong>’:</p>
<pre><code>k= ImageDataset(z)

k.__getitem__(60)
</code></pre>
<p>import sklearn.model_selection as model_selection</p>
<p>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=102)</p>
<p>class trainData():</p>
<pre><code>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)
</code></pre>
<p>from torchvision import transforms</p>
<p>train_data = trainData(torch.FloatTensor(X_train),</p>
<pre><code>                   torch.FloatTensor(y_train),

                   )
</code></pre>
<p>class testData():</p>
<pre><code>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)
</code></pre>
<p>test_data = testData(torch.FloatTensor(X_test))</p>
<p>EPOCHS = 120</p>
<p>LEARNING_RATE = 0.001</p>
<p>train_loader = DataLoader(dataset=train_data, batch_size=6, shuffle=True)</p>
<p>def binary_acc(y_pred, y_testt):</p>
<pre><code>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
</code></pre>
<p>net.train()</p>
<p>for e in range(1, EPOCHS+1):</p>
<pre><code>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)

    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}')
</code></pre>
<p><span class="hashtag">#---------------------test--------------------------</span><br>
test_loader = DataLoader(dataset=test_data)</p>
<p>y_pred_list = []</p>
<p>net.eval()</p>
<p>with torch.no_grad():</p>
<pre><code>for X_batch1 in test_loader:

    

    X_batch1= X_batch1.permute(0,3,2,1).float()

    y_test_pred =net(X_batch1)

    y_pred_tag = torch.round(y_test_pred[0,:])

    y_pred_list.append(y_pred_tag.numpy())
</code></pre>
<p>y_pred_list = [a.squeeze().tolist() for a in y_pred_list]</p>
<p>y_pred_list =np.asarray(y_pred_list)</p>
<p>from sklearn.metrics import confusion_matrix</p>
<p>cm=confusion_matrix(y_test,y_pred_list[:,0].tolist())</p>
<p>error=(cm[0,1]+cm[1,0])/724</p>
<p>acc=(cm[0,0]+cm[1,1])/724</p>