论文复现: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

相关文章

Deep Learning复习笔记0

Key Concept: Embedding: learned dense, continuous, low-dimensional representations of object 【将难以表示的对象(如图片,文本等)用连续的低维度的方式表示】 RNN: Recurrent Neural Network -> for processing sequential data (time se

康奈尔大学之论文审稿模型Reviewer2及我司七月对其的实现(含PeerRead)

前言 自从我司于23年7月开始涉足论文审稿领域之后「截止到24年6月份,我司的七月论文审稿GPT已经迭代到了第五版,详见此文的8.1 七月论文审稿GPT(从第1版到第5版)」,在业界的影响力越来越大,所以身边朋友如发现业界有相似的工作,一般都会第一时间发给我,比如本部分要介绍的康奈尔大学的reviewer2 当然,我自己也会各种看类似工作的论文,毕竟同行之间的工作一定会互相借鉴的,我们会学他们

【漏洞复现】畅捷通T+ keyEdit.aspx SQL漏洞

0x01 产品简介 畅捷通 T+ 是一款灵动,智慧,时尚的基于互联网时代开发的管理软件,主要针对中小型工贸与商贸企业,尤其适合有异地多组织机构(多工厂,多仓库,多办事处,多经销商)的企业,涵盖了财务,业务,生产等领域的应用,产品应用功能包括: 采购管理、库存管理、销售管理、生产管理、分销管理、零售管理、往来管理、现金银行管理、总账、移动应用等,融入了社交化、移动化、电子商务、互联网信息订阅等元素

【论文精读】分类扩散模型:重振密度比估计(Revitalizing Density Ratio Estimation)

文章目录 一、文章概览(一)问题的提出(二)文章工作 二、理论背景(一)密度比估计DRE(二)去噪扩散模型 三、方法(一)推导分类和去噪之间的关系(二)组合训练方法(三)一步精确的似然计算 四、实验(一)使用两种损失对于实现最佳分类器的重要性(二)去噪结果、图像质量和负对数似然 论文:Classification Diffusion Models: Revitalizing

【python】python葡萄酒国家分布情况数据分析pyecharts可视化(源码+数据集+论文)【独一无二】

👉博__主👈:米码收割机 👉技__能👈:C++/Python语言 👉公众号👈:测试开发自动化【获取源码+商业合作】 👉荣__誉👈:阿里云博客专家博主、51CTO技术博主 👉专__注👈:专注主流机器人、人工智能等相关领域的开发、测试技术。 python葡萄酒国家分布情况数据分析pyecharts可视化(源码+数据集+论文)【独一无二】 目录 python葡

论文阅读--Efficient Hybrid Zoom using Camera Fusion on Mobile Phones

这是谷歌影像团队 2023 年发表在 Siggraph Asia 上的一篇文章,主要介绍的是利用多摄融合的思路进行变焦。 单反相机因为卓越的硬件性能,可以非常方便的实现光学变焦。不过目前的智能手机,受制于物理空间的限制,还不能做到像单反一样的光学变焦。目前主流的智能手机,都是采用多摄的设计,一般来说一个主摄搭配一个长焦,为了实现主摄与长焦之间的变焦,目前都是采用数字变焦的方式,数字变焦相比于光学

【LLM之KG】CoK论文阅读笔记

研究背景 大规模语言模型(LLMs)在许多自然语言处理(NLP)任务中取得了显著进展,特别是在零样本/少样本学习(In-Context Learning, ICL)方面。ICL不需要更新模型参数,只需利用几个标注示例就可以生成预测。然而,现有的ICL和链式思维(Chain-of-Thought, CoT)方法在复杂推理任务上仍存在生成的推理链常常伴随错误的问题,导致不真实和不可靠的推理结果。

【python】python基于akshare企业财务数据对比分析可视化(源码+数据集+论文)【独一无二】

👉博__主👈:米码收割机 👉技__能👈:C++/Python语言 👉公众号👈:测试开发自动化【获取源码+商业合作】 👉荣__誉👈:阿里云博客专家博主、51CTO技术博主 👉专__注👈:专注主流机器人、人工智能等相关领域的开发、测试技术。 系列文章目录 目录 系列文章目录一、设计要求二、设计思路三、可视化分析 一、设计要求 选取中铁和贵州茅

AIGC-Animate Anyone阿里的图像到视频 角色合成的框架-论文解读

Animate Anyone: Consistent and Controllable Image-to-Video Synthesis for Character Animation 论文:https://arxiv.org/pdf/2311.17117 网页:https://humanaigc.github.io/animate-anyone/ MOTIVATION 角色动画的

【python】python股票量化交易策略分析可视化(源码+数据集+论文)【独一无二】

👉博__主👈:米码收割机 👉技__能👈:C++/Python语言 👉公众号👈:测试开发自动化【获取源码+商业合作】 👉荣__誉👈:阿里云博客专家博主、51CTO技术博主 👉专__注👈:专注主流机器人、人工智能等相关领域的开发、测试技术。 【python】python股票量化交易策略分析可视化(源码+数据集+论文)【独一无二】 目录 【python】pyt