This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 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))) | |
| ## get some random training images | |
| dataiter = iter(trainloader) | |
| images, labels = dataiter.next() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 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))) | |
| ## get some random training images | |
| dataiter = iter(trainloader) | |
| images, labels = dataiter.next() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## dummy transform | |
| dummy2_transform = transforms.Compose( | |
| [transforms.RandomRotation(45), transforms.RandomVerticalFlip()]) | |
| dummy2_result = dummy2_transform(image) | |
| plt.imshow(dummy2_result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## dummy transformation | |
| dummy_transform = transforms.Compose( | |
| [transforms.RandomRotation(45)]) | |
| dummy_result = dummy_transform(image) | |
| plt.imshow(dummy_result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| plt.imshow(image) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| image = transforms.ToPILImage(mode='L')(torch.randn(1, 96, 96)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## parameter denoting the batch size | |
| BATCH_SIZE = 32 | |
| ## transformations | |
| transform = transforms.Compose( | |
| [transforms.ToTensor()]) | |
| ## download and load training dataset | |
| trainset = torchvision.datasets.MNIST(root='./data', train=True, | |
| download=True, transform=transform) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## The usual imports | |
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| import torchvision | |
| import torchvision.transforms as transforms | |
| ## for printing image | |
| import matplotlib.pyplot as plt | |
| import numpy as np |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| test_acc = 0.0 | |
| for i, (images, labels) in enumerate(testloader, 0): | |
| images = images.to(device) | |
| labels = labels.to(device) | |
| outputs = model(images) | |
| test_acc += get_accuracy(outputs, labels, BATCH_SIZE) | |
| print('Test Accuracy: %.2f'%( test_acc/i)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for epoch in range(num_epochs): | |
| train_running_loss = 0.0 | |
| train_acc = 0.0 | |
| model = model.train() | |
| ## training step | |
| for i, (images, labels) in enumerate(trainloader): | |
| images = images.to(device) |