-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw3utils.py
66 lines (54 loc) · 2.37 KB
/
hw3utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# These are utility functions / classes that you probably dont need to alter.
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
def tensorshow(tensor,cmap=None):
img = transforms.functional.to_pil_image(tensor/2+0.5)
if cmap != None:
plt.imshow(img,cmap=cmap)
else:
plt.imshow(img)
class HW3ImageFolder(torchvision.datasets.ImageFolder):
"""A version of the ImageFolder dataset class, customized for the super-resolution task"""
def __init__(self, root, device):
super(HW3ImageFolder, self).__init__(root, transform=None)
self.device = device
def prepimg(self,img):
return (transforms.functional.to_tensor(img)-0.5)*2 # normalize tensorized image from [0,1] to [-1,+1]
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (grayscale_image, color_image) where grayscale_image is the decolorized version of the color_image.
############################################################################################################
In order to obtain the path of the image with index index you may use following piece of code. As dataset goes
over different indices you will collect image paths.
myfile = open('test_images.txt', 'a')
path = self.imgs[index][0]
myfile.write(path)
myfile.write('\n')
myfile.close()
############################################################################################################
"""
color_image,_ = super(HW3ImageFolder, self).__getitem__(index) # Image object (PIL)
grayscale_image = torchvision.transforms.functional.to_grayscale(color_image)
return self.prepimg(grayscale_image).to(self.device), self.prepimg(color_image).to(self.device)
def visualize_batch(inputs,preds,targets,save_path=''):
inputs = inputs.cpu()
preds = preds.cpu()
targets = targets.cpu()
plt.clf()
bs = inputs.shape[0]
for j in range(bs):
plt.subplot(3,bs,j+1)
assert(inputs[j].shape[0]==1)
tensorshow(inputs[j],cmap='gray')
plt.subplot(3,bs,bs+j+1)
tensorshow(preds[j])
plt.subplot(3,bs,2*bs+j+1)
tensorshow(targets[j])
if save_path is not '':
plt.savefig(save_path)
else:
plt.show(block=True)