Skip to content

Instantly share code, notes, and snippets.

@yvan
Last active September 9, 2021 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yvan/f09ed6c1c6032ca45f47f9f107c8d58c to your computer and use it in GitHub Desktop.
Save yvan/f09ed6c1c6032ca45f47f9f107c8d58c to your computer and use it in GitHub Desktop.
import os
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
from torch.utils import data
from torchvision.datasets import ImageFolder
from torchvision import transforms, datasets
from torchvision.utils import make_grid
# the number of images to process in one go
batch_size = 64
# the path where our images are
image_path = os.path.join('/scratch','yns207','imgs','mgan')
# check that we have access to a GPU, pytorch version, and the number of gpus
print('do we have gpu access? {} \n what is torch version? {} \n how many gpus? {}'.format(torch.cuda.is_available(),
torch.__version__,
torch.cuda.device_count()))
# this loads the monster data
# scales it to be 64x64, converts
# it to a torch tensor and then
# normalizes the input to be between
# -1 and 1, also shuffle the dataset
monster_transform = transforms.Compose([
transforms.Scale(64),
transforms.CenterCrop(64),
transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5)),
])
# this loads the actual files and applies the above transformation
monster_dataset = ImageFolder(image_path, monster_transform)
# thes describes how to load that data, whether to shuffle,
# how mnay cpus (workers to use), ett. it gets batches of
# data so for example grabbing 64 images at once
monster_loader = data.DataLoader(dataset=monster_dataset,
batch_size=batch_size,
shuffle=True,
num_workers=1)
# check the shape of our pach of images,
# this seems right, we have 64 images and
# they are sized 64x64x3
for img in monster_loader:
# renomalize a single image
single_image = img[0][0]
single_image= (single_image*0.5)+0.5
single_image = single_image.clamp(0,1)
single_image = single_image.numpy()
# move the dimensions around to get them right
single_image = np.transpose(single_image, (1, 2, 0))
# plot image
print('image size: ',single_image.shape)
plt.imshow(single_image)
plt.axis('off')
plt.show()
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment