阿尔茨海默病分类技术挑战赛批量部署准确率计算
1.将批量部署的结果下载到本地。
结果保存在obs://yzz/output/infer-result-2193b707-9738-46e4-8c30-390812cf59ab
发现不能在OBS跟notebook上载文件夹。单个下载文件比较麻烦。(目前没有找到“全选”)。通过下述代码,把文件夹压缩成.zip文件,再下载到本地。
def zipDir(dirpath,outFullName):
"""
压缩指定文件夹
:param dirpath: 目标文件夹路径
:param outFullName: 压缩文件保存路径+xxxx.zip
:return: 无
"""
zip = zipfile.ZipFile(outFullName,"w",zipfile.ZIP_DEFLATED)
for path,dirnames,filenames in os.walk(dirpath):
# 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
fpath = path.replace(dirpath,'')
for filename in filenames:
zip.write(os.path.join(path,filename),os.path.join(fpath,filename))
zip.close()
zipDir("obs://yzz/output/infer-result-2193b707-9738-46e4-8c30-390812cf59ab", "./result.zip")
得到result.zip,在本地解压
通过下述代码计算准确率
import re
import numpy as np
import pandas as pd
import os
def cal(sub_path):
with open("%s" % sub_path, "r") as f:
data = f.readline()
str = data[13:]
a = re.split(',', str)
a[2] = re.split(']', a[2])[0]
if 'e' in a[0]:
f = a[0].find('e')
c, b = a[0][:f].split('.')
j = int(b) / (pow(10, len(b)))
a0 = (int(c) + j) * (pow(10, int(a[0][f + 1:])))
elif '.' in a[0]:
c, b = a[0].split('.')
j = int(b) * (pow(10, (len(b) * (-1))))
a0 = int(c) + j
else:
a0 = int(a[0])
if 'e' in a[1]:
f = a[1].find('e')
c, b = a[1][:f].split('.')
j = int(b) / (pow(10, len(b)))
a1 = (int(c) + j) * (pow(10, int(a[1][f + 1:])))
elif '.' in a[1]:
c, b = a[1].split('.')
j = int(b) * (pow(10, (len(b) * (-1))))
a1 = int(c) + j
else:
a1 = int(a[1])
if 'e' in a[2]:
f = a[2].find('e')
c, b = a[2][:f].split('.')
j = int(b) / (pow(10, len(b)))
a2 = (int(c) + j) * (pow(10, int(a[2][f + 1:])))
elif '.' in a[2]:
c, b = a[2].split('.')
j = int(b) * (pow(10, (len(b) * (-1))))
a2 = int(c) + j
else:
a2 = int(a[2])
a = [a0, a1, a2]
return a.index(max(a))
# cal(r"./result\Subject_0001.npy_result.txt")
def cal_train(subject_dir, csv_path):
df = pd.read_csv(csv_path, index_col=0)
subjects = os.listdir(subject_dir)
# print(subjects)
x = []
y = []
for subject in subjects:
features_path = os.path.join(subject_dir, subject)
if not os.path.exists(features_path) or not features_path.endswith('txt'):
continue
else:
row = df.loc[subject.split('.')[0][:13]]
label = int(row['Label'])
test = cal(features_path)
x.append(test)
y.append(label)
x = np.array(x)
y = np.array(y)
return x, y
train_label, original_label = cal_train(r'./result', r'./train_open.csv')
print(train_label)
print(original_label)
#
# def acc(test_label, original_label):
# num_correct = (test_label == original_label).sum().item()
# return num_correct / len(test_label)
#
#
# print(acc(train_x, train_y))
num_correct = (train_label == original_label).sum().item()
# print(num_correct)
print(num_correct / len(train_label))
# print(len(train_label), len(original_label))
本文例子结果如下