forked from jiaqi-jiang/GLOnet_for_thin_film
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLOnet_thinfilm.py
180 lines (144 loc) · 7.15 KB
/
GLOnet_thinfilm.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
import matplotlib.pyplot as plt
import torch
import numpy as np
import math
import torch.nn as nn
import torch.nn.functional as F
from TMM import *
from tqdm import tqdm
from net import Generator, ResGenerator
class GLOnet():
def __init__(self, params):
# GPU
self.cuda = torch.cuda.is_available()
if self.cuda:
self.dtype = torch.cuda.FloatTensor
else:
self.dtype = torch.FloatTensor
# construct
if params.net == 'Res':
self.generator = ResGenerator(params)
else:
self.generator = Generator(params)
if self.cuda:
self.generator.cuda()
self.optimizer = torch.optim.Adam(self.generator.parameters(), lr=params.lr, betas = (params.beta1, params.beta2), weight_decay = params.weight_decay)
self.scheduler = torch.optim.lr_scheduler.StepLR(self.optimizer, step_size = params.step_size, gamma = params.step_size)
# training parameters
self.noise_dim = params.noise_dim
self.numIter = params.numIter
self.batch_size = params.batch_size
self.sigma = params.sigma
self.alpha_sup = params.alpha_sup
self.iter0 = 0
self.alpha = 0.1
# simulation parameters
self.user_define = params.user_define
if params.user_define:
self.n_database = params.n_database
else:
self.materials = params.materials
self.matdatabase = params.matdatabase
self.n_bot = params.n_bot.type(self.dtype) # number of frequencies or 1
self.n_top = params.n_top.type(self.dtype) # number of frequencies or 1
self.k = params.k.type(self.dtype) # number of frequencies
self.theta = params.theta.type(self.dtype) # number of angles
self.pol = params.pol # str of pol
self.target_reflection = params.target_reflection.type(self.dtype)
# 1 x number of frequencies x number of angles x (number of pol or 1)
# tranining history
self.loss_training = []
self.refractive_indices_training = []
self.thicknesses_training = []
def train(self):
self.generator.train()
# training loop
with tqdm(total=self.numIter) as t:
it = self.iter0
while True:
it +=1
# normalized iteration number
normIter = it / self.numIter
# discretizaton coeff.
self.update_alpha(normIter)
# terminate the loop
if it > self.numIter:
return
# sample z
z = self.sample_z(self.batch_size)
# generate a batch of iamges
thicknesses, refractive_indices, _ = self.generator(z, self.alpha)
# calculate efficiencies and gradients using EM solver
reflection = TMM_solver(thicknesses, refractive_indices, self.n_bot, self.n_top, self.k, self.theta, self.pol)
# free optimizer buffer
self.optimizer.zero_grad()
# construct the loss
g_loss = self.global_loss_function(reflection)
# record history
self.record_history(g_loss, thicknesses, refractive_indices)
# train the generator
g_loss.backward()
self.optimizer.step()
self.scheduler.step()
# update progress bar
t.update()
def evaluate(self, num_devices, kvector = None, inc_angles = None, pol = None, grayscale=True):
if kvector is None:
kvector = self.k
if inc_angles is None:
inc_angles = self.theta
if pol is None:
pol = self.pol
self.generator.eval()
z = self.sample_z(num_devices)
thicknesses, refractive_indices, P = self.generator(z, self.alpha)
result_mat = torch.argmax(P, dim=2).detach() # batch size x number of layer
if not grayscale:
if self.user_define:
n_database = self.n_database # do not support dispersion
else:
n_database = self.matdatabase.interp_wv(2 * math.pi/kvector, self.materials, True).unsqueeze(0).unsqueeze(0).type(self.dtype)
one_hot = torch.eye(len(self.materials)).type(self.dtype)
ref_idx = torch.sum(one_hot[result_mat].unsqueeze(-1) * n_database, dim=2)
else:
if self.user_define:
ref_idx = refractive_indices
else:
n_database = self.matdatabase.interp_wv(2 * math.pi/kvector, self.materials, True).unsqueeze(0).unsqueeze(0).type(self.dtype)
ref_idx = torch.sum(P.unsqueeze(-1) * n_database, dim=2)
reflection = TMM_solver(thicknesses, ref_idx, self.n_bot, self.n_top, kvector.type(self.dtype), inc_angles.type(self.dtype), pol)
return (thicknesses, ref_idx, result_mat, reflection)
def _TMM_solver(self, thicknesses, result_mat, kvector = None, inc_angles = None, pol = None):
if kvector is None:
kvector = self.k
if inc_angles is None:
inc_angles = self.theta
if pol is None:
pol = self.pol
n_database = self.matdatabase.interp_wv(2 * math.pi/kvector, self.materials, True).unsqueeze(0).unsqueeze(0).type(self.dtype)
one_hot = torch.eye(len(self.materials)).type(self.dtype)
ref_idx = torch.sum(one_hot[result_mat].unsqueeze(-1) * n_database, dim=2)
reflection = TMM_solver(thicknesses, ref_idx, self.n_bot, self.n_top, kvector.type(self.dtype), inc_angles.type(self.dtype), pol)
return reflection
def update_alpha(self, normIter):
self.alpha = round(normIter/0.05) * self.alpha_sup + 1.
def sample_z(self, batch_size):
return (torch.randn(batch_size, self.noise_dim, requires_grad=True)).type(self.dtype)
def global_loss_function(self, reflection):
return -torch.mean(torch.exp(-torch.mean(torch.pow(reflection - self.target_reflection, 2), dim=(1,2,3))/self.sigma))
def global_loss_function_robust(self, reflection, thicknesses):
metric = torch.mean(torch.pow(reflection - self.target_reflection, 2), dim=(1,2,3))
dmdt = torch.autograd.grad(metric.mean(), thicknesses, create_graph=True)
return -torch.mean(torch.exp((-metric - self.robust_coeff *torch.mean(torch.abs(dmdt[0]), dim=1))/self.sigma))
def record_history(self, loss, thicknesses, refractive_indices):
self.loss_training.append(loss.detach())
self.thicknesses_training.append(thicknesses.mean().detach())
self.refractive_indices_training.append(refractive_indices.mean().detach())
def viz_training(self):
plt.figure(figsize = (20, 5))
plt.subplot(131)
plt.plot(self.loss_training)
plt.ylabel('Loss', fontsize=18)
plt.xlabel('Iterations', fontsize=18)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)