parameters: (model: onnx.ModelProto, image_dir: str,
                                 features: List[str] = None, batch_size=56,
                                 num_workers=1, show_cls=False)
docstring: Collect infos of features by running model in onnxruntime
      :param model: the model
      :param image_dir: the directory of images
      :param features: names of features that need to collect, None for all features
      :param batch_size: batch size for net forward
      :param num_workers: number of thread fetching and preprocessing images
docstring_summary: Collect infos of features by running model in onnxruntime
      :param model: the model
      :param image_dir: the directory of images
      :param features: names of features that need to collect, None for all features
      :param batch_size: batch size for net forward
      :param num_workers: number of thread fetching and preprocessing images
docstring_tokens: Collect infos of features by running model in onnxruntime : param model : the model : param image_dir : the directory of images : param features : names of features that need to collect None for all features : param batch_size : batch size for net forward : param num_workers : number of thread fetching and preprocessing images
function: def collect_scales_of_features(model: onnx.ModelProto, image_dir: str,
                                 features: List[str] = None, batch_size=56,
                                 num_workers=1, show_cls=False) -> None:
      """
      Collect infos of features by running model in onnxruntime
      :param model: the model
      :param image_dir: the directory of images
      :param features: names of features that need to collect, None for all features
      :param batch_size: batch size for net forward
      :param num_workers: number of thread fetching and preprocessing images
      """
      from queue import Queue
      import threading
      import glob
  
      q = Queue()
  
      def worker(paths):
          def read_img(path, norm=True):
              import cv2
              a = cv2.imread(path)
              a = cv2.resize(a, (224, 224))
              a = a.astype(np.float32)
              a = cv2.cvtColor(a, cv2.COLOR_BGR2RGB)
              if norm:
                  a /= 255
                  # mean and std for RGB images
                  a -= [0.485, 0.456, 0.406]
                  a /= [0.229, 0.224, 0.225]
              a = np.moveaxis(a, -1, 0)
              return a
  
          for i in range(0, len(paths), batch_size):
              xs = np.stack(list(map(lambda x: read_img(x, True), paths[i:i + batch_size])))
              q.put(xs)
          q.put(None)
  
      image_exts = ['JPEG', 'jpg', 'jpeg', 'png']
      filenames = list(itertools.chain(*[glob.glob(os.path.join(image_dir, '**/*.' + ext), recursive=True)
                                         for ext in image_exts]))
      import random
      random.shuffle(filenames)
      filenames = filenames[:10000]
      file_num = len(filenames)
      num_workers = min((file_num + batch_size - 1) // batch_size, num_workers)
      chunk_num = file_num // num_workers
      threads = []
      for i in range(num_workers):
          t = threading.Thread(target=worker, args=((filenames[chunk_num * i:chunk_num * (i + 1)],)))
          t.start()
          threads.append(t)
  
      import onnxruntime as rt
      sess = rt.InferenceSession(model.SerializeToString())
      all_outputs = [x.name for x in sess.get_outputs()]
      features = all_outputs if features is None else list(OrderedSet(features) & OrderedSet(all_outputs))
      i = 0
      done_workers = 0
      if show_cls:
          from collections import defaultdict, Counter
          d = defaultdict(int)
      while True:
          xs = q.get()
          if xs is None:
              done_workers += 1
              if done_workers == num_workers:
                  break
              continue
          i += xs.shape[0]
          update_scale_and_zp('data', xs)
          from collections import OrderedDict
          res = OrderedDict(zip(features, sess.run(features, {'data': xs})))
          if show_cls:
              cls = Counter(res['mobilenetv20_output_pred_fwd'].squeeze(axis=(2,3)).argmax(axis=1))
              for key in cls:
                  d[key] += cls[key]
          for key in res:
              update_scale_and_zp(key, res[key])
          q.task_done()
          print("{}/{}".format(i, file_num))
  
      if show_cls:
          print(d)
  
      for t in threads:
          t.join()
function_tokens: def collect_scales_of_features ( model : onnx . ModelProto , image_dir : str , features : List [ str ] = None , batch_size = 56 , num_workers = 1 , show_cls = False ) -> None : from queue import Queue import threading import glob q = Queue ( ) def worker ( paths ) : def read_img ( path , norm = True ) : import cv2 a = cv2 . imread ( path ) a = cv2 . resize ( a , ( 224 , 224 ) ) a = a . astype ( np . float32 ) a = cv2 . cvtColor ( a , cv2 . COLOR_BGR2RGB ) if norm : a /= 255 # mean and std for RGB images a -= [ 0.485 , 0.456 , 0.406 ] a /= [ 0.229 , 0.224 , 0.225 ] a = np . moveaxis ( a , - 1 , 0 ) return a for i in range ( 0 , len ( paths ) , batch_size ) : xs = np . stack ( list ( map ( lambda x : read_img ( x , True ) , paths [ i : i + batch_size ] ) ) ) q . put ( xs ) q . put ( None ) image_exts = [ 'JPEG' , 'jpg' , 'jpeg' , 'png' ] filenames = list ( itertools . chain ( * [ glob . glob ( os . path . join ( image_dir , '**/*.' + ext ) , recursive = True ) for ext in image_exts ] ) ) import random random . shuffle ( filenames ) filenames = filenames [ : 10000 ] file_num = len ( filenames ) num_workers = min ( ( file_num + batch_size - 1 ) // batch_size , num_workers ) chunk_num = file_num // num_workers threads = [ ] for i in range ( num_workers ) : t = threading . Thread ( target = worker , args = ( ( filenames [ chunk_num * i : chunk_num * ( i + 1 ) ] , ) ) ) t . start ( ) threads . append ( t ) import onnxruntime as rt sess = rt . InferenceSession ( model . SerializeToString ( ) ) all_outputs = [ x . name for x in sess . get_outputs ( ) ] features = all_outputs if features is None else list ( OrderedSet ( features ) & OrderedSet ( all_outputs ) ) i = 0 done_workers = 0 if show_cls : from collections import defaultdict , Counter d = defaultdict ( int ) while True : xs = q . get ( ) if xs is None : done_workers += 1 if done_workers == num_workers : break continue i += xs . shape [ 0 ] update_scale_and_zp ( 'data' , xs ) from collections import OrderedDict res = OrderedDict ( zip ( features , sess . run ( features , { 'data' : xs } ) ) ) if show_cls : cls = Counter ( res [ 'mobilenetv20_output_pred_fwd' ] . squeeze ( axis = ( 2 , 3 ) ) . argmax ( axis = 1 ) ) for key in cls : d [ key ] += cls [ key ] for key in res : update_scale_and_zp ( key , res [ key ] ) q . task_done ( ) print ( "{}/{}" . format ( i , file_num ) ) if show_cls : print ( d ) for t in threads : t . join ( )