-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_generalization_experiments.py
170 lines (116 loc) · 5.33 KB
/
run_generalization_experiments.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
import os
import time
import argparse
import tensorflow as tf
import numpy as np
import skimage
import matplotlib.pyplot as plt
import cv2
from utils import *
from callbacks import *
from dataio import prepare_dataset
from models import create_model
import hyperparameters
import warnings
warnings.filterwarnings("ignore")
#os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image_path",
default="cameraman",
type=str,
help="path of input image")
ap.add_argument("-bh", "--base_nheads",
default=256,
type=int,
help="root number of heads for base multi-head network(for fair comparison)")
ap.add_argument("-ba", "--base_alpha",
default=32,
type=int,
help="alpha parameter for base multi-head network(for fair comparison)")
ap.add_argument("-ub", "--use_bias_sparse",
default=False,
type=bool,
help="use bias for the head part of the multi-head network")
args = vars(ap.parse_args())
image_path = args["image_path"]
Base_N_Heads = args["base_nheads"] ** 2
Base_alpha = args["base_alpha"]
use_bias_sparse = args["use_bias_sparse"]
N_Heads = [1**2, 2**2, 4**2, 8**2, 16**2, 32**2, 64**2, 128**2, 256**2]
if image_path == "cameraman":
Major_Image = skimage.data.camera()
Major_Image = Major_Image.astype(np.float32) / 255.
Minor_Image = cv2.resize(Major_Image, (256, 256))
else:
Major_Image = cv2.imread(image_path)
Major_Image = Major_Image.astype(np.float32) / 255.
if len(Major_Image.shape) == 3:
Major_Image = cv2.cvtColor(Major_Image, cv2.COLOR_BGR2GRAY)
Major_Image = cv2.resize(Major_Image, (512, 512))
Minor_Image = cv2.resize(Major_Image, (256, 256))
Base_Model_N_Parameters = calculate_parameters(hyperparameters.INPUT_COORDINATES,
hyperparameters.BASE_HIDDEN_UNITS,
hyperparameters.BASE_N_HLAYERS,
Base_N_Heads,
Base_alpha,
use_bias_sparse)
Save_Result_Callbacks_Minor = []
Save_Result_Callbacks_Major = []
Head_Historys = []
for counter, n_heads in enumerate(N_Heads):
print(30*"*", f"Head : {n_heads}", 30*"*")
learningrate_scheduler = LearningRateScheduler()
dataset = prepare_dataset(Minor_Image, n_heads)
hidden_units, alpha_sparse = calculate_hyperparameter(Base_Model_N_Parameters,
hyperparameters.INPUT_COORDINATES,
hyperparameters.BASE_HIDDEN_UNITS,
hyperparameters.BASE_N_HLAYERS,
n_heads,
Base_alpha,
use_bias_sparse)
MH_RELU_INR = create_model("multi-head",
n_heads,
hidden_units,
alpha_sparse,
use_bias_sparse)
print("Number of Parameters : ", MH_RELU_INR.count_params())
optimizer = tf.keras.optimizers.Adam(hyperparameters.LEARNING_RATE)
loss_fn = tf.keras.losses.MeanSquaredError()
MH_RELU_INR.compile(optimizer=optimizer, loss=loss_fn)
#print(MH_RELU_INR.summary())
save_result_callback_minor = CustomSaver(Minor_Image, n_heads, hyperparameters.STEP_SHOW, loss_fn)
save_result_callback_major = CustomSaver(Major_Image, n_heads, hyperparameters.STEP_SHOW, loss_fn)
start = time.time()
history = MH_RELU_INR.fit(dataset,
epochs=hyperparameters.EPOCHS,
callbacks=[learningrate_scheduler, save_result_callback_minor, save_result_callback_major],
verbose=0) # if you want to see train progress change verbose to 1
print(f"Total Time : {time.time() - start}")
Save_Result_Callbacks_Minor.append(save_result_callback_minor)
Save_Result_Callbacks_Major.append(save_result_callback_major)
psnr_minor = []
psnr_major = []
for counter in range(len(Save_Result_Callbacks_Minor)):
psnr_minor.append(Save_Result_Callbacks_Minor[counter].psnr)
psnr_major.append(Save_Result_Callbacks_Major[counter].psnr)
psnr_minor = np.array(psnr_minor)
psnr_major = np.array(psnr_major)
##################################################### Plot Result ####################################################
plt.plot(psnr_minor.T[-1,:], color='#11e3e3', marker='o')
plt.plot(psnr_major.T[-1,:], color='#ff8aac', marker='o')
plt.grid()
plt.minorticks_on()
plt.tick_params(axis='both', which='major', labelsize=11)
plt.tick_params(axis='both', which='minor', labelsize=8)
plt.xlim([-0.1, np.log2(np.sqrt(N_Heads[-1])) + 0.1])
plt.ylabel("PSNR(dB)", fontsize=10, fontweight="bold")
plt.xlabel(r'$\mathbf{log_{2}{(\sqrt[2]{Number\:of\:Heads})}}$', fontsize=10)
max_major = max(psnr_major.T[-1,:])
index_max_major = np.argmax(psnr_major.T[-1,:])
plt.scatter(index_max_major, max_major, s=100, facecolors='none', edgecolors='r')
plt.text(index_max_major - 0.5, max_major + 7, 'Best Generalization', style='italic', fontsize=8, fontweight="bold")
plt.legend(["Train", "Test"], fontsize=12)
plt.savefig("results/Generalization_log.pdf", dpi=500, bbox_inches='tight', pad_inches=0)
plt.show()
######################################################################################################################