✨✨ 欢迎大家来访Srlua的博文(づ ̄3 ̄)づ╭❤~✨✨🌟🌟 欢迎各位亲爱的读者,感谢你们抽出宝贵的时间来阅读我的文章。
我是Srlua小谢,在这里我会分享我的知识和经验。🎥
希望在这里,我们能一起探索IT世界的奥妙,提升我们的技能。🔮
记得先点赞👍后阅读哦~ 👏👏
📘📚 所属专栏:人工智能、话题分享
欢迎访问我的主页:Srlua小谢 获取更多信息和资源。✨✨🌙🌙
概述
本文复现论文 Progressive Identification of True Labels for Partial-Label Learning[1] 提出的偏标记学习方法。
随着深度神经网络的发展,机器学习任务对标注数据的需求不断增加。然而,大量的标注数据十分依赖人力资源与标注者的专业知识。弱监督学习可以有效缓解这一问题,因其不需要完全且准确的标注数据。该论文关注一个重要的弱监督学习问题——偏标记学习(Partial Label Learning),其中每个训练实例与一组候选标签相关联,但仅有一个标签是真实的。
该论文提出了一种渐进式真实标签识别方法,旨在训练过程中逐渐确定样本的真实标签。该论文所提出的方法获得了接近监督学习的性能,且与具体的网络结构、损失函数、随机优化算法无关。
算法原理
传统的监督学习常用交叉熵损失和随机梯度下降来优化深度神经网络。交叉熵损失定义如下:
LCE(x,y;θ)=∑i=1cyilogfi(x;θ)LCE(x,y;θ)=i=1∑cyilogfi(x;θ)
其中, xx 表示样本特征;y=[y1,y2,…,yc]y=[y1,y2,…,yc] 表示样本标签,其为独热码,即除了真实标签对应维度值为 1,其余为零;fi(x;θ)fi(x;θ) 表示模型预测样本 xx 标签为 ii 的概率。
该论文提出的方法使用一个软标签 y^=[y^1,y^2,…,y^c]y^=[y^1,y^2,…,y^c],其对任意 i∈[0,c]i∈[0,c] 满足 ∑iy^i=1∑iy^i=1 且 0≤y^i≤10≤y^i≤1。为了使用该软标签,论文根据候选标签集 ss 对软标签进行初始化:
y^i={1∣s∣i∈s0i∉sy^i={∣s∣10i∈si∈s
为了渐进式地识别真实标签,算法在每次更新参数之前,根据预测结果为下轮训练使用的软标签赋值:
y^i=fi(x;θ)I(i∈s)∑jfj(x;θ)I(j∈s)y^i=∑jfj(x;θ)I(j∈s)fi(x;θ)I(i∈s)
其中,I(j∈s)=1I(j∈s)=1 当且仅当 j∈sj∈s 为真,否则 I(j∈s)=0I(j∈s)=0。
核心逻辑
具体的核心逻辑如下所示:
import models
import datasets
import torch
from torch.utils.data import DataLoader
import numpy as np
import torch.optim as optim
from torch.optim.lr_scheduler import StepLR
import torch.nn.functional as F
import torchvision.transforms as transforms
from tqdm import tqdmdef CE_loss(probs, targets):"""交叉熵损失函数"""loss = -torch.sum(targets * torch.log(probs), dim = -1)loss_avg = torch.sum(loss)/probs.shape[0]return loss_avgclass Proden:def __init__(self, configs):self.configs = configsdef train(self, save = False):configs = self.configs# 读取数据集dataset_path = configs['dataset path']if configs['dataset'] == 'CIFAR-10':train_data, train_labels, test_data, test_labels = datasets.cifar10_read(dataset_path)train_dataset = datasets.Cifar(train_data, train_labels)test_dataset = datasets.Cifar(test_data, test_labels)output_dimension = 10elif configs['dataset'] == 'CIFAR-100':train_data, train_labels, test_data, test_labels = datasets.cifar100_read(dataset_path)train_dataset = datasets.Cifar(train_data, train_labels)test_dataset = datasets.Cifar(test_data, test_labels)output_dimension = 100# 生成偏标记partial_labels = datasets.generate_partial_labels(train_labels, configs['partial rate'])train_dataset.load_partial_labels(partial_labels)# 计算数据的均值和方差,用于模型输入的标准化mean = [np.mean(train_data[:, i, :, :]) for i in range(3)]std = [np.std(train_data[:, i, :, :]) for i in range(3)]normalize = transforms.Normalize(mean, std)# 设备:GPU或CPUdevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')# 加载模型if configs['model'] == 'ResNet18':model = models.ResNet18(output_dimension = output_dimension).to(device)elif configs['model'] == 'ConvNet':model = models.ConvNet(output_dimension = output_dimension).to(device)# 设置学习率等超参数lr = configs['learning rate']weight_decay = configs['weight decay']momentum = configs['momentum']optimizer = optim.SGD(model.parameters(), lr = lr, weight_decay = weight_decay, momentum = momentum)lr_step = configs['learning rate decay step']lr_decay = configs['learning rate decay rate']lr_scheduler = StepLR(optimizer, step_size=lr_step, gamma=lr_decay)for epoch_id in range(configs['epoch count']):# 训练模型train_dataloader = DataLoader(train_dataset, batch_size = configs['batch size'], shuffle = True)model.train()for batch in tqdm(train_dataloader, desc='Training(Epoch %d)' % epoch_id, ascii=' 123456789#'):ids = batch['ids']# 标准化输入data = normalize(batch['data'].to(device))partial_labels = batch['partial_labels'].to(device)targets = batch['targets'].to(device)optimizer.zero_grad()# 计算预测概率logits = model(data)probs = F.softmax(logits, dim=-1)# 更新软标签with torch.no_grad():new_targets = F.normalize(probs * partial_labels, p=1, dim=-1)train_dataset.targets[ids] = new_targets.cpu().numpy()# 计算交叉熵损失loss = CE_loss(probs, targets)loss.backward()# 更新模型参数optimizer.step()# 调整学习率lr_scheduler.step()
以上代码仅作展示,更详细的代码文件请参见附件。
效果演示
我提前在 CIFAR-10[2] 数据集和 12 层的 ConvNet[3] 网络上训练了一份模型参数。为了测试其准确率,需要配置环境并运行main.py脚本,得到结果如下:
由图可见,该算法在测试集上获得了 89.8% 的准确率。
进一步地,测试训练出的模型在真实图片上的预测结果。在线部署模型后,将一张轮船的图片输入,可以得到输出的预测类型为 “Ship”:
我所使用的数据集(CIFAR-10)共包含十个类,示意图如下:
网站提供了在线演示功能,使用者请输入一张小于1MB、类别为上述十个类别之一、长宽尽可能相等的JPG图像。
使用方式
- 解压附件压缩包并进入工作目录。如果是Linux系统,请使用如下命令:
unzip Proden-implemention.zip
cd Proden-implemention
- 代码的运行环境可通过如下命令进行配置:
pip install -r requirements.txt
- 运行如下命令以下载并解压数据集
bash download.sh
- 如果希望在本地训练模型,请运行如下命令:
python main.py -c [你的配置文件路径] -r [选择下者之一:"train"、"test"、"infer"]
- 如果希望在线部署,请运行如下命令:
python main-flask.py
参考文献
[1] Lv J, Xu M, Feng L, et al. Progressive identification of true labels for partial-label learning[C]//International conference on machine learning. PMLR, 2020: 6500-6510.
[2] Krizhevsky A, Hinton G. Learning multiple layers of features from tiny images[J]. 2009.
[3] Laine S, Aila T. Temporal ensembling for semi-supervised learning[J]. arXiv preprint arXiv:1610.02242, 2016.