本文主要是介绍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()方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!