-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathutils.py
195 lines (154 loc) · 6.79 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from __future__ import print_function
import numpy as np
from PIL import Image
import cv2
import os.path as osp
import torch
from torch.autograd import Variable
import h5py
def srgb2rgb(srgb ):
ret = np.zeros_like(srgb )
idx0 = srgb <= 0.04045
idx1 = srgb > 0.04045
ret[idx0] = srgb[idx0] / 12.92
ret[idx1] = np.power( (srgb[idx1] + 0.055) / 1.055, 2.4 )
return ret
def writeErrToScreen(errorName, errorArr, epoch, j):
print( ('[%d/%d] {0}:' % (epoch, j) ).format(errorName), end=' ')
for n in range(0, len(errorArr) ):
print('%.6f' % errorArr[n].data.item(), end = ' ')
print('.')
def writeCoefToScreen(coefName, coef, epoch, j):
print( ('[%d/%d] {0}:' % (epoch, j) ).format(coefName), end=' ')
coefNp = coef.cpu().data.numpy()
for n in range(0, len(coefNp) ):
print('%.6f' % coefNp[n], end = ' ')
print('.')
def writeNpErrToScreen(errorName, errorArr, epoch, j):
print( ('[%d/%d] {0}:' % (epoch, j) ).format(errorName), end=' ')
for n in range(0, len(errorArr) ):
print('%.6f' % errorArr[n], end = ' ')
print('.')
def writeErrToFile(errorName, errorArr, fileOut, epoch, j):
fileOut.write( ('[%d/%d] {0}:'% (epoch, j) ).format(errorName) )
for n in range(0, len(errorArr) ):
fileOut.write('%.6f ' % errorArr[n].data.item() )
fileOut.write('.\n')
def writeCoefToFile(coefName, coef, fileOut, epoch, j):
fileOut.write( ('[%d/%d] {0}: ' % (epoch, j) ).format(coefName) )
coefNp = coef.cpu().data.numpy()
for n in range(0, len(coefNp) ):
fileOut.write('%.6f ' % coefNp[n] )
fileOut.write('.\n')
def writeNpErrToFile(errorName, errorArr, fileOut, epoch, j):
fileOut.write( ('[%d/%d] {0}:' % (epoch, j) ).format(errorName) )
for n in range(0, len(errorArr) ):
fileOut.write('%.6f ' % errorArr[n] )
fileOut.write('.\n')
def turnErrorIntoNumpy(errorArr):
errorNp = []
for n in range(0, len(errorArr) ):
errorNp.append(errorArr[n].data.item() )
return np.array(errorNp)[np.newaxis, :]
def writeImageToFile(imgBatch, nameBatch, isGama = False):
batchSize = imgBatch.size(0)
for n in range(0, batchSize):
img = imgBatch[n, :, :, :].data.cpu().numpy()
img = np.clip(img, 0, 1)
if isGama:
img = np.power(img, 1.0/2.2)
img = (255 *img.transpose([1, 2, 0] ) ).astype(np.uint8)
if img.shape[2] == 1:
img = np.concatenate([img, img, img], axis=2)
img = Image.fromarray(img )
img.save(nameBatch[n] )
def writeNumpyToFile(imBatch, nameBatch):
batchSize = imBatch.size(0)
for n in range(0, batchSize):
im = imBatch[n, :, :, :].data.cpu().numpy()
np.save(nameBatch[n], im)
def writeNumpzToFile(imBatch, nameBatch):
batchSize = imBatch.size(0)
for n in range(0, batchSize):
im = imBatch[n, :, :, :].data.cpu().numpy()
np.savez_compressed(nameBatch[n], data = im)
def writeH5ToFile(imBatch, nameBatch):
batchSize = imBatch.size(0)
assert(batchSize == len(nameBatch ) )
for n in range(0, batchSize):
im = imBatch[n, :, :, :].data.cpu().numpy()
hf = h5py.File(nameBatch[n], 'w')
hf.create_dataset('data', data=im, compression = 'lzf')
hf.close()
def writeEnvToFile(envmaps, envId, envName, nrows=12, ncols=8, envHeight=8, envWidth=16, gap=1):
envmap = envmaps[envId, :, :, :, :, :].data.cpu().numpy()
envmap = np.transpose(envmap, [1, 2, 3, 4, 0] )
envRow, envCol = envmap.shape[0], envmap.shape[1]
interY = int(envRow / nrows )
interX = int(envCol / ncols )
lnrows = len(np.arange(0, envRow, interY) )
lncols = len(np.arange(0, envCol, interX) )
lenvHeight = lnrows * (envHeight + gap) + gap
lenvWidth = lncols * (envWidth + gap) + gap
envmapLarge = np.zeros([lenvHeight, lenvWidth, 3], dtype=np.float32) + 1.0
for r in range(0, envRow, interY ):
for c in range(0, envCol, interX ):
rId = int(r / interY )
cId = int(c / interX )
rs = rId * (envHeight + gap )
cs = cId * (envWidth + gap )
envmapLarge[rs : rs + envHeight, cs : cs + envWidth, :] = envmap[r, c, :, :, :]
envmapLarge = np.clip(envmapLarge, 0, 1)
envmapLarge = (255 * (envmapLarge ** (1.0/2.2) ) ).astype(np.uint8 )
cv2.imwrite(envName, envmapLarge[:, :, ::-1] )
def writeNumpyEnvToFile(envmap, envName, nrows=12, ncols=8, envHeight=8, envWidth=16, gap=1):
envRow, envCol = envmap.shape[0], envmap.shape[1]
interY = int(envRow / nrows )
interX = int(envCol / ncols )
lnrows = len(np.arange(0, envRow, interY) )
lncols = len(np.arange(0, envCol, interX) )
lenvHeight = lnrows * (envHeight + gap) + gap
lenvWidth = lncols * (envWidth + gap) + gap
envmapLarge = np.zeros([lenvHeight, lenvWidth, 3], dtype=np.float32) + 1.0
for r in range(0, envRow, interY ):
for c in range(0, envCol, interX ):
rId = int(r / interY )
cId = int(c / interX )
rs = rId * (envHeight + gap )
cs = cId * (envWidth + gap )
envmapLarge[rs : rs + envHeight, cs : cs + envWidth, :] = envmap[r, c, :, :, :]
envmapLarge = np.clip(envmapLarge, 0, 1)
envmapLarge = (255 * envmapLarge ** (1.0/2.2) ).astype(np.uint8 )
cv2.imwrite(envName, envmapLarge[:, :, ::-1] )
def predToShading(pred, envWidth = 32, envHeight = 16, SGNum = 12 ):
Az = ( (np.arange(envWidth) + 0.5) / envWidth - 0.5 )* 2 * np.pi
El = ( (np.arange(envHeight) + 0.5) / envHeight) * np.pi / 2.0
Az, El = np.meshgrid(Az, El)
Az = Az[np.newaxis, :, :]
El = El[np.newaxis, :, :]
lx = np.sin(El) * np.cos(Az)
ly = np.sin(El) * np.sin(Az)
lz = np.cos(El)
ls = np.concatenate((lx, ly, lz), axis = 0)
ls = ls[np.newaxis, :, np.newaxis, np.newaxis, :, :]
envWeight = np.cos(El) * np.sin(El )
envWeight = envWeight[np.newaxis, np.newaxis, np.newaxis, :, :]
envRow, envCol = pred.shape[2], pred.shape[3]
pred = pred.squeeze(0)
axisOrig = pred[0:3*SGNum, :, :]
lambOrig = pred[3*SGNum : 4*SGNum, :, :]
weightOrig = pred[4*SGNum : 7*SGNum, :, :]
weight = weightOrig.reshape([SGNum, 3, envRow, envCol] ) * 0.999
weight = np.tan(np.pi / 2.0 * weight )
weight = weight[:, :, :, :, np.newaxis, np.newaxis ]
axisDir = axisOrig.reshape([SGNum, 3, envRow, envCol] )
axisDir = axisDir[:, :, :, :, np.newaxis, np.newaxis]
lamb = lambOrig.reshape([SGNum, 1, envRow, envCol] ) * 0.999
lamb = np.tan(np.pi / 2.0 * lamb )
lamb = lamb[:, :, :, :, np.newaxis, np.newaxis]
mi = lamb * (np.sum(axisDir * ls, axis=1)[:, np.newaxis, :, :, :, :] - 1)
envmaps = np.sum(weight * np.exp(mi ), axis=0)
shading = (envmaps * envWeight ).reshape([3, envRow, envCol, -1] )
shading = np.sum(shading, axis = 3)
shading = np.maximum(shading, 0.0)
return shading