66 lines
1.8 KiB
Python
66 lines
1.8 KiB
Python
|
import torch
|
||
|
import torchvision
|
||
|
import torch.nn as nn
|
||
|
import torch.nn.functional as F
|
||
|
import torchvision.transforms as transforms
|
||
|
import matplotlib.pyplot as plt
|
||
|
import numpy as np
|
||
|
|
||
|
transform = transforms.Compose(
|
||
|
[transforms.ToTensor(),
|
||
|
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
|
||
|
|
||
|
trainset = torchvision.datasets.ImageFolder(
|
||
|
root='./resources/zbior_uczacy', transform=transform)
|
||
|
trainloader = torch.utils.data.DataLoader(
|
||
|
trainset, batch_size=4, shuffle=True, num_workers=2)
|
||
|
|
||
|
testset = torchvision.datasets.ImageFolder(
|
||
|
root='./resources/smieci', transform=transform)
|
||
|
testloader = torch.utils.data.DataLoader(
|
||
|
testset, batch_size=4, shuffle=False, num_workers=2)
|
||
|
|
||
|
classes = ('glass', 'metal', 'paper', 'plastic')
|
||
|
|
||
|
|
||
|
def imshow(img):
|
||
|
img = img / 2 + 0.5
|
||
|
npimg = img.numpy()
|
||
|
plt.imshow(np.transpose(npimg, (1, 2, 0)))
|
||
|
plt.show()
|
||
|
|
||
|
|
||
|
dataiter = iter(trainloader)
|
||
|
images, labels = dataiter.next()
|
||
|
|
||
|
# show images
|
||
|
imshow(torchvision.utils.make_grid(images))
|
||
|
# print labels
|
||
|
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
|
||
|
|
||
|
|
||
|
class Siec(nn.Module):
|
||
|
def __init__(self):
|
||
|
super(Siec, self).__init__()
|
||
|
self.conv1 = nn.Conv2d(1, 6, 3)
|
||
|
self.conv2 = nn.Conv2d(6, 16, 3)
|
||
|
self.fc1 = nn.Linear(16*6*6, 120)
|
||
|
self.fc2 = nn.Linear(120, 84)
|
||
|
self.fc3 = nn.Linear(84, 10)
|
||
|
|
||
|
def forward(self, x):
|
||
|
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
|
||
|
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
|
||
|
x = x.view(-1, self.num_flat_features(x))
|
||
|
x = F.relu(self.fc1(x))
|
||
|
x = F.relu(self.fc2(x))
|
||
|
x = self.fc3(x)
|
||
|
return x
|
||
|
|
||
|
def num_flat_features(self, x):
|
||
|
size = x.size()[1:] # all dimensions except the batch dimension
|
||
|
num_features = 1
|
||
|
for s in size:
|
||
|
num_features *= s
|
||
|
return num_features
|