Skip to content

Instantly share code, notes, and snippets.

View omarsar's full-sized avatar
🐙

Elvis Saravia omarsar

🐙
View GitHub Profile
## 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()
## 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()
## dummy transform
dummy2_transform = transforms.Compose(
[transforms.RandomRotation(45), transforms.RandomVerticalFlip()])
dummy2_result = dummy2_transform(image)
plt.imshow(dummy2_result)
## dummy transformation
dummy_transform = transforms.Compose(
[transforms.RandomRotation(45)])
dummy_result = dummy_transform(image)
plt.imshow(dummy_result)
plt.imshow(image)
image = transforms.ToPILImage(mode='L')(torch.randn(1, 96, 96))
## 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)
## 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
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))
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)