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

34 篇文章 3 订阅
29 篇文章 2 订阅

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))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值