论文复现:Active Learning with the Furthest NearestNeighbor Criterion for Facial Age Estimation

本文主要是介绍论文复现:Active Learning with the Furthest NearestNeighbor Criterion for Facial Age Estimation,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Furthest Nearest Neighbor 方法就是其他文章中的Descripency方法,是一种diversity samplig方法。 

 

由于特征空间是不断变化的,在特征空间上使用Descripency方法违背了该准则的初衷。

import os
import torch
import numpy as np
from copy import deepcopy
from collections import OrderedDict
from PIL import Image
from sklearn.model_selection import StratifiedKFoldclass FNN_2DLDA(object):def __init__(self, X_train, y_train, labeled, budget, X_test, y_test):self.X = X_trainself.y = y_trainself.X_test = X_testself.y_test = y_testself.nSample = X_train.shape[0]print("样本个数=",self.nSample)self.labeled = list(deepcopy(labeled))     # 已标记样本的索引self.unlabeled = self.init_unlabeled_index()self.labels = np.sort(np.unique(y_train))  # 标签列表self.nClass = len(self.labels)self.budget = deepcopy(budget)self.nRow, self.nCol = self.X[0].shape     # 图像样本的行数和列数self.K = 10self.class_mean, self.global_mean, self.class_count = self.get_init_mean()self.S_bl, self.S_br, self.S_wl, self.S_wr = self.get_init_Sbl_Sbr_Swl_Swr()self.Wl, self.Wr = self.get_Wl_Wr()self.X_feature = self.get_feature()self.batch_size = 5def init_unlabeled_index(self):# =============无标记样本索引===============unlabeled = [i for i in range(self.nSample)]for idx in self.labeled:unlabeled.remove(idx)return unlabeleddef get_init_mean(self):class_mean = torch.zeros((self.nClass, self.nRow, self.nCol))class_count = torch.zeros(self.nClass)global_mean = torch.zeros((self.nRow, self.nCol))# ========计算各类样本中心========for i in range(self.nClass):# ==获取第i个类的样本的索引==ids = []for idx in self.labeled:if self.y[idx] == self.labels[i]:ids.append(idx)class_count[i] = len(ids)class_mean[i] = torch.mean(self.X[ids], dim=0)# ==========计算全局样本中心============for i in range(self.nClass):global_mean += (class_count[i] / len(self.labeled)) * class_mean[i]return class_mean, global_mean, class_countdef get_init_Sbl_Sbr_Swl_Swr(self):# =============计算Sbl和Sbr=================S_bl = torch.zeros((self.nCol, self.nCol))S_br = torch.zeros((self.nRow, self.nRow))for i in range(self.nClass):tmp = self.class_mean[i] - self.global_meanS_bl += self.class_count[i] * torch.mm(tmp.T,tmp)S_br += self.class_count[i] * torch.mm(tmp, tmp.T)# =============计算Swl和Swr=================S_wl = torch.zeros((self.nCol, self.nCol))S_wr = torch.zeros((self.nRow, self.nRow))for i in range(self.nClass):for idx in self.labeled:if self.y[idx] == self.labels[i]:tmp = self.X[idx] - self.class_mean[i]S_wl += torch.mm(tmp.T, tmp)S_wr += torch.mm(tmp, tmp.T)return S_bl, S_br, S_wl, S_wrdef get_Wl_Wr(self):Wl_eigen_val, Wl_eigen_vec = torch.linalg.eig(torch.mm(torch.linalg.pinv(self.S_wl), self.S_bl))Wr_eigen_val, Wr_eigen_vec = torch.linalg.eig(torch.mm(torch.linalg.pinv(self.S_wr), self.S_br))odx_Wl = np.flipud(np.argsort(Wl_eigen_val))odx_Wr = np.flipud(np.argsort(Wr_eigen_val))Wl = torch.ones((self.nCol, self.K))Wr = torch.ones((self.K,self.nRow))for i in range(self.K):Wr[i] = Wr_eigen_vec[odx_Wr[i]]Wl[:,i] = Wl_eigen_vec[odx_Wl[i]]return Wl, Wrdef get_feature(self):X_featrue = torch.zeros((self.nSample, self.K, self.K))for idx in range(self.nSample):X_featrue[idx] = torch.mm(torch.mm(self.Wr,self.X[idx]),self.Wl)return X_featruedef incremental_update_X_feature(self, selected):# ========update self.class_mean============for i in range(self.nClass):tmp_count = 0tmp_mean = torch.zeros((self.nRow, self.nCol))for idx in selected:if self.y[idx] == self.labels[i]:tmp_count += 1tmp_mean += self.X[idx]self.class_mean[i] = (self.class_count[i] * self.class_mean[i] + tmp_count * tmp_mean) / (self.class_count[i] + tmp_count)self.class_count[i] = self.class_count[i] + tmp_count# =========updata self.global_mean===========for i in range(self.nClass):self.global_mean = torch.zeros((self.nRow, self.nCol))self.global_mean += (self.class_count[i] / len(self.labeled)) * self.class_mean[i]# =========updata S_bl & S_br ===========self.S_bl = torch.zeros((self.nCol, self.nCol))self.S_br = torch.zeros((self.nRow, self.nRow))for i in range(self.nClass):tmp = self.class_mean[i] - self.global_meanself.S_bl += self.class_count[i] * torch.mm(tmp.T,tmp)self.S_br += self.class_count[i] * torch.mm(tmp, tmp.T)# =============update Swl & Swr=================self.S_wl = torch.zeros((self.nCol, self.nCol))self.S_wr = torch.zeros((self.nRow, self.nRow))for i in range(self.nClass):for idx in self.labeled:if self.y[idx] == self.labels[i]:tmp = self.X[idx] - self.class_mean[i]self.S_wl += torch.mm(tmp.T, tmp)self.S_wr += torch.mm(tmp, tmp.T)Wl_eigen_val, Wl_eigen_vec = torch.linalg.eig(torch.mm(torch.linalg.pinv(self.S_wl), self.S_bl))Wr_eigen_val, Wr_eigen_vec = torch.linalg.eig(torch.mm(torch.linalg.pinv(self.S_wr), self.S_br))odx_Wl = np.flipud(np.argsort(Wl_eigen_val))odx_Wr = np.flipud(np.argsort(Wr_eigen_val))self.Wl = torch.ones((self.nCol, self.K))self.Wr = torch.ones((self.K,self.nRow))for i in range(self.K):self.Wr[i] = Wr_eigen_vec[odx_Wr[i]]self.Wl[:,i] = Wl_eigen_vec[odx_Wl[i]]# =============更新特征===============self.X_featrue = torch.zeros((self.nSample, self.K, self.K))for idx in range(self.nSample):self.X_featrue[idx] = torch.mm(torch.mm(self.Wr,self.X[idx]),self.Wl)def image_select(self, batch_size):metric_dict = OrderedDict()for idx in self.labeled:min_dist = np.infmin_index = Nonefor jdx in self.unlabeled:dist_tmp = torch.norm(self.X_feature[idx] - self.X_feature[jdx])if dist_tmp < min_dist:min_dist = dist_tmpmin_index = jdxmetric_dict[(idx,min_index)] = min_distselected = []for i in range(batch_size):tar_tuple = max(metric_dict, key=metric_dict.get)selected.append(tar_tuple[1])self.labeled.append(tar_tuple[1])self.labeled.append(tar_tuple[1])self.unlabeled.remove(tar_tuple[1])del metric_dict[tar_tuple]for idx in [tar_tuple[0], tar_tuple[1]]:min_dist = np.infmin_index = Nonefor jdx in self.unlabeled:dist_tmp = torch.norm(self.X_feature[idx] - self.X_feature[jdx])if dist_tmp < min_dist:min_dist = dist_tmpmin_index = jdxmetric_dict[(idx,min_index)] = min_distreturn selecteddef start(self):while self.budget > 0:if self.budget > self.batch_size:selected = self.image_select(batch_size=self.batch_size)self.budget -= self.batch_sizeelse:selected = self.image_select(batch_size=self.budget)self.budget = 0print("selected::",selected)# ==========如果标记预算还没用完,则还要更新模型============if self.budget > 0:self.incremental_update_X_feature(selected=selected)if __name__ == '__main__':path_dir = r"E:\PycharmProjects\DataSets\FaceData\yalefaces"# ===============基础信息=================nSample = 165nClass = 11labels = [i for i in np.arange(1,nClass+1)]nRow = 243nCol = 320Budget = 30# ==============构造标签==================y = np.zeros(165)i = 0label = 1j = 1while i < 165:if i+1 <= label*11:y[i] = labeli += 1else:label +=1# ============读取图片数据=================X = torch.zeros((nSample, nRow, nCol))index = 0for name in os.listdir(path_dir):if name.split(".")[0][:7] == "subject":img = np.array(Image.open(path_dir + "\\" + name))X[index] = torch.from_numpy(img)index += 1SKF = StratifiedKFold(n_splits=5, shuffle=True)for train_idx, test_idx in SKF.split(X=X,y=y):X_train = X[train_idx]y_train = y[train_idx]X_test = X[test_idx]y_test = y[test_idx]labeled = []label_dict = OrderedDict()for lab in np.unique(y_train):label_dict[lab] = []for idx in range(len(y_train)):label_dict[y_train[idx]].append(idx)for idxlist in label_dict.values():for jdx in np.random.choice(idxlist,size=2, replace=False):labeled.append(jdx)model = FNN_2DLDA(X_train=X_train,y_train=y_train,labeled=labeled,budget=Budget,X_test=X_test,y_test=y_test)model.start()break

这篇关于论文复现:Active Learning with the Furthest NearestNeighbor Criterion for Facial Age Estimation的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/625687

相关文章

AI hospital 论文Idea

一、Benchmarking Large Language Models on Communicative Medical Coaching: A Dataset and a Novel System论文地址含代码 大多数现有模型和工具主要迎合以患者为中心的服务。这项工作深入探讨了LLMs在提高医疗专业人员的沟通能力。目标是构建一个模拟实践环境,人类医生(即医学学习者)可以在其中与患者代理进行医学

论文翻译:arxiv-2024 Benchmark Data Contamination of Large Language Models: A Survey

Benchmark Data Contamination of Large Language Models: A Survey https://arxiv.org/abs/2406.04244 大规模语言模型的基准数据污染:一项综述 文章目录 大规模语言模型的基准数据污染:一项综述摘要1 引言 摘要 大规模语言模型(LLMs),如GPT-4、Claude-3和Gemini的快

论文阅读笔记: Segment Anything

文章目录 Segment Anything摘要引言任务模型数据引擎数据集负责任的人工智能 Segment Anything Model图像编码器提示编码器mask解码器解决歧义损失和训练 Segment Anything 论文地址: https://arxiv.org/abs/2304.02643 代码地址:https://github.com/facebookresear

论文翻译:ICLR-2024 PROVING TEST SET CONTAMINATION IN BLACK BOX LANGUAGE MODELS

PROVING TEST SET CONTAMINATION IN BLACK BOX LANGUAGE MODELS https://openreview.net/forum?id=KS8mIvetg2 验证测试集污染在黑盒语言模型中 文章目录 验证测试集污染在黑盒语言模型中摘要1 引言 摘要 大型语言模型是在大量互联网数据上训练的,这引发了人们的担忧和猜测,即它们可能已

OmniGlue论文详解(特征匹配)

OmniGlue论文详解(特征匹配) 摘要1. 引言2. 相关工作2.1. 广义局部特征匹配2.2. 稀疏可学习匹配2.3. 半稠密可学习匹配2.4. 与其他图像表示匹配 3. OmniGlue3.1. 模型概述3.2. OmniGlue 细节3.2.1. 特征提取3.2.2. 利用DINOv2构建图形。3.2.3. 信息传播与新的指导3.2.4. 匹配层和损失函数3.2.5. 与Super

BERT 论文逐段精读【论文精读】

BERT: 近 3 年 NLP 最火 CV: 大数据集上的训练好的 NN 模型,提升 CV 任务的性能 —— ImageNet 的 CNN 模型 NLP: BERT 简化了 NLP 任务的训练,提升了 NLP 任务的性能 BERT 如何站在巨人的肩膀上的?使用了哪些 NLP 已有的技术和思想?哪些是 BERT 的创新? 1标题 + 作者 BERT: Pre-trainin

[论文笔记]LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale

引言 今天带来第一篇量化论文LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale笔记。 为了简单,下文中以翻译的口吻记录,比如替换"作者"为"我们"。 大语言模型已被广泛采用,但推理时需要大量的GPU内存。我们开发了一种Int8矩阵乘法的过程,用于Transformer中的前馈和注意力投影层,这可以将推理所需

Detectorn2预训练模型复现:数据准备、训练命令、日志分析与输出目录

Detectorn2预训练模型复现:数据准备、训练命令、日志分析与输出目录 在深度学习项目中,目标检测是一项重要的任务。本文将详细介绍如何使用Detectron2进行目标检测模型的复现训练,涵盖训练数据准备、训练命令、训练日志分析、训练指标以及训练输出目录的各个文件及其作用。特别地,我们将演示在训练过程中出现中断后,如何使用 resume 功能继续训练,并将我们复现的模型与Model Zoo中的

UMI复现代码运行逻辑全流程(一)——eval_real.py(尚在更新)

一、文件夹功能解析 全文件夹如下 其中,核心文件作用为: diffusion_policy:扩散策略核心文件夹,包含了众多模型及基础库 example:标定及配置文件 scripts/scripts_real:测试脚本文件,区别在于前者倾向于单体运行,后者为整体运行 scripts_slam_pipeline:orb_slam3运行全部文件 umi:核心交互文件夹,作用在于构建真

文章解读与仿真程序复现思路——电力自动化设备EI\CSCD\北大核心《考虑燃料电池和电解槽虚拟惯量支撑的电力系统优化调度方法》

本专栏栏目提供文章与程序复现思路,具体已有的论文与论文源程序可翻阅本博主免费的专栏栏目《论文与完整程序》 论文与完整源程序_电网论文源程序的博客-CSDN博客https://blog.csdn.net/liang674027206/category_12531414.html 电网论文源程序-CSDN博客电网论文源程序擅长文章解读,论文与完整源程序,等方面的知识,电网论文源程序关注python