I followed the mnist example github_pytorch_mnist_example
I got almost everthing the same…
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5, 1)
self.conv2 = nn.Conv2d(20, 50, 5, 1)
self.fc1 = nn.Linear(4*4*50, 500)
self.fc2 = nn.Linear(500, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2, 2)
x = x.view(-1, 4*4*50)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return F.log_softmax(x, dim=1)
the train
function and test
function stayed also (mostly) the same, omited args, instead I only pass log_interval to the train
functions.
instead of using args I initialised the needed values by assignment:
log_interval = 64;
batch_size = 64;
test_batch_size = 64;
use_cuda = False; #not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(1); #args.seed
device = torch.device("cuda" if use_cuda else "cpu");
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {};
lr = 0.01;
momentum = 0.5;
init the test loader
# init the train loader
# ...
# init the test loader
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('./data', train=False,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=test_batch_size, shuffle=True, **kwargs
)
train the model:
model = Net().to(device);
optimizer = optim.SGD(model.parameters(), lr=lr, momentum=momentum);
epochs = 3
for epoch in range(1, epochs + 1):
train(model, device, train_loader, optimizer, epoch, log_interval)
test(model, device, test_loader)
and this works fine so far, model trains and got good output.
But this is where the example ends.
My aim is to create a mnist example from zero to production.
For this the next thing I need to know is how to predict a single image.
I did not found documentation to that topic.
I tried this (which worked in PyTorch 0.4 imo):
single_loaded_img = test_loader.dataset.data[0]
single_loaded_img = single_loaded_img.to(device)
single_loaded_img = Variable(single_loaded_img)
out_predict = model(single_loaded_img)
but this returned:
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [20, 1, 5, 5], but got 2-dimensional input of size [28, 28] instead
I kind of understand that the model is expecting batch-ed?! data?
But I have no clue how to transform the image in a way that the model can use it to predict.
I would like the solution but I would more like to understand what I have to do, what knowledge I miss…