@@ -81,61 +81,61 @@ def eval_unet3d():
81
81
82
82
def eval_retinanet ():
83
83
# RetinaNet with ResNeXt50_32X4D
84
+ from examples .mlperf .dataloader import batch_load_retinanet
85
+ from extra .datasets .openimages import normalize , download_dataset , BASEDIR
84
86
from extra .models .resnet import ResNeXt50_32X4D
85
87
from extra .models .retinanet import RetinaNet
86
- mdl = RetinaNet (ResNeXt50_32X4D ())
87
- mdl .load_from_pretrained ()
88
-
89
- input_mean = Tensor ([0.485 , 0.456 , 0.406 ]).reshape (1 , - 1 , 1 , 1 )
90
- input_std = Tensor ([0.229 , 0.224 , 0.225 ]).reshape (1 , - 1 , 1 , 1 )
91
- def input_fixup (x ):
92
- x = x .permute ([0 ,3 ,1 ,2 ]) / 255.0
93
- x -= input_mean
94
- x /= input_std
95
- return x
96
-
97
- from extra .datasets .openimages import download_dataset , iterate , BASEDIR
98
88
from pycocotools .coco import COCO
99
89
from pycocotools .cocoeval import COCOeval
100
90
from contextlib import redirect_stdout
91
+ tlog ("imports" )
92
+
93
+ mdl = RetinaNet (ResNeXt50_32X4D ())
94
+ mdl .load_from_pretrained ()
95
+ tlog ("loaded models" )
96
+
101
97
coco = COCO (download_dataset (base_dir := getenv ("BASE_DIR" , BASEDIR ), 'validation' ))
102
98
coco_eval = COCOeval (coco , iouType = "bbox" )
103
99
coco_evalimgs , evaluated_imgs , ncats , narea = [], [], len (coco_eval .params .catIds ), len (coco_eval .params .areaRng )
100
+ tlog ("loaded dataset" )
104
101
105
- from tinygrad .engine .jit import TinyJit
106
- mdlrun = TinyJit (lambda x : mdl (input_fixup (x )).realize ())
107
-
108
- n , bs = 0 , 8
102
+ iterator = batch_load_retinanet (coco , True , Path (base_dir ), getenv ("BS" , 8 ), shuffle = False )
103
+ def data_get ():
104
+ x , img_ids , img_sizes , cookie = next (iterator )
105
+ return x .to (Device .DEFAULT ).realize (), img_ids , img_sizes , cookie
106
+ n = 0
107
+ proc = data_get ()
108
+ tlog ("loaded initial data" )
109
109
st = time .perf_counter ()
110
- for x , targets in iterate (coco , base_dir , bs ):
111
- dat = Tensor (x .astype (np .float32 ))
112
- mt = time .perf_counter ()
113
- if dat .shape [0 ] == bs :
114
- outs = mdlrun (dat ).numpy ()
115
- else :
116
- mdlrun ._jit_cache = []
117
- outs = mdl (input_fixup (dat )).numpy ()
118
- et = time .perf_counter ()
119
- predictions = mdl .postprocess_detections (outs , input_size = dat .shape [1 :3 ], orig_image_sizes = [t ["image_size" ] for t in targets ])
120
- ext = time .perf_counter ()
121
- n += len (targets )
122
- print (f"[{ n } /{ len (coco .imgs )} ] == { (mt - st )* 1000 :.2f} ms loading data, { (et - mt )* 1000 :.2f} ms to run model, { (ext - et )* 1000 :.2f} ms for postprocessing" )
123
- img_ids = [t ["image_id" ] for t in targets ]
124
- coco_results = [{"image_id" : targets [i ]["image_id" ], "category_id" : label , "bbox" : box .tolist (), "score" : score }
110
+ while proc is not None :
111
+ GlobalCounters .reset ()
112
+ proc = (mdl (normalize (proc [0 ])), proc [1 ], proc [2 ], proc [3 ])
113
+ run = time .perf_counter ()
114
+ # load the next data here
115
+ try : next_proc = data_get ()
116
+ except StopIteration : next_proc = None
117
+ nd = time .perf_counter ()
118
+ predictions , img_ids = mdl .postprocess_detections (proc [0 ].numpy (), orig_image_sizes = proc [2 ]), proc [1 ]
119
+ coco_results = [{"image_id" : img_ids [i ], "category_id" : label , "bbox" : box .tolist (), "score" : score }
125
120
for i , prediction in enumerate (predictions ) for box , score , label in zip (* prediction .values ())]
126
121
with redirect_stdout (None ):
127
122
coco_eval .cocoDt = coco .loadRes (coco_results )
128
123
coco_eval .params .imgIds = img_ids
129
124
coco_eval .evaluate ()
130
125
evaluated_imgs .extend (img_ids )
131
126
coco_evalimgs .append (np .array (coco_eval .evalImgs ).reshape (ncats , narea , len (img_ids )))
132
- st = time .perf_counter ()
127
+ n += len (proc [0 ])
128
+ et = time .perf_counter ()
129
+ tlog (f"****** { (run - st )* 1000 :7.2f} ms to enqueue, { (et - run )* 1000 :7.2f} ms to realize ({ (nd - run )* 1000 :7.2f} ms fetching). { (len (proc ))/ (et - st ):8.2f} examples/sec. { GlobalCounters .global_ops * 1e-12 / (et - st ):5.2f} TFLOPS" )
130
+ st = et
131
+ proc , next_proc = next_proc , None
133
132
134
133
coco_eval .params .imgIds = evaluated_imgs
135
134
coco_eval ._paramsEval .imgIds = evaluated_imgs
136
135
coco_eval .evalImgs = list (np .concatenate (coco_evalimgs , - 1 ).flatten ())
137
136
coco_eval .accumulate ()
138
137
coco_eval .summarize ()
138
+ tlog ("done" )
139
139
140
140
def eval_rnnt ():
141
141
# RNN-T
0 commit comments