from torchvision import transforms
from models.model import CNNModel
# from models.model_onlysource import source_CNNModel
import numpy as np
from torch.autograd import Variable
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import pickle
import sys
import pandas as pd

testpath = sys.argv[1]  #testpath
which_one = sys.argv[2]  #`mnistm`, `usps` or `svhn`
csv_output_path = sys.argv[3]  #e.g. `hw3_data/digits/mnistm/test_pred.csv`

mymodel = CNNModel()

target_image_root = testpath
print("target_image_root", target_image_root)
if which_one == 'mnistm':
    mymodel.load_state_dict(torch.load('USPS2MNI.pkl'))
    #target_image_root = "/home/xiec/PycharmProjects/DLCV-HW3/digits/mnistm/"
elif which_one == 'usps':
    mymodel.load_state_dict(torch.load('SVHN2USPS.pkl'))
    #target_image_root = "/home/xiec/PycharmProjects/DLCV-HW3/digits/usps/"
elif which_one == 'svhn':
    mymodel.load_state_dict(torch.load('MNI2SVHN.pkl'))
    #target_image_root = "/home/xiec/PycharmProjects/DLCV-HW3/digits/svhn/"

image_size = 28
batch_size = 32
Пример #2
0
train_list = os.path.join(target_image_root, 'mnist_m_train_labels.txt')

dataset_target = GetLoader(data_root=os.path.join(target_image_root,
                                                  'mnist_m_train'),
                           data_list=train_list,
                           transform=img_transform)

dataloader_target = torch.utils.data.DataLoader(dataset=dataset_target,
                                                batch_size=batch_size,
                                                shuffle=True,
                                                num_workers=8)

# load model

my_net = CNNModel()

# setup optimizer

optimizer = optim.Adam(my_net.parameters(), lr=lr)

loss_class = torch.nn.NLLLoss()
loss_domain = torch.nn.NLLLoss()

if cuda:
    my_net = my_net.cuda()
    loss_class = loss_class.cuda()
    loss_domain = loss_domain.cuda()

for p in my_net.parameters():
    p.requires_grad = True
Пример #3
0
def dann(dataloader_source, dataloader_target, image_size, batch_size,
         source_dataset_name, target_dataset_name, n_label):

    model_root = os.path.join(os.path.dirname(__file__), os.pardir, 'models')
    # print(model_root)
    cuda = False
    cudnn.benchmark = True
    lr = 1e-3
    # batch_size = 128
    # image_size = 28
    n_epoch = 100

    manual_seed = random.randint(1, 10000)
    random.seed(manual_seed)
    torch.manual_seed(manual_seed)
    my_net = CNNModel(n_label)

    # setup optimizer

    optimizer = optim.Adam(my_net.parameters(), lr=lr)

    loss_class = torch.nn.NLLLoss()
    loss_domain = torch.nn.NLLLoss()

    if cuda:
        my_net = my_net.cuda()
        loss_class = loss_class.cuda()
        loss_domain = loss_domain.cuda()

    for p in my_net.parameters():
        p.requires_grad = True

    # training

    for epoch in range(n_epoch):

        len_dataloader = min(len(dataloader_source), len(dataloader_target))
        data_source_iter = iter(dataloader_source)
        data_target_iter = iter(dataloader_target)

        i = 0
        while i < len_dataloader:

            p = float(i + epoch * len_dataloader) / n_epoch / len_dataloader
            alpha = 2. / (1. + np.exp(-10 * p)) - 1

            # training model using source data
            data_source = data_source_iter.next()
            s_img, s_label = data_source

            my_net.zero_grad()
            batch_size = len(s_label)
            input_img = torch.FloatTensor(batch_size, 3, image_size,
                                          image_size)
            class_label = torch.LongTensor(batch_size)
            domain_label = torch.zeros(batch_size)
            domain_label = domain_label.long()

            if cuda:
                s_img = s_img.cuda()
                s_label = s_label.cuda()
                input_img = input_img.cuda()
                class_label = class_label.cuda()
                domain_label = domain_label.cuda()

            input_img.resize_as_(s_img).copy_(s_img)
            class_label.resize_as_(s_label).copy_(s_label)

            class_output, domain_output = my_net(input_data=input_img,
                                                 alpha=alpha)
            err_s_label = loss_class(class_output, class_label)
            err_s_domain = loss_domain(domain_output, domain_label)

            # training model using target data
            data_target = data_target_iter.next()
            t_img, _ = data_target

            batch_size = len(t_img)

            input_img = torch.FloatTensor(batch_size, 3, image_size,
                                          image_size)
            domain_label = torch.ones(batch_size)
            domain_label = domain_label.long()

            if cuda:
                t_img = t_img.cuda()
                input_img = input_img.cuda()
                domain_label = domain_label.cuda()

            input_img.resize_as_(t_img).copy_(t_img)

            _, domain_output = my_net(input_data=input_img, alpha=alpha)
            err_t_domain = loss_domain(domain_output, domain_label)
            err = err_t_domain + err_s_domain + err_s_label
            err.backward()
            optimizer.step()

            i += 1

            print ('epoch: %d, [iter: %d / all %d], err_s_label: %f, err_s_domain: %f, err_t_domain: %f' \
                % (epoch, i, len_dataloader, err_s_label.cpu().data.numpy(),
                    err_s_domain.cpu().data.numpy(), err_t_domain.cpu().data.numpy()))

        torch.save(
            my_net,
            '{0}/mnist_mnistm_model_epoch_{1}.pth'.format(model_root, epoch))
        test(source_dataset_name, epoch, image_size)
        test(target_dataset_name, epoch, image_size)

    print('done')
Пример #4
0
from dataset.data_loader import GetLoader
from torchvision import datasets
from torchvision import transforms
from models.model import CNNModel
# from models.model_onlysource import source_CNNModel
import numpy as np
from torch.autograd import Variable
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import pickle
import sys

testpath = sys.argv[1]
path_to_result = sys.argv[2]

mymodel = CNNModel()

mymodel.load_state_dict(torch.load('USPS2MNI.pkl'))
# mymodel.load_state_dict(torch.load('MNI2SVHN.pkl'))
# mymodel.load_state_dict(torch.load('SVHN2USPS.pkl'))

image_size = 28
batch_size = 256
alpha = 0
##target
img_transform_target = transforms.Compose([
    transforms.Resize(image_size),
    transforms.ToTensor(),
    transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
])
Пример #5
0
Created on Fri Nov 30 12:00:28 2018

@author: gaoyi
"""

import params
from utils import get_data_loader, init_model, init_random_seed
from models.model import CNNModel
from core import train
from core.train import eval_
if __name__ == '__main__':
    # init random seed
    init_random_seed(params.manual_seed)

    #set loader
    src_data_loader = get_data_loader(params.src_dataset)
    src_data_loader_eval = get_data_loader(params.src_dataset, train=False)
    tgt_data_loader = get_data_loader(params.tgt_dataset)
    tgt_data_loader_eval = get_data_loader(params.tgt_dataset, train=False)

    #set models
    cnn_model = init_model(net=CNNModel(), restore=params.model_restore)

    if not (cnn_model.restored and params.model_trained):
        train(cnn_model, src_data_loader, tgt_data_loader,
              src_data_loader_eval, tgt_data_loader_eval)

    else:
        eval_(cnn_model, src_data_loader_eval, 'src')
        eval_(cnn_model, tgt_data_loader_eval, 'tgt')
Пример #6
0
                                                batch_size=batch_size,
                                                shuffle=True,
                                                num_workers=4)

dataloader_target = torch.utils.data.DataLoader(dataset=get_dataset(
    args.target, image_size),
                                                batch_size=batch_size,
                                                shuffle=True,
                                                num_workers=4)

# load model

if args.use_deco:
    my_net = Combo()
else:
    my_net = CNNModel()

# setup optimizer

optimizer = optim.Adam(my_net.parameters(), lr=lr)

loss_class = torch.nn.NLLLoss()
loss_domain = torch.nn.NLLLoss()

if cuda:
    my_net = my_net.cuda()
    loss_class = loss_class.cuda()
    loss_domain = loss_domain.cuda()

for p in my_net.parameters():
    p.requires_grad = True
Пример #7
0
target_train_list = os.path.join(target_image_root, 'train.csv')

dataset_target = GetLoader(path=os.path.join(target_image_root),
                           transform=img_transform_target,
                           train=True)

dataloader_target = torch.utils.data.DataLoader(
    dataset=dataset_target,
    batch_size=batch_size,
    shuffle=True,
)  #num_workers=6

# load model

my_net = CNNModel()

# setup optimizer

optimizer = optim.Adam(my_net.parameters(), lr=lr)

loss_class = torch.nn.NLLLoss()
loss_domain = torch.nn.NLLLoss()

if cuda:
    my_net = my_net.cuda()
    loss_class = loss_class.cuda()
    loss_domain = loss_domain.cuda()

for p in my_net.parameters():
    p.requires_grad = True
Пример #8
0
dataloader_source = torch.utils.data.DataLoader(dataset=dataset_source,
                                                batch_size=batch_size,
                                                shuffle=True,
                                                num_workers=4)

train_list = os.path.join(target_image_root, 'mnist_m_train_labels.txt')
dataset_target = GetLoader(data_root=os.path.join(target_image_root,
                                                  'mnist_m_train'),
                           data_list=train_list,
                           transform=img_transform_target)
dataloader_target = torch.utils.data.DataLoader(dataset=dataset_target,
                                                batch_size=batch_size,
                                                shuffle=True,
                                                num_workers=4)

net = CNNModel()

# setup optimizer
optimizer = optim.Adam(net.parameters(), lr=lr)

loss_class = torch.nn.NLLLoss()
loss_domain = torch.nn.NLLLoss()

if cuda:
    net = net.cuda()
    loss_class = loss_class.cuda()
    loss_domain = loss_domain.cuda()

for p in net.parameters():
    p.requires_grad = True
Пример #9
0
                    type=float,
                    help="Parameter of the mixture of losses")
parser.add_argument('--cuda', dest='cuda', action='store_true')
parser.add_argument('--no-cuda', dest='cuda', action='store_false')
parser.set_defaults(cuda=torch.cuda.is_available())
parser.add_argument('--verbose', dest='verbose', action='store_true')
parser.set_defaults(verbose=False)
parser.add_argument('--ent_reg', dest='ent_reg', action='store_true')
parser.set_defaults(ent_reg=False)
parser.add_argument('--reg_lambda', default=0.001, type=float)

args = parser.parse_args()

torch.random.manual_seed(42)

net = CNNModel()
if args.cuda:
    net.cuda()
    print("Using CUDA")
    cudnn.benchmark = True
else:
    print("Using CPU")

for p in net.parameters():
    p.requires_grad = True

# Optimizer
optimizer = optim.Adam(net.parameters(), lr=args.learning_rate)

# Pass to cuda