def validate(img_list, crsval_mode=0):    
    train_suffix, val_suffix = generate_crsval_suffix(crsval_mode)    
    RegMat = utils.load_model(directories['models'], regress_mode, advanced_mode, train_suffix)
    
    if advanced_mode['Sparse']:
        # Loading precollected sparse data
        with open(os.path.join(directories['precal'], 'sparse_all_data'+val_suffix+'.pkl'), 'rb') as handle:
            gt_data = pickle.load(handle)
        nearest_neighbors = np.load(os.path.join(directories['precal'], 'sparse_neighbor_idx'+val_suffix+'.npy')).astype(int)
        num_anchors, num_neighbors = nearest_neighbors.shape
        
        # Regularizing Regression Matrix
        print("  Validating Regression Matrix...")
        for i in range(num_anchors):
            if i%200 == 100:
                print('    anchor', i)
            nearest_idx = nearest_neighbors[i, :]
            gt_data_nearest = {}
            gt_data_nearest['spec'] = gt_data['spec'][nearest_idx, :]
            gt_data_nearest['rgb'] = gt_data['rgb'][nearest_idx, :]
            
            regress_input, _ = utils.data_transform(gt_data_nearest, regress_mode, advanced_mode, crsval_mode, resources)
            RegMat[i] = utils.regularize(RegMat[i], regress_input, gt_data_nearest, advanced_mode, 
                                         cost_funcs['val'], resources, show_graph=False)
        
        utils.save_model(RegMat, directories['models'], regress_mode, advanced_mode, train_suffix + val_suffix) 
        
    else:
        # Check if precollected data exists
        if advanced_mode['Data_Augmentation']:
            dir_precal_data = os.path.join(directories['precal'], 'all_data'+val_suffix+'_aug'+str(advanced_mode['Data_Augmentation'][0])+'.pkl')
        else:
            dir_precal_data = os.path.join(directories['precal'], 'all_data'+val_suffix+'.pkl')
        
        if os.path.isfile(dir_precal_data):
            with open(dir_precal_data, 'rb') as handle:
                gt_data = pickle.load(handle)
        else:
            print("  Preparing Validation Images...")
            gt_data = utils.collect_gt_data(directories['data'], img_list, resources['cmf'], 2000, augment=advanced_mode['Data_Augmentation'])    
            with open(dir_precal_data, 'wb') as handle:
                pickle.dump(gt_data, handle, protocol=pickle.HIGHEST_PROTOCOL)
            
        print("  Validating Regression Matrix...")
        regress_input, _ = utils.data_transform(gt_data, regress_mode, advanced_mode, crsval_mode, resources)
        
        RegMat = utils.regularize(RegMat, regress_input, gt_data, advanced_mode, 
                                  cost_funcs['val'], resources, show_graph=False)
        
        utils.save_model(RegMat, directories['models'], regress_mode, advanced_mode, train_suffix + val_suffix)
def train(img_list, crsval_mode=0):
    print("  Training Regression Matrix...")
    train_suffix, _ = generate_crsval_suffix(crsval_mode)
    
    if advanced_mode['Sparse']:
        # Loading pretrained sparse data
        assert os.path.isdir(directories['sparse_label']), 'Please run sparse.py first'
        with open(os.path.join(directories['precal'], 'sparse_all_data'+train_suffix+'.pkl'), 'rb') as handle:
            gt_data = pickle.load(handle)
        nearest_neighbors = np.load(os.path.join(directories['precal'], 'sparse_neighbor_idx'+train_suffix+'.npy')).astype(int)
        num_anchors, num_neighbors = nearest_neighbors.shape
        
        # Training "Multiple" Regression Matrix
        RegMat = []
        for i in range(num_anchors):
            RegMat.append(utils.RegressionMatrix(regress_mode, advanced_mode))
            
        for i in range(num_anchors):
            nearest_idx = nearest_neighbors[i, :]
            gt_data_nearest = {}
            gt_data_nearest['spec'] = gt_data['spec'][nearest_idx, :]
            gt_data_nearest['rgb'] = gt_data['rgb'][nearest_idx, :]
            
            regress_input, regress_output = utils.data_transform(gt_data_nearest, regress_mode, advanced_mode, crsval_mode, resources)
            RegMat[i].update(regress_input, regress_output)
    else:
        RegMat = utils.RegressionMatrix(regress_mode, advanced_mode)
        if advanced_mode['Data_Augmentation']:
            with open(os.path.join(directories['precal'], 'sparse_all_data'+train_suffix+'.pkl'), 'rb') as handle:
                gt_data = pickle.load(handle)
            
            for i in range(advanced_mode['Data_Augmentation'][1]):
                gt_data_aug = {}
                gt_data_aug['spec'] = utils.apply_random_scale(gt_data['spec'], base=advanced_mode['Data_Augmentation'][0])
                gt_data_aug['rgb'] =  gt_data_aug['spec'] @ resources['cmf']
                regress_input, regress_output = utils.data_transform(gt_data_aug, regress_mode, advanced_mode, crsval_mode, resources)
                RegMat.update(regress_input, regress_output)
        else:    
            for img_name in img_list:
                spec_img = load_icvl_data(directories['data'], img_name[:-1]) # 31 x H x W
                gt_data = {}
                gt_data['spec'], gt_data['rgb'] = utils.cal_gt_data(spec_img, resources['cmf'], augment=advanced_mode['Data_Augmentation'])        
                
                regress_input, regress_output = utils.data_transform(gt_data, regress_mode, advanced_mode, crsval_mode, resources)
                RegMat.update(regress_input, regress_output)

    utils.save_model(RegMat, directories['models'], regress_mode, advanced_mode, train_suffix)
Exemplo n.º 3
0
    # Tensorboad
    writer = SummaryWriter(PJ(os.getcwd(), "results", "logs", exp_name))
    # Saving config file
    shutil.copy(config_path, PJ(save_root, f"{exp_name}.yaml"))

    # Show some experiment info
    print(f"Model: {config['model']}")
    print(f"Pretrained: {config['pretrained']}, Freeze: {config['freeze']}")

    ########################################
    # Data loader
    ########################################
    class2idx = config['class2idx']
    idx2class = {class2idx[k]: k for k in class2idx.keys()}
    # Dataset
    transform = data_transform(config, train=True)
    trainset = CIFAR10Dataset(config['data_root'], config['train_file'],
                              class2idx, transform)
    transform = data_transform(config, train=False)
    valset = CIFAR10Dataset(config['data_root'], config['val_file'], class2idx,
                            transform)
    # Dataloader
    trainloader = DataLoader(trainset,
                             config['batch_size'],
                             shuffle=True,
                             num_workers=config['num_workers'])
    valloader = DataLoader(valset,
                           config['test_size'],
                           shuffle=False,
                           num_workers=config['num_workers'])
def main():
    parser = argparse.ArgumentParser(
        description='Pytorch 12 tasks Baseline Training')

    # save and load
    parser.add_argument('--data-dir', dest='data_dir', type=str)
    parser.set_defaults(data_dir='decathlon-1.0-data')

    parser.add_argument('--model-weight-path',
                        dest='model_weight_path',
                        type=str)
    parser.set_defaults(model_weight_path=None)

    parser.add_argument('--log-dir', dest='log_dir', type=str)
    parser.set_defaults(log_dir='log_save')

    parser.add_argument('--model-save-dir', dest='model_save_dir', type=str)
    parser.set_defaults(model_save_dir='model_weights')

    # network
    parser.add_argument('--depth', dest='depth', type=int)
    parser.set_defaults(depth=28)

    parser.add_argument('--widen-factor', dest='widen_factor', type=int)
    parser.set_defaults(widen_factor=1)

    # training
    parser.add_argument('--lr', dest='lr', type=float)
    parser.set_defaults(lr=0.1)

    parser.add_argument('--batch-size', dest='batch_size', type=int)
    parser.set_defaults(batch_size=128)

    parser.add_argument('--epoch', dest='epoch', type=int)
    parser.set_defaults(epoch=200)

    parser.add_argument('--weight-decay', dest='weight_decay', type=float)
    parser.set_defaults(weight_decay=5e-4)

    parser.add_argument('--lr-step', dest='lr_step', type=str)
    parser.set_defaults(lr_step='[60, 120, 160]')

    parser.add_argument('--lr-drop-ratio', dest='lr_drop_ratio', type=float)
    parser.set_defaults(lr_drop_ratio=0.2)

    parser.add_argument('--gpu', dest='gpu', type=str)
    parser.set_defaults(gpu='0')

    args = parser.parse_args()

    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
    epoch_step = json.loads(args.lr_step)
    prj_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    log_dir = os.path.join(prj_dir, args.log_dir)
    model_save_dir = os.path.join(prj_dir, args.model_save_dir)
    data_dir = os.path.join(prj_dir, args.data_dir)

    if not os.path.exists(log_dir):
        os.mkdir(log_dir)
    if not os.path.exists(model_save_dir):
        os.mkdir(model_save_dir)
    if not os.path.exists(data_dir):
        os.mkdir(data_dir)

    data_name = [
        'cifar100', 'aircraft', 'daimlerpedcls', 'dtd', 'gtsrb', 'omniglot',
        'svhn', 'ucf101', 'vgg-flowers', 'cifar10', 'caltech256', 'sketches'
    ]

    data_class = [100, 100, 2, 47, 43, 1623, 10, 101, 102, 10, 257, 250]
    im_train_set = [0] * len(data_class)
    im_test_set = [0] * len(data_class)
    total_epoch = args.epoch
    avg_cost = np.zeros([total_epoch, len(data_class), 4], dtype=np.float32)
    for i in range(len(data_class)):
        im_train_set[i] = torch.utils.data.DataLoader(
            torchvision.datasets.ImageFolder(
                os.path.join(data_dir, data_name[i], 'train'),
                transform=data_transform(data_dir, data_name[i], train=True)),
            batch_size=args.batch_size,
            shuffle=True,
            num_workers=4,
            pin_memory=True)
        im_test_set[i] = torch.utils.data.DataLoader(
            torchvision.datasets.ImageFolder(
                os.path.join(data_dir, data_name[i], 'val'),
                transform=data_transform(data_dir, data_name[i], train=False)),
            batch_size=args.batch_size,
            shuffle=True,
            num_workers=4,
            pin_memory=True)

        # define WideResNet model
        device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        WideResNet_ins = WideResNet(depth=args.depth,
                                    widen_factor=args.widen_factor,
                                    num_classes=data_class[i]).to(device)
        optimizer = optim.SGD(WideResNet_ins.parameters(),
                              lr=args.lr,
                              weight_decay=args.weight_decay,
                              nesterov=True,
                              momentum=0.9)
        scheduler = optim.lr_scheduler.MultiStepLR(optimizer,
                                                   epoch_step,
                                                   gamma=args.lr_drop_ratio)

        if args.model_weight_path is not None:
            model_weight_path = os.path.join(prj_dir, args.model_weight_path)
            WideResNet_ins.load_state_dict(torch.load(model_weight_path))
            print('[*]Model loaded : ({})'.format(
                os.path.basename(model_weight_path)))

        print('Training DATASET:{}'.format(data_name[i]))
        time_onedataset = time.time()
        for index in range(total_epoch):
            scheduler.step()
            time_epoch = time.time()

            cost = np.zeros(2, dtype=np.float32)
            train_dataset = iter(im_train_set[i])
            train_batch = len(train_dataset)
            WideResNet_ins.train()
            for k in range(train_batch):
                train_data, train_label = train_dataset.next()
                train_label = train_label.type(torch.LongTensor)
                train_data, train_label = train_data.to(
                    device), train_label.to(device)
                train_pred1 = WideResNet_ins(train_data)[0]

                # reset optimizer with zero gradient
                optimizer.zero_grad()
                train_loss1 = WideResNet_ins.model_fit(
                    train_pred1,
                    train_label,
                    device=device,
                    num_output=data_class[i])
                train_loss = torch.mean(train_loss1)
                train_loss.backward()
                optimizer.step()

                # calculate training loss and accuracy
                train_predict_label1 = train_pred1.data.max(1)[1]
                train_acc1 = train_predict_label1.eq(
                    train_label).sum().item() / train_data.shape[0]

                cost[0] = torch.mean(train_loss1).item()
                cost[1] = train_acc1
                avg_cost[index, i, :2] += cost / train_batch

            # evaluating test data
            WideResNet_ins.eval()
            test_dataset = iter(im_test_set[i])
            test_batch = len(test_dataset)
            with torch.no_grad():
                for k in range(test_batch):
                    test_data, test_label = test_dataset.next()
                    test_label = test_label.type(torch.LongTensor)
                    test_data, test_label = test_data.to(
                        device), test_label.to(device)
                    test_pred1 = WideResNet_ins(test_data)[0]

                    test_loss1 = WideResNet_ins.model_fit(
                        test_pred1,
                        test_label,
                        device=device,
                        num_output=data_class[i])

                    # calculate testing loss and accuracy
                    test_predict_label1 = test_pred1.data.max(1)[1]
                    test_acc1 = test_predict_label1.eq(
                        test_label).sum().item() / test_data.shape[0]

                    cost[0] = torch.mean(test_loss1).item()
                    cost[1] = test_acc1
                    avg_cost[index, i, 2:] += cost / test_batch

            print(
                'EPOCH: {:04d} | DATASET: {:s} || TRAIN: {:.4f} {:.4f} || TEST: {:.4f} {:.4f} TIME: {:.2f} minutes {:.2f} seconds'
                .format(index, data_name[i], avg_cost[index, i, 0],
                        avg_cost[index, i, 1], avg_cost[index, i, 2],
                        avg_cost[index, i,
                                 3], (time.time() - time_epoch) // 60,
                        (time.time() - time_epoch) % 60))
            print('=' * 100)

            if not os.path.exists(os.path.join(model_save_dir, 'baseline')):
                os.mkdir(os.path.join(model_save_dir, 'baseline'))

            if not os.path.exists(
                    os.path.join(model_save_dir, 'baseline', data_name[i])):
                os.mkdir(os.path.join(model_save_dir, 'baseline',
                                      data_name[i]))

            if index % 5 == 0:
                torch.save(
                    WideResNet_ins.state_dict(),
                    os.path.join(
                        model_save_dir, 'baseline', data_name[i],
                        'wideresnet{}_{}_{}.pt'.format(args.depth,
                                                       args.widen_factor,
                                                       index)))

        torch.save(
            WideResNet_ins.state_dict(),
            os.path.join(
                model_save_dir, 'baseline', data_name[i],
                'wideresnet{}_{}_final.pt'.format(args.depth,
                                                  args.widen_factor)))
        print(
            'DATASET: {:s} : Time consumed: {:.2f} hours {:.2f} minutes {:.2f} seconds'
            .format(data_name[i], (time.time() - time_onedataset) // 3600,
                    ((time.time() - time_onedataset) % 3600) // 60,
                    (time.time() - time_onedataset) % 60))

    np.save(
        os.path.join(
            log_dir, 'wideresnet{}_{}_train_log_baseline.npy'.format(
                args.depth, args.widen_factor)))
Exemplo n.º 5
0
# coding: utf-8

# In[1]:

import numpy as np
from sklearn.model_selection import train_test_split
from utils import data_transform, data_process, data_vectorise, classify, evaluate

# In[2]:

print("Reading original file...")
original_file = "reuters-train.en"
training_file = data_transform(original_file)

# In[3]:

print("Preprocessing data...")
processed_file = data_process(training_file)

# In[ ]:

print("Vectorising features...")
X, y = data_vectorise(processed_file)

# In[ ]:

print("Splitting data...")
X_train, X_test, y_train, y_test = train_test_split(X[:30000],
                                                    y[:30000],
                                                    test_size=0.3)
def test(img_list, crsval_mode=0, file_name=()):
    print("  Testing...")
    train_suffix, val_suffix = generate_crsval_suffix(crsval_mode)  
    if regress_mode['type'] == 'HSCNN-R':
        hscnn_model, hscnn_type = load_hscnn_R_model(directories['HSCNN-R'], regress_mode, advanced_mode, crsval_mode)
    else:
        RegMat = utils.load_model(directories['models'], regress_mode, advanced_mode, train_suffix + val_suffix)
      
    if advanced_mode['Sparse']:
        for img_name in img_list:
            print("    ", img_name[:-1])
            spec_img = load_icvl_data(directories['data'], img_name[:-1]) # 31 x H x W
            gt_data = {} 
            gt_data['spec'], gt_data['rgb'] = utils.cal_gt_data(spec_img, resources['cmf'], augment=False)
            
            wp_spec = load_icvl_white_point(resources['wp_list'], resources['name_list'], img_name)
            nearest_anchor = np.load(os.path.join(directories['sparse_label'], img_name[:-5]+'_label.npy')).astype(int)
            active_anchors = np.unique(nearest_anchor).astype(int)
                
            gt_data = process_colors(gt_data, resources, wp_spec, cost_funcs['test'], regress_mode['target_wavelength'], exposure=1)
            
            write_row = [img_name]
            for exposure in advanced_mode['Exposure']:                     
                regress_input, _ = utils.data_transform(gt_data, regress_mode, advanced_mode, crsval_mode, resources, exposure)
                num_data = regress_input.shape[0]
                recovery = {'spec': np.zeros((num_data, regress_mode['dim_spec'])),
                            'rgb': np.zeros((num_data, regress_mode['dim_rgb']))}  
                                
                for i in active_anchors:
                    is_nearest = nearest_anchor == i
                    rgb_nearest = gt_data['rgb'][is_nearest, :]
                    regress_input_nearest = regress_input[is_nearest, :]
                    recovery_part = utils.recover(RegMat[i].get_matrix(), regress_input_nearest, advanced_mode, resources, rgb_nearest, exposure)

                    recovery['spec'][is_nearest, :] = recovery_part['spec']
                    recovery['rgb'][is_nearest, :] = recovery_part['rgb']
                recovery = process_colors(recovery, resources, wp_spec, cost_funcs['test'], regress_mode['target_wavelength'], exposure, gt_data)
                for cost_func in cost_funcs['test']:
                    cost = cost_func(gt_data, recovery, exposure) 
                    for tmode_key, tmode_func in test_modes.items():
                        write_row.append(tmode_func(cost))

            if len(file_name):
                write2csvfile(file_name, write_row)
    else:
        for img_name in img_list:
            print("    ", img_name[:-1])
            spec_img = load_icvl_data(directories['data'], img_name[:-1]) # 31 x H x W
            gt_data = {} 
            gt_data['spec'], gt_data['rgb'] = utils.cal_gt_data(spec_img, resources['cmf'], augment=False)
            
            wp_spec = load_icvl_white_point(resources['wp_list'], resources['name_list'], img_name)
            gt_data = process_colors(gt_data, resources, wp_spec, cost_funcs['test'], regress_mode['target_wavelength'], exposure=1) 
            
            write_row = [img_name]
            for exposure in advanced_mode['Exposure']:
                if regress_mode['type'] == 'HSCNN-R':
                    recovery = {}
                    recovery = utils.recover_HSCNN_R(gt_data['rgb'], spec_img.shape, resources, hscnn_model, hscnn_type, exposure)

                else:
                    regress_input, _ = utils.data_transform(gt_data, regress_mode, advanced_mode, crsval_mode, resources, exposure)
                    recovery = {}
                    recovery = utils.recover(RegMat.get_matrix(), regress_input, advanced_mode, resources, gt_data['rgb'], exposure)   
                recovery = process_colors(recovery, resources, wp_spec, cost_funcs['test'], regress_mode['target_wavelength'], exposure, gt_data)                
                
                for cost_func in cost_funcs['test']:
                    cost = cost_func(gt_data, recovery, exposure)
                    for tmode_key, tmode_func in test_modes.items():
                        write_row.append(tmode_func(cost))   
            
            if len(file_name):
                write2csvfile(file_name, write_row)
Exemplo n.º 7
0
val = df[len_train:len_train + len_val]
test = df[len_train + len_val:]

# del zero rows from train, val, and test
train = train[~(train == 0).all(axis=1)]
val = val[~(val == 0).all(axis=1)]
test = test[~(test == 0).all(axis=1)]

scaler = StandardScaler()
train = scaler.fit_transform(train)
val = scaler.transform(val)
test = scaler.transform(test)

# x with the shape [:, 1, window, num_nodes] where 1 means the channel
# y with the shape [:, num_nodes]
x_train, y_train = data_transform(train, window, horizon, device)
x_val, y_val = data_transform(val, window, horizon, device)
x_test, y_test = data_transform(test, window, horizon, device)

train_data = TensorDataset(x_train, y_train)
train_iter = DataLoader(train_data, batch_size, shuffle=True)
val_data = TensorDataset(x_val, y_val)
val_iter = DataLoader(val_data, batch_size)
test_data = TensorDataset(x_test, y_test)
test_iter = DataLoader(test_data, batch_size)
"""
STGCN Training

"""
# create a network instance
model = STGCN_WAVE(channels, window, num_nodes, G, drop_prob,
Exemplo n.º 8
0
def main():
    parser = argparse.ArgumentParser(
        description='Pytorch wideresnet finetuning')

    # save and load
    parser.add_argument('--data-dir', dest='data_dir', type=str)
    parser.set_defaults(data_dir='decathlon-1.0-data')

    parser.add_argument('--model-weight-dir',
                        dest='model_weight_dir',
                        type=str)
    parser.set_defaults(model_weight_dir='model_weights')

    parser.add_argument('--transfer-result-dir',
                        dest='transfer_result_dir',
                        type=str)
    parser.set_defaults(transfer_result_dir='transfer_result_fc_all')

    # network
    parser.add_argument('--depth', dest='depth', type=int)
    parser.set_defaults(depth=28)

    parser.add_argument('--widen-factor', dest='widen_factor', type=int)
    parser.set_defaults(widen_factor=1)

    # finetuning
    parser.add_argument('--fc', action='store_true')
    parser.set_defaults(target=False)

    parser.add_argument('--lr', dest='lr', type=float)
    parser.set_defaults(lr=0.1)

    parser.add_argument('--batch-size', dest='batch_size', type=int)
    parser.set_defaults(batch_size=128)

    parser.add_argument('--epoch', dest='epoch', type=int)
    parser.set_defaults(epoch=200)

    parser.add_argument('--weight-decay', dest='weight_decay', type=float)
    parser.set_defaults(weight_decay=5e-4)

    parser.add_argument('--lr-step', dest='lr_step', type=str)
    parser.set_defaults(lr_step='[60, 120, 160]')

    parser.add_argument('--lr-drop-ratio', dest='lr_drop_ratio', type=float)
    parser.set_defaults(lr_drop_ratio=0.2)

    parser.add_argument('--gpu', dest='gpu', type=str)
    parser.set_defaults(gpu='0')

    args = parser.parse_args()

    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu

    prj_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    model_weight_dir = os.path.join(prj_dir, args.model_weight_dir)
    data_dir = os.path.join(prj_dir, args.data_dir)
    transfer_result_dir = os.path.join(prj_dir, args.transfer_result_dir)

    if not os.path.exists(transfer_result_dir):
        os.mkdir(transfer_result_dir)
    epoch_step = json.loads(args.lr_step)
    data_name = [
        'cifar100', 'aircraft', 'daimlerpedcls', 'dtd', 'gtsrb', 'omniglot',
        'svhn', 'ucf101', 'vgg-flowers', 'cifar10', 'caltech256', 'sketches'
    ]
    data_class = [100, 100, 2, 47, 43, 1623, 10, 101, 102, 10, 257, 250]
    im_train_set = [0] * len(data_class)
    im_test_set = [0] * len(data_class)
    total_epoch = args.epoch
    avg_cost = np.zeros(
        [total_epoch, len(data_class),
         len(data_class), 4], dtype=np.float32)  # :[epoch, target, source, 4]
    if args.fc:
        trainlog_save = 'wideresnet{}_{}_cost_fc.npy'.format(
            args.depth, args.widen_factor)
    else:
        trainlog_save = 'wideresnet{}_{}_cost_all.npy'.format(
            args.depth, args.widen_factor)
    print('Fintuned using {}'.format(data_dir))
    for i in range(len(data_class)):  # 12 tasks
        im_train_set[i] = torch.utils.data.DataLoader(
            torchvision.datasets.ImageFolder(
                os.path.join(data_dir, data_name[i], 'train'),
                transform=data_transform(data_dir, data_name[i], train=True)),
            batch_size=args.batch_size,
            shuffle=True,
            num_workers=4,
            pin_memory=True)
        im_test_set[i] = torch.utils.data.DataLoader(
            torchvision.datasets.ImageFolder(
                os.path.join(data_dir, data_name[i], 'val'),
                transform=data_transform(data_dir, data_name[i], train=False)),
            batch_size=args.batch_size,
            shuffle=True,
            num_workers=4,
            pin_memory=True)

        # define WRN model
        device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        WideResNet_ins = WideResNet(depth=args.depth,
                                    widen_factor=args.widen_factor,
                                    num_classes=data_class[i]).to(device)

        para_optim = []
        count = 0
        for k in WideResNet_ins.parameters():
            count += 1
            if count > 106:
                para_optim.append(k)
            else:
                k.requires_grad = False

        # Fine-tune fully-connected ?
        if args.fc:
            optimizer = optim.SGD(para_optim,
                                  lr=args.lr,
                                  weight_decay=args.weight_decay,
                                  nesterov=True,
                                  momentum=0.9)
        else:
            optimizer = optim.SGD(WideResNet_ins.parameters(),
                                  lr=args.lr,
                                  weight_decay=args.weight_decay,
                                  nesterov=True,
                                  momentum=0.9)
        scheduler = optim.lr_scheduler.MultiStepLR(optimizer,
                                                   epoch_step,
                                                   gamma=args.lr_drop_ratio)

        if not os.path.exists(os.path.join(transfer_result_dir, data_name[i])):
            os.mkdir(os.path.join(transfer_result_dir, data_name[i]))

        for k, src in enumerate(data_name):
            if src == data_name[i]:
                continue

            # load src's model params
            if model_weight_dir is not None:
                if args.fc:
                    pretrained_dict = torch.load(
                        os.path.join(
                            model_weight_dir, 'baseline', src,
                            'wideresnet{}_{}_final.pt'.format(
                                args.depth, args.widen_factor)))
                    model_dict = WideResNet_ins.state_dict()
                    pretrained_dict = {
                        k_: v_
                        for k_, v_ in pretrained_dict.items()
                        if k_ != 'linear.0.weight' and k_ != 'linear.0.bias'
                    }
                    model_dict.update(pretrained_dict)
                    WideResNet_ins.load_state_dict(model_dict)
                else:
                    WideResNet_ins.load_state_dict(
                        torch.load(
                            os.path.join(
                                model_weight_dir, 'baseline', src,
                                'wideresnet{}_{}_final.pt'.format(
                                    args.depth, args.widen_factor))))
                print('[*] Loaded {} ==> {}'.format(src, data_name[i]))
            else:
                raise IOError('Transfer must have source!')

            print(
                'Start Finetuning DATASET:{} from Pretrained-MODEL:{}'.format(
                    data_name[i], src))
            time_onedataset = time.time()
            for index in range(total_epoch):
                scheduler.step()
                time_epoch = time.time()

                cost = np.zeros(2, dtype=np.float32)
                train_dataset = iter(im_train_set[i])
                train_batch = len(train_dataset)
                WideResNet_ins.train()
                for _ in range(train_batch):
                    train_data, train_label = train_dataset.next()
                    train_label = train_label.type(torch.LongTensor)
                    train_data, train_label = train_data.to(
                        device), train_label.to(device)
                    train_pred1 = WideResNet_ins(train_data)[0]

                    # reset optimizer with zero gradient
                    optimizer.zero_grad()
                    train_loss1 = WideResNet_ins.model_fit(
                        train_pred1,
                        train_label,
                        device=device,
                        num_output=data_class[i])
                    train_loss = torch.mean(train_loss1)
                    train_loss.backward()
                    optimizer.step()

                    # calculate training loss and accuracy
                    train_predict_label1 = train_pred1.data.max(1)[1]
                    train_acc1 = train_predict_label1.eq(
                        train_label).sum().item() / train_data.shape[0]

                    cost[0] = torch.mean(train_loss1).item()
                    cost[1] = train_acc1
                    avg_cost[index, i, k, 0:2] += cost / train_batch

                # evaluating test data
                WideResNet_ins.eval()
                test_dataset = iter(im_test_set[i])
                test_batch = len(test_dataset)
                for _ in range(test_batch):
                    test_data, test_label = test_dataset.next()
                    test_label = test_label.type(torch.LongTensor)
                    test_data, test_label = test_data.to(
                        device), test_label.to(device)
                    test_pred1 = WideResNet_ins(test_data)[0]

                    test_loss1 = WideResNet_ins.model_fit(
                        test_pred1,
                        test_label,
                        device=device,
                        num_output=data_class[i])

                    # calculate testing loss and accuracy
                    test_predict_label1 = test_pred1.data.max(1)[1]
                    test_acc1 = test_predict_label1.eq(
                        test_label).sum().item() / test_data.shape[0]

                    cost[0] = torch.mean(test_loss1).item()
                    cost[1] = test_acc1
                    avg_cost[index, i, k, 2:] += cost / test_batch

                print(
                    'EPOCH: {:04d} | DATASET: {:s} Finetuned from {:s} || TRAIN: {:.4f} {:.4f} || TEST: {:.4f} {:.4f} TIME: {:.2f} minutes {:.2f} seconds'
                    .format(index, data_name[i], src, avg_cost[index, i, k, 0],
                            avg_cost[index, i, k, 1], avg_cost[index, i, k, 2],
                            avg_cost[index, i, k,
                                     3], (time.time() - time_epoch) // 60,
                            (time.time() - time_epoch) % 60))
                print('=' * 100)

                if not os.path.exists(
                        os.path.join(transfer_result_dir, data_name[i], src)):
                    os.mkdir(
                        os.path.join(transfer_result_dir, data_name[i], src))

                if index % 5 == 0:
                    torch.save(
                        WideResNet_ins.state_dict(),
                        os.path.join(
                            transfer_result_dir, data_name[i], src,
                            'wideresnet{}_{}_{}.pt'.format(
                                args.depth, args.widen_factor, index)))

            torch.save(
                WideResNet_ins.state_dict(),
                os.path.join(
                    transfer_result_dir, data_name[i], src,
                    'wideresnet{}_{}_final.pt'.format(args.depth,
                                                      args.widen_factor)))
            print(
                'DATASET: {:s} Finetuned from {:s} : Time consumed: {:.2f} minutes {:.2f} seconds'
                .format(data_name[i], src,
                        (time.time() - time_onedataset) // 60,
                        (time.time() - time_onedataset) % 60))
            cost_ = avg_cost[:, i, k, :]
            np.save(
                os.path.join(transfer_result_dir, data_name[i], src,
                             'cost.npy'), cost_)

    np.save(os.path.join(transfer_result_dir, trainlog_save), avg_cost)
Exemplo n.º 9
0
def compute_errors(model, use_gpu):
    # loading test images
    test_dir = "NYU_depth_v2_test_set"
    test_img_paths = [
        join(test_dir, f) for f in listdir(test_dir)
        if isfile(join(test_dir, f)) and f.endswith("_rgb.png")
    ]
    test_label_paths = [
        join(test_dir, f) for f in listdir(test_dir)
        if isfile(join(test_dir, f)) and f.endswith("_dpth.png")
    ]
    test_img_paths.sort()
    test_label_paths.sort()
    test_files = [[x, y] for (x, y) in zip(test_img_paths, test_label_paths)]

    preds = np.zeros((466, 582, 654))
    labels = np.zeros((466, 582, 654))

    print("\nRunning the model on the test set...")
    for idx, pair in enumerate(test_files):
        img = cv2.imread(pair[0])[..., ::-1]
        # this division is necessary, because the depth values were multiplied by 6000 and saved as 16 bit images
        label = cv2.imread(pair[1], -1) / 6000

        # resize and center crop image to input size
        img = cv2.resize(img,
                         (int(img.shape[1] * 0.55), int(img.shape[0] * 0.55)),
                         interpolation=cv2.INTER_NEAREST)
        img = img[4:img.shape[0] - 4, 16:img.shape[1] - 16]

        # center crop label to output size
        label = label[7:label.shape[0] - 7, 29:label.shape[1] - 29]

        # load into the labels array
        labels[:, :, idx] = label

        img = data_transform(img)
        img = img[None, :, :, :]

        # running model on the image
        if use_gpu:
            img = img.cuda()

        # running model and upsampling the prediction
        pred = model(img)
        # upsampling
        pred = F.interpolate(pred,
                             size=(466, 582),
                             mode="bilinear",
                             align_corners=False)
        pred = pred.cpu().data.numpy()

        # load into the predictions array
        preds[:, :, idx] = pred[0, 0, :, :]

    # calculating errors
    rel_error = np.mean(np.abs(preds - labels) / labels)
    print("\nMean Absolute Relative Error: {:.6f}".format(rel_error))

    rmse = np.sqrt(np.mean((preds - labels)**2))
    print("Root Mean Squared Error: {:.6f}".format(rmse))

    log10 = np.mean(np.abs(np.log10(preds) - np.log10(labels)))
    print("Mean Log10 Error: {:.6f}".format(log10))

    acc = np.maximum(preds / labels, labels / preds)
    delta1 = np.mean(acc < 1.25)
    print("Delta1: {:.6f}".format(delta1))

    delta2 = np.mean(acc < 1.25**2)
    print("Delta2: {:.6f}".format(delta2))

    delta3 = np.mean(acc < 1.25**3)
    print("Delta3: {:.6f}".format(delta3))
Exemplo n.º 10
0
data_name = ['aircraft', 'cifar100']
data_class = [100, 100]
task_num = len(data_name)
for i in range(10):
#     im_train_set[i] = torch.utils.data.DataLoader(torchvision.datasets.ImageFolder(data_path + data_name[i] + '/train',
#                                                   transform=data_transform(data_path, data_name[i])),
#                                                   batch_size=128,
#                                                   shuffle=True,
#                                                   num_workers=4, pin_memory=True)
#     im_val_set[i] = torch.utils.data.DataLoader(torchvision.datasets.ImageFolder(data_path + data_name[i] + '/val',
#                                                 transform=data_transform(data_path,data_name[i])),
#                                                 batch_size=128,
#                                                 shuffle=True,
#                                                 num_workers=4, pin_memory=True)
    im_test_set[i] = torch.utils.data.DataLoader(torchvision.datasets.ImageFolder(data_path + all_data_name[i] + '/val',
                                                 transform=data_transform(data_path, all_data_name[i], train=False)),
                                                 batch_size=args.batch_size,
                                                 shuffle=False)
print('-----All dataset loaded-----')

# define WRN model
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if args.mode_model=='WideRes':
    model = WideResNet(depth=28, widen_factor=4, num_classes=data_class, fc=args.fc).to(device)
elif args.mode_model=='ResNet18':
    model = ResNetBaseNet(data_class, args.fc).to(device)
    
# BatchNormalizeを含む場合のDataParallel
# model = convert_model(model).cuda()
# model = DataParallelWithCallback(model, device_ids=[0, 1, 2, 3]) 
# optimizer = optim.SGD(model.parameters(), lr=0.1, weight_decay=5e-5, nesterov=True, momentum=0.9)
Exemplo n.º 11
0
------------------------------------------------------------------------
データロード
------------------------------------------------------------------------
'''
'''
    データセットは,do_task_listに記載のあるタスクのデータのみロードする.
    im_train_set:学習データ
    im_val_set:valデータ
'''
im_train_set = {}
im_val_set = {}
im_test_set = {}
if args.mode == 'test':
    for task_name in do_task_list:
        im_test_set[task_name] = torch.utils.data.DataLoader(torchvision.datasets.ImageFolder(data_path + task_name + '/test',
                                                    transform=data_transform(data_path,task_name, train=False)),
                                                    batch_size=args.batch_size,
                                                    shuffle=False,
                                                    num_workers=4, pin_memory=True)
        print('{} loaded'.format(task_name))
else:
    for task_name in do_task_list:
        im_train_set[task_name] = torch.utils.data.DataLoader(torchvision.datasets.ImageFolder(data_path + task_name + '/train',
                                                    transform=data_transform(data_path,task_name)),
                                                    batch_size=args.batch_size,
                                                    shuffle=True,
                                                    num_workers=4, pin_memory=True)
        im_val_set[task_name] = torch.utils.data.DataLoader(torchvision.datasets.ImageFolder(data_path + task_name + '/val',
                                                    transform=data_transform(data_path,task_name, train=False)),
                                                    batch_size=args.batch_size,
                                                    shuffle=False,
Exemplo n.º 12
0
                                         min_freq=config['min_freq'])

vocab_size = len(index2word)
oov_size = len(word2index) - len(index2word)

with open(word2index_path, 'wb') as handle:
    pickle.dump(word2index, handle)
with open(index2word_path, 'wb') as handle:
    pickle.dump(index2word, handle)

glove = load_glove(config['glove_path'], vocab_size, word2index)
np.save(glove_path, glove)

# transform data

data_transform(train_data, word2index, train_trg_path)
data_transform(val_data, word2index, val_trg_path)
data_transform(test_data, word2index, test_trg_path)

# save log

log = {
    'vocab_size': vocab_size,
    'oov_size': oov_size,
    'train_size': len(train_data),
    'val_size': len(val_data),
    'test_size': len(test_data)
}
with open(log_path, 'w') as handle:
    yaml.safe_dump(log,
                   handle,