80 likes | 88 Views
This assignment code demonstrates the implementation of a simple neural network architecture using PyTorch for image classification. It covers the process of defining the network parts, loading the data into tensors, creating the model, and fitting the model. Important concepts such as convolutional layers, pooling layers, batch normalization, and data augmentation are also explored.
E N D
Overview of Neural Network Architecture Assignment Code Geoff Hulten
PyTorch • Deep learning platform – easy to use • Install from: https://pytorch.org/get-started/locally/ • CUDA enables GPU training – 100s of times faster • Docs: • https://pytorch.org/docs/stable/index.html • https://pytorch.org/docs/stable/nn.html <- important
Define the Neural Network Parts importtorch classSimpleBlinkNeuralNetwork(torch.nn.Module): def __init__(self, hiddenNodes = 20): super(SimpleBlinkNeuralNetwork, self).__init__() # Down sample the image to 12x12 self.avgPooling = torch.nn.AvgPool2d(kernel_size = 2, stride = 2) # Fully connected layer to all the down-sampled pixels to all the hidden nodes self.fullyConnectedOne = torch.nn.Sequential( torch.nn.Linear(12*12, hiddenNodes), torch.nn.Sigmoid() ) # Fully connected layer from the hidden layer to a single output node self.outputLayer = torch.nn.Sequential( torch.nn.Linear(hiddenNodes, 1), torch.nn.Sigmoid() )
Run Forward Pass def forward(self, x): # Apply the layers created at initialization time in order out = self.avgPooling(x) out = out.reshape(out.size(0), -1) out = self.fullyConnectedOne(out) out = self.outputLayer(out) return out
Load the Data into Tensors fromPILimportImage importtorchvision.transformsastransforms importtorch # Load the images and then convert them into tensors (no normalization) xTrainImages = [ Image.open(path) forpathinxTrainRaw ] xTrain = torch.stack([ transforms.ToTensor()(image) forimageinxTrainImages ]) xTestImages = [ Image.open(path) forpathinxTestRaw ] xTest = torch.stack([ transforms.ToTensor()(image) forimageinxTestImages ]) yTrain = torch.Tensor([ [ yValue ] foryValueinyTrainRaw ]) yTest = torch.Tensor([ [ yValue ] foryValueinyTestRaw ])
Create the Model and Support # Create the model and set up: # the loss function to use (Mean Square Error) # the optimization method (Stochastic Gradient Descent) and the step size model = SimpleBlinkNeuralNetwork.SimpleBlinkNeuralNetwork(hiddenNodes = 5) lossFunction = torch.nn.MSELoss(reduction='sum') optimizer = torch.optim.SGD(model.parameters(), lr=1e-4, momentum=0.9)
Fit the Model foriinrange(500): # Do the forward pass yTrainPredicted = model(xTrain) # Compute the training set loss loss = lossFunction(yTrainPredicted, yTrain) print(i, loss.item()) # Reset the gradients in the network to zero optimizer.zero_grad() # Backprop the errors from the loss on this iteration loss.backward() # Do a weight update step optimizer.step()
Things to look into to succeed at this assignment: • torch.nn.Conv2d • torch.nn.ReLu • torch.nn.MaxPool2d • torch.nn.BatchNorm2d • torch.nn.Dropout • Tuning the number of filters/nodes, the optimizer, and the training iterations • torchvision.transforms – Data augmentation (e.g. mirroring training data)