# Part 1
import numpy as np # numpy for numerical computation
import torch # Torch for Pytorch base (for deep learning)
import torchvision # TorchVision for image processing
import matplotlib.pyplot as plt # Plotting tools
from time import time # Timing tools
from torchvision import datasets, transforms # Import dataset and transform functions
from torch import nn, optim # Import neural network and optimization classes
import cv2 # Import computer vision tools
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,)),
])
# transform is a global class object that does the transform for you.
# Download the training data and the validation data
trainset = datasets.MNIST('C:/Users/Stanley Chan/Dropbox/Python/ColorSpace/', download=True, train=True, transform=transform)
valset = datasets.MNIST('C:/Users/Stanley Chan/Dropbox/Python/ColorSpace/', download=True, train=False, transform=transform)
# Define the loader that loads images into the correct format
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
valloader = torch.utils.data.DataLoader(valset, batch_size=64, shuffle=True)
# Create iteration objection, images, and labels
dataiter = iter(trainloader)
images, labels = dataiter.next()
# Here we construct the neural network. There is no training involved yet. We are only defining the model.
input_size = 784
hidden_sizes = [128, 64, 32]
output_size = 10
model = nn.Sequential(nn.Linear(input_size, hidden_sizes[0]),
nn.ReLU(),
nn.Linear(hidden_sizes[0], hidden_sizes[1]),
nn.ReLU(),
nn.Linear(hidden_sizes[1], hidden_sizes[2]),
nn.ReLU(),
nn.Linear(hidden_sizes[2], output_size),
nn.LogSoftmax(dim=1))
criterion = nn.NLLLoss()
images, labels = next(iter(trainloader))
images = images.view(images.shape[0], -1)
logps = model(images) #log probabilities
loss = criterion(logps, labels) #calculate the NLL loss
optimizer = optim.SGD(model.parameters(), lr=0.003, momentum=0.9)
time0 = time()
epochs = 2
for e in range(epochs):
running_loss = 0
for images, labels in trainloader:
# Flatten MNIST images into a 784 long vector
images = images.view(images.shape[0], -1)
# Training pass
optimizer.zero_grad()
output = model(images)
loss = criterion(output, labels)
#This is where the model learns by backpropagating
loss.backward()
#And optimizes its weights here
optimizer.step()
running_loss += loss.item()
else:
print("Epoch {} - Training loss: {}".format(e, running_loss/len(trainloader)))
print("\nTraining Time (in minutes) =",(time()-time0)/60)
(This code uses the validation dataset)
images, labels = next(iter(valloader))
img = images[0].view(1, 784)
with torch.no_grad():
logps = model(img)
ps = torch.exp(logps)
probab = list(ps.numpy()[0])
print("Predicted Digit =", probab.index(max(probab)))
plt.imshow(images[0].numpy().squeeze(), cmap='gray_r');
(This code uses real data)
# Using cv2.imread() method
# Using 0 to read image in grayscale mode
img = cv2.imread('digit_2.png', 0) # You will need to change the filename
# resize image to 28x28
img = cv2.resize(img, (28,28))
# flip the image color
# this is optional; and feel free to change it
for i in range(0,img.shape[0]):
for j in range(0,img.shape[1]):
if img[i,j]>125:
img[i,j] = 0
else:
img[i,j] = 255 - img[i,j]
# plot the processed image
plt.imshow(img, cmap='gray_r')
# input the processed image to the network and make prediction
img = transform(img).view(1, 784)
with torch.no_grad():
logps = model(img)
ps = torch.exp(logps)
probab = list(ps.numpy()[0])
print("Predicted Hand-written Digit =", probab.index(max(probab)))
%%shell
jupyter nbconvert --to html /content/ECE595_Demo_deeplearn.ipynb