Hi everybody!
I am trying to test prediction on my CNN model. I have 2 classes(Noise and Original) and accuracy on test data was ~97-98%. So now, i want to test it with data, which model did not see at all. If i understood correctly, i need to make a folder where will be unseen by model modified with Noise images and non modified images. But i have a problem, i do not really understand how i can give those test images without subfolders(classes). Because when model was learning, there were two subfolders - Noise & Original, but now i want to get an answer from model, which class the image is.
Now code looks like this:
import matplotlib.pyplot as plt
import numpy as np
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import os
import pandas as pd
transform = transforms.Compose([
transforms.Resize(32),
transforms.CenterCrop(32),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
exam_data_path = 'D:/RTU/dataset/ready_dataset_2classes/exam'
weigths_path = 'D:/RTU/dataset/weights_done/weights_noise_original037-97%.pt'
exam_data = torchvision.datasets.ImageFolder(root=exam_data_path, transform=transform)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# convolutional layer (sees 32x32x3 image tensor)
self.conv1 = nn.Conv2d(3, 16, 3, padding=1)
# convolutional layer (sees 16x16x16 tensor)
self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
# convolutional layer (sees 8x8x32 tensor)
self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
# max pooling layer
self.pool = nn.MaxPool2d(2, 2)
# linear layer (64 * 4 * 4 -> 500)
self.fc1 = nn.Linear(64 * 4 * 4, 500)
# linear layer (500 -> 10)
self.fc2 = nn.Linear(500, 250)
self.fc3 = nn.Linear(250, 2)
# dropout layer (p=0.25)
self.dropout = nn.Dropout(0.25) #0.25
def forward(self, x):
# add sequence of convolutional and max pooling layers
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = self.pool(F.relu(self.conv3(x)))
# flatten image input
x = x.view(-1, 64 * 4 * 4)
# add dropout layer
x = self.dropout(x)
# add 1st hidden layer, with relu activation function
x = F.relu(self.fc1(x))
# add dropout layer
x = self.dropout(x)
# add 2nd hidden layer, with relu activation function
x = F.relu(self.fc2(x))
x = self.dropout(x)
x = self.fc3(x)
return x
index = -1
# Disable grad
with torch.no_grad():
# Retrieve item
for x in range(10):
index = index + 1
item = exam_data[index]
image = item[0]
true_target = item[1]
mlp = Net()
optimizer = optim.SGD(mlp.parameters(), lr=0.01)
epoch=5
valid_loss_min = np.Inf
checkpoint = torch.load(weigths_path , map_location=torch.device('cpu'))
mlp.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
epoch = checkpoint['epoch']
valid_loss_min = checkpoint['valid_loss_min']
print(image)
image = image.reshape(1 , 3, 32, 32)
mlp.eval()
# Generate prediction
prediction = mlp(image)
# Predicted class value using argmax
predicted_class = np.argmax(prediction)
# Reshape image
image = image.reshape(32, 32, 3)
# Show result
plt.imshow(image)
plt.title(f'Prediction: {predicted_class} - Actual target: {true_target}')
plt.show()
As i understood, i need to make the own dataset. I found some info in PyTorch documentation, but there are a lot of new information, which i do not understand at all and my mind is blowing
Maybe somebody can help me and explain in a lighter language how i can solve it.