-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCNN.py
More file actions
295 lines (247 loc) · 8.55 KB
/
CNN.py
File metadata and controls
295 lines (247 loc) · 8.55 KB
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
####################################
# This code is for pos rec analysis
# in PandaX-4T, using CNN as the architecture
# for training.
#####################################
import torch
import numpy as np
import json, time, sys
import pandas as pd
import glob
import pickle as pkl
#########################
# seeding
#########################
SEED = int(time.time()/1000) + int(time.time())%1000
_ = np.random.seed(SEED)
_ = torch.manual_seed(SEED)
#########################
# Hard-coded
#########################
WorkDir = '/home/ustc/WorkSpace/PandaX-4T/PosRecNN'
LogDir = WorkDir+'/logs'
WeightDir = WorkDir+'/weights'
FigureDir = WorkDir+'/figs'
OutputDir = WorkDir+'/outputs'
TrainData = [
'/home/ustc/WorkSpace/PandaX-4T/PosRecNN/samples2/train_v2_smeared_job1_image.pkl',
'/home/ustc/WorkSpace/PandaX-4T/PosRecNN/samples2/train_v2_smeared_job2_image.pkl', # for debug use, temporarily disabled
]
# TrainData = WorkDir+'/samples/tmp_train_mixed.json' # just for debugging phase
TestData = [
'/home/ustc/WorkSpace/PandaX-4T/PosRecNN/samples2/test_v2_smeared_image.pkl',
]
TotalPMTNum = 169 # +199
TotalPixelNum = 128 #image pixel, padding the ones more than 169
# from Huangdi @ 2020-09-11
# Note it has be converted to MC ids
# commented is the database pmt ids started from 1
# InhibitedPMTs = [5,6,176,252,302,348,353,365]
InhibitedPMTs = [164, 163, 253, 290, 212, 286, 329, 343]
######################
## Input
######################
if len(sys.argv)<2:
Message = 'python3 CNN.py <mode (train or validate or test or inference)> <weight file> <number of iterations> <file head (test filename if inference mode)> < (only for validate mode) skip number of weight files> <(only for validate mode) Iter range lower> <(only for validate mode) Iter range upper> <(only for inference) inference filename> <(only for inference> inference output filename>\n'
Message += 'Note <weight file> does not function if the mode is validate.'
print(Message)
exit()
Mode = sys.argv[1]
WeightFilename = None
if len(sys.argv)>2:
WeightFilename = sys.argv[2]
NumIter = 1000
if len(sys.argv)>3:
NumIter = int(sys.argv[3])
FileHead = 'baseline'
if len(sys.argv)>4:
FileHead = sys.argv[4]
SkipNum = 1
if len(sys.argv)>5:
SkipNum = int(sys.argv[5])
IterRangeLower = 0
IterRangeUpper = 1000000000000
if len(sys.argv)>6:
IterRangeLower = int(sys.argv[6])
IterRangeUpper = int(sys.argv[7])
InferenceFilename = None
if len(sys.argv)>8:
InferenceFilename = sys.argv[8]
InferenceOutputFilename = None
if len(sys.argv)>9:
InferenceOutputFilename = sys.argv[9]
# check
if Mode not in ['train', 'test', 'validate', 'inference']:
raise ValueError("Mode must be either train, validate or test or inference!")
if WeightFilename=='None' or WeightFilename=='none':
WeightFilename = None
if Mode=='inference':
TestData = [InferenceFilename]
########################
# Load json file
#######################
from pattern_plot import ChToMCPMTIDs, ToDataCoord, ToMCCoord, ChToDataPMTIDs
from Libs_cnn import FormDataset_Data, FormDataset_MC
dataset = []
DataSets = TrainData
if Mode!='train':
DataSets = TestData
for Filename in DataSets:
dataset_array = pkl.load(open(Filename,'rb'))
NumEvents = len(dataset_array)
print("===>>> File: "+Filename+" Loaded! Total event number = "+str(NumEvents))
if Mode=='validate':
# hardcode
# if validate, just use the first 1000 events
NumEvents = 1000
dataset.extend(dataset_array[:NumEvents])
print("===>>> Number of events = "+str(dataset.__len__()))
print("===>>> Each image shape = "+str(dataset.__getitem__(1)[0].shape))
data_loader = None
from SparseDataLoader import sparse_collate
if Mode=='train':
data_loader = torch.utils.data.DataLoader(
dataset,
batch_size=128,
shuffle=True,
num_workers=1,
pin_memory=True,
collate_fn=sparse_collate
)
else:
data_loader = torch.utils.data.DataLoader(
dataset,
batch_size=32,
shuffle=False,
num_workers=1,
pin_memory=True,
collate_fn=sparse_collate
)
##################
# blob
##################
from Libs_cnn import CNN
class BLOB:
pass
blob = BLOB()
blob.net = CNN().float().cuda() # construct Lenet, use GPU
blob.criterion = torch.nn.MSELoss() # L2 loss
blob.optimizer = torch.optim.Adam(blob.net.parameters()) # use Adam optimizer algorithm
blob.softmax = torch.nn.Softmax(dim=1) # not for training, but softmax score for each class
blob.iteration = 0 # integer count for the number of train steps
blob.data = None # data for training/analysis
blob.label = None # label for training/analysis
#############################
## Main
#############################
from Libs_cnn import restore_state
# first restore weights
if WeightFilename is not None:
restore_state(blob, WeightFilename)
StartNumIter = blob.iteration
EndNumIter = blob.iteration + NumIter
###############################
# train
# not tested
###############################
from Libs_cnn import train_loop
if Mode=='train':
train_loss = train_loop(blob, data_loader, EndNumIter, WeightDir, FileHead)
# save to log
LogFilename = LogDir+'/'+FileHead+'-log-'+str(StartNumIter)+"-"+str(EndNumIter)+'.csv'
Dict = {}
Dict['iter'] = np.linspace(StartNumIter, EndNumIter-1, NumIter)
Dict['loss'] = train_loss
df = pd.DataFrame(Dict)
df.to_csv(LogFilename)
exit()
###############################
# Validate
# not tested
###############################
import glob
from Libs_cnn import test_loop
if Mode=='validate':
# Get the all the weight files with Filehead
WeightFilenames = glob.glob(WeightDir+'/'+FileHead+'*')
# Obtain the iteration number
IterNum = []
for weight_filename in WeightFilenames:
IterNum.append(eval(
weight_filename.split(WeightDir+'/'+FileHead+'-')[-1].split('.')[0]
))
# convert to numpy array
WeightFilenames = np.asarray(WeightFilenames)
IterNum = np.asarray(IterNum)
# Sort
inds = np.argsort(IterNum)
WeightFilenames = WeightFilenames[inds]
IterNum = IterNum[inds]
# cut off
inds1 = np.where(IterNum>IterRangeLower)[0]
inds2 = np.where(IterNum<IterRangeUpper)[0]
inds = np.intersect1d(inds1, inds2)
WeightFilenames = WeightFilenames[inds]
IterNum = IterNum[inds]
# Skip weight files
if SkipNum>1:
WeightFilenames = WeightFilenames[::SkipNum]
IterNum = IterNum[::SkipNum]
# Loop over
Losses = []
for weight_filename in WeightFilenames:
# restore the state
restore_state(blob, weight_filename)
# get the prediction
res = test_loop(blob, data_loader)
loss = np.average(res['loss'])
Losses.append(loss)
# print out
print("===>>> File: "+weight_filename+' processed!\n')
# save to pandas
import os
OutputValidationFilename = LogDir+'/'+FileHead+'-'+'validate.csv'
if not os.path.isfile(OutputValidationFilename):
OutDict = {}
OutDict['iter'] = IterNum
OutDict['loss'] = Losses
df = pd.DataFrame(OutDict)
df.to_csv(OutputValidationFilename)
else:
df = pd.read_csv(OutputValidationFilename)
IterNum = IterNum.tolist()
IterNum.extend(df.iter.tolist())
Losses.extend(df.loss.tolist())
# re-sort
IterNum = np.asarray(IterNum)
Losses = np.asarray(Losses)
inds = np.argsort(IterNum)
IterNum = IterNum[inds]
Losses = Losses[inds]
# then save
OutDict = {}
OutDict['iter'] = IterNum
OutDict['loss'] = Losses
df = pd.DataFrame(OutDict)
df.to_csv(OutputValidationFilename)
exit()
###############################
# test & inference
# not tested
###############################
res = test_loop(blob, data_loader)
prediction = res['prediction']
label = res['label']
from pattern_plot import ToDataPMTIDs
import pickle as pkl
# Output
OutputFilename = OutputDir+'/'+FileHead+'-'+str('%d' % blob.iteration)+'-output.pkl'
if Mode=='inference':
OutputFilename = InferenceOutputFilename
OutDict = {}
OutDict['prediction'] = prediction
OutDict['label'] = label
pkl.dump(
OutDict,
open(OutputFilename,'wb')
)