4
基础环境
使用Pyintaller打包tensorflow程序后,执行报错:设备无法初始化
2024-03-29 14:35:00
81

问题描述

测试环境为 Tensorflow2.9.3 + DTK23.04.1,对一个简单的tensorflow程序进行打包:

import os
import tensorflow
import tensorflow.compat.v1 as tf
import numpy as np
import cv2
from matplotlib import pyplot as plt
import glob
class CarplateMask(object):
    INPUT_NAME = 'input:0'
    YOLO_NAME = 'output:0'

    def __init__(self, frozen_graph):
        self.graph = tf.Graph()
        graph_def = tf.GraphDef()
        with tf.gfile.GFile(frozen_graph, 'rb') as fid:
            serialized_graph = fid.read()
            graph_def.ParseFromString(serialized_graph)

        with self.graph.as_default():
            tf.import_graph_def(graph_def, name='')

        self.sess = tf.Session(graph=self.graph)

    def run(self, image):
        preds = self.sess.run([self.YOLO_NAME], feed_dict={self.INPUT_NAME: image})
        return preds

if __name__ == "__main__":
    import time
    model = CarplateMask('/home/yolov5.tensorflow/carplate20230627.pb')
    for file in glob.glob('/home/yolov5.tensorflow/imgs/*.jpg')[:]:
        print("start")
        image_ori = cv2.imread(file)
        image = image_ori.copy()
        image = cv2.resize(image,(640, 640))
        start_time = time.time()
        pred = model.run(np.expand_dims(image/255, 0))[0]
        elapsed_time = time.time() - start_time
        print("end, elapsed: ",elapsed_time)

直接执行python程序可以正常执行,进行pyinstaller detect.py后,执行 ./dist/detect/detect  文件显示设备初始话错误:

attachmentId-image.png


解决方案

pyinstaller进行打包时,会将很多库打包进 ./dist/{project}/_internal/  内,本例中重复打包了DTK runtime动态库 libgalaxyhip.so.5  ,导致重复链接。删除 ./dist/detect/_internal/libgalaxyhip.so.5  后,执行恢复正常。

全部评论(0)
只看楼主
暂无数据
--
楼主
Level.3
0 帖子
楼主热帖
暂无数据