mask rcnn 部署小技巧

int8進行網絡傳輸

目的:

  1. 我們項目需要用到instance segmentation, 所以 rcnn_mask輸出層數(shù)據(jù)量特別大,同時因為圖片尺寸有1024*1024*3這么大.
  2. 如果不壓縮一下直接進行網絡傳輸,數(shù)據(jù)量會很大,接口延遲很高.
  3. 為了部署后,請求推理服務時,數(shù)據(jù)輸入層和輸出層的傳輸量減少.
  4. float32 轉變成 int8,傳輸量減少了4倍.

處理:

輸入層:

先轉換成 int8,然后再轉成float32

 def build(self, mode, config):
         input_image = KL.Input(
            shape=[None, None, config.IMAGE_SHAPE[2]], name="input_image",dtype=tf.int8)
        input_image = KL.Input(tensor=K.cast(input_image,dtype= tf.float32))
        

輸出層:

   mrcnn_mask = build_fpn_mask_graph(detection_boxes, mrcnn_feature_maps,
                                             input_image_meta,
                                             config.MASK_POOL_SIZE,
                                             config.NUM_CLASSES,
                                             train_bn=config.TRAIN_BN)
              #模型導出時加上去
           mrcnn_mask = KL.Lambda(lambda x: x*255)(mrcnn_mask)
           mrcnn_mask = KL.Lambda(lambda x: tf.cast(x,dtype=tf.int8))(mrcnn_mask)

云函數(shù)服務 拿到mrcnn_mask輸出層數(shù)據(jù)后,再轉換成 float32

模型導出:

導出時注意 output 名稱要對應.

def save_model():
   config = QuesConfig()
   PRETRAINED_MODEL_PATH = "/xxxxx/weights"
   MODEL_NAME = 'xxxxx_0601.h5'
   export_dir = "./saved_model/1/"
   h5_to_saved_model(config, PRETRAINED_MODEL_PATH, MODEL_NAME, export_dir)
   
def h5_to_saved_model(config, model_dir, model_name, export_dir):
   if tf.gfile.Exists(export_dir):
       tf.gfile.DeleteRecursively(export_dir)
   config.display()

   model = modellib.MaskRCNN(mode="inference", config=config, model_dir=model_dir)
   model_path = os.path.join(model_dir, model_name)
   model.load_weights(model_path, by_name=True)
   with K.get_session() as session:
       save_m(export_dir, session)
   
def save_m(export_dir, session):
   if tf.gfile.Exists(export_dir):
       tf.gfile.DeleteRecursively(export_dir)
   # legacy_init_op = tf.group(tf.tables_initializer())
   builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
   signature_inputs = {
       "input_image":
           session.graph.get_tensor_by_name("input_image:0"),
       "input_image_meta":
           session.graph.get_tensor_by_name("input_image_meta:0"),
       "input_anchors":
           session.graph.get_tensor_by_name("input_anchors:0"),
   }
   signature_outputs = {
       'mrcnn_detection':
           session.graph.get_tensor_by_name('mrcnn_detection/Reshape_1:0'),
       'mrcnn_mask':
           session.graph.get_tensor_by_name('lambda_5/Cast:0')
   }

   sigs = {}
   sigs['serving_default'] = tf.saved_model.signature_def_utils.predict_signature_def(
       inputs=signature_inputs,
       outputs=signature_outputs)
   builder.add_meta_graph_and_variables(
       session,
       [tf.saved_model.tag_constants.SERVING],
       signature_def_map=sigs

   )
   builder.save()

模型調用

構建網絡請求:

   def mold_image(self, images):
      """Expects an RGB image (or array of images) and subtracts
      the mean pixel and converts it to float. Expects image
      colors in RGB order.
      """
      mold_image_fp32 = images.astype(np.float32) - self.config.MEAN_PIXEL
      mold_image_int8 = mold_image_fp32.astype(np.int8)
      return mold_image_int8

解析響應數(shù)據(jù):

 # mrcnn_mask = tf.make_ndarray(response.outputs['mrcnn_mask'])
   tensor = response.outputs['mrcnn_mask']
   shape = [d.size for d in tensor.tensor_shape.dim]
   values = np.fromiter(tensor.int_val, dtype=np.int8)
   mrcnn_mask = values.reshape(shape)
   mrcnn_mask = mrcnn_mask.astype(np.float32)

本篇文章由一文多發(fā)平臺ArtiPub自動發(fā)布

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容