배경호

add source code

# # Created by .ignore support plugin (hsz.mobi)
# ### Example user template template
# ### Example user template
# # IntelliJ project files
# .idea
# *.iml
# out
# gen
# ### Python template
# # Byte-compiled / optimized / DLL files
__pycache__/
# *.py[cod]
# *$py.class
# # C extensions
# *.so
# # Distribution / packaging
# .Python
# build/
# develop-eggs/
# dist/
# downloads/
# eggs/
# .eggs/
# lib/
# lib64/
# parts/
# sdist/
# var/
# wheels/
# pip-wheel-metadata/
# share/python-wheels/
# *.egg-info/
# .installed.cfg
# *.egg
# MANIFEST
# # PyInstaller
# # Usually these files are written by a python script from a template
# # before PyInstaller builds the exe, so as to inject date/other infos into it.
# *.manifest
# *.spec
# # Installer logs
# pip-log.txt
# pip-delete-this-directory.txt
# # Unit test / coverage reports
# htmlcov/
# .tox/
# .nox/
# .coverage
# .coverage.*
# .cache
# nosetests.xml
# coverage.xml
# *.cover
# .hypothesis/
# .pytest_cache/
# # # Translations
# *.mo
# *.pot
# # Django stuff:
# *.log
# local_settings.py
# db.sqlite3
# # Flask stuff:
# instance/
# .webassets-cache
# # Scrapy stuff:
# .scrapy
# # Sphinx documentation
# docs/_build/
# # PyBuilder
# target/
# # Jupyter Notebook
# .ipynb_checkpoints
# # IPython
# profile_default/
# ipython_config.py
# # pyenv
# .python-version
# # pipenv
# # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# # However, in case of collaboration, if having platform-specific dependencies or dependencies
# # having no cross-platform support, pipenv may install dependencies that don't work, or not
# # install all needed dependencies.
# #Pipfile.lock
# # celery beat schedule file
# celerybeat-schedule
# # SageMath parsed files
# *.sage.py
# # Environments
# .env
# .venv
# env/
# venv/
# ENV/
# env.bak/
# venv.bak/
# # Spyder project settings
# .spyderproject
# .spyproject
# # Rope project settings
# .ropeproject
# # mkdocs documentation
# /site
# # mypy
# .mypy_cache/
# .dmypy.json
# dmypy.json
# # Pyre type checker
# .pyre/
# ### JetBrains template
# # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# # User-specific stuff
# .idea/**/workspace.xml
# .idea/**/tasks.xml
# .idea/**/usage.statistics.xml
# .idea/**/dictionaries
# .idea/**/shelf
# # Generated files
# .idea/**/contentModel.xml
# # Sensitive or high-churn files
# .idea/**/dataSources/
# .idea/**/dataSources.ids
# .idea/**/dataSources.local.xml
# .idea/**/sqlDataSources.xml
# .idea/**/dynamic.xml
# .idea/**/uiDesigner.xml
# .idea/**/dbnavigator.xml
# # Gradle
# .idea/**/gradle.xml
# .idea/**/libraries
# # Gradle and Maven with auto-import
# # When using Gradle or Maven with auto-import, you should exclude module files,
# # since they will be recreated, and may cause churn. Uncomment if using
# # auto-import.
# # .idea/modules.xml
# # .idea/*.iml
# # .idea/modules
# # *.iml
# # *.ipr
# # CMake
# cmake-build-*/
# # Mongo Explorer plugin
# .idea/**/mongoSettings.xml
# # File-based project format
# *.iws
# # IntelliJ
# out/
# # mpeltonen/sbt-idea plugin
# .idea_modules/
# # JIRA plugin
# atlassian-ide-plugin.xml
# # Cursive Clojure plugin
# .idea/replstate.xml
# # Crashlytics plugin (for Android Studio and IntelliJ)
# com_crashlytics_export_strings.xml
# crashlytics.properties
# crashlytics-build.properties
# fabric.properties
# # Editor-based Rest Client
# .idea/httpRequests
# # Android studio 3.1+ serialized cache file
# .idea/caches/build_file_checksums.ser
./data/
data/
# .idea/
generated/
# PyTorch-ADDA-mixup
A PyTorch implementation added MIXUO for Adversarial Discriminative Domain Adaptation.
Confirmed improved performance by mixing up target domian and source domain
# Usage
It works on MNIST -> USPS , SVHN -> MNIST , USPS -> MNIST, MNIST -> MNIST-M
Only 10,000 of the total data were used.(usps excluded)
<pre>
<code>
python main.py
</code>
</pre>
## adda
This repo is based on https://github.com/corenel/pytorch-adda , https://github.com/Fujiki-Nakamura/ADDA.PyTorch
Reference
https://arxiv.org/abs/1702.05464
#
import os
import torch
import torch.optim as optim
from torch import nn
from core import test
import params
from utils import make_cuda, mixup_data
def train_tgt(source_cnn, target_cnn, critic,
src_data_loader, tgt_data_loader):
"""Train encoder for target domain."""
####################
# 1. setup network #
####################
source_cnn.eval()
target_cnn.encoder.train()
critic.train()
isbest = 0
# setup criterion and optimizer
criterion = nn.CrossEntropyLoss()
#target encoder
optimizer_tgt = optim.Adam(target_cnn.parameters(),
lr=params.adp_c_learning_rate,
betas=(params.beta1, params.beta2),
weight_decay=params.weight_decay
)
#Discriminator
optimizer_critic = optim.Adam(critic.parameters(),
lr=params.d_learning_rate,
betas=(params.beta1, params.beta2),
weight_decay=params.weight_decay
)
####################
# 2. train network #
####################
len_data_loader = min(len(src_data_loader), len(tgt_data_loader))
for epoch in range(params.num_epochs):
# zip source and target data pair
data_zip = enumerate(zip(src_data_loader, tgt_data_loader))
for step, ((images_src, _), (images_tgt, _)) in data_zip:
# make images variable
images_src = make_cuda(images_src)
images_tgt = make_cuda(images_tgt)
###########################
# 2.1 train discriminator #
###########################
# zero gradients for optimizer
optimizer_critic.zero_grad()
# extract and concat features
feat_src = source_cnn.encoder(images_src)
feat_tgt = target_cnn.encoder(images_tgt)
feat_concat = torch.cat((feat_src, feat_tgt), 0)
# predict on discriminator
pred_concat = critic(feat_concat.detach())
# prepare real and fake label
label_src = make_cuda(torch.zeros(feat_src.size(0)).long())
label_tgt = make_cuda(torch.ones(feat_tgt.size(0)).long())
label_concat = torch.cat((label_src, label_tgt), 0)
# compute loss for critic
loss_critic = criterion(pred_concat, label_concat)
loss_critic.backward()
# optimize critic
optimizer_critic.step()
pred_cls = torch.squeeze(pred_concat.max(1)[1])
acc = (pred_cls == label_concat).float().mean()
############################
# 2.2 train target encoder #
############################
# zero gradients for optimizer
optimizer_critic.zero_grad()
optimizer_tgt.zero_grad()
# extract and target features
feat_tgt = target_cnn.encoder(images_tgt)
# predict on discriminator
pred_tgt = critic(feat_tgt)
# prepare fake labels
label_tgt = make_cuda(torch.zeros(feat_tgt.size(0)).long())
# compute loss for target encoder
loss_tgt = criterion(pred_tgt, label_tgt)
loss_tgt.backward()
# optimize target encoder
optimizer_tgt.step()
#######################
# 2.3 print step info #
#######################
if ((epoch % 10 ==0 )&((step + 1) % len_data_loader== 0)):
print("Epoch [{}/{}] Step [{}/{}]:"
"d_loss={:.5f} g_loss={:.5f} acc={:.5f}"
.format(epoch,
params.num_epochs,
step + 1,
len_data_loader,
loss_critic.item(),
loss_tgt.item(),
acc.item()))
torch.save(critic.state_dict(), os.path.join(
params.model_root,
"ADDA-critic-final.pt"))
torch.save(target_cnn.state_dict(), os.path.join(
params.model_root,
"ADDA-target_cnn-final.pt"))
return target_cnn
\ No newline at end of file
import torch.nn as nn
import torch
import torch.optim as optim
import params
from utils import make_cuda, save_model, LabelSmoothingCrossEntropy,mixup_data,mixup_criterion
from random import *
import sys
from torch.utils.data import Dataset,DataLoader
import os
from core import test
from utils import make_cuda, mixup_data
class CustomDataset(Dataset):
def __init__(self,img,label):
self.x_data = img
self.y_data = label
def __len__(self):
return len(self.x_data)
def __getitem__(self, idx):
x = self.x_data[idx]
y = self.y_data[idx]
return x, y
def train_src(model, source_data_loader,target_data_loader,valid_loader):
"""Train classifier for source domain."""
####################
# 1. setup network #
####################
model.train()
target_data_loader = list(target_data_loader)
# setup criterion and optimizer
optimizer = optim.Adam(
model.parameters(),
lr=params.pre_c_learning_rate,
betas=(params.beta1, params.beta2),
weight_decay=params.weight_decay
)
if params.labelsmoothing:
criterion = LabelSmoothingCrossEntropy(smoothing= params.smoothing)
else:
criterion = nn.CrossEntropyLoss()
####################
# 2. train network #
####################
len_data_loader = min(len(source_data_loader), len(target_data_loader))
for epoch in range(params.num_epochs_pre+1):
data_zip = enumerate(zip(source_data_loader, target_data_loader))
for step, ((images, labels), (images_tgt, _)) in data_zip:
# make images and labels variable
images = make_cuda(images)
labels = make_cuda(labels.squeeze_())
# zero gradients for optimizer
optimizer.zero_grad()
target=target_data_loader[randint(0, len(target_data_loader)-1)]
images, lam = mixup_data(images,target[0])
# compute loss for critic
preds = model(images)
# loss = mixup_criterion(criterion, preds, labels, labels_tgt, lam)
loss = criterion(preds, labels)
# optimize source classifier
loss.backward()
optimizer.step()
# # eval model on test set
if ((epoch) % params.eval_step_pre == 0):
print(f"Epoch [{epoch}/{params.num_epochs_pre}]",end='')
if valid_loader is not None:
test.eval_tgt(model, valid_loader)
else:
test.eval_tgt(model, source_data_loader)
# save model parameters
if ((epoch + 1) % params.save_step_pre == 0):
save_model(model, "our-source_cnn-{}.pt".format(epoch + 1))
# # save final model
save_model(model, "our-source_cnn-final.pt")
return model
def train_tgt(source_cnn, target_cnn, critic,
src_data_loader, tgt_data_loader,valid_loader):
"""Train encoder for target domain."""
####################
# 1. setup network #
####################
source_cnn.eval()
target_cnn.encoder.train()
critic.train()
isbest = 0
# setup criterion and optimizer
criterion = nn.CrossEntropyLoss()
#target encoder
optimizer_tgt = optim.Adam(target_cnn.parameters(),
lr=params.adp_c_learning_rate,
betas=(params.beta1, params.beta2),
weight_decay=params.weight_decay
)
#Discriminator
optimizer_critic = optim.Adam(critic.parameters(),
lr=params.d_learning_rate,
betas=(params.beta1, params.beta2),
weight_decay=params.weight_decay
)
####################
# 2. train network #
####################
data_len = min(len(src_data_loader), len(tgt_data_loader))
for epoch in range(params.num_epochs+1):
# zip source and target data pair
data_zip = enumerate(zip(src_data_loader, tgt_data_loader))
for step, ((images_src, _), (images_tgt, _)) in data_zip:
# make images variable
images_src = make_cuda(images_src)
images_tgt = make_cuda(images_tgt)
###########################
# 2.1 train discriminator #
###########################
# mixup data
images_src, _ = mixup_data(images_src,images_tgt)
# zero gradients for optimizer
optimizer_critic.zero_grad()
# extract and concat features
feat_src = source_cnn.encoder(images_src)
feat_tgt = target_cnn.encoder(images_tgt)
feat_concat = torch.cat((feat_src, feat_tgt), 0)
# predict on discriminator
pred_concat = critic(feat_concat.detach())
# prepare real and fake label
label_src = make_cuda(torch.zeros(feat_src.size(0)).long())
label_tgt = make_cuda(torch.ones(feat_tgt.size(0)).long())
label_concat = torch.cat((label_src, label_tgt), 0)
# compute loss for critic
loss_critic = criterion(pred_concat, label_concat)
loss_critic.backward()
# optimize critic
optimizer_critic.step()
pred_cls = torch.squeeze(pred_concat.max(1)[1])
acc = (pred_cls == label_concat).float().mean()
############################
# 2.2 train target encoder #
############################
# zero gradients for optimizer
optimizer_critic.zero_grad()
optimizer_tgt.zero_grad()
# extract and target features
feat_tgt = target_cnn.encoder(images_tgt)
# predict on discriminator
pred_tgt = critic(feat_tgt)
# prepare fake labels
label_tgt = make_cuda(torch.zeros(feat_tgt.size(0)).long())
# compute loss for target encoder
loss_tgt = criterion(pred_tgt, label_tgt)
loss_tgt.backward()
# optimize target encoder
optimizer_tgt.step()
#######################
# 2.3 print step info #
#######################
if ((epoch % 10 ==0 )& ((step + 1) % data_len == 0)):
print("Epoch [{}/{}] Step [{}/{}]:"
"d_loss={:.5f} g_loss={:.5f} acc={:.5f}"
.format(epoch ,
params.num_epochs,
step + 1,
data_len,
loss_critic.item(),
loss_tgt.item(),
acc.item()))
if valid_loader is not None:
test.eval_tgt(target_cnn,valid_loader)
torch.save(critic.state_dict(), os.path.join(
params.model_root,
"our-critic-final.pt"))
torch.save(target_cnn.state_dict(), os.path.join(
params.model_root,
"our-target_cnn-final.pt"))
return target_cnn
\ No newline at end of file
import torch.nn as nn
import torch.optim as optim
import torch
import params
from utils import make_cuda, save_model, LabelSmoothingCrossEntropy,mixup_data
from random import *
import sys
def train_src(model, source_data_loader):
"""Train classifier for source domain."""
####################
# 1. setup network #
####################
model.train()
# setup criterion and optimizer
optimizer = optim.Adam(
model.parameters(),
lr=params.pre_c_learning_rate,
betas=(params.beta1, params.beta2),
weight_decay=params.weight_decay
)
if params.labelsmoothing:
criterion = LabelSmoothingCrossEntropy(smoothing= params.smoothing)
else:
criterion = nn.CrossEntropyLoss()
####################
# 2. train network #
####################
for epoch in range(params.num_epochs_pre):
for step, (images, labels) in enumerate(source_data_loader):
# make images and labels variable
images = make_cuda(images)
labels = make_cuda(labels.squeeze_())
# zero gradients for optimizer
optimizer.zero_grad()
# compute loss for critic
preds = model(images)
loss = criterion(preds, labels)
# optimize source classifier
loss.backward()
optimizer.step()
# # eval model on test set
if ((epoch ) % params.eval_step_pre == 0):
print(f"Epoch [{epoch}/{params.num_epochs_pre}]",end='')
eval_src(model, source_data_loader)
# save model parameters
if ((epoch + 1) % params.save_step_pre == 0):
save_model(model, "ADDA-source_cnn-{}.pt".format(epoch + 1))
# # save final model
save_model(model, "ADDA-source_cnn-final.pt")
return model
def eval_src(model, data_loader):
"""Evaluate classifier for source domain."""
# set eval state for Dropout and BN layers
model.eval()
with torch.no_grad():
# init loss and accuracy
loss = 0
acc = 0
# evaluate network
for (images, labels) in data_loader:
images = make_cuda(images)
labels = make_cuda(labels).squeeze_()
preds = model(images)
pred_cls = preds.data.max(1)[1]
acc += pred_cls.eq(labels.data).cpu().sum().item()
acc /= len(data_loader.dataset)
print("Avg Accuracy = {:2%}".format( acc))
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 5 15:03:50 2018
@author: gaoyi
"""
import torch
import torch.nn as nn
from utils import make_cuda
def eval_tgt(model, data_loader):
"""Evaluation for target encoder by source classifier on target dataset."""
# set eval state for Dropout and BN layers
model.eval()
# init loss and accuracy
loss = 0
acc = 0
with torch.no_grad():
# evaluate network
for (images, labels) in data_loader:
images = make_cuda(images)
labels = make_cuda(labels).squeeze_()
preds = model(images)
_, preds = torch.max(preds.data, 1)
acc += (preds == labels).float().sum()/images.shape[0]
acc /= len(data_loader)
print("Avg Accuracy = {:2%}".format(acc))
\ No newline at end of file
from .mnist import get_mnist
from .mnist_m import get_mnist_m
from .usps import get_usps
from .svhn import get_svhn
from .customdata import get_custom
__all__ = (get_mnist_m, get_mnist,get_usps,get_svhn,get_custom)
from torchvision import transforms, datasets
import torch
import params
def get_custom(train,adp=False,size = 0):
pre_process = transforms.Compose([transforms.Resize(params.image_size),
transforms.ToTensor(),
# transforms.Normalize((0.5),(0.5)),
])
custom_dataset = datasets.ImageFolder(
root = params.custom_dataset_root ,
transform = pre_process,
)
length = len(custom_dataset)
train_set, val_set = torch.utils.data.random_split(custom_dataset, [int(length*0.9), length-int(length*0.9)])
if train:
train_set,_ = torch.utils.data.random_split(train_set, [size,len(train_set)-size])
custom_data_loader = torch.utils.data.DataLoader(
train_set if train else val_set,
batch_size= params.adp_batch_size if adp else params.batch_size,
shuffle=True,
drop_last=True
)
return custom_data_loader
\ No newline at end of file
import torch
from torchvision import datasets, transforms
import torch.utils.data as data_utils
import params
def get_mnist(train,adp = False,size = 0):
"""Get MNIST dataset loader."""
# image pre-processing
pre_process = transforms.Compose([transforms.Resize(params.image_size),
transforms.ToTensor(),
# transforms.Normalize((0.5),(0.5)),
transforms.Lambda(lambda x: x.repeat(3, 1, 1)),
])
# dataset and data loader
mnist_dataset = datasets.MNIST(root=params.mnist_dataset_root,
train=train,
transform=pre_process,
download=True)
if train:
# perm = torch.randperm(len(mnist_dataset))
# indices = perm[:10000]
mnist_dataset,_ = data_utils.random_split(mnist_dataset, [size,len(mnist_dataset)-size])
# size = len(mnist_dataset)
# train, valid = data_utils.random_split(mnist_dataset,[size-int(size*params.train_val_ratio),int(size*params.train_val_ratio)])
# train_loader = torch.utils.data.DataLoader(
# dataset=train,
# batch_size= params.adp_batch_size if adp else params.batch_size,
# shuffle=True,
# drop_last=True)
# valid_loader = torch.utils.data.DataLoader(
# dataset=valid,
# batch_size= params.adp_batch_size if adp else params.batch_size,
# shuffle=True,
# drop_last=True)
# return train_loader,valid_loader
mnist_data_loader = torch.utils.data.DataLoader(
dataset=mnist_dataset,
batch_size= params.adp_batch_size if adp else params.batch_size,
shuffle=True,
drop_last=True)
return mnist_data_loader
\ No newline at end of file
import torch.utils.data as data
from PIL import Image
import os
import params
from torchvision import transforms
import torch
import torch.utils.data as data_utils
class GetLoader(data.Dataset):
def __init__(self, data_root, data_list, transform=None):
self.root = data_root
self.transform = transform
f = open(data_list, 'r')
data_list = f.readlines()
f.close()
self.n_data = len(data_list)
self.img_paths = []
self.img_labels = []
for data_ in data_list:
self.img_paths.append(data_[:-3])
self.img_labels.append(data_[-2])
def __getitem__(self, item):
img_paths, labels = self.img_paths[item], self.img_labels[item]
imgs = Image.open(os.path.join(self.root, img_paths)).convert('RGB')
if self.transform is not None:
imgs = self.transform(imgs)
labels = int(labels)
return imgs, labels
def __len__(self):
return self.n_data
def get_mnist_m(train,adp=False,size= 0 ):
if train == True:
mode = 'train'
else:
mode = 'test'
train_list = os.path.join(params.mnist_m_dataset_root, 'mnist_m_{}_labels.txt'.format(mode))
# image pre-processing
pre_process = transforms.Compose([
transforms.Resize(params.image_size),
# transforms.Grayscale(3),
transforms.ToTensor(),
# transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
# transforms.Grayscale(1),
]
)
dataset_target = GetLoader(
data_root=os.path.join(params.mnist_m_dataset_root, 'mnist_m_{}'.format(mode)),
data_list=train_list,
transform=pre_process)
if train:
# perm = torch.randperm(len(dataset_target))
# indices = perm[:10000]
dataset_target,_ = data_utils.random_split(dataset_target, [size,len(dataset_target)-size])
# size = len(dataset_target)
# train, valid = data_utils.random_split(dataset_target,[size-int(size*params.train_val_ratio),int(size*params.train_val_ratio)])
# train_loader = torch.utils.data.DataLoader(
# dataset=train,
# batch_size= params.adp_batch_size if adp else params.batch_size,
# shuffle=True,
# drop_last=True)
# valid_loader = torch.utils.data.DataLoader(
# dataset=valid,
# batch_size= params.adp_batch_size if adp else params.batch_size,
# shuffle=True,
# drop_last=True)
# return train_loader,valid_loader
dataloader = torch.utils.data.DataLoader(
dataset=dataset_target,
batch_size= params.adp_batch_size if adp else params.batch_size,
shuffle=True,
drop_last=True)
return dataloader
\ No newline at end of file
import torch
from torchvision import datasets, transforms
import params
import torch.utils.data as data_utils
def get_svhn(train,adp=False,size=0):
"""Get SVHN dataset loader."""
# image pre-processing
pre_process = transforms.Compose([
transforms.Resize(params.image_size),
transforms.Grayscale(3),
transforms.ToTensor(),
# transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
# dataset and data loader
svhn_dataset = datasets.SVHN(root=params.svhn_dataset_root,
split='train' if train else 'test',
transform=pre_process,
download=True)
if train:
# perm = torch.randperm(len(svhn_dataset))
# indices = perm[:10000]
svhn_dataset,_ = data_utils.random_split(svhn_dataset, [size,len(svhn_dataset)-size])
# size = len(svhn_dataset)
# train, valid = data_utils.random_split(svhn_dataset,[size-int(size*params.train_val_ratio),int(size*params.train_val_ratio)])
# train_loader = torch.utils.data.DataLoader(
# dataset=train,
# batch_size= params.adp_batch_size if adp else params.batch_size,
# shuffle=True,
# drop_last=True)
# valid_loader = torch.utils.data.DataLoader(
# dataset=valid,
# batch_size= params.adp_batch_size if adp else params.batch_size,
# shuffle=True,
# drop_last=True)
# return train_loader,valid_loader
svhn_data_loader = torch.utils.data.DataLoader(
dataset=svhn_dataset,
batch_size= params.adp_batch_size if adp else params.batch_size,
shuffle=True,
drop_last=True)
return svhn_data_loader
\ No newline at end of file
import torch
from torchvision import datasets, transforms
from torch.utils.data.dataset import random_split
import params
import torch.utils.data as data_utils
def get_usps(train,adp=False,size=0):
"""Get usps dataset loader."""
# image pre-processing
pre_process = transforms.Compose([transforms.Resize(params.image_size),
transforms.ToTensor(),
# transforms.Normalize((0.5),(0.5)),
transforms.Lambda(lambda x: x.repeat(3, 1, 1)),
# transforms.Grayscale(1),
])
# dataset and data loader
usps_dataset = datasets.USPS(root=params.usps_dataset_root,
train=train,
transform=pre_process,
download=True)
if train:
usps_dataset, _ = data_utils.random_split(usps_dataset, [size,len(usps_dataset)-size])
# size = len(usps_dataset)
# train, valid = data_utils.random_split(usps_dataset,[size-int(size*params.train_val_ratio),int(size*params.train_val_ratio)])
# train_loader = torch.utils.data.DataLoader(
# dataset=train,
# batch_size= params.adp_batch_size if adp else params.batch_size,
# shuffle=True,
# drop_last=True)
# valid_loader = torch.utils.data.DataLoader(
# dataset=valid,
# batch_size= params.adp_batch_size if adp else params.batch_size,
# shuffle=True,
# drop_last=True)
# return train_loader,valid_loader
usps_data_loader = torch.utils.data.DataLoader(
dataset=usps_dataset,
batch_size= params.adp_batch_size if adp else params.batch_size,
shuffle=True,
drop_last=True)
return usps_data_loader
\ No newline at end of file
import params
from utils import get_data_loader, init_model, init_random_seed,mixup_data
from core import pretrain , adapt , test,mixup
import torch
from models.models import *
import numpy as np
import sys
if __name__ == '__main__':
# init random seed
init_random_seed(params.manual_seed)
print(f"Is cuda availabel? {torch.cuda.is_available()}")
#set loader
print("src data loader....")
src_data_loader = get_data_loader(params.src_dataset,adp=False,size = 10000)
src_data_loader_eval = get_data_loader(params.src_dataset,train=False)
print("tgt data loader....")
tgt_data_loader = get_data_loader(params.tgt_dataset,adp=False,size = 50000)
tgt_data_loader_eval = get_data_loader(params.tgt_dataset, train=False)
print(f"scr data size : {len(src_data_loader.dataset)}")
print(f"tgt data size : {len(tgt_data_loader.dataset)}")
print("start training")
source_cnn = CNN(in_channels=3).to("cuda")
target_cnn = CNN(in_channels=3, target=True).to("cuda")
discriminator = Discriminator().to("cuda")
source_cnn = mixup.train_src(source_cnn, src_data_loader,tgt_data_loader,None)
# source_cnn.load_state_dict(torch.load("./generated/models/our-source_cnn-final.pt"))
test.eval_tgt(source_cnn, tgt_data_loader_eval)
target_cnn.load_state_dict(source_cnn.state_dict())
tgt_encoder = mixup.train_tgt(source_cnn, target_cnn, discriminator,
src_data_loader,tgt_data_loader,None)
print("=== Evaluating classifier for encoded target domain ===")
print(f"mixup : {params.lammax} {params.src_dataset} -> {params.tgt_dataset} ")
print("Eval | source_cnn | src_data_loader_eval")
test.eval_tgt(source_cnn, src_data_loader_eval)
print(">>> Eval | source_cnn | tgt_data_loader_eval <<<")
test.eval_tgt(source_cnn, tgt_data_loader_eval)
print(">>> Eval | target_cnn | tgt_data_loader_eval <<<")
test.eval_tgt(target_cnn, tgt_data_loader_eval)
from torch import nn
import torch.nn.functional as F
import params
class Encoder(nn.Module):
def __init__(self, in_channels=1, h=256, dropout=0.5):
super(Encoder, self).__init__()
self.conv1 = nn.Conv2d(in_channels, 20, kernel_size=5, stride=1)
self.conv2 = nn.Conv2d(20, 50, kernel_size=5, stride=1)
self.bn1 = nn.BatchNorm2d(20)
self.bn2 = nn.BatchNorm2d(50)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.relu = nn.ReLU()
self.dropout =nn.Dropout2d(p= dropout)
# self.dropout = nn.Dropout(dropout)
self.fc1 = nn.Linear(800, 500)
# for m in self.modules():
# if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
# nn.init.kaiming_normal_(m.weight)
def forward(self, x):
bs = x.size(0)
x = self.pool(self.relu(self.bn1(self.conv1(x))))
x = self.pool(self.relu(self.bn2(self.dropout(self.conv2(x)))))
x = x.view(bs, -1)
# x = self.dropout(x)W
x = self.fc1(x)
return x
class Classifier(nn.Module):
def __init__(self, n_classes, dropout=0.5):
super(Classifier, self).__init__()
self.l1 = nn.Linear(500, n_classes)
# for m in self.modules():
# if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
# nn.init.kaiming_normal_(m.weight)
def forward(self, x):
x = self.l1(x)
return x
class CNN(nn.Module):
def __init__(self, in_channels=1, n_classes=10, target=False):
super(CNN, self).__init__()
self.encoder = Encoder(in_channels=in_channels)
self.classifier = Classifier(n_classes)
if target:
for param in self.classifier.parameters():
param.requires_grad = False
def forward(self, x):
x = self.encoder(x)
x = self.classifier(x)
return x
class Discriminator(nn.Module):
def __init__(self, h=500):
super(Discriminator, self).__init__()
self.l1 = nn.Linear(500, h)
self.l2 = nn.Linear(h, h)
self.l3 = nn.Linear(h, 2)
# self.slope =params.slope
self.relu = nn.ReLU()
# for m in self.modules():
# if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
# nn.init.kaiming_normal_(m.weight)
def forward(self, x):
x = self.relu(self.l1(x))
x = self.relu(self.l2(x))
x = self.l3(x)
return x
import torch
# params for dataset and data loader
data_root = "data"
image_size = 28
#restore
model_root = 'generated\\models'
# params for target dataset
# 'mnist_m', 'usps', 'svhn' "custom"
#dataset root
mnist_dataset_root = data_root
mnist_m_dataset_root = data_root+'\\mnist_m'
usps_dataset_root = data_root+'\\usps'
svhn_dataset_root = data_root+'\\svhn'
custom_dataset_root = data_root+'\\custom\\'
# params for training network
num_gpu = 1
log_step_pre = 10
log_step = 10
eval_step_pre = 10
##epoch
save_step_pre = 100
manual_seed = 1234
d_input_dims = 500
d_hidden_dims = 500
d_output_dims = 2
d_model_restore = 'generated\\models\\ADDA-critic-final.pt'
## sorce target
src_dataset = 'custom'
tgt_dataset = 'custom'
# params for optimizing models
# # lam 0.3
#mnist -> custom
num_epochs_pre = 20
num_epochs = 50
batch_size = 128
adp_batch_size = 128
pre_c_learning_rate = 2e-4
adp_c_learning_rate = 1e-4
d_learning_rate = 1e-4
beta1 = 0.5
beta2 = 0.999
weight_decay = 0
# #usps -> custom
# #lam 0.1
# num_epochs_pre = 5
# num_epochs = 20
# batch_size = 256
# pre_c_learning_rate = 1e-4
# adp_c_learning_rate = 2e-5
# d_learning_rate = 1e-5
# beta1 = 0.5
# beta2 = 0.999
# weight_decay = 2e-4
# #mnist_m -> custom
# #lam 0.1
# num_epochs_pre = 30
# num_epochs = 50
# batch_size = 256
# adp_batch_size = 256
# pre_c_learning_rate = 1e-3
# adp_c_learning_rate = 1e-4
# d_learning_rate = 1e-4
# beta1 = 0.5
# beta2 = 0.999
# weight_decay = 2e-4
# # params for optimizing models
#lam 0.3
# #mnist -> mnist_m
# num_epochs_pre = 50
# num_epochs = 100
# batch_size = 256
# adp_batch_size = 256
# pre_c_learning_rate = 2e-4
# adp_c_learning_rate = 2e-4
# d_learning_rate = 2e-4
# beta1 = 0.5
# beta2 = 0.999
# weight_decay = 0
# # source 10000 target 50000
# # params for optimizing models
# #svhn -> mnist
# num_epochs_pre = 20
# num_epochs = 30
# batch_size = 128
# adp_batch_size = 128
# pre_c_learning_rate = 2e-4
# adp_c_learning_rate = 1e-4
# d_learning_rate = 1e-4
# beta1 = 0.5
# beta2 = 0.999
# weight_decay = 2.5e-4
# # mnist->usps
# num_epochs_pre = 50
# num_epochs = 100
# batch_size = 256
# adp_batch_size = 256
# pre_c_learning_rate = 2e-4
# adp_c_learning_rate = 2e-4
# d_learning_rate = 2e-4
# beta1 = 0.5
# beta2 = 0.999
# weight_decay =0
# # usps->mnist
# num_epochs_pre = 50
# num_epochs = 100
# batch_size = 256
# pre_c_learning_rate = 2e-4
# adp_c_learning_rate = 2e-4
# d_learning_rate =2e-4
# beta1 = 0.5
# beta2 = 0.999
# weight_decay =0
#
use_load = False
train =False
#ratio mix target
lammax = 0.0
lammin = 0.0
labelsmoothing = False
smoothing = 0.3
\ No newline at end of file
import os
import random
import torch
import torch.backends.cudnn as cudnn
from torch.autograd import Variable
import params
from dataset import get_mnist, get_mnist_m, get_usps,get_svhn,get_custom
import numpy as np
import itertools
import torch.nn.functional as F
import params
def make_cuda(tensor):
"""Use CUDA if it's available."""
if torch.cuda.is_available():
tensor = tensor.cuda()
return tensor
def denormalize(x, std, mean):
"""Invert normalization, and then convert array into image."""
out = x * std + mean
return out.clamp(0, 1)
def init_weights(layer):
"""Init weights for layers w.r.t. the original paper."""
layer_name = layer.__class__.__name__
if layer_name.find("Conv") != -1:
layer.weight.data.normal_(0.0, 0.02)
elif layer_name.find("BatchNorm") != -1:
layer.weight.data.normal_(1.0, 0.02)
layer.bias.data.fill_(0)
def init_random_seed(manual_seed):
"""Init random seed."""
seed = None
if manual_seed is None:
seed = random.randint(1, 10000)
else:
seed = manual_seed
#for REPRODUCIBILITY
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
print("use random seed: {}".format(seed))
random.seed(seed)
torch.manual_seed(seed)
np.random.seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
def get_data_loader(name,train=True,adp=False,size = 0):
"""Get data loader by name."""
if name == "mnist":
return get_mnist(train,adp,size)
elif name == "mnist_m":
return get_mnist_m(train,adp,size)
elif name == "usps":
return get_usps(train,adp,size)
elif name == "svhn":
return get_svhn(train,adp,size)
elif name == "custom":
return get_custom(train,adp,size)
def init_model(net, restore=None):
"""Init models with cuda and weights."""
# init weights of model
# net.apply(init_weights)
print(f'restore file : {restore}')
# restore model weights
if restore is not None and os.path.exists(restore):
net.load_state_dict(torch.load(restore))
net.restored = True
print("Restore model from: {}".format(os.path.abspath(restore)))
# check if cuda is available
if torch.cuda.is_available():
net.cuda()
return net
def save_model(net, filename):
"""Save trained model."""
if not os.path.exists(params.model_root):
os.makedirs(params.model_root)
torch.save(net.state_dict(),
os.path.join(params.model_root, filename))
print("save pretrained model to: {}".format(os.path.join(params.model_root,
filename)))
class LabelSmoothingCrossEntropy(torch.nn.Module):
def __init__(self,smoothing):
super(LabelSmoothingCrossEntropy, self).__init__()
self.smoothing = smoothing
def forward(self, y, targets,smoothing=0.1):
confidence = 1. - self.smoothing
log_probs = F.log_softmax(y, dim=-1) # 예측 확률 계산
true_probs = torch.zeros_like(log_probs)
true_probs.fill_(self.smoothing / (y.shape[1] - 1))
true_probs.scatter_(1, targets.data.unsqueeze(1), confidence) # 정답 인덱스의 정답 확률을 confidence로 변경
return torch.mean(torch.sum(true_probs * -log_probs, dim=-1)) # negative log likelihood
#mixup only data, not label
def mixup_data(source,target):
max = params.lammax
min = params.lammin
lam = (max-min)*torch.rand((1))+min
lam=lam.cuda()
target = target.cuda()
mixed_source = (1 - lam) * source + lam* target
return mixed_source, lam
def mixup_criterion(criterion, pred, y_a, y_b, lam):
return 0.9* criterion(pred, y_a) + 0.1 * criterion(pred, y_b)
\ No newline at end of file