-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel24-s4-mask-from-rechits.py
291 lines (197 loc) · 9.34 KB
/
model24-s4-mask-from-rechits.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
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python
# like model06 but with dropout layer only applied
# to the rechits variables, not the other (track iso)
# variables
import rechitmodelutils
import numpy as np
import math
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
# see http://torch.ch/blog/2016/02/04/resnets.html
# and https://discuss.pytorch.org/t/pytorch-performance/3079
# this seems not to exist in PyTorch ?
# torch.backends.cudnn.fastest = True
torch.backends.cudnn.benchmark = True
# rechits_dim = (7,23)
rechits_dim = (5,5)
add_s4_estimates = True
#----------------------------------------------------------------------
# model
#----------------------------------------------------------------------
# BCE loss with some reshaping
class MyLoss(torch.nn.modules.loss._WeightedLoss):
### def __init__(self, weights_tensor):
### super(MyLoss, self).__init__(weights_tensor)
###
### # expects numerical labels, not one hot encoded labels
### # see also https://discuss.pytorch.org/t/feature-request-nllloss-crossentropyloss-that-accepts-one-hot-target/2724
### # self.loss = nn.CrossEntropyLoss(weights_tensor, size_average = False)
###
### # does not work either
### # self.loss = nn.MultiLabelSoftMarginLoss(weights_tensor, size_average = False)
def forward(self, input, target):
input = input[0]
batch_size = input.size(0)
# does not work
# return torch.nn.functional.binary_cross_entropy(input, target, self.weight, size_average = self.size_average)
torch.nn.modules.loss._assert_no_grad(target)
# calculate the per cell binary cross entropy loss ourselves
# out = target * torch.log(input) + (1 - target) * torch.log(1 - input)
loss = 0
for i in range(batch_size):
loss += self.weight[i] * torch.nn.functional.binary_cross_entropy(input[i], target[i], size_average = False)
if self.size_average:
loss /= self.weight.sum()
return loss
#----------------------------------------------------------------------
from ReLU import ReLU
from Reshape import Reshape
class Model(nn.Module):
#----------------------------------------
def __init__(self, rechits_dim):
super(Model, self).__init__()
input_size = rechits_dim[0] * rechits_dim[1]
hidden_size = 2 * input_size
# network to learn the weights
# depending on the input shape
self.weightsModel = nn.Sequential(
Reshape(-1, input_size),
nn.Linear(in_features = input_size, out_features = hidden_size), ReLU(),
nn.Linear(in_features = hidden_size, out_features = hidden_size), ReLU(),
nn.Linear(in_features = hidden_size, out_features = hidden_size), ReLU(),
nn.Linear(in_features = hidden_size, out_features = hidden_size), ReLU(),
# IMPORTANT: do NOT add a ReLU here after the last layer otherwise
# the sigmoid output will always be >= 0.5 !!
nn.Linear(in_features = hidden_size, out_features = input_size),
# in principle we want outputs in the range 0..1 and
# they should sum to four (not to one !)
#
# but for the moment we use (independent) sigmoids and therefore
# can have arbitrary shapes
#
# (we could also put a constraint that there is a (2x2) window
# but let's try not to impose this)
nn.Sigmoid(),
# in principle we don't need to reshape
# back to the original form because
# in the end we calculate a dot product anyway
Reshape(-1, rechits_dim[0], rechits_dim[1]),
)
#----------------------------------------
def forward(self, x):
# x is a list
xval = x[0]
# np.set_printoptions(threshold=np.nan,
# precision=3,
# linewidth = 1000)
weights = self.weightsModel.forward(xval)
# also calculate the s4 value from the mask
# for testing (note that we do NOT include
# them into the loss)
if add_s4_estimates:
minibatch_size = weights.size(0)
tower = xval[:,0]
# print "tower=",tower.size(),weights.view(minibatch_size, -1).size(), tower.view(minibatch_size, -1).size()
weighted_sum = (weights.view(minibatch_size, -1) * tower.view(minibatch_size, -1)).sum(1)
# we sum over dimensions 1 and 2 (keeping the minibatch dimension)
# we do this in reverse order to avoid shifting the indices
denominator = tower.sum(dim = 2).sum(dim = 1)
s4 = weighted_sum / denominator
return [ weights, s4 ]
else:
return [ weights ]
#----------------------------------------------------------------------
def makeModel():
for dim in rechits_dim:
assert dim % 2 == 1
return Model(rechits_dim)
#----------------------------------------------------------------------
# function to prepare input data samples
#----------------------------------------------------------------------
unpacker = rechitmodelutils.RecHitsUnpacker(
rechits_dim[0], # width
rechits_dim[1], # height
# for shifting 18,18 to 4,12
recHitsXoffset = -18 + rechits_dim[0] / 2 + 1,
recHitsYoffset = -18 + rechits_dim[1] / 2 + 1,
)
#----------------------------------------------------------------------
import torch.utils.data
class MyDataset(torch.utils.data.Dataset):
# see also http://pytorch.org/tutorials/beginner/data_loading_tutorial.html
#----------------------------------------
def __init__(self, dataset):
self.weights = dataset['weights']
if add_s4_estimates:
self.s4 = dataset['phoIdInput/s4']
self.nrows = len(self.weights)
# unpack rechits here
self.recHits = unpacker.unpack(dataset, range(self.nrows))
# find the four (adjacent) cells
# giving S4
conv_weights = np.ones((2,2))
# find the coordinate of the center rechit
rechits_mid = np.array([rechits_dim[0] / 2, rechits_dim[1] / 2])
towers = self.recHits[:,0,
(rechits_mid[0]-2):(rechits_mid[0]+3),
(rechits_mid[1]-2):(rechits_mid[1]+3),
]
self.masks = np.zeros((len(towers), rechits_dim[0], rechits_dim[1]), dtype = 'float32')
# we need to shift back from (5x5) maximum to the 7x23 window
# center of 5x5
shift = rechits_mid - np.array([2,2])
# find the windows with the maximum 2x2 sum
import scipy.signal
nrows = len(towers)
for i in range(nrows):
conv = scipy.signal.convolve2d(towers[i], conv_weights, mode = 'valid')
# the maximum coordinate is the top left of the window
# note that in some cases the window is not unique
# (e.g. a single value surrounded by zeros)
maxpos = np.unravel_index(conv.argmax(), conv.shape)
# maxpos comes from the 5x5 tower, we need to shift it
# back to the 7x23 image
maxpos = np.array(maxpos) + shift
self.masks[i,
maxpos[0]:(maxpos[0]+2),
maxpos[1]:(maxpos[1]+2)] = 1
# DEBUG
if i < 0:
np.set_printoptions(linewidth = 300, suppress = True, precision = 2)
print "tower="; print towers[i]
print "mask="; print self.masks[i]
print "s4 cacluated=", np.dot(recHits[i].ravel(), masks[i].ravel()) / towers[i].sum()
if add_s4_estimates:
# also calculate our S4 based on these masks
numerator = (towers.reshape(nrows, -1) * self.masks.reshape(nrows, -1)).sum(axis = 1)
denominator = towers.reshape(nrows, -1).sum(axis = 1)
self.s4recalculated = numerator / denominator
#----------------------------------------
def __len__(self):
return self.nrows
#----------------------------------------
def __getitem__(self, index):
result = [ self.weights[index],
self.masks[index], # target to learn
[ self.recHits[index] ] # inputs
]
if add_s4_estimates:
result.append([ self.s4[index], self.s4recalculated[index] ])
return result
#----------------------------------------------------------------------
def makeDataSet(dataset):
# note that this is defined in the model file because
# the dataset we make out of the input files
# is model dependent
return MyDataset(dataset)
additionalVars = ['phoIdInput/s4']
normalizeAdditionalVars = False
#----------------------------------------------------------------------
def makeLoss(numOutputNodes, weightsTensor, trainWeights, testWeights):
trainWeights = trainWeights.reshape((-1,1))
testWeights = testWeights.reshape((-1,1))
# for the moment consider the absolute MSE loss
# later we could consider taking the MSE loss
# of the ratio output / target value
return MyLoss(weightsTensor), trainWeights, testWeights