-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
279 lines (231 loc) · 10.5 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""
# -----------------------------------------------------------
VARIOUS UTILITIES AND PLOT FUNCTIOS USED IN SHMGAN
Uses Packages:
Python 3.8
CUDA 11.8
cuDnn 8.0
Tensorflow 2.5/2.6 + Keras 2.4
(C) 2023 Atif Anwer, INSA Rouen, France
Email: atif.anwer@insa-rouen.fr
BLOCKS:
1. SHMGAN FUNCTIONS
2. SAVE FUNCTIONS
3. PRINT STUFF
4. VARIOUS PLOT FUNCTIONS
# -----------------------------------------------------------
"""
import os
import cv2
import h5py
import numpy as np
import tensorflow as tf
from matplotlib import cm
from matplotlib import pyplot as plt
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# --------------------------------------------------------------
# SHMGAN FUNCTIONS |
# --------------------------------------------------------------
def check_gpu():
# ----------------------------------------
# SETUP GPU
# ----------------------------------------
# # Testing and enabling GPU
os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices'
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
print( tf.test.is_built_with_cuda() )
gpus = tf.config.list_physical_devices( 'GPU' )
print( "[ => ] Num GPUs Available: ", len( gpus ) )
for gpu in gpus:
print( "Name:", gpu.name, " Type:", gpu.device_type )
tf.config.experimental.set_memory_growth(gpus[0], True)
os.system("nvidia-smi --query-gpu=gpu_name --format=csv")
# https://www.tensorflow.org/api_docs/python/tf/config/threading/set_inter_op_parallelism_threads
tf.config.threading.set_inter_op_parallelism_threads( 0 )
tf.config.threading.set_intra_op_parallelism_threads( 0 )
tf.config.experimental.enable_tensor_float_32_execution(enabled=True)
# https://www.tensorflow.org/api_docs/python/tf/config/optimizer/set_experimental_options
tf.config.optimizer.set_experimental_options({'constant_folding': True})
tf.config.optimizer.set_experimental_options({'layout_optimizer': True})
tf.config.optimizer.set_experimental_options({'remapping': True})
tf.config.optimizer.set_experimental_options({'loop_optimization': True})
tf.config.optimizer.set_experimental_options({'pin_to_host_optimization': True})
tf.config.set_soft_device_placement(True)
os.environ['TF_ENABLE_AUTO_MIXED_PRECISION'] = '1'
# ------------------------------------------
def calculate_estimate_diffuse( args, OriginalImageStack, imgStack_0deg, imgStack_45deg, imgStack_90deg, imgStack_135deg ):
filepath = args.path
dirname = os.path.dirname( filepath )
# Estimated diffuse images pre-calculated and saved
estDiffFolder = os.path.join( dirname, "Estimated_Diffuse" )
b = []
g = []
r = []
estimated_diffuse_stack = []
filenames_est_diffuse = []
i = 0
for orig, img0, img45, img90, img135 in zip( OriginalImageStack, imgStack_0deg, imgStack_45deg, imgStack_90deg, imgStack_135deg ):
# Note: Each img variable is a 3 channel image; so we can split it up in BGR
blue, green, red = cv2.split( img0 )
b.append( blue )
g.append( green )
r.append( red )
blue, green, red = cv2.split( img45 )
b.append( blue )
g.append( green )
r.append( red )
blue, green, red = cv2.split( img90 )
b.append( blue )
g.append( green )
r.append( red )
blue, green, red = cv2.split( img135 )
b.append( blue )
g.append( green )
r.append( red )
b_min = np.amin( b, axis = 0 )
g_min = np.amin( g, axis = 0 )
r_min = np.amin( r, axis = 0 )
merged = cv2.merge( [b_min, g_min, r_min] )
i += 1
# WRITE the image to a file if required. Can eb commented out if req
name = estDiffFolder + "/" + 'Result_' + str( i ) + '_ed.png'
# cv2.imwrite(name, merged)
filenames_est_diffuse.append( name )
# Stack the estimated diffuse images for later use in loop
estimated_diffuse_stack.append( merged )
# clear data before next loop; avoiding any data overwriting issues
b.clear()
g.clear()
r.clear()
merged.fill( 0 ) # clear the vars before calculating
return estimated_diffuse_stack
# ------------------------------------------
# White balance
# Source: https://stackoverflow.com/questions/46390779/automatic-white-balancing-with-grayworld-assumption
# ------------------------------------------
# def white_balance( input_image ):
# result = cv2.cvtColor( input_image, cv2.COLOR_RGB2LAB )
# avg_a = np.average( result[:, :, 1] )
# avg_b = np.average( result[:, :, 2] )
# result[:, :, 1] = result[:, :, 1] - ((avg_a - 128) * (result[:, :, 0] / 255.0) * 1.1)
# result[:, :, 2] = result[:, :, 2] - ((avg_b - 128) * (result[:, :, 0] / 255.0) * 1.1)
# whiteBalancedImage = cv2.cvtColor( result, cv2.COLOR_LAB2RGB )
# return whiteBalancedImage
# ------------------------------------------------------------
# SAVE FUNCTIONS |
# ------------------------------------------------------------
def save_dataset_hdf5( image_stack ):
save_path = './estimated_diffuse_images.hdf5'
hf = h5py.File( save_path, 'a' ) # open a hdf5 file
hf.create_dataset( 'default', data = image_stack, compression = "gzip", compression_opts = 9 )
hf.close() # close the hdf5 file
print( '\n [ => ] Dataset Saved. hdf5 file size: %d bytes' % os.path.getsize( save_path ) )
# --------------------------------------------------------------
# PRINT STUFF |
# --------------------------------------------------------------
# ------------------------------------------
# Print iterations progress
# https://github.com/Kal213/StarGAN-Tutorial-Tensorflow-2.3/blob/main/datagen.py
# Usage: printProgressBar(step % 1000, 999, decimals=2)
# ------------------------------------------
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 50, fill = '█'):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print('\r %s |%s| %s%% %s' % (prefix, bar, percent, suffix), end = '\r')
# Print New Line on Complete
if iteration == total:
print()
print()
# --------------------------------------------------------------
# VARIOUS PLOT FUNCTIONS |
# --------------------------------------------------------------
# ------------------------------------------
# https://github.com/Ha0Tang/AttentionGAN/blob/68a478a944bb45d288d67f99fe110ddf087fd84d/AttentionGAN-v1-multi/solver.py#L123
# --------------------------------------------------
"""Convert the range from [-1, 1] to [0, 1]."""
# --------------------------------------------------
def rescale_01( input_tensor ):
rescaled_01 = tf.math.divide_no_nan( \
tf.math.subtract(input_tensor,tf.math.reduce_min(input_tensor) ) , \
tf.math.subtract( tf.math.reduce_max(input_tensor), tf.math.reduce_min(input_tensor)))
return rescaled_01
# ------------------------------------------
# Plot an image grid
# Sauce: https://www.tensorflow.org/tensorboard/image_summaries
def image_grid( im1, im2, im3, im4, im5 ):
"""Return a 5x5 grid of the MNIST images as a matplotlib figure."""
# Create a figure to contain the plot.
figure = plt.figure(figsize=(15,5))
plt.subplot(1, 5, 1, title="0")
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow( ( tf.squeeze( im1 ).numpy().astype("float32")) )
plt.subplot(1, 5, 2, title="45")
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow( ( tf.squeeze( im2 ).numpy().astype("float32")) )
plt.subplot(1, 5, 3, title="90")
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow( ( tf.squeeze( im3 ).numpy().astype("float32")) )
plt.subplot(1, 5, 4, title="135")
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow( ( tf.squeeze( im4 ).numpy().astype("float32")) )
plt.subplot(1, 5, 5, title="ED")
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow( ( tf.squeeze( im5 ).numpy().astype("float32")) )
return figure
# ------------------------------------------
# Taking an input tensor and plotting each channel (image+mask) for debugging randomness
def debug_plot( input_tensor ):
figure = plt.figure( figsize=(15,10) )
input_tensor = tf.squeeze( input_tensor )
channel = 0
for index in range ( 5 ):
channel += 1
figure.add_subplot( 2, 5, channel, title=str(channel-1))
# plt.tight_layout()
plt.xticks([])
plt.yticks([])
plt.imshow( (input_tensor[:, :, channel-1]).numpy().astype("float32"), cmap=cm.gray, )
figure.add_subplot( 2, 5, channel+5, title="Mask "+str(channel+5))
plt.xticks([])
plt.yticks([])
plt.imshow( input_tensor[:, :, channel-1+5], cmap=cm.gray, vmin=0, vmax=1 )
return figure
# ------------------------------------------
# Universal debug plotting for single or multi-channel images. Call whenever required
def plot_single_image( input_tensor, title = ""):
figure = plt.figure( figsize=(10,15) )
if len( input_tensor[0,0,0,:] ) == 1:
plt.imshow( ( tf.squeeze ( input_tensor ).numpy().astype("float32") ), cmap=cm.gray )
plt.title(label=title)
else:
figure.add_subplot( 4, 1, 1, title="Orig")
plt.imshow( tf.squeeze( input_tensor ).numpy().astype("float32") )
y,cb,cr = tf.split( tf.squeeze(input_tensor), 3, axis=2)
figure.add_subplot( 4, 1, 2, title="Ch1")
plt.imshow( tf.squeeze(rescale_01(y)).numpy().astype("float32"), cmap=cm.gray)
figure.add_subplot( 4, 1, 3, title="Ch2")
plt.imshow(tf.squeeze(rescale_01(cb)).numpy().astype("float32"), cmap=cm.gray)
figure.add_subplot( 4, 1, 4, title="Ch3")
plt.imshow(tf.squeeze(rescale_01(cr)).numpy().astype("float32"), cmap=cm.gray)