@@ -26,12 +26,11 @@ def perform_certification_test(base_classifier: tf.keras.Model,
2626 10 , sigma )
2727 if not os .path .exists (outfile .split ('sigma' )[0 ]):
2828 os .makedirs (outfile .split ('sigma' )[0 ])
29-
30- f = open (outfile , 'w' )
31- print ("idx\t label\t predict\t radius\t correct\t time" , file = f ,
29+
30+ with open (outfile , "w" ) as f :
31+ print ("idx\t label\t predict\t radius\t correct\t time" , file = f ,
3232 flush = True )
3333 print ("idx\t label\t predict\t radius\t correct\t time" , flush = True )
34- f .close ()
3534
3635 images , labels = dataset
3736 for i in range (len (images )):
@@ -53,12 +52,11 @@ def perform_certification_test(base_classifier: tf.keras.Model,
5352
5453 time_elapsed = str (
5554 datetime .timedelta (seconds = (after_time - before_time )))
56-
57- f = open (outfile , 'a' )
58- print ("{}\t {}\t {}\t {:.3}\t {}\t {}" .format (
59- i , label , prediction , radius , correct , time_elapsed ),
60- file = f , flush = True )
55+
56+ with open (outfile , "a" ) as f :
57+ print ("{}\t {}\t {}\t {:.3}\t {}\t {}" .format (
58+ i , label , prediction , radius , correct , time_elapsed ),
59+ file = f , flush = True )
6160 print ("{}\t {}\t {}\t {:.3}\t {}\t {}" .format (
6261 i , label , prediction , radius , correct , time_elapsed ),
6362 flush = True )
64- f .close ()
0 commit comments