1
+ from __future__ import print_function
1
2
import os ,sys
2
3
import shutil
3
4
import time
@@ -51,22 +52,22 @@ def main():
51
52
model = resnet_example .resnet14 (pretrained = False , num_classes = 5 , input_channels = 1 )
52
53
model .cuda ()
53
54
54
- print "Loaded model: " ,model
55
+ print ( "Loaded model: " ,model )
55
56
56
57
57
58
# define loss function (criterion) and optimizer
58
59
criterion = nn .CrossEntropyLoss ().cuda ()
59
60
60
61
# training parameters
61
- lr = 1.0e-3
62
+ lr = 1.0e-4
62
63
momentum = 0.9
63
64
weight_decay = 1.0e-3
64
- batchsize = 50
65
- batchsize_valid = 500
65
+ batchsize = 20
66
+ batchsize_valid = 20
66
67
start_epoch = 0
67
68
epochs = 1500
68
- nbatches_per_epoch = 10000 / batchsize
69
- nbatches_per_valid = 1000 / batchsize_valid
69
+ nbatches_per_epoch = int ( 100 / batchsize )
70
+ nbatches_per_valid = int ( 100 / batchsize_valid )
70
71
71
72
optimizer = torch .optim .SGD (model .parameters (), lr ,
72
73
momentum = momentum ,
@@ -75,8 +76,8 @@ def main():
75
76
cudnn .benchmark = True
76
77
77
78
# dataset
78
- iotrain = LArCVDataset ("train_dataloader.cfg" , "ThreadProcessor" , loadallinmem = True )
79
- iovalid = LArCVDataset ("valid_dataloader.cfg" , "ThreadProcessorTest" )
79
+ iotrain = LArCVDataset ("train_dataloader.cfg" , "ThreadProcessor" , loadallinmem = False )
80
+ iovalid = LArCVDataset ("valid_dataloader.cfg" , "ThreadProcessorTest" , loadallinmem = False )
80
81
81
82
iotrain .start (batchsize )
82
83
iovalid .start (batchsize_valid )
@@ -93,20 +94,18 @@ def main():
93
94
img = data ["image" ]
94
95
lbl = data ["label" ]
95
96
img_np = np .zeros ( (img .shape [0 ], 1 , 256 , 256 ), dtype = np .float32 )
96
- lbl_np = np .zeros ( (lbl .shape [0 ]), dtype = np .int )
97
97
for j in range (img .shape [0 ]):
98
98
imgtemp = img [j ].reshape ( (256 ,256 ) )
99
- print imgtemp .shape
99
+ print ( imgtemp .shape )
100
100
img_np [j ,0 ,:,:] = padandcrop (imgtemp )
101
101
lbl_np [j ] = np .argmax (lbl [j ])
102
-
103
- print "Train label"
104
- print lbl_np
102
+ print ("Train label" )
103
+ print (lbl_np )
105
104
106
105
datatest = iovalid [0 ]
107
106
imgtest = data ["image" ]
108
- print "Test image shape"
109
- print imgtest .shape
107
+ print ( "Test image shape" )
108
+ print ( imgtest .shape )
110
109
111
110
iotrain .stop ()
112
111
iovalid .stop ()
@@ -116,29 +115,29 @@ def main():
116
115
for epoch in range (start_epoch , epochs ):
117
116
118
117
adjust_learning_rate (optimizer , epoch , lr )
119
- print "Epoch [%d]: " % (epoch ),
120
- for param_group in optimizer .param_groups :
121
- print "lr=%.3e" % (param_group ['lr' ]),
122
- print
118
+ # print( "Epoch [%d]: "%(epoch),)
119
+ # for param_group in optimizer.param_groups:
120
+ # print( "lr=%.3e"%(param_group['lr']),)
121
+ # print()
123
122
124
123
# train for one epoch
125
124
try :
126
125
train_ave_loss , train_ave_acc = train (iotrain , model , criterion , optimizer , nbatches_per_epoch , epoch , 50 )
127
- except Exception , e :
128
- print "Error in training routine!"
129
- print e .message
130
- print e .__class__ .__name__
126
+ except Exception as e :
127
+ print ( "Error in training routine!" )
128
+ print ( e .message )
129
+ print ( e .__class__ .__name__ )
131
130
traceback .print_exc (e )
132
131
break
133
- print "Epoch [%d] train aveloss=%.3f aveacc=%.3f" % (epoch ,train_ave_loss ,train_ave_acc )
132
+ print ( "Epoch [%d] train aveloss=%.3f aveacc=%.3f" % (epoch ,train_ave_loss ,train_ave_acc ) )
134
133
135
134
# evaluate on validation set
136
135
try :
137
136
prec1 = validate (iovalid , model , criterion , nbatches_per_valid , 1 )
138
- except Exception , e :
139
- print "Error in validation routine!"
140
- print e .message
141
- print e .__class__ .__name__
137
+ except Exception as e :
138
+ print ( "Error in validation routine!" )
139
+ print ( e .message )
140
+ print ( e .__class__ .__name__ )
142
141
traceback .print_exc (e )
143
142
break
144
143
@@ -177,7 +176,9 @@ def train(train_loader, model, criterion, optimizer, nbatches, epoch, print_freq
177
176
model .train ()
178
177
179
178
for i in range (0 ,nbatches ):
180
- #print "epoch ",epoch," batch ",i," of ",nbatches
179
+ #print("epoch ",epoch," batch ",i," of ",nbatches)
180
+ optimizer .zero_grad ()
181
+
181
182
batchstart = time .time ()
182
183
183
184
end = time .time ()
@@ -195,28 +196,24 @@ def train(train_loader, model, criterion, optimizer, nbatches, epoch, print_freq
195
196
imgtmp = img [j ].reshape ( (256 ,256 ) )
196
197
img_np [j ,0 ,:,:] = padandcropandflip (imgtmp ) # data augmentation
197
198
lbl_np [j ] = np .argmax (lbl [j ])
198
- input = torch .from_numpy (img_np ).cuda ()
199
- target = torch .from_numpy (lbl_np ).cuda ()
199
+ #print(lbl[j]," ",lbl_np[j])
200
+ input_var = torch .from_numpy (img_np ).cuda ()
201
+ target_var = torch .from_numpy (lbl_np ).cuda ()
202
+ #print("target: ",target_var,target_var.shape)
200
203
201
204
# measure data formatting time
202
- format_time .update (time .time () - end )
203
-
204
-
205
- input_var = torch .autograd .Variable (input )
206
- target_var = torch .autograd .Variable (target )
205
+ format_time .update (time .time () - end )
207
206
208
207
# compute output
209
208
end = time .time ()
210
209
output = model (input_var )
211
210
loss = criterion (output , target_var )
212
-
213
211
# measure accuracy and record loss
214
- prec1 = accuracy (output .data , target , topk = (1 ,))
215
- losses .update (loss .data [ 0 ], input .size (0 ))
216
- top1 .update (prec1 [0 ], input .size (0 ))
212
+ prec1 = accuracy (output .detach (), target_var , topk = (1 ,))
213
+ losses .update (loss .detach (). cpu (). item (), input_var .size (0 ))
214
+ top1 .update (prec1 [0 ], input_var .size (0 ))
217
215
218
216
# compute gradient and do SGD step
219
- optimizer .zero_grad ()
220
217
loss .backward ()
221
218
optimizer .step ()
222
219
train_time .update (time .time ()- end )
@@ -233,7 +230,7 @@ def train(train_loader, model, criterion, optimizer, nbatches, epoch, print_freq
233
230
train_time .val ,train_time .avg ,
234
231
losses .val ,losses .avg ,
235
232
top1 .val ,top1 .avg )
236
- print "Epoch: [%d][%d/%d]\t Time %.3f (%.3f)\t Data %.3f (%.3f)\t Format %.3f (%.3f)\t Train %.3f (%.3f)\t Loss %.3f (%.3f)\t Prec@1 %.3f (%.3f)" % status
233
+ print ( "Epoch: [%d][%d/%d]\t Time %.3f (%.3f)\t Data %.3f (%.3f)\t Format %.3f (%.3f)\t Train %.3f (%.3f)\t Loss %.3f (%.3f)\t Prec@1 %.3f (%.3f)" % status )
237
234
#print('Epoch: [{0}][{1}/{2}]\t'
238
235
# 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
239
236
# 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
@@ -262,28 +259,26 @@ def validate(val_loader, model, criterion, nbatches, print_freq):
262
259
for j in range (img .shape [0 ]):
263
260
img_np [j ,0 ,:,:] = img [j ].reshape ( (256 ,256 ) )
264
261
lbl_np [j ] = np .argmax (lbl [j ])
265
- input = torch .from_numpy (img_np ).cuda ()
262
+ inimg_var = torch .from_numpy (img_np ).cuda ()
266
263
target = torch .from_numpy (lbl_np ).cuda ()
267
264
268
- input_var = torch .autograd .Variable (input , volatile = True )
269
- target_var = torch .autograd .Variable (target , volatile = True )
270
-
271
265
# compute output
272
- output = model (input_var )
273
- loss = criterion (output , target_var )
266
+ with torch .no_grad ():
267
+ output = model (inimg_var )
268
+ loss = criterion (output , target )
274
269
275
- # measure accuracy and record loss
276
- prec1 = accuracy (output .data , target , topk = (1 ,))
277
- losses .update (loss .data [ 0 ], input .size (0 ))
278
- top1 .update (prec1 [0 ], input .size (0 ))
270
+ # measure accuracy and record loss
271
+ prec1 = accuracy (output .detach () , target , topk = (1 ,))
272
+ losses .update (loss .detach (). cpu (). item (), inimg_var .size (0 ))
273
+ top1 .update (prec1 [0 ], inimg_var .size (0 ))
279
274
280
275
# measure elapsed time
281
276
batch_time .update (time .time () - end )
282
277
end = time .time ()
283
278
284
279
if i % print_freq == 0 :
285
280
status = (i ,nbatches ,batch_time .val ,batch_time .avg ,losses .val ,losses .avg ,top1 .val ,top1 .avg )
286
- print "Test: [%d/%d]\t Time %.3f (%.3f)\t Loss %.3f (%.3f)\t Prec@1 %.3f (%.3f)" % status
281
+ # print( "Test: [%d/%d]\tTime %.3f (%.3f)\tLoss %.3f (%.3f)\tPrec@1 %.3f (%.3f)"%status)
287
282
#print('Test: [{0}/{1}]\t'
288
283
# 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
289
284
# 'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
@@ -293,7 +288,7 @@ def validate(val_loader, model, criterion, nbatches, print_freq):
293
288
294
289
#print(' * Prec@1 {top1.avg:.3f}'
295
290
# .format(top1=top1))
296
- print "Test:Result* Prec@1 %.3f\t Loss %.3f" % (top1 .avg ,losses .avg )
291
+ print ( "Test:Result* Prec@1 %.3f\t Loss %.3f" % (top1 .avg ,losses .avg ) )
297
292
298
293
return float (top1 .avg )
299
294
@@ -353,8 +348,8 @@ def dump_lr_schedule( startlr, numepochs ):
353
348
for epoch in range (0 ,numepochs ):
354
349
lr = startlr * (0.5 ** (epoch // 300 ))
355
350
if epoch % 10 == 0 :
356
- print "Epoch [%d] lr=%.3e" % (epoch ,lr )
357
- print "Epoch [%d] lr=%.3e" % (epoch ,lr )
351
+ print ( "Epoch [%d] lr=%.3e" % (epoch ,lr ) )
352
+ print ( "Epoch [%d] lr=%.3e" % (epoch ,lr ) )
358
353
return
359
354
360
355
if __name__ == '__main__' :
0 commit comments