TopK排序三种办法的性能比较和sort()方法

2024-05-24 07:32

本文主要是介绍TopK排序三种办法的性能比较和sort()方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

TopK排序三种办法的性能比较

start = time.time()
fo = open(resultname, “w”)
task = n2cube.dpuCreateTask(kernel, 0)
print("\n")
height, width, channel, mean = preprocess.parameter(task, KERNEL_CONV_INPUT)
#print(“outputMean = %f”%mean[0])
for i in range(imagenumber):
path = os.path.join(image_folder, listimage[i])
img = cv2.imread(path)
softmax = predict_label(img, task, scale, mean, height, width, channel)
TopK(softmax,listimage[i],fo)
fo.close()
n2cube.dpuDestroyTask(task)
end = time.time()
fps = float(imagenumber/(end-start))
latency = 1000/fps
print("\n%.2f FPS\n" % fps)
print(“latency = %f ms\n” % latency)

第一种

 def TopK(softmax,imagename,fo):q = queue.PriorityQueue()for i in range(len(softmax)):q.put((-softmax[i],i))print(imagename)for i in range(top):ki = q.get()print("ki[0] = %f   ki[1] = %d"%(-ki[0],ki[1])) print('top{} prob = {} name = {}'.format(i, -ki[0],lines[ki[1]]))fo.write(imagename+" "+str(ki[1])+"\n") 

第二种

cnt = [i for i in range(outputchannel)]
def TopK(softmax,imagename, cnt, fo):print(imagename)pair = zip(softmax, cnt)pair = sorted(pair, reverse=True)softmax_new1, cnt_new = zip(*pair)for i in range(top):fo.write(imagename+" "+str(cnt_new[i])+"\n")          print("cnt_new = %d" % cnt_new[i])print("softmax_new1 = %f" % softmax_new1[i])'''

第三种

def TopK(softmax,imagename, cnt, fo):print(imagename)for i in range(top):num = np.argmax(softmax)fo.write(imagename+" "+str(num)+"\n")  print("num = %d" % num)print("softmax = %f" % softmax[num])softmax[num] = 0

int maxArrayIntCount(float* arr, int size)
{
int nCount = 0;
for (int i = 1; i < size; i++)
{
if (arr[i] > arr[nCount])
{
nCount = i;
}
}
return nCount;
}

void TopK(float *d, int size, int k, string imageName) {
//void TopK(const float *d, int size, int k, vector<string> &vkinds, string imgname) {
//  cout << "Load image : " << imageName << endl;int num;for (auto i = 0; i < k; ++i) {num = maxArrayIntCount(d, size);out << imageName << " " << num << endl;d[num] = 0;
//    cout << "number = " << num << endl; }   
}  

sort()

>>> model_path="/home/john/Vitis-AI_1.1/savertest/cifar/build/keras_model"
>>> listfile = [i for i in os.listdir(model_path) if i.endswith("h5")]
>>> listfile
['epoch.006.val_acc.0.17.h5', 'epoch.003.val_acc.0.10.h5', 'epoch.002.val_acc.0.12.h5', 'epoch.005.val_acc.0.15.h5', 'epoch.001.val_acc.0.10.h5', 'epoch.007.val_acc.0.19.h5', 'epoch.004.val_acc.0.14.h5']
>>> listfile.sort()
>>> listfile
['epoch.001.val_acc.0.10.h5', 'epoch.002.val_acc.0.12.h5', 'epoch.003.val_acc.0.10.h5', 'epoch.004.val_acc.0.14.h5', 'epoch.005.val_acc.0.15.h5', 'epoch.006.val_acc.0.17.h5', 'epoch.007.val_acc.0.19.h5']
>>> listfile[::-1]
['epoch.007.val_acc.0.19.h5', 'epoch.006.val_acc.0.17.h5', 'epoch.005.val_acc.0.15.h5', 'epoch.004.val_acc.0.14.h5', 'epoch.003.val_acc.0.10.h5', 'epoch.002.val_acc.0.12.h5', 'epoch.001.val_acc.0.10.h5']

listfile[::-1]必须赋值新变量,因为不会改变自身

model_path=keras_hdf5
listfile = [i for i in os.listdir(model_path) if i.endswith("h5")] 
print("listfile = {}".format(listfile))
listfile.sort()
listfile=listfile[::-1]
print("listfile = {}".format(listfile))

这篇关于TopK排序三种办法的性能比较和sort()方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/997765

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Python版本与package版本兼容性检查方法总结

《Python版本与package版本兼容性检查方法总结》:本文主要介绍Python版本与package版本兼容性检查方法的相关资料,文中提供四种检查方法,分别是pip查询、conda管理、PyP... 目录引言为什么会出现兼容性问题方法一:用 pip 官方命令查询可用版本方法二:conda 管理包环境方法

Linux云服务器手动配置DNS的方法步骤

《Linux云服务器手动配置DNS的方法步骤》在Linux云服务器上手动配置DNS(域名系统)是确保服务器能够正常解析域名的重要步骤,以下是详细的配置方法,包括系统文件的修改和常见问题的解决方案,需要... 目录1. 为什么需要手动配置 DNS?2. 手动配置 DNS 的方法方法 1:修改 /etc/res

JavaScript对象转数组的三种方法实现

《JavaScript对象转数组的三种方法实现》本文介绍了在JavaScript中将对象转换为数组的三种实用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友... 目录方法1:使用Object.keys()和Array.map()方法2:使用Object.entr

SpringBoot中ResponseEntity的使用方法举例详解

《SpringBoot中ResponseEntity的使用方法举例详解》ResponseEntity是Spring的一个用于表示HTTP响应的全功能对象,它可以包含响应的状态码、头信息及响应体内容,下... 目录一、ResponseEntity概述基本特点:二、ResponseEntity的基本用法1. 创

java中判断json key是否存在的几种方法

《java中判断jsonkey是否存在的几种方法》在使用Java处理JSON数据时,如何判断某一个key是否存在?本文就来介绍三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目http://www.chinasem.cn录第一种方法是使用 jsONObject 的 has 方法

java中ssh2执行多条命令的四种方法

《java中ssh2执行多条命令的四种方法》本文主要介绍了java中ssh2执行多条命令的四种方法,包括分号分隔、管道分隔、EOF块、脚本调用,可确保环境配置生效,提升操作效率,具有一定的参考价值,感... 目录1 使用分号隔开2 使用管道符号隔开3 使用写EOF的方式4 使用脚本的方式大家平时有没有遇到自