-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathegcCrop.py
290 lines (248 loc) · 8.68 KB
/
egcCrop.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
# egcCrop.py
# Author: Alejandro Sanchez
# Created: Jul 3, 2018
#
# Testing classifier with variable smaller crop sizes
import numpy as np
import h5py
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset, ConcatDataset, DataLoader
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt
electron_input_file = "SingleElectronFlatPt10To160_2016_25ns_Moriond17MC_PoissonOOTPU_IMG_RH1_n225k.hdf5"
photon_input_file = "SinglePhotonFlatPt10To160_2016_25ns_Moriond17MC_PoissonOOTPU_IMG_RH1_n225k.hdf5"
validation_fraction = 0.15
num_epochs = 20
use_gpu = True
crop_size = 16
### Prepare Data ###
print("Fetching data...")
assert crop_size > 0 and crop_size <= 32
class myData(Dataset):
'''
custom dataset class that splits input file into training and validation
sets.
'''
def __init__(self, data_file, val_cut):
f = h5py.File(data_file, 'r')
self.inputs = torch.tensor(f['X_crop0'][:]).view(-1, 1, 32, 32)
#answers = torch.tensor(f['y'][:], dtype=torch.int).view(-1)
#self.targets = torch.zeros(len(answers), 2)
#for i, entry in enumerate(self.targets):
# entry[answers[i]] = 1.0
self.targets = torch.tensor(f['y'][:], dtype=torch.float).view(-1)
self.pt_ = torch.tensor(f['pho_pT0'][:]).view(-1)
self.trainset = [self.inputs[self.valCut(val_cut):],
self.targets[self.valCut(val_cut):],
self.pt_[self.valCut(val_cut):]]
self.valset = [self.inputs[:self.valCut(val_cut)],
self.targets[:self.valCut(val_cut)],
self.pt_[:self.valCut(val_cut)]]
self.use_train_set = True
self.cropped = False
def __len__(self):
if self.use_train_set:
return len(self.trainset[0])
else:
return len(self.valset[0])
def __getitem__(self, index):
if self.use_train_set:
return self.trainset[0][index], self.trainset[1][index],\
self.trainset[2][index]
else:
return self.valset[0][index], self.valset[1][index],\
self.valset[2][index]
def valCut(self, fraction):
return int(len(self.inputs) * fraction)
def useTrainSet(self, use_train_set_):
'''
if set to true(default), uses training set. if false, uses validation
set.
'''
self.use_train_set = use_train_set_
def crop(self, newSize):
assert newSize > 0 and newSize <= 32
if self.cropped:
print("warning: dataset already cropped. skipping crop()")
return
print("cropping images to {}x{}".format(newSize, newSize))
oldImgs = self.inputs
self.inputs = torch.empty(len(oldImgs), 1, newSize, newSize)
for i, img in enumerate(oldImgs):
if newSize % 2 == 0:
self.inputs[i] = img[:, (16 - newSize//2):(16 + newSize//2),
(16 - newSize//2):(16 + newSize//2)]
else:
self.inputs[i] = img[:, (16 - newSize//2 - 1):(16 + newSize//2),
(16 - newSize//2 - 1):(16 + newSize//2)]
self.cropped = True
return
ele_data = myData(electron_input_file, validation_fraction)
pho_data = myData(photon_input_file, validation_fraction)
ele_data.crop(crop_size)
pho_data.crop(crop_size)
ele_data.useTrainSet(True)
pho_data.useTrainSet(True)
train_data = ConcatDataset((ele_data, pho_data))
trainloader = DataLoader(train_data, batch_size=500, shuffle=True,
num_workers=10, pin_memory=True)
ele_data.useTrainSet(False)
pho_data.useTrainSet(False)
val_data = ConcatDataset((ele_data, pho_data))
valloader = DataLoader(val_data, batch_size=500, shuffle=False, num_workers=10,
pin_memory=True)
### Prepare Classifier ###
print("Initializing Classifier...")
class Flatten(nn.Module):
def forward(self, x):
return x.view(x.size()[0], -1)
class Item(nn.Module):
def forward(self, x):
return x.view(-1)
net = nn.Sequential(
nn.Conv2d( 1, 16, 3),
nn.ReLU(),
nn.Conv2d(16, 16, 3),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(16, 32, 3),
nn.ReLU(),
nn.Conv2d(32, 32, 3),
nn.ReLU(),
nn.MaxPool2d(2),
Flatten(),
nn.Linear(800, 256),
nn.ReLU(),
nn.Linear(256, 128),
nn.ReLU(),
#nn.Linear(128, 2)
nn.Linear(128, 1),
Item()
)
### Train Classifier ###
net.cuda()
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(net.parameters())
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, patience=2,
verbose=True, min_lr=1e-6,
threshold=1e-3, factor=0.2)
net.eval()
ele_data.useTrainSet(False)
pho_data.useTrainSet(False)
loss = 0.
pred, value = np.array([]), np.array([])
with torch.no_grad():
for data in valloader:
inputs, labels, pt = data
inputs, labels = inputs.cuda(), labels.cuda()
outputs = net(inputs)
loss += criterion(outputs, labels).item()
outputs = F.sigmoid(outputs)
#value = np.concatenate((value, labels[:,1].cpu().numpy()))
#pred = np.concatenate((pred, outputs[:,1].cpu().numpy()))
value = np.concatenate((value, labels.cpu().numpy()))
pred = np.concatenate((pred, outputs.cpu().numpy()))
loss /= len(valloader)
correct, total = 0, 0
correct = (np.around(pred) == value).sum()
total = len(value)
acc = correct/total
auc = roc_auc_score(value, pred)
print("Epoch: %2d" % (0))
print("loss: %.5f" % (loss))
print("acc: %.5f" % (acc))
print("auc: %.5f" % (auc))
print(pred[:6])
print(pred[-6:])
print()
ptdist = np.array([])
for epoch in range(num_epochs):
net.train()
ele_data.useTrainSet(True)
pho_data.useTrainSet(True)
for data in trainloader:
inputs, labels, pt = data
inputs, labels = inputs.cuda(), labels.cuda()
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
scheduler.step(loss)
net.eval()
ele_data.useTrainSet(False)
pho_data.useTrainSet(False)
loss = 0.
pred, value = np.array([]), np.array([])
with torch.no_grad():
for data in valloader:
inputs, labels, pt = data
inputs, labels = inputs.cuda(), labels.cuda()
outputs = net(inputs)
loss += criterion(outputs, labels).item()
outputs = F.sigmoid(outputs)
#value = np.concatenate((value, labels[:,1].cpu().numpy()))
#pred = np.concatenate((pred, outputs[:,1].cpu().numpy()))
value = np.concatenate((value, labels.cpu().numpy()))
pred = np.concatenate((pred, outputs.cpu().numpy()))
if epoch + 1 == num_epochs:
ptdist = np.concatenate((ptdist, pt.numpy()))
loss /= len(valloader)
correct, total = 0, 0
correct = (np.around(pred) == value).sum()
total = len(value)
acc = correct/total
auc = roc_auc_score(value, pred)
print("Epoch: %2d" % (epoch + 1))
print("loss: %.5f" % (loss))
print("acc: %.5f" % (acc))
print("auc: %.5f" % (auc))
print(pred[:6])
print(pred[-6:])
print()
### Create Plots ###
print("Generating Plots (Performing convoluted for loops)")
ele_pt = np.array([])
ele_pred = np.array([])
pho_pt = np.array([])
pho_pred = np.array([])
for i, label in enumerate(value):
if label == 0:
ele_pt = np.append(ele_pt, ptdist[i])
ele_pred = np.append(ele_pred, pred[i])
elif label == 1:
pho_pt = np.append(pho_pt, ptdist[i])
pho_pred = np.append(pho_pred, pred[i])
auc_bin_size = 10
auc_bins = np.arange(20,160,auc_bin_size)
aucs = np.array([])
for interval in auc_bins:
int_true = np.array([])
int_pred = np.array([])
for i, pt_ in enumerate(ptdist):
if pt_ > interval and pt_ < interval + auc_bin_size:
int_true = np.append(int_true, value[i])
int_pred = np.append(int_pred, pred[i])
aucs = np.append(aucs, roc_auc_score(int_true, int_pred))
print("Plotting")
plt.figure(figsize=(10,10))
plt.subplot(221)
plt.hist2d(ele_pt, ele_pred, bins=20, range=((20,160),(0,1)))
plt.xlabel("pt")
plt.ylabel("classifier output")
plt.title("Electrons")
plt.subplot(222)
plt.hist2d(pho_pt, pho_pred, bins=20, range=((20,160),(0,1)))
plt.xlabel("pt")
plt.ylabel("classifier output")
plt.title("Photons")
plt.subplot(223)
plt.plot(auc_bins + 5, aucs)
plt.xlabel("pt")
plt.ylabel("roc auc")
plt.title("roc auc over pt (10 GeV intervals)")
plt.tight_layout()
plt.show()