-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
57 lines (44 loc) · 1.26 KB
/
utils.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
import cv2 as cv
import torchvision.transforms as transforms
import numpy as np
import matplotlib.pyplot as plt
import torch
from torch.autograd import Variable
from PIL import Image
def to_variable(x,requires_grad=True):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x,requires_grad)
def show(img):
#print(img.shape)
pilTrans = transforms.ToPILImage()
pilImg = pilTrans(img)
s = np.array(pilImg)
plt.figure()
plt.imshow(s)
def show_gray(img):
print(img.shape)
pilTrans = transforms.ToPILImage()
pilImg = pilTrans(img)
s = np.array(pilImg)
plt.figure()
plt.imshow(s)
def save_gray(img, path):
pilTrans = transforms.ToPILImage()
pilImg = pilTrans(img)
print('Image saved to ', path)
pilImg.save(path)
def predict(model, img, epoch, path):
to_tensor = transforms.ToTensor() # Transforms 0-255 numbers to 0 - 1.0.
im = to_tensor(img)
#show(im)
inp = to_variable(im.unsqueeze(0), False)
#print(inp.size())
out = model(inp)
map_out = out.cpu().data.squeeze(0)
#show_gray(map_out)
new_path = path + str(epoch) + ".png"
save_gray(map_out, new_path)
#s = np.array(Image.open(new_path))
#plt.figure()
#plt.imshow(s)