In [2]:
from torchvision import datasets,transforms
1 Prepare data¶
In [3]:
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5))])
In [4]:
trainset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', train = True, transform=transform, download=True)
In [5]:
trainloader = torch.utils.data.DataLoader(trainset, shuffle=True, batch_size=64)
In [6]:
testset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', train = False, transform=transform, download = True)
In [7]:
testloader = torch.utils.data.DataLoader(testset, shuffle=False, batch_size=64)
2 Design Model¶
In [8]:
from torch import nn, optim
import torch.nn.functional as F
In [9]:
class Model(nn.Module):
def __init__(self):
super().__init__()
self.l1 = nn.Linear(784, 256)
self.l2 = nn.Linear(256, 128)
self.l3 = nn.Linear(128,64)
self.l4 = nn.Linear(64,10)
def forward(self,x):
x = x.view(x.shape[0],-1)
x = F.relu(self.l1(x))
x = F.relu(self.l2(x))
x = F.relu(self.l3(x))
x = F.log_softmax(self.l4(x), dim=1)
return x
In [10]:
model = Model()
In [11]:
criterion = nn.NLLLoss()
optimizer = optim.SGD(model.parameters(),lr=0.005)
3 Train¶
In [12]:
epoch = 30
train_losses, test_losses = [],[]
for e in range(epoch):
train_loss = 0
test_loss = 0
accuracy = 0
for images, labels in trainloader:
optimizer.zero_grad()
op = model(images)
loss = criterion(op, labels)
train_loss += loss.item()
loss.backward()
optimizer.step()
else:
with torch.no_grad():
model.eval()
for images,labels in testloader:
log_ps = model(images)
prob = torch.exp(log_ps)
top_probs, top_classes = prob.topk(1, dim=1)
equals = labels == top_classes.view(labels.shape)
accuracy += equals.type(torch.FloatTensor).mean()
test_loss += criterion(log_ps, labels)
model.train()
print("Epoch: {}/{}.. ".format(e+1, epoch),
"Training Loss: {:.3f}.. ".format(train_loss/len(trainloader)),
"Test Loss: {:.3f}.. ".format(test_loss/len(testloader)),
"Test Accuracy: {:.3f}".format(accuracy/len(testloader)))
train_losses.append(train_loss/len(trainloader))
test_losses.append(test_loss/len(testloader))
In [13]:
import matplotlib.pyplot as plt
%matplotlib inline
In [14]:
plt.plot(train_losses,label = "Train losses")
plt.plot(test_losses, label = "Test losses")
plt.legend()
Out[14]:
4 Model with dropout¶
In [15]:
class Model(nn.Module):
def __init__(self):
super().__init__()
self.l1 = nn.Linear(784, 256)
self.l2 = nn.Linear(256, 128)
self.l3 = nn.Linear(128,64)
self.l4 = nn.Linear(64,10)
self.dropout = nn.Dropout(p=0.2)
def forward(self,x):
x = x.view(x.shape[0],-1)
x = self.dropout(F.relu(self.l1(x)))
x = self.dropout(F.relu(self.l2(x)))
x = self.dropout(F.relu(self.l3(x)))
x = F.log_softmax(self.l4(x), dim=1)
return x
In [16]:
model = Model()
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.parameters(),lr=0.005)
In [17]:
epoch = 30
train_losses, test_losses = [],[]
for e in range(epoch):
train_loss = 0
test_loss = 0
accuracy = 0
for images, labels in trainloader:
optimizer.zero_grad()
op = model(images)
loss = criterion(op, labels)
train_loss += loss.item()
loss.backward()
optimizer.step()
else:
with torch.no_grad():
model.eval()
for images,labels in testloader:
log_ps = model(images)
prob = torch.exp(log_ps)
top_probs, top_classes = prob.topk(1, dim=1)
equals = labels == top_classes.view(labels.shape)
accuracy += equals.type(torch.FloatTensor).mean()
test_loss += criterion(log_ps, labels)
model.train()
print("Epoch: {}/{}.. ".format(e+1, epoch),
"Training Loss: {:.3f}.. ".format(train_loss/len(trainloader)),
"Test Loss: {:.3f}.. ".format(test_loss/len(testloader)),
"Test Accuracy: {:.3f}".format(accuracy/len(testloader)))
train_losses.append(train_loss/len(trainloader))
test_losses.append(test_loss/len(testloader))
In [18]:
plt.plot(train_losses,label = "Train losses")
plt.plot(test_losses, label = "Test losses")
plt.legend()
Out[18]:
In [19]:
import numpy as np
5 Inference¶
In [20]:
def imshow(image, ax=None, title=None, normalize=True):
"""Imshow for Tensor."""
if ax is None:
fig, ax = plt.subplots()
image = image.numpy().transpose((1, 2, 0))
if normalize:
mean = np.array([0.5, 0.5, 0.5])
std = np.array([0.5, 0.5, 0.5])
image = std * image + mean
image = np.clip(image, 0, 1)
ax.imshow(image)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.tick_params(axis='both', length=0)
ax.set_xticklabels('')
ax.set_yticklabels('')
In [21]:
image,labels = next(iter(testloader))
In [22]:
labels
Out[22]:
In [23]:
imshow(image[0])
In [24]:
def view_classify(img, ps, version="MNIST", title = 0):
''' Function for viewing an image and it's predicted classes.
'''
title_order = ['T-shirt/top',
'Trouser',
'Pullover',
'Dress',
'Coat',
'Sandal',
'Shirt',
'Sneaker',
'Bag',
'Ankle Boot']
ps = ps.data.numpy().squeeze()
fig, (ax1, ax2) = plt.subplots(figsize=(6,9), ncols=2)
ax1.imshow(img.resize_(1, 28, 28).numpy().squeeze())
ax1.axis('off')
ax2.barh(np.arange(10), ps)
ax2.set_aspect(0.1)
ax2.set_yticks(np.arange(10))
if version == "MNIST":
ax2.set_yticklabels(np.arange(10))
elif version == "Fashion":
ax2.set_yticklabels(title_order, size='small');
ax2.set_title('Class Probability')
ax2.set_xlim(0, 1.1)
plt.tight_layout()
plt.title("True: " + title_order[title])
In [25]:
def display_test(i):
model.eval()
dataiter = iter(testloader)
images, labels = dataiter.next()
img = images[i]
lbl = labels[i]
with torch.no_grad():
output = model.forward(img)
ps = torch.exp(output)
view_classify(img.view(1,28,28), ps, version='Fashion', title=lbl)
In [27]:
for i in range(10):
r = np.random.randint(0,63)
display_test(r)
No comments :
Post a Comment