tflite 合并模型和条件预测

发布于 2025-01-14 07:43:52 字数 2910 浏览 1 评论 0原文

我有一个问题,希望你能得到一些答案或建议。如何使用 tflite 中前两个模型的预测条件将三个 CNN 模型合并为一个模型?

我首先尝试解释我的实际解决方案和我实施的步骤。

我的想法是,我训练了三个不同的模型来区分 4 个最终类别,前两个模型预测 2 个输出(一个最终类别 (fc) 和一个子类别 (sc)),第三个模型预测 2 个最终类别。

我分别训练了这三个模型,它们具有相同的架构但不同的数据集。这 3 个数据集包含相同的图像,但根据最终类对子类的归属进行不同的组织。与单一模型的 74% 相比,该解决方案为我提供了 89% 的更好测试结果。这就是为什么我想保留这三个模型并进行有条件的组合。

现在我正在使用一个类,该类加载 tflit 模型、加载图像、执行预测并对图像进行分类。如下:

class model1_model2_model3_pipline:
  def __init__(self,model_path):
    self.load_model(model_path)

  def load_model(self,model_path):    
    self.tflite_interpreter = tf.lite.Interpreter(model_path=model_path)
    self.tflite_interpreter.allocate_tensors()
    self.input_details = self.tflite_interpreter.get_input_details()
    self.output_details = self.tflite_interpreter.get_output_details()
    self.d, self.w, self.h, self.ch = self.input_details[0]['shape'] 

  def load_image(self,image_path):
    imarray = cv2.imread(f'{image_path}')
    image = cv2.cvtColor(imarray, cv2.COLOR_BGR2GRAY)
    image = cv2.resize(image, (self.h, self.w))
    image = np.expand_dims(image, 2)
    image = np.array(image,  dtype=self.input_details[0]['dtype'])
    image = np.expand_dims(image, axis=0)
    return image

  def mpredict(self,image):     
    tflite_interpreter = self.tflite_interpreter
    tflite_interpreter.set_tensor(self.input_details[0]['index'], image)
    st = time.time()
    tflite_interpreter.invoke()
    elp = time.time() - st
    return elp

  def classify_image(self, top_k=1):
    output_details = self.output_details[0]
    output = np.squeeze(self.tflite_interpreter.get_tensor(output_details['index']))
    if output_details['dtype'] == np.uint8:
      scale, zero_point = output_details['quantization']
      output = scale * (output - zero_point)
    ordered = np.argpartition(-output, top_k)
    return [(i, output[i]) for i in ordered[:top_k]]

  def predict_single_image(self,image_path):
    img = self.load_image2(image_path)
    self.mpredict(img)
    out = self.classify_image(top_k=1)
    return out

现在我必须创建该类的三个实例,并为每个模型预测定义条件来执行预测。

# fc: final-class, sc: sub-class
MAP_CHARACTERS_model1 = {
  0: 'fc1',
  1: 'sc1',
  }
MAP_CHARACTERS_model2 = {
  0: 'fc2',
  1: 'sc2',
  }

MAP_CHARACTERS_model3 = {
  0: 'fc3',
  1: 'fc4',
  }

x_model1 = model1_model2_model3_pipline(model1_pth) #pth for path
x_model2 = model1_model2_model3_pipline(model2_pth)
x_model3 = model1_model2_model3_pipline(model3_pth)

out_model1 = x_model1.predict_single_image(im_pth)
predicted = MAP_CHARACTERS_model1[out_model1[0][0]]

if predicted == 'sc1':
  out_model2 = x_model2.predict_single_image(im_pth)
  predicted = MAP_CHARACTERS_model2[out_model2[0][0]]
  if predicted =='sc2':
    out_model3 = x_model3.predict_single_image(im_pth)
    predicted = MAP_CHARACTERS_model3[out_model3[0][0]]

我想将这三个模型合并为一个模型,并且只需一步即可执行预测。有没有解决方案可以在 tflite 上执行此过程?

I have a question and I wish you can have some answers or proposition. How can I merge three CNN models into one single model using conditions on the prediction of the first two models in tflite ?

I try first to explain my actual solution and the steps I implemented.

The idea is that I have trained three different models to distinguish 4 final classes, the first two models predict 2 outputs (One final classe (fc) and One Sub-classe (sc)), the third model predicts 2 final classes.

I have trained the three models separately, they have the same architecture but different datasets. the 3 datasets contain the same images but are differently organised according to the belonging of the final classes to subclasses. This solution gives me a better testing result of 89% compared to a single model which gives 74%. That's why I want to keep the three models and make a conditional combination.

Now I'am using a class that, loads a tflit model, loads the image, performs the prediction, and classify the image. As follows:

class model1_model2_model3_pipline:
  def __init__(self,model_path):
    self.load_model(model_path)

  def load_model(self,model_path):    
    self.tflite_interpreter = tf.lite.Interpreter(model_path=model_path)
    self.tflite_interpreter.allocate_tensors()
    self.input_details = self.tflite_interpreter.get_input_details()
    self.output_details = self.tflite_interpreter.get_output_details()
    self.d, self.w, self.h, self.ch = self.input_details[0]['shape'] 

  def load_image(self,image_path):
    imarray = cv2.imread(f'{image_path}')
    image = cv2.cvtColor(imarray, cv2.COLOR_BGR2GRAY)
    image = cv2.resize(image, (self.h, self.w))
    image = np.expand_dims(image, 2)
    image = np.array(image,  dtype=self.input_details[0]['dtype'])
    image = np.expand_dims(image, axis=0)
    return image

  def mpredict(self,image):     
    tflite_interpreter = self.tflite_interpreter
    tflite_interpreter.set_tensor(self.input_details[0]['index'], image)
    st = time.time()
    tflite_interpreter.invoke()
    elp = time.time() - st
    return elp

  def classify_image(self, top_k=1):
    output_details = self.output_details[0]
    output = np.squeeze(self.tflite_interpreter.get_tensor(output_details['index']))
    if output_details['dtype'] == np.uint8:
      scale, zero_point = output_details['quantization']
      output = scale * (output - zero_point)
    ordered = np.argpartition(-output, top_k)
    return [(i, output[i]) for i in ordered[:top_k]]

  def predict_single_image(self,image_path):
    img = self.load_image2(image_path)
    self.mpredict(img)
    out = self.classify_image(top_k=1)
    return out

Now I have to make three instances of the class and perform prediction with defining conditions for each model prediction.

# fc: final-class, sc: sub-class
MAP_CHARACTERS_model1 = {
  0: 'fc1',
  1: 'sc1',
  }
MAP_CHARACTERS_model2 = {
  0: 'fc2',
  1: 'sc2',
  }

MAP_CHARACTERS_model3 = {
  0: 'fc3',
  1: 'fc4',
  }

x_model1 = model1_model2_model3_pipline(model1_pth) #pth for path
x_model2 = model1_model2_model3_pipline(model2_pth)
x_model3 = model1_model2_model3_pipline(model3_pth)

out_model1 = x_model1.predict_single_image(im_pth)
predicted = MAP_CHARACTERS_model1[out_model1[0][0]]

if predicted == 'sc1':
  out_model2 = x_model2.predict_single_image(im_pth)
  predicted = MAP_CHARACTERS_model2[out_model2[0][0]]
  if predicted =='sc2':
    out_model3 = x_model3.predict_single_image(im_pth)
    predicted = MAP_CHARACTERS_model3[out_model3[0][0]]

I want to merge these three models into one single model and perform the prediction at only one step. Is there a solution to do this process on tflite ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2025-01-21 07:43:52

简单/推荐的方法是在 TF 中添加额外的调节。这将为您提供 TF 中的一个大模型,可以满足您的需求,然后将此合并模型转换为 TFLite。

The easy/recommended way is to add the extra conditioning in TF. This will get you one big model in TF that does what you want, then convert this merged model to TFLite.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文