-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainCaloX_IDEA_CNN_3D_1C.py
243 lines (198 loc) · 9.47 KB
/
trainCaloX_IDEA_CNN_3D_1C.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
import pandas as pd
import numpy as np
import awkward
import itertools
import copy
import logging
logging.basicConfig(level=logging.INFO, format='[%(asctime)s] %(levelname)s: %(message)s')
batch_size = 28
batch_sizeV = 6
nfiles = 27000
#nfiles = 110
epochs = 100
def generator(batch_size,nfiles):
samples_per_file = 30
number_of_batches = int(samples_per_file/batch_size)
counter=0
fcnt = 101
df = pd.read_pickle('/lustre/research/hep/jdamgov/idea_ntpl_v1/IDEA_pi_pkl3_3D_3cm_50ps_NoProp/GNN.pi150GeV_100.pkl.gz')
df["Label"]=df["Label"]/1000.
nhits = len(df["Points"][0])
samples_per_file = len(df)
number_of_batches = int(samples_per_file/batch_size)
while 1:
try:
np1 = np.array(df["Features"].to_list(),dtype="int16")[batch_size*counter:batch_size*(counter+1),:,:1].sum(axis=-1)
np2 = np.array(df["Features"].to_list(),dtype="int16")[batch_size*counter:batch_size*(counter+1),:,1:].sum(axis=-1)
np3=np.concatenate([np1.reshape(batch_size,nhits,1),
np2.reshape(batch_size,nhits,1)],axis=2)
X_batch = {'points':np.array(df["Points"][batch_size*counter:batch_size*(counter+1)].to_list(),dtype="float32"),
#'features':np.array(df["Features"][batch_size*counter:batch_size*(counter+1)].to_list(),dtype="int16"),
'features':np2,
'mask':np.array(df["Mask"][batch_size*counter:batch_size*(counter+1)].to_list(),dtype="int16").reshape(batch_size,nhits,1)}
y_batch=np.array(df["Label"][batch_size*counter:batch_size*(counter+1)].to_list(),dtype="float32")
counter += 1
points = X_batch['points']
energies = X_batch['features']
X_b = []
for evt in range(0,len(points)):
point=points[evt]
energy=energies[evt]
var = np.zeros((200,35,35),dtype=np.float32)
for ihit in range(0,len(point)):
hitP=point[ihit]
hitE=energy[ihit]
X=int(hitP[0]+49)
Y=int(hitP[1]+15)
Z=int(hitP[2]+10)
if X>-1 and Y>-1 and Z>-1 and Z<35 and Y<35 and X<200: var[X][Y][Z]+=hitE
#else:
# if(y_batch[evt]>8.): print("--- ",y_batch[evt], X,Y,Z,hitE)
#R = np.sum(var)/np.sum(energies[evt])
#if(y_batch[evt]>8.): print(y_batch[evt],R)
#print(var.shape)
X_b.append(var)
X_b = np.asarray(X_b).reshape(len(points), 200, 35, 35, 1)
#print(X_b.shape)
#print(y_batch.shape)
#yield X_batch, y_batch
X_b_sum = X_b.sum(1).sum(1).sum(1)[:,0]
yield [X_b,X_b_sum], y_batch
except:
print("Something went wrong with the data")
counter += 1
#restart counter to yeild data in the next epoch as well
if fcnt > nfiles :
fcnt = 100
if counter >= number_of_batches:
counter = 0
df = pd.read_pickle('/lustre/research/hep/jdamgov/idea_ntpl_v1/IDEA_pi_pkl3_3D_3cm_50ps_NoProp/GNN.pi150GeV_'+str(fcnt)+'.pkl.gz')
df["Label"]=df["Label"]/1000.
samples_per_file = len(df)
number_of_batches = int(samples_per_file/batch_size)
fcnt += 1
def val_generator(batch_size,nfiles):
counter=0
fcnt = nfiles +1
df = pd.read_pickle('/lustre/research/hep/jdamgov/idea_ntpl_v1/IDEA_pi_pkl3_3D_3cm_50ps_NoProp/GNN.pi150GeV_'+str(fcnt)+'.pkl.gz')
nhits = len(df["Points"][0])
samples_per_file = len(df)
number_of_batches = samples_per_file/batch_size
df["Label"]=df["Label"]/1000.
while 1:
try:
np1 = np.array(df["Features"].to_list(),dtype="int16")[batch_size*counter:batch_size*(counter+1),:,:1].sum(axis=-1)
np2 = np.array(df["Features"].to_list(),dtype="int16")[batch_size*counter:batch_size*(counter+1),:,1:].sum(axis=-1)
np3=np.concatenate([np1.reshape(batch_size,nhits,1),
np2.reshape(batch_size,nhits,1)],axis=2)
X_batch = {'points':np.array(df["Points"][batch_size*counter:batch_size*(counter+1)].to_list(),dtype="float32"),
'features':np2,
#'features':np.array(df["Features"][batch_size*counter:batch_size*(counter+1)].to_list(),dtype="int16"),
'mask':np.array(df["Mask"][batch_size*counter:batch_size*(counter+1)].to_list(),dtype="int16").reshape(batch_size,nhits,1)}
y_batch=np.array(df["Label"][batch_size*counter:batch_size*(counter+1)].to_list(),dtype="float32")
counter += 1
points = X_batch['points']
energies = X_batch['features']
X_b = []
for evt in range(0,len(points)):
point=points[evt]
energy=energies[evt]
var = np.zeros((200,35,35),dtype=np.float32)
for ihit in range(0,len(point)):
hitP=point[ihit]
hitE=energy[ihit]
X=int(hitP[0]+49)
Y=int(hitP[1]+15)
Z=int(hitP[2]+10)
if X>-1 and Y>-1 and Z>-1 and Z<35 and Y<35 and X<200: var[X][Y][Z]+=hitE
#else:
# if(y_batch[evt]>8.): print("--- ",y_batch[evt], X,Y,Z,hitE)
#R = np.sum(var)/np.sum(energies[evt])
#if(y_batch[evt]>8.): print(y_batch[evt],R)
#print(var.shape)
X_b.append(var)
X_b = np.asarray(X_b).reshape(len(points), 200, 35, 35, 1)
#print(X_b.shape)
#print(y_batch.shape)
X_b_sum = X_b.sum(1).sum(1).sum(1)[:,0]
yield [X_b,X_b_sum], y_batch
except:
print("Something went wrong with the data")
counter += 1
#restart counter to yeild data in the next epoch as well
if fcnt > nfiles+3000 :
fcnt = nfiles +1
if counter >= number_of_batches:
counter = 0
df = pd.read_pickle('/lustre/research/hep/jdamgov/idea_ntpl_v1/IDEA_pi_pkl3_3D_3cm_50ps_NoProp/GNN.pi150GeV_'+str(fcnt)+'.pkl.gz')
df["Label"]=df["Label"]/1000.
samples_per_file = len(df)
number_of_batches = int(samples_per_file/batch_size)
fcnt += 1
import tensorflow as tf
from tensorflow import keras
#from tf_keras_model import get_particle_net, get_particle_net_lite
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.experimental.list_logical_devices('GPU')
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
print(e)
#strategy = tf.distribute.MirroredStrategy()
strategy = tf.distribute.MirroredStrategy(cross_device_ops=tf.distribute.HierarchicalCopyAllReduce())
with strategy.scope():
input_layer1 = tf.keras.layers.Input((200, 35, 35, 1))
input_layer2 = tf.keras.layers.Input((1))
conv_layer1 = tf.keras.layers.Conv3D(64, kernel_size=(7,3,3) , activation='relu')(input_layer1)
conv_layer2 = tf.keras.layers.Conv3D(32, kernel_size=(5,3,3) , activation='relu')(conv_layer1)
pooling_layer1 = tf.keras.layers.MaxPool3D()(conv_layer2)
conv_layer3 = tf.keras.layers.Conv3D(32, kernel_size=(5,3,3) , activation='relu')(pooling_layer1)
conv_layer4 = tf.keras.layers.Conv3D(32, kernel_size=(5,3,3) , activation='relu')(conv_layer3)
bnorm_layer2 = tf.keras.layers.BatchNormalization()(conv_layer4)
pooling_layer2 = tf.keras.layers.MaxPool3D()(bnorm_layer2)
conv_layer5 = tf.keras.layers.Conv3D(32, 3 , activation='relu')(pooling_layer2)
conv_layer6 = tf.keras.layers.Conv3D(32, 3 , activation='relu')(conv_layer5)
conv_layer7 = tf.keras.layers.Conv3D(8, kernel_size=(5,1,1) , activation='relu')(conv_layer6)
#pooling_layer3 = tf.keras.layers.MaxPool3D()(conv_layer6)
#flatn_layer = tf.keras.layers.Flatten()(pooling_layer3)
flatn_layer = tf.keras.layers.Flatten()(conv_layer7)
conc_layer = tf.keras.layers.Concatenate(axis=1)([flatn_layer,input_layer2])
#dense_layer1 = tf.keras.layers.Dense(units=1024, activation='relu')(conc_layer)
dense_layer2 = tf.keras.layers.Dense(units=768, activation='relu')(conc_layer)
dense_layer2 = tf.keras.layers.Dropout(0.3)(dense_layer2)
dense_layer3 = tf.keras.layers.Dense(units=128, activation='relu')(dense_layer2)
dense_layer4 = tf.keras.layers.Dense(units=32, activation='relu')(dense_layer3)
dense_layer4 = tf.keras.layers.Dropout(0.2)(dense_layer4)
output_layer = tf.keras.layers.Dense(units=1, activation='linear')(dense_layer4)
model = tf.keras.Model(inputs=[input_layer1,input_layer2], outputs=output_layer)
model.compile(loss='mean_squared_logarithmic_error',
optimizer=keras.optimizers.Adam() )
model.summary()
# Prepare model model saving directory.
import os
save_dir = 'model_checkpoints'
model_name = 'CNN_3D_3cm_50ps_3D_noProp_1C_more0ph.loss_{val_loss:01.6f}.e{epoch:03d}.h5'
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
filepath = os.path.join(save_dir, model_name)
# Prepare callbacks for model saving and for learning rate adjustment.
checkpoint = keras.callbacks.ModelCheckpoint(filepath=filepath,
monitor='val_loss',
verbose=1,
save_best_only=True)
progress_bar = keras.callbacks.ProgbarLogger()
early = keras.callbacks.EarlyStopping(monitor="val_loss",
mode="min", patience=12)
callbacks = [checkpoint,early]
model.fit(
generator(batch_size,nfiles),
steps_per_epoch = (nfiles-100)*(30/batch_size),
epochs=epochs,
validation_data=val_generator(batch_sizeV,nfiles),
validation_steps=3000*int(30/batch_sizeV),
callbacks=callbacks
,use_multiprocessing=True, workers=4, max_queue_size=60
)