-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaveimage_utils.py
183 lines (115 loc) · 5.3 KB
/
saveimage_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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import torch
from torchvision import transforms
from timeit import default_timer as timer
from torchvision.utils import save_image,make_grid
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from torch import Tensor
import numpy as np
import os
from datetime import timedelta
##
imshowParams = {
'cmap' : 'gray',
'interpolation' : 'none'
}
ext = '.pdf'
savefigParams = {
}
def imshow_version(x, mode='all',padding=0):
if mode=='all':
y=x[:16].squeeze(1)
im=make_grid(y.view(-1,1,x.size()[-2],x.size()[-1]), 4,padding=padding)
return np.transpose(im.numpy(),(1,2,0))[:,:,0]
def save_fig(x, path, name, iteration=None, doCurrent=False, figshow=False, Title=None, scaleEach=False):
if Title is None:
Title = name
fig =plt.figure(1,figsize= (20,20))
im=np.transpose(make_grid(x,nrow=int(np.sqrt(x.size()[0])),padding=0,scale_each=scaleEach,normalize=scaleEach).numpy(),(1,2,0))[:,:,0]
plt.imshow(im, **imshowParams)
if not scaleEach: ## don't draw colorbar if each panel is caled separately
plt.colorbar(fraction=0.046, pad=0.04)
if torch.any(torch.isnan(x)):
Title += " *******WARNING, NaN's detected********"
plt.title(Title)
if doCurrent:
fig.savefig(os.path.join(path, 'current-' + name + ext), **savefigParams )
if iteration is not None:
name = iteration + "-" + name
fig.savefig(os.path.join(path, name + ext), **savefigParams )
# if you want raw data saved:
# save_image(x, os.path.join(path, name + '.png'), normalize=True)
plt.close(fig)
def save_fig_single(x, path, name, nrow=None, iteration=None, doCurrent=False, figshow=False, Title=None, scaleEach=False):
Title = name if Title is None else Title
nrow=int(np.sqrt(x.size()[0])) if nrow is None else nrow
fig =plt.figure(1,figsize= (10*nrow,10*x.shape[0]//nrow))
im=np.transpose(make_grid(x,nrow,padding=int(0.05*x.shape[-1]),scale_each=scaleEach,normalize=scaleEach).numpy(),(1,2,0))[:,:,0]
plt.imshow(im, **imshowParams)
plt.axis('off')
if torch.any(torch.isnan(x)):
Title += " *******WARNING, NaN's detected********"
plt.title(Title)
if doCurrent:
fig.savefig(os.path.join(path, 'current-' + name + ext), **savefigParams )
if iteration is not None:
name = iteration + "-" + name
fig.savefig(os.path.join(path, name + ext), **savefigParams )
# if you want raw data saved:
# save_image(x, os.path.join(path, name + '.png'), normalize=True)
plt.close(fig)
def save_fig_single_separate(x, path, name, nrow=None, iteration=None, doCurrent=False, figshow=False, Title=None, scaleEach=False, vminvalue=None, vmaxvalue=None):
Title = name if Title is None else Title
nrow=int(np.sqrt(x.size()[0])) if nrow is None else nrow
fig =plt.figure(1)#,figsize= (10*nrow,10*x.shape[0]//nrow))
x=x.numpy()
#string=str(x.shape[0]//nrow)+str(nrow)
for i, im in enumerate(x):
#plt.subplot(string+str(i))
if vminvalue is not None:
vmin=vminvalue
vmax=vmaxvalue
plt.imshow(im.squeeze(), **imshowParams, vmin=vmin, vmax=vmax)
else:
plt.imshow(im.squeeze(), **imshowParams)
plt.axis('off')
fig.savefig(os.path.join(path, name + ext), **savefigParams,bbox_inches='tight' ,pad_inches=-0.1)
plt.close(fig)
# if you want raw data saved:
# save_image(x, os.path.join(path, name + '.png'), normalize=True)
def save_fig_double(x, y, path, name, iteration=None, doCurrent=False, \
figshow=False, Title1= None,Title2= None, sameColorbar=False, mask=None ):
fig =plt.figure(2,figsize= (40,20))
with torch.no_grad():
if mask is not None:
xNonZero=x+(mask.cpu()==0).float()*(x.max()+x.min())/2
yNonZero=y+(mask.cpu()==0).float()*(y.max()+y.min())/2
else:
xNonZero=x
yNonZero=y
vmax = np.max([torch.max(xNonZero), torch.max(yNonZero)])
vmin = np.min([torch.min(xNonZero), torch.min(yNonZero)])
plt.subplot(1,2,1)
if sameColorbar:
plt.imshow(imshow_version(x,padding=0), vmin=vmin, vmax=vmax, **imshowParams)
else:
plt.imshow(imshow_version(x,padding=0),vmin=torch.min(xNonZero), vmax=torch.max(xNonZero), **imshowParams)
if torch.any(torch.isnan(x)):
Title1 += " *******WARNING, NaN's detected********"
plt.title(Title1)
plt.colorbar(fraction=0.046, pad=0.04)
plt.subplot(1,2,2)
if sameColorbar:
plt.imshow(imshow_version(y,padding=0), vmin=vmin, vmax=vmax, **imshowParams)
else:
plt.imshow(imshow_version(y,padding=0), vmin=torch.min(yNonZero), vmax=torch.max(yNonZero), **imshowParams)
if torch.any(torch.isnan(y)):
Title2 += " *******WARNING, NaN's detected********"
plt.title(Title2)
plt.colorbar(fraction=0.046, pad=0.04)
if doCurrent:
fig.savefig(os.path.join(path, 'current-' + name + ext), **savefigParams )
if iteration is not None:
name = iteration + "-" + name
fig.savefig(os.path.join(path, name + ext), **savefigParams )
plt.close(fig)