实验基于论文: Class-Balanced Loss Based on Effective Number of Samples
论文解读:https://blog.csdn.net/weixin_41735859/article/details/105637597
Class-balanced-loss代码地址:https://github.com/vandit15/Class-balanced-loss-pytorch
resnet18代码参考链接:https://blog.csdn.net/sunqiande88/article/details/80100891
第一步:制作数据集
论文中通过公式 n = n i u i n = n_iu^i n=niui, i i i为类索引.制作长尾cifar10数据集.以下代码以不均匀比例100为例.论文作者制作好的数据集,我们也可以通过科学上网点击该谷歌云链接下载.
loadcifar.py
import torch
import torch.utils.data as Data
import torchvision.transforms as transforms
import numpy as np
from PIL import Image
def unpickle(file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
# 从源文件读取数据
# 返回 train_data[12406,3072]和labels[12406]
# test_data[10000,3072]和labels[10000]
def get_data(train=False):
data = None
labels = None
new_data = None
new_labels = []
if train == True:
for i in range(1, 6):
batch = unpickle('data/cifar-10-batches-py/data_batch_' + str(i))
if i == 1:
data = batch[b'data']
labels = batch[b'labels']
else:
data = np.concatenate([data, batch[b'data']])
labels = np.concatenate([labels, batch[b'labels']])
count = np.zeros((10),dtype=np.int)
for i in range(len(labels)):
labels[i] = labels[i].reshape(1,1)
data[i] = data[i].reshape((1,3072))
# 设置 n = n_iu^i
if count[labels[i]] < int(np.floor(5000 * ((1 / 100) ** (1 / 9)) ** (labels[i]))):
count[labels[i]] += 1
if i == 0:
new_data = data[i]
else:
new_data = np.concatenate([new_data,data[i]])
new_labels.append(labels[i])
else:
continue
new_labels = np.array(new_labels)
new_data = new_data.reshape(-1,3072)
else:
batch = unpickle('data/cifar-10-batches-py/test_batch')
new_data = batch[b'data']
new_labels = batch[b'labels']
return new_data, new_labels
# 图像预处理函数,Compose会将多个transform操作包在一起
# 对于彩色图像,色彩通道不存在平稳特性
transform = transforms.Compose([
# ToTensor是指把PIL.Image(RGB) 或者numpy.ndarray(H x W x C)
# 从0到255的值映射到0到1的范围内,并转化成Tensor格式。
transforms.ToTensor(),
# Normalize函数将图像数据归一化到[-1,1]
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
# 将标签转换为torch.LongTensor
def target_transform(label):
label = np.array(label)
target = torch.from_numpy(label).long()
return target
'''
自定义数据集读取框架来载入cifar10数据集
需要继承data.Dataset
'''
class Cifar10_Dataset(Data.Dataset):
def __init__(self, train=True, transform=None, target_transform=None):
# 初始化文件路径
self.transform = transform
self.target_transform = target_transform
self.train = train
# 载入训练数据集
if self.train:
self.train_data, self.train_labels = get_data(train)
num = self.train_data.shape[0]
self.train_data = self.train_data.reshape((num, 3, 32, 32))
# 将图像数据格式转换为[height,width,channels]方便预处理
self.train_data = self.train_data.transpose((0, 2, 3, 1))
# 载入测试数据集
else:
self.test_data, self.test_labels = get_data()
self.test_data = self.test_data.reshape((10000, 3, 32, 32))
self.test_data = self.test_data.transpose((0, 2, 3, 1))
pass
def __getitem__(self, index):
# 从数据集中读取一个数据并对数据进行
# 预处理返回一个数据对,如(data,label)
if self.train:
img, label = self.train_data[index], self.train_labels[index]
else:
img, label = self.test_data[index], self.test_labels[index]
img = Image.fromarray(img)
# 图像预处理
if self.transform is not None:
img = self.transform(img)
# 标签预处理
if self.target_transform is not None:
target = self.target_transform(label)
return img, target
def __len__(self):
# 返回数据集的size
if self.train:
return len(self.train_data)
else:
return len(self.test_data)
if __name__ == '__main__':
# 读取训练集和测试集
train_data = Cifar10_Dataset(True, transform, target_transform)
print('size of train_data:{}'.format(train_data.__len__()))
test_data = Cifar10_Dataset(False, transform, target_transform)
print('size of test_data:{}'.format(test_data.__len__()))
第二步:定义损失函数
源码链接:https://github.com/vandit15/Class-balanced-loss-pytorch
要注意的是,源码需要修改,不然无法调用gpu.
修改好的代码为:
class_balanced_loss.py
import numpy as np
import torch
import torch.nn.functional as F
from torch.optim import lr_scheduler
import torch.optim as optim
def focal_loss(labels, logits, alpha, gamma):
"""Compute the focal loss between `logits` and the ground truth `labels`.
Focal loss = -alpha_t * (1-pt)^gamma * log(pt)
where pt is the probability of being classified to the true class.
pt = p (if true class), otherwise pt = 1 - p. p = sigmoid(logit).
Args:
labels: A float tensor of size [batch, num_classes].
logits: A float tensor of size [batch, num_classes].
alpha: A float tensor of size [batch_size]
specifying per-example weight for balanced cross entropy.
gamma: A float scalar modulating loss from hard and easy examples.
Returns:
focal_loss: A float32 scalar representing normalized total loss.
"""
BCLoss = F.binary_cross_entropy_with_logits(input = logits, target = labels,reduction = "none")
if gamma == 0.0:
modulator = 1.0
else:
modulator = torch.exp(-gamma * labels * logits - gamma * torch.log(1 +
torch.exp(-1.0 * logits)))
loss = modulator * BCLoss
weighted_loss = alpha * loss
focal_loss = torch.sum(weighted_loss)
focal_loss /= torch.sum(labels)
return focal_loss
def CB_loss(labels, logits, samples_per_cls, no_of_classes, loss_type, beta, gamma):
"""Compute the Class Balanced Loss between `logits` and the ground truth `labels`.
Class Balanced Loss: ((1-beta)/(1-beta^n))*Loss(labels, logits)
where Loss is one of the standard losses used for Neural Networks.
Args:
labels: A int tensor of size [batch].
logits: A float tensor of size [batch, no_of_classes].
samples_per_cls: A python list of size [no_of_classes].
no_of_classes: total number of classes. int
loss_type: string. One of "sigmoid", "focal", "softmax".
beta: float. Hyperparameter for Class balanced loss.
gamma: float. Hyperparameter for Focal loss.
Returns:
cb_loss: A float tensor representing class balanced loss
"""
effective_num = 1.0 - np.power(beta, samples_per_cls)
weights = (1.0 - beta) / np.array(effective_num)
weights = weights / np.sum(weights) * no_of_classes
# print(weights.shape)
labels_one_hot = F.one_hot(labels, no_of_classes).float().cuda()
# print(labels_one_hot.shape)
weights = torch.tensor(weights).float()
# 增加维度
weights = weights.unsqueeze(0).cuda()
# print(weights)
# labels_one_hot.shape[0] -- batch_size
weights = weights.repeat(labels_one_hot.shape[0],1) * labels_one_hot
weights = weights.sum(1)
weights = weights.unsqueeze(1)
weights = weights.repeat(1,no_of_classes)
if loss_type == "focal":
cb_loss = focal_loss(labels_one_hot, logits, weights, gamma)
elif loss_type == "sigmoid":
cb_loss = F.binary_cross_entropy_with_logits(input = logits,target = labels_one_hot, weight = weights)
elif loss_type == "softmax":
pred = logits.softmax(dim = 1)
cb_loss = F.binary_cross_entropy(input = pred, target = labels_one_hot, weight = weights)
return cb_loss
第三步:训练
cifartrain.py
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import argparse
from resnet18 import ResNet18
import os
from class_balanced_loss import CB_loss
import numpy as np
import torch.nn.functional as F
from loadcifar import Cifar10_Dataset
from torch.optim import lr_scheduler
from sigmoidCE import sigmoidlose
# 定义是否使用GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 参数设置,使得我们能够手动输入命令行参数,就是让风格变得和Linux命令行差不多
parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
parser.add_argument('--outf', default='./model/', help='folder to output images and model checkpoints') #输出结果保存路径
args = parser.parse_args()
# 超参数设置
EPOCH = 200 #遍历数据集次数
pre_epoch = 0 # 定义已经遍历数据集的次数
BATCH_SIZE = 128 #批处理尺寸(batch_size)
LR = 0.1 #学习率
# 准备数据集并预处理
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4), #先四周填充0,在吧图像随机裁剪成32*32
transforms.RandomHorizontalFlip(), #图像一半的概率翻转,一半的概率不翻转
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), #R,G,B每层的归一化用到的均值和方差
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
def target_transform(label):
label = np.array(label)
target = torch.from_numpy(label).long()
return target
# trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train) #训练数据集
# trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2) #生成一个个batch进行批训练,组成batch的时候顺序打乱取
trainset = Cifar10_Dataset(True, transform=transform_train, target_transform=target_transform )#训练数据集
trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2) #生成一个个batch进行批训练,组成batch的时候顺序打乱取
print('size of train_data:{}'.format(trainset.__len__()))
testset = Cifar10_Dataset(False, transform=transform_test, target_transform=target_transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2)
print('size of testset_data:{}'.format(testset.__len__()))
# Cifar-10的标签
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
# 模型定义-ResNet
net = ResNet18().to(device)
# 定义损失函数和优化方式
optimizer = optim.SGD(net.parameters(), lr=LR, momentum=0.9, weight_decay=5e-4) #优化方式为mini-batch momentum-SGD,并采用L2正则化(权重衰减)
scheduler = lr_scheduler.MultiStepLR(optimizer, [160, 180], 0.1)
# 训练
if __name__ == "__main__":
num = []
for i in range(10):
num.append(int(np.floor(5000 * ((1 / 100) ** (1 / 9)) ** (i))))
num = np.array(num)
# num = torch.from_numpy(num)
# num = num.to(device)
if not os.path.exists(args.outf):
os.makedirs(args.outf)
best_acc = 0 #2 初始化best test accuracy
print("Start Training, Resnet-18!") # 定义遍历数据集的次数
with open("acc.txt", "w") as f:
with open("log.txt", "w")as f2:
for epoch in range(pre_epoch, EPOCH):
scheduler.step()
print('\nEpoch: %d' % (epoch + 1))
net.train()
sum_loss = 0.0
correct = 0.0
total = 0.0
for i, data in enumerate(trainloader, 0):
# 准备数据
length = len(trainloader)
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
# forward + backward
outputs = net(inputs)
loss = CB_loss(labels = labels, logits = outputs,
samples_per_cls = num, no_of_classes = 10,
loss_type = "sigmoid", beta = 0.9999, gamma=2)
# loss = sigmoidlose(labels, outputs)
loss.backward()
optimizer.step()
# 每训练1个batch打印一次loss和准确率
sum_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += predicted.eq(labels.data).cpu().sum()
print('[epoch:%d, iter:%d] Loss: %.03f | Acc: %.3f%% | Lr: %.03f'
% (epoch + 1, (i + 1 + epoch * length), sum_loss / (i + 1),
100. * float(correct) / total, optimizer.state_dict()['param_groups'][0]['lr']))
f2.write('%03d %05d |Loss: %.03f | Acc: %.3f%% | Lr: %.03f'
% (epoch + 1, (i + 1 + epoch * length), sum_loss / (i + 1),
100. * float(correct) / total, optimizer.state_dict()['param_groups'][0]['lr']))
f2.write('\n')
f2.flush()
# 每训练完一个epoch测试一下准确率
print("Waiting Test!")
with torch.no_grad():
correct = 0
total = 0
for data in testloader:
net.eval()
images, labels = data
images, labels = images.to(device), labels.to(device)
outputs = net(images)
# 取得分最高的那个类 (outputs.data的索引号)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
print('测试分类准确率为:%.3f%%' % (100 * float(correct) / total))
acc = 100. * float(correct) / total
# 将每次测试结果实时写入acc.txt文件中
print('Saving model......')
torch.save(net.state_dict(), '%s/net_%03d.pth' % (args.outf, epoch + 1))
f.write("EPOCH=%03d,Accuracy= %.3f%%| Lr: %.03f" % (epoch + 1, acc,
optimizer.state_dict()['param_groups'][0]['lr']))
f.write('\n')
f.flush()
# 记录最佳测试分类准确率并写入best_acc.txt文件中
if acc > best_acc:
f3 = open("best_acc.txt", "w")
f3.write("EPOCH=%d,best_acc= %.3f%%" % (epoch + 1, acc))
f3.close()
best_acc = acc
print("Training Finished, TotalEPOCH=%d" % EPOCH)
论文中不平衡因子为100,基准网络为 ResNet-32 的的情况下,使用sigmoid损失函数时,分类错误率为29.55%;使用类平衡损失函数时,分类错误率为:25.43%.准确率提升约 4 个百分点。
本博客中使用的基准网络为 Res-Net18 实验结果,使用sigmoid损失函数时,分类错误率为26.64%;使用类平衡损失函数时,分类错误率为:25.85%.准确率提升约 1 个百分点。表明类平衡损失函数对长尾数据集分类准确度有所提升,但是由于基准网络不同,效果没有论文中那么明显。