-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutil.py
More file actions
145 lines (112 loc) · 4.47 KB
/
util.py
File metadata and controls
145 lines (112 loc) · 4.47 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""This module contains simple helper functions """
from __future__ import print_function
import torch
import numpy as np
from PIL import Image
import os
#####
from torchvision import utils
def unnormalize(img_tensor, unnormalize_STD=[0.5], unnormalize_MEAN=[0.5]):
#img_tensor = img_tensor.transpose(1, 3)
MEAN_tensor = torch.Tensor(unnormalize_STD).type(img_tensor.type())
STD_tensor = torch.Tensor(unnormalize_MEAN).type(img_tensor.type())
#print(img_tensor.type(), MEAN_tensor.type(), STD_tensor.type())
img_tensor = img_tensor * STD_tensor + MEAN_tensor
#img_tensor = img_tensor.transpose(1, 3)
return img_tensor
def save_current_images(results, img_path, unnormalize_STD=[0.5], unnormalize_MEAN=[0.5]):
'''
:param results (OrderedDict) : -- an ordered dictionary that stores (name, tensor ) pairs
:param img_path (str): -- the string is used to save image paths
Notice: (1) one-channel image
(2) the value of unnormalize_STD and unnormalize_MEAN comes from normalize_params of dataset
:return:
'''
tensor_list =[]
for label, im_data in results.items():
if label != 'mask_M':
tensor_list.append(unnormalize(im_data, unnormalize_STD = unnormalize_STD, unnormalize_MEAN = unnormalize_MEAN))
else:
tensor_list.append(im_data)
if len(tensor_list) == 5:
group_tensor = torch.cat((tensor_list[0].data, tensor_list[1].data, tensor_list[2].data,
tensor_list[3].data, tensor_list[4].data), -1)
utils.save_image(group_tensor, img_path, nrow=1, padding=0)
# convert RGB to L and save it
imgL = Image.open(img_path).convert('L')
imgL.save(img_path)
else:
print('The number of Input tensor is wrong !')
####
def tensor2im(input_image, imtype=np.uint8):
""""Converts a Tensor array into a numpy image array.
Parameters:
input_image (tensor) -- the input image tensor array
imtype (type) -- the desired type of the converted numpy array
"""
if not isinstance(input_image, np.ndarray):
if isinstance(input_image, torch.Tensor): # get the data from a variable
image_tensor = input_image.data
else:
return input_image
image_numpy = image_tensor[0].cpu().float().numpy() # convert it into a numpy array
if image_numpy.shape[0] == 1: # grayscale to RGB
image_numpy = np.tile(image_numpy, (3, 1, 1))
image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0 # post-processing: tranpose and scaling
else: # if it is a numpy array, do nothing
image_numpy = input_image
return image_numpy.astype(imtype)
def diagnose_network(net, name='network'):
"""Calculate and print the mean of average absolute(gradients)
Parameters:
net (torch network) -- Torch network
name (str) -- the name of the network
"""
mean = 0.0
count = 0
for param in net.parameters():
if param.grad is not None:
mean += torch.mean(torch.abs(param.grad.data))
count += 1
if count > 0:
mean = mean / count
print(name)
print(mean)
def save_image(image_numpy, image_path):
"""Save a numpy image to the disk
Parameters:
image_numpy (numpy array) -- input numpy array
image_path (str) -- the path of the image
"""
image_pil = Image.fromarray(image_numpy)
image_pil.save(image_path)
def print_numpy(x, val=True, shp=False):
"""Print the mean, min, max, median, std, and size of a numpy array
Parameters:
val (bool) -- if print the values of the numpy array
shp (bool) -- if print the shape of the numpy array
"""
x = x.astype(np.float64)
if shp:
print('shape,', x.shape)
if val:
x = x.flatten()
print('mean = %3.3f, min = %3.3f, max = %3.3f, median = %3.3f, std=%3.3f' % (
np.mean(x), np.min(x), np.max(x), np.median(x), np.std(x)))
def mkdirs(paths):
"""create empty directories if they don't exist
Parameters:
paths (str list) -- a list of directory paths
"""
if isinstance(paths, list) and not isinstance(paths, str):
for path in paths:
mkdir(path)
else:
mkdir(paths)
def mkdir(path):
"""create a single empty directory if it didn't exist
Parameters:
path (str) -- a single directory path
"""
if not os.path.exists(path):
os.makedirs(path)