weixin_40183383 2021-03-18 21:49 采纳率: 100%
浏览 1619
已结题

基于tensorflow的模型做预测时,cpu占用率过高,如何降低程序的cpu占用率?

 如图,在cpu服务器,liunux系统下跑,基于tensorflow的模型做预测时,cpu占用率过高,最多达到了700%,严重影响服务器上其他程序的运行,请问可以怎么改程序,降低程序的cpu占用率?

 def test(self, height, width, input_path, output_path,checkpoint_path):
        imgsName = sorted(os.listdir(input_path))#遍历文件夹中的所有图像
        H, W = height, width
        inp_chns = 3 if self.args.model == 'color' else 1
        self.batch_size = 1 if self.args.model == 'color' else 1
        model_name = "deblur.model"
        ckpt_name = model_name + '-' + '15000'
        tf.reset_default_graph()
        graph = tf.get_default_graph() 
        inputs = tf.placeholder(shape=[self.batch_size, H, W, inp_chns], dtype=tf.float32) #输入占位符
        outputs = self.generator(inputs, reuse=False)#建立计算图
        saver = tf.train.Saver(tf.global_variables(), max_to_keep=2)
        sess=tf.Session(graph=graph,config=tf.ConfigProto(device_count={"CPU": 1},allow_soft_placement=True,inter_op_parallelism_threads=1,intra_op_parallelism_threads=1,use_per_session_threads=True))#设置sess
        saver.restore(sess, os.path.join(checkpoint_path, 'B5678-1-60-noise7', ckpt_name))#加载训练的模型
        for imgName in imgsName: #循环处理之前遍历的图像
            blur =cv2.imread(os.path.join(input_path, imgName),-1)#读入图
            h, w = blur.shape
            x=h//512
            #print(x)
            y=w//512
            #print(y)
            if x>y:
                blur = np.pad(blur, ((0, ((x+1)*512 - h)), (0,((x+1)*512 - w))), 'edge') #把图像扩充为512*512的整数倍方便裁切
                after_deblur=np.zeros((((x+1)*512), ((x+1)*512))) #建立相同大小空矩阵
            if x<=y:
                blur = np.pad(blur, ((0, ((y+1)*512 - h)), (0,((y+1)*512 - w))), 'edge')   #把图像扩充为512*512的整数倍方便裁切
                after_deblur=np.zeros((((y+1)*512), ((y+1)*512)))#建立相同大小空矩阵
            #把图像切分成512*512的小图,依次送入神经网络得到结果
            starttotal = time.time()
            for ii in range(x+1):
                for jj in range(y+1):
                    blurPad = blur[ii * 512:(ii + 1) * 512, jj * 512:(jj + 1) * 512]      #按顺序裁切成512*512的图像块
                    blurPad = np.expand_dims(blurPad, -1)
                    blurPad = np.expand_dims(blurPad, 0)
                    if self.args.model != 'color':
                        blurPad = np.transpose(blurPad, (3, 1, 2, 0))
                    start = time.time()
                    deblur = sess.run(outputs, feed_dict={inputs: blurPad / 4095.0})#把图像块送入计算图中sess.run计算
                    duration = time.time() - start
                    res = deblur[-1]
                    res = np.clip(res, a_min=0, a_max=1)
                    if self.args.model != 'color':
                        res = np.transpose(res, (3, 1, 2, 0))
                    res = res[0, :, :, :] * 4095.0
                    res = (res.astype(np.uint16))
                    res = np.squeeze(res)
                    after_deblur = (after_deblur.astype(np.uint16))
                    after_deblur[ii * 512:(ii + 1) * 512, jj * 512:(jj + 1) * 512]=res #用计算得到的结果替换空矩阵相同位置的值
            durationtotal = time.time() - starttotal
            print('total time use %4.3fs' % (durationtotal))
            #print(after_deblur.shape)
            after_deblur = after_deblur[:h, :w] 
            after_deblur = np.clip(after_deblur, a_min=0, a_max=4095)
            #print(after_deblur.shape)
            imtiff = Image.fromarray(after_deblur)
            imtiff.save(os.path.join(output_path,imgName)) #写出图像
        sess.close()
        del sess
  • 写回答

10条回答 默认 最新

  • 关注

    楼主,看下这两个方法能不能减少内存?我看你写的那个排序,imgsName = sorted(os.listdir(input_path))并没有起到排序的作用,正确的排序我写在代码里了,你也可以审视一下是否需要排序,如果不需要,也可以减少一些内存

    #第1种
    def test(self, height, width, input_path, output_path,checkpoint_path):
        #imgsName = sorted(os.listdir(input_path))#遍历文件夹中的所有图像
        
        from glob import glob
        input_path = glob(path +"\*")  #*.jpg 
        input_path.sort(key=lambda x:eval(os.path.basename(x).split(".")[0]))  #直接返回路径和名称,不用os.path.join(input_path, imgName)
        imgsName = iter(tuple(input_path))  #遍历文件夹中的所有图像  
        
        H, W = height, width
        inp_chns = 3 if self.args.model == 'color' else 1
        self.batch_size = 1 if self.args.model == 'color' else 1
        model_name = "deblur.model"
        ckpt_name = model_name + '-' + '15000'
        tf.reset_default_graph()
        graph = tf.get_default_graph() 
        inputs = tf.placeholder(shape=[self.batch_size, H, W, inp_chns], dtype=tf.float32) #输入占位符
        outputs = self.generator(inputs, reuse=False)#建立计算图
        saver = tf.train.Saver(tf.global_variables(), max_to_keep=2)
        sess=tf.Session(graph=graph,config=tf.ConfigProto(device_count={"CPU": 1},allow_soft_placement=True,inter_op_parallelism_threads=1,intra_op_parallelism_threads=1,use_per_session_threads=True))#设置sess
        saver.restore(sess, os.path.join(checkpoint_path, 'B5678-1-60-noise7', ckpt_name))#加载训练的模型
        for imgName in imgsName: #循环处理之前遍历的图像
            blur =cv2.imread(imgName,-1)#读入图
            h, w = blur.shape
            x=h//512
            #print(x)
            y=w//512
            #print(y)
            if x>y:
                blur = np.pad(blur, ((0, ((x+1)*512 - h)), (0,((x+1)*512 - w))), 'edge') #把图像扩充为512*512的整数倍方便裁切
                after_deblur=np.zeros((((x+1)*512), ((x+1)*512))) #建立相同大小空矩阵
            if x<=y:
                blur = np.pad(blur, ((0, ((y+1)*512 - h)), (0,((y+1)*512 - w))), 'edge')   #把图像扩充为512*512的整数倍方便裁切
                after_deblur=np.zeros((((y+1)*512), ((y+1)*512)))#建立相同大小空矩阵
            #把图像切分成512*512的小图,依次送入神经网络得到结果
            starttotal = time.time()
            for ii in range(x+1):
                for jj in range(y+1):
                    blurPad = blur[ii * 512:(ii + 1) * 512, jj * 512:(jj + 1) * 512]      #按顺序裁切成512*512的图像块
                    blurPad = np.expand_dims(blurPad, -1)
                    blurPad = np.expand_dims(blurPad, 0)
                    if self.args.model != 'color':
                        blurPad = np.transpose(blurPad, (3, 1, 2, 0))
                    start = time.time()
                    deblur = sess.run(outputs, feed_dict={inputs: blurPad / 4095.0})#把图像块送入计算图中sess.run计算
                    duration = time.time() - start
                    res = deblur[-1]
                    res = np.clip(res, a_min=0, a_max=1)
                    if self.args.model != 'color':
                        res = np.transpose(res, (3, 1, 2, 0))
                    res = res[0, :, :, :] * 4095.0
                    res = (res.astype(np.uint16))
                    res = np.squeeze(res)
                    after_deblur = (after_deblur.astype(np.uint16))
                    after_deblur[ii * 512:(ii + 1) * 512, jj * 512:(jj + 1) * 512]=res #用计算得到的结果替换空矩阵相同位置的值
            durationtotal = time.time() - starttotal
            print('total time use %4.3fs' % (durationtotal))
            #print(after_deblur.shape)
            after_deblur = after_deblur[:h, :w] 
            after_deblur = np.clip(after_deblur, a_min=0, a_max=4095)
            #print(after_deblur.shape)
            imtiff = Image.fromarray(after_deblur)
            imtiff.save(os.path.join(output_path,imgName)) #写出图像
        sess.close()
        del sess
        
    #第二种
    def test(self, height, width, input_path, output_path,checkpoint_path):
        
        #imgsName = sorted(os.listdir(input_path))#遍历文件夹中的所有图像
        
        input_image = os.listdir(path)
        input_image.sort(key=lambda x:eval(x.split(".")[0]))
        imgsName = iter(tuple(input_image))
        
        H, W = height, width
        inp_chns = 3 if self.args.model == 'color' else 1
        self.batch_size = 1 if self.args.model == 'color' else 1
        model_name = "deblur.model"
        ckpt_name = model_name + '-' + '15000'
        tf.reset_default_graph()
        graph = tf.get_default_graph() 
        inputs = tf.placeholder(shape=[self.batch_size, H, W, inp_chns], dtype=tf.float32) #输入占位符
        outputs = self.generator(inputs, reuse=False)#建立计算图
        saver = tf.train.Saver(tf.global_variables(), max_to_keep=2)
        sess=tf.Session(graph=graph,config=tf.ConfigProto(device_count={"CPU": 1},allow_soft_placement=True,inter_op_parallelism_threads=1,intra_op_parallelism_threads=1,use_per_session_threads=True))#设置sess
        saver.restore(sess, os.path.join(checkpoint_path, 'B5678-1-60-noise7', ckpt_name))#加载训练的模型
        for imgName in imgsName: #循环处理之前遍历的图像
            blur =cv2.imread(os.path.join(input_path, imgName),-1)#读入图
            h, w = blur.shape
            x=h//512
            #print(x)
            y=w//512
            #print(y)
            if x>y:
                blur = np.pad(blur, ((0, ((x+1)*512 - h)), (0,((x+1)*512 - w))), 'edge') #把图像扩充为512*512的整数倍方便裁切
                after_deblur=np.zeros((((x+1)*512), ((x+1)*512))) #建立相同大小空矩阵
            if x<=y:
                blur = np.pad(blur, ((0, ((y+1)*512 - h)), (0,((y+1)*512 - w))), 'edge')   #把图像扩充为512*512的整数倍方便裁切
                after_deblur=np.zeros((((y+1)*512), ((y+1)*512)))#建立相同大小空矩阵
            #把图像切分成512*512的小图,依次送入神经网络得到结果
            starttotal = time.time()
            for ii in range(x+1):
                for jj in range(y+1):
                    blurPad = blur[ii * 512:(ii + 1) * 512, jj * 512:(jj + 1) * 512]      #按顺序裁切成512*512的图像块
                    blurPad = np.expand_dims(blurPad, -1)
                    blurPad = np.expand_dims(blurPad, 0)
                    if self.args.model != 'color':
                        blurPad = np.transpose(blurPad, (3, 1, 2, 0))
                    start = time.time()
                    deblur = sess.run(outputs, feed_dict={inputs: blurPad / 4095.0})#把图像块送入计算图中sess.run计算
                    duration = time.time() - start
                    res = deblur[-1]
                    res = np.clip(res, a_min=0, a_max=1)
                    if self.args.model != 'color':
                        res = np.transpose(res, (3, 1, 2, 0))
                    res = res[0, :, :, :] * 4095.0
                    res = (res.astype(np.uint16))
                    res = np.squeeze(res)
                    after_deblur = (after_deblur.astype(np.uint16))
                    after_deblur[ii * 512:(ii + 1) * 512, jj * 512:(jj + 1) * 512]=res #用计算得到的结果替换空矩阵相同位置的值
            durationtotal = time.time() - starttotal
            print('total time use %4.3fs' % (durationtotal))
            #print(after_deblur.shape)
            after_deblur = after_deblur[:h, :w] 
            after_deblur = np.clip(after_deblur, a_min=0, a_max=4095)
            #print(after_deblur.shape)
            imtiff = Image.fromarray(after_deblur)
            imtiff.save(os.path.join(output_path,imgName)) #写出图像
        sess.close()
        del sess
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示