76 KiB
76 KiB
import torch
import torchvision
import torchvision.transforms as transforms
from PIL import Image
import os
transform = transforms.Compose(
[ transforms.Resize(32),
transforms.Pad(10, fill=255),
transforms.CenterCrop((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
]
)
def check_image(path):
try:
im = Image.open(path)
im.verify()
return True
except:
print(path)
return False
finally:
im.close()
#transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225]) <-- this could part of the problem because supposedly 0.5 0.5 is used for grayscale images
trainset = torchvision.datasets.ImageFolder(root='../datasets/Damskie_mini/', transform=transform,is_valid_file = check_image)
testset = torchvision.datasets.ImageFolder(root='../datasets/Damskie_mini_test/', transform=transform,is_valid_file = check_image)
#, is_valid_file = check_image
trainloader = torch.utils.data.DataLoader(trainset, batch_size=16, shuffle=True, num_workers=12, drop_last=True)
testloader = torch.utils.data.DataLoader(testset, batch_size=16, shuffle=True, num_workers=12, drop_last=True)
print("Done")
Done
import matplotlib.pyplot as plt
import numpy as np
# functions to show an image
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()
print(images[0].min())
# show images
imshow(torchvision.utils.make_grid(images))
# print labels
#print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
tensor(-0.7882)
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 18)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Net()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
net.to(device)
cuda:0
Net( (conv1): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1)) (pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1)) (fc1): Linear(in_features=400, out_features=120, bias=True) (fc2): Linear(in_features=120, out_features=84, bias=True) (fc3): Linear(in_features=84, out_features=18, bias=True) )
%%time
import torch.optim as optim
# Assuming that we are on a CUDA machine, this should print a CUDA device:
print(device)
BATCH_SIZE = 16
EPOCHS = 15
OUTPUTS= 1
LR = 0.025
MINI_BATCH_SIZE = 500
print("wololo1")
criterion = nn.CrossEntropyLoss()
print("wololo3")
optimizer = optim.SGD(net.parameters(), lr=LR, momentum=0.9)
print("wololo4")
for epoch in range(EPOCHS): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data[0].to(device), data[1].to(device)
#inputs, labels = data
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % MINI_BATCH_SIZE == MINI_BATCH_SIZE - 1: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i-1,running_loss / MINI_BATCH_SIZE))
running_loss = 0.0
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels = data[0].to(device), data[1].to(device)
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
print(outputs)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: %.2f %%' % (
100.0 * correct / total))
print('Finished Training')
cuda:0 wololo1 wololo3 wololo4 [1, 498] loss: 2.228 [1, 998] loss: 2.298 Accuracy of the network on the 10000 test images: 29.24 % [2, 498] loss: 2.171 [2, 998] loss: 2.312 Accuracy of the network on the 10000 test images: 30.47 % [3, 498] loss: 2.146 [3, 998] loss: 2.262 Accuracy of the network on the 10000 test images: 28.68 % [4, 498] loss: 2.201 [4, 998] loss: 2.335 Accuracy of the network on the 10000 test images: 31.08 % [5, 498] loss: 2.197 [5, 998] loss: 2.223 Accuracy of the network on the 10000 test images: 32.20 % [6, 498] loss: 2.279 [6, 998] loss: 2.253 Accuracy of the network on the 10000 test images: 24.39 % [7, 498] loss: 2.237 [7, 998] loss: 2.246 Accuracy of the network on the 10000 test images: 32.92 % [8, 498] loss: 2.223 [8, 998] loss: 2.195 Accuracy of the network on the 10000 test images: 23.49 % [9, 498] loss: 2.249 [9, 998] loss: 2.239 Accuracy of the network on the 10000 test images: 27.79 % [10, 498] loss: 2.261 [10, 998] loss: 2.262 Accuracy of the network on the 10000 test images: 28.63 % [11, 498] loss: 2.238 [11, 998] loss: 2.299 Accuracy of the network on the 10000 test images: 26.84 % [12, 498] loss: 2.198 [12, 998] loss: 2.344 Accuracy of the network on the 10000 test images: 30.41 % [13, 498] loss: 2.240 [13, 998] loss: 2.282 Accuracy of the network on the 10000 test images: 28.68 % [14, 498] loss: 2.263 [14, 998] loss: 2.230 Accuracy of the network on the 10000 test images: 31.75 % [15, 498] loss: 2.298 [15, 998] loss: 2.278 Accuracy of the network on the 10000 test images: 30.97 % Finished Training CPU times: user 1min 23s, sys: 20.6 s, total: 1min 44s Wall time: 2min 59s
import requests
from torch.autograd import Variable
url1 = "https://chillizet-static.hitraff.pl/uploads/productfeeds/images/99/dd/house-klapki-friends-czarny.jpg"
url2 = "https://e-obuwniczy.pl/pol_pl_POLBUTY-BUT-BAL-VENETTO-635-SKORA-LICOWA-CZARNY-2551_5.jpg"
url3 = "https://bhp-nord.pl/33827-thickbox_default/but-s1p-portwest-steelite-tove-ft15.jpg"
url4 = "https://www.sklepmartes.pl/174554-thickbox_default/dzieciece-kalosze-cosy-wellies-kids-2076-victoria-blue-bejo.jpg"
img = Image.open(requests.get(url4, stream=True).raw)
image_tensor = transform(img).float()
imshow(image_tensor)
image_tensor = image_tensor.unsqueeze_(0)
inputi = Variable(image_tensor)
shoe_names = { 0: "Balerinki", 1: "Botki", 2:"Creepersy", 3: "Czolenka", 4:"Domowe", 5:"Espadryle",
6:"Glany", 7:"Kalosze", 8:"Klapki", 9:"Kozaki", 10:"Mokasyny", 11:"Polbuty", 12:"Pozostale",
13:"Sandaly", 14:"Sniegowce", 15:"Sportowe", 16:"Tenisowki", 17:"Trekkingowe"}
output = net(inputi.to(device))
_, predicted = torch.max(output.data, 1)
print(shoe_names[int(predicted)])
[0;31m---------------------------------------------------------------------------[0m [0;31mNameError[0m Traceback (most recent call last) [0;32m<ipython-input-1-9770c3effe28>[0m in [0;36m<module>[0;34m[0m [1;32m 8[0m [0murl4[0m [0;34m=[0m [0;34m"https://www.sklepmartes.pl/174554-thickbox_default/dzieciece-kalosze-cosy-wellies-kids-2076-victoria-blue-bejo.jpg"[0m[0;34m[0m[0;34m[0m[0m [1;32m 9[0m [0;34m[0m[0m [0;32m---> 10[0;31m [0mimg[0m [0;34m=[0m [0mImage[0m[0;34m.[0m[0mopen[0m[0;34m([0m[0mrequests[0m[0;34m.[0m[0mget[0m[0;34m([0m[0murl4[0m[0;34m,[0m [0mstream[0m[0;34m=[0m[0;32mTrue[0m[0;34m)[0m[0;34m.[0m[0mraw[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0m[1;32m 11[0m [0;34m[0m[0m [1;32m 12[0m [0mimage_tensor[0m [0;34m=[0m [0mtransform[0m[0;34m([0m[0mimg[0m[0;34m)[0m[0;34m.[0m[0mfloat[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0;31mNameError[0m: name 'Image' is not defined
import os, sys
# Open a file
path = "../datasets/Damskie_mini/"
dirs = os.listdir( path )
# This would print all the files and directories
for file in dirs:
print(file)
trainset.class_to_idx
Trekkingowe Balerinki Sniegowce Mokasyny Czolenka Domowe Glany Creepersy Polbuty Sandaly Pozostale Botki Kalosze Kozaki Espadryle Tenisowki Sportowe Klapki
{'Balerinki': 0, 'Botki': 1, 'Creepersy': 2, 'Czolenka': 3, 'Domowe': 4, 'Espadryle': 5, 'Glany': 6, 'Kalosze': 7, 'Klapki': 8, 'Kozaki': 9, 'Mokasyny': 10, 'Polbuty': 11, 'Pozostale': 12, 'Sandaly': 13, 'Sniegowce': 14, 'Sportowe': 15, 'Tenisowki': 16, 'Trekkingowe': 17}
Sniegowce
'\n??????????????\nimport os, sys\n\n# Open a file\npath = "./datasets/Damskie_mini/"\ndirs = os.listdir( path )\n\n# This would print all the files and directories\nfor file in dirs:\n print(file)\n'
import numpy as np
import os.path
import csv
torch.save(net, 'nn.pth')
torch.save(net.state_dict(), "nn-state-dict.pth")