前言

在前一篇文章【深度域自适应】一、DANN与梯度反转层(GRL)详解中,我们主要讲解了DANN的网络架构与梯度反转层(GRL)的基本原理,接下来这篇文章中我们将主要复现DANN论文Unsupervised Domain Adaptation by Backpropagation中MNIST和MNIST-M数据集的迁移训练实验。


一、MNIST和MNIST-M介绍

为了利用DANN实现MNIST和MNIST-M数据集的迁移训练,我们首先需要获取到MNIST和MNIST-M数据集。其中MNIST数据集很容易获取,官网下载链接为:MNSIT。需要下载的文件如下图所示蓝色的4个文件。

由于tensorflow和keras深度融合,我们可以通过keras的相关API进行MNIST数据集,如下:

from tensorflow.keras.datasets import mnist# 导入MNIST数据集
(X_train,y_train),(X_test,y_test) = mnist.load_data()

MNIST-M数据集由MNIST数字与BSDS500数据集中的随机色块混合而成。那么要像生成MNIST-M数据集,请首先下载BSDS500数据集。BSDS500数据集的官方下载地址为:BSDS500。
以下是BSDS500数据集官方网址相关截图,点击下图中蓝框的连接即可下载数据。

下载好BSDS500数据集后,我们必须根据MNIST和BSDS500数据集来生成MNIST-M数据集,生成数据集的脚本create_mnistm.py如下:

# -*- coding: utf-8 -*-
# @Time    : 2021/7/24 下午1:50
# @Author  : Dai Pu wei
# @Email   : 771830171@qq.com
# @File    : create_mnistm.py
# @Software: PyCharmfrom __future__ import absolute_import
from __future__ import division
from __future__ import print_functionimport os
import tarfile
import numpy as np
import pickle as pkl
import skimage.io
import skimage.transform
from tensorflow.keras.datasets import mnistrand = np.random.RandomState(42)def compose_image(mnist_data, background_data):"""这是将MNIST数据和BSDS500数据进行融合成MNIST-M数据的函数:param mnist_data: MNIST数据:param background_data: BDSD500数据,作为背景图像:return:"""# 随机融合MNIST数据和BSDS500数据w, h, _ = background_data.shapedw, dh, _ = mnist_data.shapex = np.random.randint(0, w - dw)y = np.random.randint(0, h - dh)bg = background_data[x:x + dw, y:y + dh]return np.abs(bg - mnist_data).astype(np.uint8)def mnist_to_img(x):"""这是实现MNIST数据格式转换的函数,0/1数据位转化为RGB数据集:param x: 0/1格式MNIST数据:return:"""x = (x > 0).astype(np.float32)d = x.reshape([28, 28, 1]) * 255return np.concatenate([d, d, d], 2)def create_mnistm(X,background_data):"""这是生成MNIST-M数据集的函数,MNIST-M数据集介绍可见:http://jmlr.org/papers/volume17/15-239/15-239.pdf:param X: MNIST数据集:param background_data: BSDS500数据集,作为背景:return:"""# 遍历所有MNIST数据集,生成MNIST-M数据集X_ = np.zeros([X.shape[0], 28, 28, 3], np.uint8)for i in range(X.shape[0]):if i % 1000 == 0:print('Processing example', i)# 随机选择背景图像bg_img = rand.choice(background_data)# 0/1数据位格式MNIST数据转换为RGB格式mnist_image = mnist_to_img(X[i])# 将MNIST数据和BSDS500数据背景进行融合mnist_image = compose_image(mnist_image, bg_img)X_[i] = mnist_imagereturn X_def run_main():"""这是主函数"""# 初始化路径BST_PATH = os.path.abspath('./model_data/dataset/BSR_bsds500.tgz')mnist_dir = os.path.abspath("model_data/dataset/MNIST")mnistm_dir = os.path.abspath("model_data/dataset/MNIST_M")# 导入MNIST数据集(X_train,y_train),(X_test,y_test) = mnist.load_data()# 加载BSDS500数据集f = tarfile.open(BST_PATH)train_files = []for name in f.getnames():if name.startswith('BSR/BSDS500/data/images/train/'):train_files.append(name)print('Loading BSR training images')background_data = []for name in train_files:try:fp = f.extractfile(name)bg_img = skimage.io.imread(fp)background_data.append(bg_img)except:continue# 生成MNIST-M训练数据集和验证数据集print('Building train set...')train = create_mnistm(X_train,background_data)print(np.shape(train))print('Building validation set...')valid = create_mnistm(X_test,background_data)print(np.shape(valid))# 将MNIST数据集转化为RGB格式X_train = np.expand_dims(X_train,-1)X_test = np.expand_dims(X_test,-1)X_train = np.concatenate([X_train,X_train,X_train],axis=3)X_test = np.concatenate([X_test,X_test,X_test],axis=3)y_train = np.array(y_train).astype(np.int32)y_test = np.array(y_test).astype(np.int32)# 保存MNIST数据集为pkl文件if not os.path.exists(mnist_dir):os.mkdir(mnist_dir)with open(os.path.join(mnist_dir, 'mnist_data.pkl'), 'wb') as f:pkl.dump({'train': X_train,'train_label': y_train,'val': X_test,'val_label':y_test}, f, pkl.HIGHEST_PROTOCOL)# 保存MNIST-M数据集为pkl文件if not os.path.exists(mnistm_dir):os.mkdir(mnistm_dir)with open(os.path.join(mnistm_dir, 'mnist_m_data.pkl'), 'wb') as f:pkl.dump({'train': train,'train_label':y_train,'val': valid,'val_label':y_test}, f, pkl.HIGHEST_PROTOCOL)# 计算数据集平均值,用于数据标准化print(np.shape(X_train))print(np.shape(X_test))print(np.shape(train))print(np.shape(valid))print(np.shape(y_train))print(np.shape(y_test))pixel_mean = np.vstack([X_train,train,X_test,valid]).mean((0,1,2))print(np.shape(pixel_mean))print(pixel_mean)if __name__ == '__main__':run_main()

二、参数配置类config

由于整个DANN-MNIST网络的训练过程中涉及到很多超参数,因此为了整个项目的编程方便,我们利用面向对象的思想将所有的超参数放置到一个类中,即参数配置类config。这个参数配置类config的代码如下:

# -*- coding: utf-8 -*-
# @Time    : 2020/2/15 15:05
# @Author  : Dai PuWei
# @Email   : 771830171@qq.com
# @File    : config.py
# @Software: PyCharmimport osclass config(object):__defualt_dict__ = {"pre_model_path":None,"checkpoints_dir":os.path.abspath("./checkpoints"),"logs_dir":os.path.abspath("./logs"),"config_dir":os.path.abspath("./config"),"image_input_shape":(28,28,3),"image_size":28,"init_learning_rate": 1e-2,"momentum_rate":0.9,"batch_size":256,"epoch":500,"pixel_mean":[45.652287,45.652287,45.652287],}def __init__(self,**kwargs):"""这是参数配置类的初始化函数:param kwargs: 参数字典"""# 初始化相关配置参数self.__dict__.update(self. __defualt_dict__)# 根据相关传入参数进行参数更新self.__dict__.update(kwargs)if not os.path.exists(self.checkpoints_dir):os.makedirs(self.checkpoints_dir)if not os.path.exists(self.logs_dir):os.makedirs(self.logs_dir)if not os.path.exists(self.config_dir):os.makedirs(self.config_dir)def set(self,**kwargs):"""这是参数配置的设置函数:param kwargs: 参数字典:return:"""# 根据相关传入参数进行参数更新self.__dict__.update(kwargs)def save_config(self,time):"""这是保存参数配置类的函数:param time: 时间点字符串:return:"""# 更新相关目录self.checkpoints_dir = os.path.join(self.checkpoints_dir,time)self.logs_dir = os.path.join(self.logs_dir,time)self.config_dir = os.path.join(self.config_dir,time)if not os.path.exists(self.config_dir):os.makedirs(self.config_dir)if not os.path.exists(self.checkpoints_dir):os.makedirs(self.checkpoints_dir)if not os.path.exists(self.logs_dir):os.makedirs(self.logs_dir)config_txt_path = os.path.join(self.config_dir,"config.txt")with open(config_txt_path,'a') as f:for key,value in self.__dict__.items():if key in ["checkpoints_dir","logs_dir","config_dir"]:value = os.path.join(value,time)s = key+": "+value+"\n"f.write(s)

三、梯度反转层(GradientReversalLayer)

在DANN中比较重要的模块就是梯度反转层(Gradient Reversal Layer, GRL)的实现。GRL的tf2.x代码实现如下:

import tensorflow as tf
from tensorflow.keras.layers import Layer@tf.custom_gradient
def gradient_reversal(x,alpha=1.0):def grad(dy):return -dy * alpha, Nonereturn x, gradclass GradientReversalLayer(Layer):def __init__(self,**kwargs):"""这是梯度反转层的初始化函数:param kwargs: 参数字典"""super(GradientReversalLayer,self).__init__(kwargs)def call(self, x,alpha=1.0):"""这是梯度反转层的初始化函数:param x: 输入张量:param alpha: alpha系数,默认为1:return:"""return gradient_reversal(x,alpha)

在上述代码中@ops.RegisterGradient(grad_name)修饰 _flip_gradients(op, grad)函数,即自定义该层的梯度取反。同时gradient_override_map函数主要用于解决使用自己定义的函数方式来求梯度的问题,gradient_override_map函数的参数值为一个字典。即字典中value表示使用该值表示的函数代替key表示的函数进行梯度运算。


四、 DANN类代码

DANN论文Unsupervised Domain Adaptation by Backpropagation中给出MNIST和MNIST-M数据集的迁移训练实验的网络,网络架构图如下图所示。

接下来,我们将利用tensorflow2.4.0来搭建整个DANN-MNIST网络,DANN-MNIST网络结构代码如下:

# -*- coding: utf-8 -*-
# @Time    : 2020/2/14 20:27
# @Author  : Dai PuWei
# @Email   : 771830171@qq.com
# @File    : MNIST2MNIST_M.py
# @Software: PyCharmimport tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import MaxPool2D
from tensorflow.keras.layers import Activationdef build_feature_extractor():"""这是特征提取子网络的构建函数:param image_input: 图像输入张量:param name: 输出特征名称:return:"""model = tf.keras.Sequential([Conv2D(filters=32, kernel_size=5,strides=1),#tf.keras.layers.BatchNormalization(),Activation('relu'),MaxPool2D(pool_size=(2, 2), strides=2),Conv2D(filters=48, kernel_size=5,strides=1),#tf.keras.layers.BatchNormalization(),Activation('relu'),MaxPool2D(pool_size=(2, 2), strides=2),Flatten(),])return modeldef build_image_classify_extractor():"""这是搭建图像分类器模型的函数:param image_classify_feature: 图像分类特征张量:return:"""model = tf.keras.Sequential([Dense(100),#tf.keras.layers.BatchNormalization(),Activation('relu'),#tf.keras.layers.Dropout(0.5),Dense(100,activation='relu'),#tf.keras.layers.Dropout(0.5),Dense(10,activation='softmax',name="image_cls_pred"),])return modeldef build_domain_classify_extractor():"""这是搭建域分类器的函数:param domain_classify_feature: 域分类特征张量:return:"""# 搭建域分类器model = tf.keras.Sequential([Dense(100),#tf.keras.layers.BatchNormalization(),Activation('relu'),#tf.keras.layers.Dropout(0.5),Dense(2, activation='softmax', name="domain_cls_pred")])return model

六、实验结果

下面主要包括了MNIST和MNIST-M数据集在自适应训练过程中学习率、梯度反转层参数λ\lambdaλ、训练集和验证集的图像分类损失、域分类损失、图像分类精度、域分类精度和模型总损失的可视化。

首先是超参数学习率和梯度反转层参数λ\lambdaλ在训练过程中的数据可视化。

接着是训练数据集和验证数据集的图像分类精度和域分类精度在训练过程中的数据可视化,其中蓝色代表训练集,红色代表验证集。训练精度是在源域数据集即MNIST数据集上的统计结果,验证精度是在目标域数据集即MNIST-M数据集上的统计结果。 由于RTX30显卡的精度高,MNIST和MNIST-M数据集的自适应训练的训练结果稳定在86%左右,比原始论文的81.49%精度高出不少也就在情理之中。

最后是训练数据集和验证数据集的图像分类损失和域分类损失在训练过程中的数据可视化,其中蓝色代表训练集,红色代表验证集。

后记

最初实现DANN使用tf1.x框架,后期发现由于GRL的特殊性,tf1.和GRL与复杂网络结构,如YOLO v3之间的适配度较低,因此现已将代码全面升到tf2.x,未如有需要也会支持pytorch。原始tf1.x的项目代码地址为:DANN-MNIST的tf1分支,tf2.x的项目代码地址如下:

  • DANN-MNIST的tf2和master分支(tf2和master分支合并)
  • DANN-MNIST-tf2

欢迎大家在CSDN和Github上一键三连

【深度域自适应】二、利用DANN实现MNIST和MNIST-M数据集迁移训练相关推荐

  1. 【深度域自适应】一、DANN与梯度反转层(GRL)详解

    前言 在当前人工智能的如火如荼在各行各业得到广泛应用,尤其是人工智能也因此从各个方面影响当前人们的衣食住行等日常生活.这背后的原因都是因为如CNN.RNN.LSTM和GAN等各种深度神经网络的强大性能 ...

  2. 【深度域自适应】DANN梯度反转层(GRL)理解

    加入GRL的目的是为了让领域判别器和特征提取器之间形成一种对抗.先不管标签分类器,领域判别器就是GAN里的判别器,目的就是区分出real(目标域)与fake(源域),所以领域判别器的目的同样也是最小化 ...

  3. 【深度域适配】二、利用DANN实现MNIST和MNIST-M数据集迁移训练

    知乎专栏链接:https://zhuanlan.zhihu.com/p/109057360 CSDN链接:https://daipuweiai.blog.csdn.net/article/detail ...

  4. 【迁移学习】深度域自适应网络DANN模型

    DANN Domain-Adversarial Training of Neural Networks in Tensorflow 域适配:目标域与源域的数据分布不同但任务相同下的迁移学习. 模型建立 ...

  5. 【迁移学习】深度域自适应网络DANN在EEG睡眠质量检测上的应用

    应用DANN进行睡眠质量检测 论文:Sleep Quality Estimation with Adversarial Domain Adaptation:From Laboratory to Rea ...

  6. 浙江大学 | 深度域自适应和小目标检测挑战、技术和数据集:调研

    来源:专知本文为论文,建议阅读5分钟 本文对域自适应目标检测(DDA-OD)和小目标检测(tiny OD)两大检测方向进行全面调研,详细介绍了代表性算法.涨点技术和相关数据集. 论文标题: A Sur ...

  7. 【深度域适配】一、DANN与梯度反转层(GRL)详解

    CSDN博客原文链接:https://blog.csdn.net/qq_30091945/article/details/104478550 知乎专栏原文链接:https://zhuanlan.zhi ...

  8. TGRS2020/遥感:Multisource Domain Adaptation for Remote Sensing Using Deep Neural Netw基于深度神经网络的遥感多源域自适应

    TGRS2020/遥感:Multisource Domain Adaptation for Remote Sensing Using Deep Neural Networks基于深度神经网络的遥感多源 ...

  9. 遥感-Deep Covariance Alignment for Domain Adaptive Remote Sensing Image Segmentation域自适应遥感图像分割中深度协方差对齐

    Deep Covariance Alignment for Domain Adaptive Remote Sensing Image Segmentation域自适应遥感图像分割中的深度协方差对齐 0 ...

最新文章

  1. android 之ListView的布局填充器
  2. maven 插件在线安装
  3. 转: HTTP 错误 401.1 - 未经授权:访问由于凭据无效被拒绝的另类解决方案
  4. 速递 | 在线教育行业 12 大核心场景案例全解析!
  5. linux 程序返回值 139,Linux系统监控之ssh登陆自动139邮件短信提醒
  6. docker搭建mysql主从
  7. wps中,点空格或者enter之后首字母变大写问题
  8. 美国全国步枪协会遭 Grief 勒索软件攻击
  9. 详解Java异常Throwable、Error、Exception、RuntimeException的区别
  10. 红黑树与平衡二叉树_大佬用这近百张图来给我解释红黑树,看完直接跪了!
  11. kindeditor 编辑器手机访问默认为HTML编辑
  12. CAD注册表:Release Number、Product ID、Locale ID
  13. NXP mpc5744p MBDT编译工具链配置
  14. 燃石、泛生子会师纳斯达克,从招股书看异同(文末附IPO招股书)
  15. 超带感的AR大屏互动,多元化应用场景!
  16. Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解
  17. multisim中轻触开关在哪_proteus中这个开关在哪
  18. MLAPI的升级之路
  19. 史上最全国家统计局划分代码爬取
  20. 【论文阅读】InfoGAN: Interpretable Representation Learning by Information Maximizing GAN

热门文章

  1. EAS BOS锚定设置
  2. 枚举的定义和使用,位域相关知识
  3. 2023年2月《中国数据库行业分析报告》正式发布(含精彩内容概览)
  4. 【FPGA】8B/10B编码--转自wiki百科
  5. 【计算机网络】网络通信协议
  6. 内网环境下docker安装PMM 并监控 PG与MySQL
  7. Android 应用程序开发
  8. 舔砖加瓦(java)之异常
  9. 双非研二师弟的春招总结和实习感悟
  10. windows获取显卡的显存