def run_numpy_pb(): args = parse_arg() config = tf.ConfigProto(allow_soft_placement=True, inter_op_parallelism_threads=1, intra_op_parallelism_threads=1) config.mlu_options.save_offline_model = True model_name = os.path.basename(args.numpy_pb).split(".")[0] image_name = os.path.basename(args.image).split(".")[0] g = tf.Graph() with g.as_default(): with tf.gfile.FastGFile(args.numpy_pb,'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) tf.import_graph_def(graph_def, name='') img = cv.imread(args.image) X = cv.resize(img, (256, 256)) with tf.Session(config=config) as sess: sess.graph.as_default() sess.run(tf.global_variables_initializer()) input_tensor1 = sess.graph.get_tensor_by_name('X_content:0') input_tensor2 = sess.graph.get_tensor_by_name('moments_15/PowerDifference:0') output_tensor = sess.graph.get_tensor_by_name('add_37:0') start_time = time.time() input_2 = power_diff_numpy(sess.graph.get_tensor_by_name('Conv2D_13:0').eval(feed_dict={input_tensor1:[X]}),sess.graph.get_tensor_by_name('moments_15/StopGradient:0').eval(feed_dict={input_tensor1:[X]}),2) ret = sess.run(output_tensor, feed_dict={input_tensor1:[X], input_tensor2:input_2}) end_time = time.time() print("Numpy inference(MLU) time is: ",end_time-start_time) img1 = tf.reshape(ret,[256,256,3]) img_numpy = img1.eval(session=sess) cv.imwrite(image_name + '_' + model_name + '_mlu.jpg',img_numpy)
def main(): value = 256 start = time.time() input_x = np.loadtxt("../data/in_x.txt") input_y = np.loadtxt("../data/in_y.txt") input_pow = np.loadtxt("../data/in_z.txt") output = np.loadtxt("../data/out.txt") end = time.time() print("load data cost " + str((end - start)*1000) + "ms") input_x = np.reshape(input_x,(1,value,value,3)) input_y = np.reshape(input_y,(1,1,1,3)) output = np.reshape(output, (-1)) start = time.time() res = power_difference_op(input_x, input_y, input_pow) end = time.time() - start print("comput C++ op cost "+ str(end*1000) + "ms" ) res = np.reshape(res,(-1)) err = sum(abs(res - output))/sum(output) print("C++ op err rate= " + str(err*100)) start = time.time() res = power_diff_numpy(input_x, input_y,input_pow) end = time.time() print("comput numpy op cost "+ str((end-start)*1000) + "ms") res = np.reshape(res,(-1)) err = sum(abs(res - output))/sum(output) print("numpy op err rate= "+ str(err*100))
def main(): value = 256 input_x = np.random.randn(1, value, value, 3) input_y = np.random.randn(1, 1, 1, 3) input_pow = np.zeros((2)) start = time.time() res = power_difference_op(input_x, input_y, input_pow) end = time.time() - start print("comput BCL op cost " + str(end * 1000) + "ms") start = time.time() output = power_diff_numpy(input_x, input_y, len(input_pow)) end = time.time() res = np.reshape(res, (-1)) output = np.reshape(output, (-1)) print("comput op cost " + str((end - start) * 1000) + "ms") err = sum(abs(res - output)) / sum(output) print("err rate= " + str(err * 100))
def run_numpy_pb(numpy_pb, image): config = tf.ConfigProto(allow_soft_placement=True, inter_op_parallelism_threads=1, intra_op_parallelism_threads=1) model_name = os.path.basename(numpy_pb).split(".")[0] image_name = os.path.basename(image).split(".")[0] g = tf.Graph() with g.as_default(): with tf.gfile.FastGFile(numpy_pb,'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) tf.import_graph_def(graph_def, name='') img = cv.imread(image) X = cv.resize(img, (256, 256)) with tf.Session(config=config) as sess: # TODO:完成Numpy版本 Pb模型的推理 sess.run(tf.global_variables_initializer()) # 根据输入名称获得输入的tensor input_tensor = sess.graph.get_tensor_by_name('X_content:0') # 获取两个输出节点, 作为numpy算子的输入 out_tmp_tensor_1 = sess.graph.get_tensor_by_name('Conv2D_13:0') out_tmp_tensor_2 = sess.graph.get_tensor_by_name('moments_15/StopGradient:0') start_time = time.time() # 执行第一次sessionrun, 得到numpy算子的两个输入值,注意此时两个输入的shape不同 input_x, input_y = sess.run([out_tmp_tensor_1, out_tmp_tensor_2], feed_dict={input_tensor:[X]}) input_pow = 2 output = power_diff_numpy(input_x, input_y, input_pow).reshape(1, 256, 256, 3) # 根据添加的输入节点名称获得输入tensor input_tensor_new = sess.graph.get_tensor_by_name('moments_15/PowerDifference:0') # 完整推理最终输出的tensor output_tensor = sess.graph.get_tensor_by_name('add_37:0') # 执行第二次session run,输入图片数据以及上一步骤numpy计算的数据 ret = sess.run(output_tensor, feed_dict={input_tensor:[X], input_tensor_new: output}) end_time = time.time() print("Numpy inference(CPU) time is: ",end_time-start_time) img1 = tf.reshape(ret,[256,256,3]) img_numpy = img1.eval(session=sess) cv.imwrite(image_name + '_' + model_name + '_cpu.jpg',img_numpy)