|
|
@ -1,8 +1,9 @@ |
|
|
|
import os |
|
|
|
import sys |
|
|
|
import datetime |
|
|
|
from tensorboardX import SummaryWriter |
|
|
|
|
|
|
|
import torch |
|
|
|
|
|
|
|
sys.path.append('../') |
|
|
|
from models.model import SVHNmodel |
|
|
|
from core.train import train_dann |
|
|
@ -11,29 +12,32 @@ from utils.utils import get_data_loader, init_model, init_random_seed |
|
|
|
|
|
|
|
class Config(object): |
|
|
|
# params for path |
|
|
|
dataset_root = os.path.expanduser(os.path.join('~', 'Datasets')) |
|
|
|
model_name = "svhn-mnist" |
|
|
|
model_base = '/home/wogong/models/pytorch-dann' |
|
|
|
model_root = os.path.expanduser(os.path.join('~', 'Models', 'pytorch-DANN', model_name)) |
|
|
|
note = 'paper-structure' |
|
|
|
model_root = os.path.join(model_base, model_name, note + '_' + datetime.datetime.now().strftime('%m%d_%H%M%S')) |
|
|
|
os.makedirs(model_root) |
|
|
|
config = os.path.join(model_root, 'config.txt') |
|
|
|
finetune_flag = False |
|
|
|
lr_adjust_flag = 'simple' |
|
|
|
src_only_flag = False |
|
|
|
|
|
|
|
# params for datasets and data loader |
|
|
|
batch_size = 128 |
|
|
|
|
|
|
|
# params for source dataset |
|
|
|
src_dataset = "svhn" |
|
|
|
src_image_root = os.path.join('/home/wogong/datasets', 'svhn') |
|
|
|
src_model_trained = True |
|
|
|
src_classifier_restore = os.path.join(model_root, src_dataset + '-source-classifier-final.pt') |
|
|
|
|
|
|
|
# params for target dataset |
|
|
|
tgt_dataset = "mnist" |
|
|
|
tgt_image_root = os.path.join('/home/wogong/datasets', 'mnist') |
|
|
|
tgt_model_trained = True |
|
|
|
dann_restore = os.path.join(model_root, src_dataset + '-' + tgt_dataset + '-dann-final.pt') |
|
|
|
|
|
|
|
# params for pretrain |
|
|
|
num_epochs_src = 100 |
|
|
|
log_step_src = 10 |
|
|
|
save_step_src = 50 |
|
|
|
eval_step_src = 20 |
|
|
|
|
|
|
|
# params for training dann |
|
|
|
gpu_id = '0' |
|
|
|
|
|
|
@ -41,7 +45,7 @@ class Config(object): |
|
|
|
num_epochs = 200 |
|
|
|
log_step = 50 |
|
|
|
save_step = 100 |
|
|
|
eval_step = 5 |
|
|
|
eval_step = 1 |
|
|
|
|
|
|
|
## for office |
|
|
|
# num_epochs = 1000 |
|
|
@ -53,21 +57,22 @@ class Config(object): |
|
|
|
alpha = 0 |
|
|
|
|
|
|
|
# params for optimizing models |
|
|
|
lr = 2e-4 |
|
|
|
lr = 0.01 |
|
|
|
momentum = 0.9 |
|
|
|
weight_decay = 1e-6 |
|
|
|
|
|
|
|
params = Config() |
|
|
|
logger = SummaryWriter(params.model_root) |
|
|
|
device = torch.device("cuda:" + params.gpu_id if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
|
|
# init random seed |
|
|
|
init_random_seed(params.manual_seed) |
|
|
|
|
|
|
|
# init device |
|
|
|
device = torch.device("cuda:" + params.gpu_id if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
|
|
# load dataset |
|
|
|
src_data_loader = get_data_loader(params.src_dataset, params.dataset_root, params.batch_size, train=True) |
|
|
|
src_data_loader_eval = get_data_loader(params.src_dataset, params.dataset_root, params.batch_size, train=False) |
|
|
|
tgt_data_loader = get_data_loader(params.tgt_dataset, params.dataset_root, params.batch_size, train=True) |
|
|
|
tgt_data_loader_eval = get_data_loader(params.tgt_dataset, params.dataset_root, params.batch_size, train=False) |
|
|
|
src_data_loader = get_data_loader(params.src_dataset, params.src_image_root, params.batch_size, train=True) |
|
|
|
src_data_loader_eval = get_data_loader(params.src_dataset, params.src_image_root, params.batch_size, train=False) |
|
|
|
tgt_data_loader = get_data_loader(params.tgt_dataset, params.tgt_image_root, params.batch_size, train=True) |
|
|
|
tgt_data_loader_eval = get_data_loader(params.tgt_dataset, params.tgt_image_root, params.batch_size, train=False) |
|
|
|
|
|
|
|
# load dann model |
|
|
|
dann = init_model(net=SVHNmodel(), restore=None) |
|
|
@ -75,4 +80,4 @@ dann = init_model(net=SVHNmodel(), restore=None) |
|
|
|
# train dann model |
|
|
|
print("Training dann model") |
|
|
|
if not (dann.restored and params.dann_restore): |
|
|
|
dann = train_dann(dann, params, src_data_loader, tgt_data_loader, tgt_data_loader_eval, device) |
|
|
|
dann = train_dann(dann, params, src_data_loader, tgt_data_loader, tgt_data_loader_eval, device, logger) |
|
|
|