Testing the model resnet

username: adam_kossemtini
cooked: <p>hi guys,<br>
i have created a resnet model(pytorch) but i don’t know how i can testing it with the test set!<br>
who can help me with a code?</p>

username: Mughees
cooked: <p>explain your query? you just want to test that your model is working or not?</p>

username: adam_kossemtini
cooked: <p>i would like to predict the model with the all test_set</p>

username: Mughees
cooked: <p>Have you trained your model on training_data? after that just follow these tutorials. <a href="https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html" rel="nofollow noopener">one</a>, <a href="https://pytorch.org/tutorials/intermediate/quantized_transfer_learning_tutorial.html" rel="nofollow noopener">two</a></p>

username: adam_kossemtini
cooked: <p>yes, and i got accuracy 90%</p>

username: adam_kossemtini
cooked: <p>i split my data into x_train,x_test,y_train,y_test</p>

username: Mughees
cooked: <p>x_test and y_test is your test set. so it means you have already done that</p>

username: adam_kossemtini
cooked: <p>yes, but i would like to do the predict with them after training the model</p>

username: Mughees
cooked: <p>can you post your training code here</p>

username: adam_kossemtini
cooked: <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(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
</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=101)</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 = 20</p>
<p>LEARNING_RATE = 0.001</p>
<p>def binary_acc(y_pred, y_test):</p>
<pre><code>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
</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_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}')</code></pre>