ML helper functions

Numpy与Torch对应函数

Numpy Torch 作用
np.random.permutation torch.randperm 返回随机打乱的index
np.newaxis torch.unsqueeze 增加一个维度
np.ravel torch.ravel 将一个tensor打平
np.concatenate torch.cat 将多个tensor按照某个维度连接起来
np.stack torch.stack 多个tensor拼起来,并放在一个新的维度上
np.transpose torch.permute 调换维度

增加维度

data[None, ...]

sort reverse

a[::-1].sort()

ECE

def cal_ece(self, logits, labels):
        n_bins = 10
        bin_boundaries = np.linspace(0, 1, n_bins + 1)
        self.bin_lowers = bin_boundaries[:-1]
        self.bin_uppers = bin_boundaries[1:]
        softmaxes = F.softmax(logits, dim=1)
        confidences, predicts = torch.max(softmaxes, dim=1)
        accuracies = predicts.eq(labels)
        ece = torch.zeros(1, device=logits.device)
        for bin_lower, bin_upper in zip(self.bin_lowers, self.bin_uppers):
            # Calculated |confidence - accuracy| in each bin
            in_bin = confidences.gt(bin_lower.item()) * confidences.le(bin_upper.item())
            prop_in_bin = in_bin.float().mean()
            if prop_in_bin.item() > 0:
                accuracy_in_bin = accuracies[in_bin].float().mean()
                avg_confidence_in_bin = confidences[in_bin].mean()
                ece += torch.abs(avg_confidence_in_bin - accuracy_in_bin) * prop_in_bin
        return ece

Read csv

def read_result(file):
    data = pd.read_csv(open(file,"r"),delimiter=",",skiprows=0,usecols=[1,2,3])
    all_scores = []
    all_labels = []
    all_predicts = []
    for _, item in data.iterrows():
        all_labels.append(int(item[1] == 'EBV'))
        val = item[2]
#         print(val.split(' ')[0][1:], val.split(' ')[1][:-1])
        scores = [float(re.split(r'\s+', val)[0][1:]), float(re.split(r'\s+', val)[1][:-1])]
        predict = np.argmax(scores)
        all_scores.append(scores)
        all_predicts.append(predict)
    return np.array(all_scores), np.array(all_labels), np.array(all_predicts)

Plot PIL image

import matplotlib.pyplot as plt
plt.imshow(img)

Plot multiple images

row = col = 5
plt.figure(figsize=(20, 20))
j = 0
for index in all_index[:25]:
    if j >= row * col:
        break
    img, label = cifar10_clean[int(index)]
    plt.subplot(row, col, j + 1)
    plt.imshow(img)
    plt.title('label: {}'.format(cifar10_clean.classes[label]))
    plt.axis('off')
    j += 1
plt.show()