如图,在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