示例#1
0
def main():
    param_file, data_set, network_params, optimizer_params, debug = setup()

    if data_set == CIFAR10:
        (train_images, train_labels,
         cv_images, cv_labels,
         test_images, test_labels) = load_data(CIFAR10)

        if debug:
            train_images = train_images[0:100]
            train_labels = train_labels[0:100]

        in_dim = (32, 32, 3)
        d_out = 10

    elif data_set == MNIST:
        (train_images, train_labels,
         cv_images, cv_labels,
         test_images, test_labels) = load_data(MNIST)
        in_dim = (28, 28, 1)
        d_out = 10

    else:
        raise ValueError('Invalid data_set {}'.format(data_set))

    bnn = BinaryNeuralNetwork(
        in_dim=in_dim,
        out_dim=d_out,
        network_params=network_params,
        optimizer_params=optimizer_params,
        param_file=param_file
    )

    bnn.fit(train_images, train_labels, cv_images, cv_labels)
def model_logistic_regression(sample):
    x_train, x_test, y_train, y_test = load_data()
    logistic_regression = LogisticRegression()
    fit_log_reg = logistic_regression.fit(x_train, y_train)
    prediction = fit_log_reg.predict(sample)
    prob = fit_log_reg.predict_proba(sample)
    return prediction, prob
def benchmark_ensemble_selection(args):
    from ensemble.ensemble_selection.ensemble_selection import EnsembleSelection
    task_type = STRING_TO_TASK_TYPES[args.task_type]
    path = args.data_path

    metric = CLASSIFICATION_METRICS[args.metric]

    X, y, X_test, y_test = load_data(path)
    print(X.shape)

    if args.n_init and args.replacements:
        n_init = int(args.n_init)
        model = EnsembleSelection(task_type, metric, no_iterations=1000,
                                  sorted_initialization=True, with_replacements=True,
                                  n_init=n_init)
        name = "EnsembleSelectionSortedInitialization_WithReplacements"

    elif args.n_init:
        n_init = int(args.n_init)
        model = EnsembleSelection(task_type, metric, no_iterations=1000,
                                  sorted_initialization=True, n_init=n_init)
        name = "EnsembleSelectionSortedInitialization"
    elif args.replacements:
        model = EnsembleSelection(task_type, metric, no_iterations=1000,
                                  with_replacements=True)
        name = "EnsembleSelectionReplacements"
    else:
        model = EnsembleSelection(task_type, metric, no_iterations=1000)
        name = "EnsembleSelection"

    return apply_model(args, X=X, y=y, X_test=X_test, y_test=y_test, metric=metric, task_type=task_type, model=model, name=name, dataset=get_datasetname(path))
示例#4
0
def setUpGlobal(cls):
    path = './data/datasets/75191'
    a, b, c, d = load_data(path)
    cls.basemodels_preds = a
    cls.train_y = b
    cls.test_preds = c
    cls.test_y = d
def benchmark_stacking_mlr(args):
    from ensemble.stacking.stacking_MLR import StackingMLR
    task_type = STRING_TO_TASK_TYPES[args.task_type]
    path = args.data_path
    name = "StackingMLR"

    metric = CLASSIFICATION_METRICS[args.metric]

    X, y, X_test, y_test = load_data(path)

    model = StackingMLR()
    return apply_model(args, X=X, y=y, X_test=X_test, y_test=y_test, metric=metric, task_type=task_type, model=model, name=name, dataset=get_datasetname(path))
def benchmark_stacking_bayesian_avg_mcmc(args):
    from ensemble.stacking.stacking_bayesian_avg_mcmc import BayesianAverageMCMC
    task_type = STRING_TO_TASK_TYPES[args.task_type]
    path = args.data_path

    name = "StackingBayesianAverageMCMC"
    metric = CLASSIFICATION_METRICS[args.metric]

    X, y, X_test, y_test = load_data(path)

    model = BayesianAverageMCMC(n_samples=10000)
    return apply_model(args, X=X, y=y, X_test=X_test, y_test=y_test, metric=metric, task_type=task_type, model=model, name=name, dataset=get_datasetname(path))
def benchmark_stacking_agnostic_bayesian(args):
    from ensemble.stacking.stacking_agnostic_bayesian import AgnosticBayesian

    task_type = STRING_TO_TASK_TYPES[args.task_type]
    path = args.data_path
    name =  "StackingAgnosticBayesian"

    metric = CLASSIFICATION_METRICS[args.metric]

    X, y, X_test, y_test = load_data(path)

    model = AgnosticBayesian(task_type, metric, n_bootstraps=500)
    return apply_model(args, X=X, y=y, X_test=X_test, y_test=y_test, metric=metric, task_type=task_type, model=model, name=name, dataset=get_datasetname(path))
def predictor_gradient_boosting():
    new_sample = pd.read_csv('/Users/karola/PycharmProjects/breast_cancer/data/test_samples/sample.csv')
    # print(np.array(new_sample.transpose))
    x_train, x_test, y_train, y_test = load_data()

    xgboost = XGBClassifier()

    fit_xgboost = xgboost.fit(x_train, y_train)
    # print(x_test)
    # print(x_test.iloc[1].transpose)
    prediction = fit_xgboost.predict(new_sample)
    print(prediction)
    return prediction
def benchmark_best_model(args):

    task_type = STRING_TO_TASK_TYPES[args.task_type]
    path = args.data_path
    dataset = get_datasetname(path)
    metric = CLASSIFICATION_METRICS[args.metric]
    name = "SINGLE BEST"
    X, y, X_test, y_test = load_data(path)
    n_basemodels = X.shape[0]
    best_model = np.argmax(calculate_score(y, X[m,:,:], task_type, metric) for m in range(n_basemodels))
    perf = calculate_score(y_test, X_test[best_model, :, :], task_type, metric)

    return {dataset: {name: {str(metric): perf}}}
def benchmark_stacking_bayesian_avg(args):
    from ensemble.stacking.stacking_bayesian_avg import BayesianAverage
    task_type = STRING_TO_TASK_TYPES[args.task_type]
    path = args.data_path

    name = "StackingBayesianAverage"
    metric = CLASSIFICATION_METRICS[args.metric]

    X, y, X_test, y_test = load_data(path)

    if args.multi:
        model = BayesianAverage(multi=True)
        name = "StackingBayesianAverageMultiClass"

    model = BayesianAverage()
    name = "StackingBayesianAverageBinaryClass"
    return apply_model(args, X=X, y=y, X_test=X_test, y_test=y_test, metric=metric, task_type=task_type, model=model, name=name, dataset=get_datasetname(path))
def benchmark_stacking_logit_reg(args):
    from ensemble.stacking.stacking_logit_reg import StackingLogitReg
    task_type = STRING_TO_TASK_TYPES[args.task_type]
    path = args.data_path

    name = "StackingLogitReg"

    metric = CLASSIFICATION_METRICS[args.metric]

    X, y, X_test, y_test = load_data(path)

    if args.reg_l1:
        model = StackingLogitReg(regularizer="l1")
        name = "StackingLogitRegularized_l1"

    model = StackingLogitReg(regularizer="l2")
    name = "StackingLogitRegularized_l2"
    return apply_model(args, X=X, y=y, X_test=X_test, y_test=y_test, metric=metric, task_type=task_type, model=model, name=name, dataset=get_datasetname(path))
def setUpGlobal(cls):
    """
    dataset = datasets.load_breast_cancer()
    X = dataset.data
    y = dataset.target
    a, b, c, d = compute_level1_dataset(X, y)
    cls.basemodels_preds = a
    cls.test_preds = b
    cls.train_y = c
    cls.test_y = d
    """
    path = "./data/datasets/75103"
    print("data_loaded")
    a, b, c, d = load_data(path)
    cls.basemodels_preds = a
    cls.train_y = b
    cls.test_preds = c
    cls.test_y = d
    __basemodels_score__(cls.basemodels_preds, cls.train_y)
        self.w = w
        return w

    def test(self, x, y):
        y_pred = []
        for x_i in x:
            temp = self.sigmoid(np.dot(x_i, self.w))
            temp = 1 if temp >= 0.5 else 0
            y_pred.append(temp)

        print accuracy_score(y_test, y_pred)
        print classification_report(y, y_pred)

if __name__ == '__main__':
    x, y = load_data()
    x_train, y_train, x_test, y_test = x[:80], y[:80], x[80:], y[80:]

    lr = LR()
    lr.fit(x_train, y_train, 20)
    lr.test(x_test, y_test)

    """
    效果是0.6, sklearn可以做到100%.不得不服
    from sklearn.linear_model import LogisticRegression
    lr = LogisticRegression()
    lr.fit(x_train, y_train)
    y_pred = lr.predict(x_test)
    print accuracy_score(y_test, y_pred)
    """
示例#14
0
def test_fc():
	data = ld.load_data()
	tdata = fc.feature_creation(data)
	assert len(tdata.columns) == 74
示例#15
0
文件: train.py 项目: bityangke/EADER
def main(config):
    """ Main training function """
    print(f'Running experiment with arguments:\n{config}')

    base_experiment_directory = os.path.join('results', config.experiment_name)
    if not os.path.exists(base_experiment_directory):
        os.makedirs(base_experiment_directory)

    if config.seed is not None:
        torch.manual_seed(config.seed)
        torch.backends.cudnn.deterministic = True

    if config.evaluate and not config.resume:
        raise ValueError(
            'Can only evaluate from a given checkpoint, use "resume" param')

    if config.share_weights and config.adversarial_model:
        raise ValueError(
            'When sharing weights, only the localizer needs to be set')

    # Load data
    train_loader = load_data('train', config.dataset_root,
                             config.img_resolution, config.batch_size,
                             config.download_dataset)
    val_batch_size = 1 if config.evaluate else config.batch_size
    validation_loader = load_data('val', config.dataset_root,
                                  config.img_resolution, val_batch_size)
    print(
        f'Training set size: {len(train_loader.dataset)}, validation set size: {len(validation_loader.dataset)}'
    )

    localizer = load_localizer(config.localizer_model,
                               not config.localizer_from_scratch,
                               config.attention_type, NUM_CLASSES)
    if torch.cuda.is_available():
        localizer = localizer.cuda()
    localizer_optimizer = load_optimizer(config.optimizer,
                                         localizer.parameters(),
                                         config.learning_rate)

    if config.adversarial_model:
        adversarial = load_adversarial(config.adversarial_model,
                                       not config.adversarial_from_scratch,
                                       NUM_CLASSES)
        if torch.cuda.is_available():
            adversarial = adversarial.cuda()
        adversarial_optimizer = load_optimizer(
            config.adversarial_optimizer, adversarial.parameters(),
            config.adversarial_learning_rate)
    else:
        adversarial = None
        adversarial_optimizer = None

    train_steps = (config.train_steps_localizer,
                   config.train_steps_adversarial)
    training_phase = TrainingPhase(localizer, adversarial, len(train_loader),
                                   train_steps)

    start_epoch = 1

    # Load weights (from checkpoint)
    if config.resume:
        if not os.path.isfile(config.resume):
            raise Exception(f'Given checkpoint {config.resume} is not a file')
        checkpoint = torch.load(config.resume)
        localizer.load_state_dict(checkpoint['localizer'])
        localizer_optimizer.load_state_dict(checkpoint['localizer_optimizer'])
        adversarial.load_state_dict(checkpoint['adversarial'])
        adversarial_optimizer.load_state_dict(
            checkpoint['adversarial_optimizer'])
        start_epoch = checkpoint['epoch'] + 1
        if config.evaluate:
            print(f'Evaluating with weights from epoch {start_epoch - 1}')
            experiment_directory = os.path.join(base_experiment_directory,
                                                str(start_epoch))
            if not os.path.exists(experiment_directory):
                os.makedirs(experiment_directory)
            validate(localizer,
                     adversarial,
                     validation_loader,
                     experiment_directory,
                     LABELS,
                     config.segmentation_map_threshold,
                     NUM_CLASSES,
                     evaluate=True,
                     save_results=True)
            return

    for epoch in range(start_epoch, config.epochs + 1):
        experiment_directory = os.path.join(base_experiment_directory,
                                            str(epoch))
        if not os.path.exists(experiment_directory):
            os.makedirs(experiment_directory)
        print(f'Epoch: {epoch}/{config.epochs}')
        save_results = epoch % 3 == 0  # Save results (heatmaps, masks) every 3 epochs
        localizer.train()
        if adversarial is not None:
            adversarial.train()
        train(localizer, adversarial, localizer_optimizer,
              adversarial_optimizer, train_loader, epoch, config.alpha,
              config.beta, training_phase, config.share_weights)

        localizer.eval()
        if adversarial is not None:
            adversarial.eval()
        validate(localizer,
                 adversarial,
                 validation_loader,
                 experiment_directory,
                 LABELS,
                 config.segmentation_map_threshold,
                 NUM_CLASSES,
                 save_results=save_results)

        # Save checkpoint
        state = {
            'epoch':
            epoch,
            'localizer':
            localizer.state_dict(),
            'localizer_optimizer':
            localizer_optimizer.state_dict(),
            'adversarial':
            adversarial.state_dict() if adversarial is not None else None,
            'adversarial_optimizer':
            adversarial_optimizer.state_dict()
            if adversarial_optimizer is not None else None
        }

        save_location = os.path.join(base_experiment_directory,
                                     'checkpoint.pth.tar')
        torch.save(state, save_location)
示例#16
0
def main():
    print('Loss & Optimizer')
    if args.loss == 'triplet':
        args.triplet = True
        criterion = TripletLoss(margin=args.margin, swap=args.swap)
    elif args.loss == 'triplet_distance':
        args.triplet = True
        criterion = TripletLoss(margin=args.margin, swap=args.swap, dist=True)
    else:
        args.triplet = False
        criterion = ContrastiveLoss(margin=args.margin)

    print('Prepare data')
    train_loader, valid_loader, valid_gallery_loader, test_loader, test_gallery_loader, in_size = load_data(
        args.dataset,
        args.data_path,
        triplet=args.triplet,
        batch_size=args.batch_size,
        prefetch=args.prefetch,
        set_partition=args.set_partition)

    print('Create model')
    if args.model == 'GAT':
        net = models.GNN_GAT(in_size,
                             args.hidden,
                             args.out_size,
                             dropout=args.dropout)
    elif args.model == 'GRU':
        net = models.GNN_GRU(in_size,
                             args.hidden,
                             args.out_size,
                             dropout=args.dropout)

    distNet = distance.SoftHd(args.out_size)

    optimizer = torch.optim.Adam(list(net.parameters()) +
                                 list(distNet.parameters()),
                                 args.learning_rate,
                                 weight_decay=args.decay)
    scheduler = StepLR(optimizer, 5, gamma=args.gamma)

    print('Check CUDA')
    if args.cuda and args.ngpu > 1:
        print('\t* Data Parallel **NOT TESTED**')
        net = torch.nn.DataParallel(net, device_ids=list(range(args.ngpu)))

    if args.cuda:
        print('\t* CUDA')
        net, distNet = net.cuda(), distNet.cuda()
        criterion = criterion.cuda()

    start_epoch = 0
    best_perf = 0
    early_stop_counter = 0
    if args.load is not None:
        print('Loading model')
        checkpoint = load_checkpoint(args.load)
        net.load_state_dict(checkpoint['state_dict'])
        distNet.load_state_dict(checkpoint['state_dict_dist'])
        start_epoch = checkpoint['epoch']
        best_perf = checkpoint['best_perf']

    if not args.test:
        print('***Train***')

        for epoch in range(start_epoch, args.epochs):

            loss_train = train(train_loader, [net, distNet], optimizer,
                               args.cuda, criterion, epoch)
            acc_valid, map_valid = test(valid_loader,
                                        valid_gallery_loader, [net, distNet],
                                        args.cuda,
                                        validation=True)

            # Early-Stop + Save model
            if map_valid.avg > best_perf:
                best_perf = map_valid.avg
                early_stop_counter = 0
                if args.save is not None:
                    save_checkpoint(
                        {
                            'epoch': epoch + 1,
                            'state_dict': net.state_dict(),
                            'state_dict_dist': distNet.state_dict(),
                            'best_perf': best_perf
                        },
                        directory=args.save,
                        file_name='checkpoint')
            else:
                if early_stop_counter >= args.early_stop:
                    print('Early Stop epoch {}'.format(epoch))
                    break
                early_stop_counter += 1

            # Logger
            if args.log:
                # Scalars
                logger.add_scalar('loss_train', loss_train.avg)
                logger.add_scalar('acc_valid', acc_valid.avg)
                logger.add_scalar('map_valid', map_valid.avg)
                logger.add_scalar('learning_rate', scheduler.get_lr()[0])
                logger.step()

            scheduler.step()
        # Load Best model in case of save it
        if args.save is not None:
            print('Loading best  model')
            best_model_file = os.path.join(args.save, 'checkpoint.pth')
            checkpoint = load_checkpoint(best_model_file)
            net.load_state_dict(checkpoint['state_dict'])
            distNet.load_state_dict(checkpoint['state_dict_dist'])
            print('Best model at epoch {epoch} and acc {acc}%'.format(
                epoch=checkpoint['epoch'], acc=checkpoint['best_perf']))

    print('***Valid***')
    test(valid_loader, valid_gallery_loader, [net, distNet], args.cuda)
    print('***Test***')
    test(test_loader, test_gallery_loader, [net, distNet], args.cuda)
    sys.exit()
示例#17
0
def main(args):

    # Step 1: init data folders
    '''if os.path.exists('save_state/'+args.regime+'/normalization_stats.pkl'):
        print('Loading normalization stats')
        x_mean, x_sd = misc.load_file('save_state/'+args.regime+'/normalization_stats.pkl')
    else:
        x_mean, x_sd = preprocess.save_normalization_stats(args.regime)
        print('x_mean: %.3f, x_sd: %.3f' % (x_mean, x_sd))'''

    val_loader=load_data(args, "val")

    tb=TensorBoard(args.model_dir)

    # Step 2: init neural networks
    print("network is:",args.net)
    if args.net == 'Reab3p16':
        model = Reab3p16(args)
    elif args.net=='RN_mlp':
        model =WildRelationNet()
    if args.gpunum > 1:
        model = nn.DataParallel(model, device_ids=range(args.gpunum))

    weights_path = args.path_weight+"/"+args.load_weight

    if os.path.exists(weights_path) and args.restore:
        pretrained_dict = torch.load(weights_path)
        model_dict = model.state_dict()
        pretrained_dict1 = {}
        for k, v in pretrained_dict.items():
            if k in model_dict:
                pretrained_dict1[k] = v
                #print(k)
        model_dict.update(pretrained_dict1)
        model.load_state_dict(model_dict)

        print('load weight')

    style_raven={65:0, 129:1, 257:2, 66:3, 132:4, 36:5, 258:6, 136:7, 264:8, 72:9, 130:10
         , 260:11, 40:12, 34:13, 49:14, 18:15, 20:16, 24:17}
    model.cuda()
    optimizer = optim.SGD(model.parameters(), lr=args.lr,momentum=args.mo, weight_decay=5e-4)
   
    if args.gpunum>1:
        optimizer = nn.DataParallel(optimizer, device_ids=range(args.gpunum))

    iter_count = 1
    epoch_count = 1
    #iter_epoch=int(len(train_files) / args.batch_size)
    print(time.strftime('%H:%M:%S', time.localtime(time.time())), 'training')
    style_raven_len = len(style_raven)
    
    if args.rl_style=="dqn":
        dqn = DQN()
    elif args.rl_style=="ddpg":
        ram = MemoryBuffer(1000)
        ddpg = Trainer(style_raven_len*4+2, style_raven_len, 1, ram)
    alpha_1=0.1

    if args.rl_style=="dqn":
        a = dqn.choose_action([0.5] * 3)  # TODO
    elif args.rl_style=="ddpg":
        action_ = ddpg.get_exploration_action(np.zeros([style_raven_len*4+2]).astype(np.float32),alpha_1)
    if args.type_loss:loss_fn=nn.BCELoss()
    best_acc=0.0
    while True:
        since=time.time()
        print(action_)
        for i in range(style_raven_len):
            tb.scalar_summary("action/a"+str(i), action_[i], epoch_count)

        data_files = preprocess.provide_data(args.regime, style_raven_len, action_,style_raven)

        train_files = [data_file for data_file in data_files if 'train' in data_file]
        print("train_num:", len(train_files))
        train_loader = torch.utils.data.DataLoader(Dataset(args,train_files), batch_size=args.batch_size, shuffle=True,
                                                   num_workers=args.numwork)
        model.train()
        iter_epoch = int(len(train_files) / args.batch_size)
        acc_part_train=np.zeros([style_raven_len,2]).astype(np.float32)

        mean_loss_train= np.zeros([style_raven_len, 2]).astype(np.float32)
        loss_train=0
        for x, y,style,me in train_loader:
            if x.shape[0]<10:
                print(x.shape[0])
                break
            x, y ,meta = Variable(x).cuda(), Variable(y).cuda(), Variable(me).cuda()
            if args.gpunum > 1:
                optimizer.module.zero_grad()
            else:
                optimizer.zero_grad()
            if args.type_loss:
                pred_train, pred_meta= model(x)
            else:
                pred_train = model(x)
            loss_ = F.nll_loss(pred_train, y,reduce=False)
            loss=loss_.mean() if not args.type_loss else loss_.mean()+10*loss_fn(pred_meta,meta)
            loss.backward()
            if args.gpunum > 1:
                optimizer.module.step()
            else:
                optimizer.step()
            iter_count += 1
            pred = pred_train.data.max(1)[1]
            correct = pred.eq(y.data).cpu()
            loss_train+=loss.item()
            for num, style_pers in enumerate(style):
                style_pers = style_pers[:-4].split("/")[-1].split("_")[3:]
                for style_per in style_pers:
                    style_per=int(style_per)
                    if correct[num] == 1:
                        acc_part_train[style_per, 0] += 1
                    acc_part_train[style_per, 1] += 1
                    #mean_pred_train[style_per,0] += pred_train[num,y[num].item()].data.cpu()
                    #mean_pred_train[style_per, 1] += 1
                    mean_loss_train[style_per,0] += loss_[num].item()
                    mean_loss_train[style_per, 1] += 1
            accuracy_total = correct.sum() * 100.0 / len(y)

            if iter_count %10 == 0:
                iter_c = iter_count % iter_epoch
                print(time.strftime('%H:%M:%S', time.localtime(time.time())),
                      ('train_epoch:%d,iter_count:%d/%d, loss:%.3f, acc:%.1f') % (
                      epoch_count, iter_c, iter_epoch, loss, accuracy_total))
                tb.scalar_summary("train_loss",loss,iter_count)
        loss_train=loss_train/len(train_files)
        #mean_pred_train=[x[0]/ x[1] for x in mean_pred_train]
        mean_loss_train=[x[0]/ x[1] for x in mean_loss_train]
        acc_part_train = [x[0] / x[1] if x[1]!=0 else 0  for x in acc_part_train]
        print(acc_part_train)
        if epoch_count %args.lr_step ==0:
            print("change lr")
            adjust_learning_rate(optimizer, epoch_count, args.lr_step,args.gpunum)
        time_elapsed = time.time() - since
        print('train epoch in {:.0f}h {:.0f}m {:.0f}s'.format(
            time_elapsed // 3600, time_elapsed // 60 % 60, time_elapsed % 60))
        #acc_p=np.array([x[0]/x[1] for x in acc_part])
        #print(acc_p)
        with torch.no_grad():
            model.eval()
            accuracy_all = []
            iter_test=0
            acc_part_val = np.zeros([style_raven_len, 2]).astype(np.float32)
            for x, y, style,me in val_loader:
                iter_test+=1
                x, y = Variable(x).cuda(), Variable(y).cuda()
                pred,_ = model(x)
                pred = pred.data.max(1)[1]
                correct = pred.eq(y.data).cpu().numpy()
                accuracy = correct.sum() * 100.0 / len(y)
                for num, style_pers in enumerate(style):
                    style_pers = style_pers[:-4].split("/")[-1].split("_")[3:]
                    for style_per in style_pers:
                        style_per = int(style_per)
                        if correct[num] == 1:
                            acc_part_val[style_per, 0] += 1
                        acc_part_val[style_per, 1] += 1
                accuracy_all.append(accuracy)

                # if iter_test % 10 == 0:
                #
                #     print(time.strftime('%H:%M:%S', time.localtime(time.time())),
                #           ('test_iter:%d, acc:%.1f') % (
                #               iter_test, accuracy))

        accuracy_all = sum(accuracy_all) / len(accuracy_all)
        acc_part_val = [x[0] / x[1] if x[1]!=0 else 0 for x in acc_part_val ]
        baseline_rl=70
        reward=np.mean(acc_part_val)*100-baseline_rl
        tb.scalar_summary("valreward", reward,epoch_count)
        action_list=[x for x in a]
        cur_state=np.array(acc_part_val+acc_part_train+action_list+mean_loss_train
                           +[loss_train]+[epoch_count]).astype(np.float32)
        #np.expand_dims(, axis=0)
        if args.rl_style == "dqn":
            a = dqn.choose_action(cur_state)  # TODO
        elif args.rl_style == "ddpg":
            a = ddpg.get_exploration_action(cur_state,alpha_1)

        if alpha_1<1:
            alpha_1+=0.005#0.1
        if epoch_count > 1:
            if args.rl_style == "dqn":dqn.store_transition(last_state, a, reward , cur_state)
            elif args.rl_style == "ddpg":ram.add(last_state, a, reward, cur_state)


        if epoch_count > 1:
            if args.rl_style == "dqn":dqn.learn()
            elif args.rl_style == "ddpg":loss_actor, loss_critic=ddpg.optimize()
            print('------------------------------------')
            print('learn q learning')
            print('------------------------------------')
            tb.scalar_summary("loss_actor", loss_actor, epoch_count)
            tb.scalar_summary("loss_critic", loss_critic, epoch_count)


        last_state=cur_state
        time_elapsed = time.time() - since
        print('test epoch in {:.0f}h {:.0f}m {:.0f}s'.format(
            time_elapsed // 3600, time_elapsed // 60 % 60, time_elapsed % 60))
        print('------------------------------------')
        print(('epoch:%d, acc:%.1f') % (epoch_count, accuracy_all))
        print('------------------------------------')
        if accuracy_all>best_acc:
            best_acc=max(best_acc,accuracy_all)
            #ddpg.save_models(args.model_dir + '/', epoch_count)
            save_state(model.state_dict(), args.model_dir + "/epochbest")
        epoch_count += 1
        if epoch_count%20==0:
            print("save weights")
            ddpg.save_models(args.model_dir+'/',epoch_count )
            save_state(model.state_dict(), args.model_dir+"/epoch"+str(epoch_count))
示例#18
0
def main(unused_argv):
    tf.logging.set_verbosity(tf.logging.INFO)
    eval_type = FLAGS.eval_type

    imagenet_pretrained = FLAGS.imagenet_pretrained

    NUM_CLASSES = 400
    if eval_type == 'rgb600':
        NUM_CLASSES = 600

    if eval_type not in ['rgb', 'rgb600', 'flow', 'joint']:
        raise ValueError('Bad `eval_type`, must be one of rgb, rgb600, flow, joint')

    if eval_type == 'rgb600':
        kinetics_classes = [x.strip() for x in open(_LABEL_MAP_PATH_600)]
    else:
        kinetics_classes = [x.strip() for x in open(_LABEL_MAP_PATH)]

    if eval_type in ['rgb', 'rgb600', 'joint']:
        # RGB input has 3 channels.
        rgb_input = tf.placeholder(
            tf.float32,
            shape=(1, _SAMPLE_VIDEO_FRAMES, _IMAGE_SIZE, _IMAGE_SIZE, 3))

        with tf.variable_scope('RGB'):
            rgb_model = i3d.InceptionI3d(
                NUM_CLASSES, spatial_squeeze=True, final_endpoint='Logits')
            rgb_logits, _ = rgb_model(
                rgb_input, is_training=False, dropout_keep_prob=1.0)

        rgb_variable_map = {}
        for variable in tf.global_variables():

            if variable.name.split('/')[0] == 'RGB':
                if eval_type == 'rgb600':
                    rgb_variable_map[variable.name.replace(':0', '')[len('RGB/inception_i3d/'):]] = variable
                else:
                    rgb_variable_map[variable.name.replace(':0', '')] = variable

        rgb_saver = tf.train.Saver(var_list=rgb_variable_map, reshape=True)

    if eval_type in ['flow', 'joint']:
        # Flow input has only 2 channels.
        flow_input = tf.placeholder(
            tf.float32,
            shape=(1, _SAMPLE_VIDEO_FRAMES, _IMAGE_SIZE, _IMAGE_SIZE, 2))
        with tf.variable_scope('Flow'):
            flow_model = i3d.InceptionI3d(
                NUM_CLASSES, spatial_squeeze=True, final_endpoint='Logits')
            flow_logits, _ = flow_model(
                flow_input, is_training=False, dropout_keep_prob=1.0)
        flow_variable_map = {}
        for variable in tf.global_variables():
            if variable.name.split('/')[0] == 'Flow':
                flow_variable_map[variable.name.replace(':0', '')] = variable
        flow_saver = tf.train.Saver(var_list=flow_variable_map, reshape=True)

    if eval_type == 'rgb' or eval_type == 'rgb600':
        model_logits = rgb_logits
    elif eval_type == 'flow':
        model_logits = flow_logits
    else:
        model_logits = rgb_logits + flow_logits
    model_predictions = tf.nn.softmax(model_logits)

    with tf.Session() as sess:

        feed_dict = {}
        if eval_type in ['rgb', 'rgb600', 'joint']:
            if imagenet_pretrained:
                rgb_saver.restore(sess, _CHECKPOINT_PATHS['rgb_imagenet'])
            else:
                rgb_saver.restore(sess, _CHECKPOINT_PATHS[eval_type])
            tf.logging.info('RGB checkpoint restored')

            sample_pool = os.listdir(_SAMPLE_ROOT_)
            sample_pool.sort()

            results = []

            for vid_name in tqdm(sample_pool):
                rgb_sample = load_data(os.path.join(_SAMPLE_ROOT_, vid_name))
                #tf.logging.info('RGB data loaded, shape=%s', str(rgb_sample.shape))
                feed_dict[rgb_input] = rgb_sample

                # if eval_type in ['flow', 'joint']:
                #     if imagenet_pretrained:
                #         flow_saver.restore(sess, _CHECKPOINT_PATHS['flow_imagenet'])
                #     else:
                #         flow_saver.restore(sess, _CHECKPOINT_PATHS['flow'])
                #     tf.logging.info('Flow checkpoint restored')
                #     flow_sample = np.load(_SAMPLE_PATHS['flow'])
                #     tf.logging.info('Flow data loaded, shape=%s', str(flow_sample.shape))
                #     feed_dict[flow_input] = flow_sample

                out_logits, out_predictions = sess.run(
                    [model_logits, model_predictions],
                    feed_dict=feed_dict)

                out_logits = out_logits[0]
                out_predictions = out_predictions[0]

                results.append(out_predictions)

        plot_results(results, ap_num=4, full_range=360, grad=15, variable='ForTest', class_id=260)
        gt_labels = [260]*len(results)
        top1, top5 = top_k_accuracy(results, gt_labels, k=(1, 5))
        print("Top-1 Accuracy = {:.02f}".format(top1 * 100))
        print("Top-5 Accuracy = {:.02f}".format(top5 * 100))
示例#19
0
def main(unused_argv):
    tf.logging.set_verbosity(tf.logging.INFO)
    eval_type = FLAGS.eval_type

    imagenet_pretrained = FLAGS.imagenet_pretrained

    NUM_CLASSES = 400
    if eval_type == 'rgb600':
        NUM_CLASSES = 600

    if eval_type not in ['rgb', 'rgb600', 'flow', 'joint']:
        raise ValueError('Bad `eval_type`, must be one of rgb, rgb600, flow, joint')

    if eval_type == 'rgb600':
        kinetics_classes = [x.strip() for x in open(_LABEL_MAP_PATH_600)]
    else:
        kinetics_classes = [x.strip() for x in open(_LABEL_MAP_PATH)]

    if eval_type in ['rgb', 'rgb600', 'joint']:
        # RGB input has 3 channels.
        rgb_input = tf.placeholder(
            tf.float32,
            shape=(1, _SAMPLE_VIDEO_FRAMES, _IMAGE_SIZE, _IMAGE_SIZE, 3))

        with tf.variable_scope('RGB'):
            rgb_model = i3d.InceptionI3d(
                NUM_CLASSES, spatial_squeeze=True, final_endpoint='Logits')
            rgb_logits, _ = rgb_model(
                rgb_input, is_training=False, dropout_keep_prob=1.0)

        rgb_variable_map = {}
        for variable in tf.global_variables():

            if variable.name.split('/')[0] == 'RGB':
                if eval_type == 'rgb600':
                    rgb_variable_map[variable.name.replace(':0', '')[len('RGB/inception_i3d/'):]] = variable
                else:
                    rgb_variable_map[variable.name.replace(':0', '')] = variable

        rgb_saver = tf.train.Saver(var_list=rgb_variable_map, reshape=True)

    if eval_type in ['flow', 'joint']:
        # Flow input has only 2 channels.
        flow_input = tf.placeholder(
            tf.float32,
            shape=(1, _SAMPLE_VIDEO_FRAMES, _IMAGE_SIZE, _IMAGE_SIZE, 2))
        with tf.variable_scope('Flow'):
            flow_model = i3d.InceptionI3d(
                NUM_CLASSES, spatial_squeeze=True, final_endpoint='Logits')
            flow_logits, _ = flow_model(
                flow_input, is_training=False, dropout_keep_prob=1.0)
        flow_variable_map = {}
        for variable in tf.global_variables():
            if variable.name.split('/')[0] == 'Flow':
                flow_variable_map[variable.name.replace(':0', '')] = variable
        flow_saver = tf.train.Saver(var_list=flow_variable_map, reshape=True)

    if eval_type == 'rgb' or eval_type == 'rgb600':
        model_logits = rgb_logits
    elif eval_type == 'flow':
        model_logits = flow_logits
    else:
        model_logits = rgb_logits + flow_logits
    model_predictions = tf.nn.softmax(model_logits)

    csvFile = open('transfer_ucf101.csv', 'a')
    writer = csv.writer(csvFile)
    writer.writerow(['Actnames', 'Top-1 accuracy', 'Top-5 accuracy'])

    with tf.Session() as sess:

        feed_dict = {}
        if eval_type in ['rgb', 'rgb600', 'joint']:
            if imagenet_pretrained:
                rgb_saver.restore(sess, _CHECKPOINT_PATHS['rgb_imagenet'])
            else:
                rgb_saver.restore(sess, _CHECKPOINT_PATHS[eval_type])
            tf.logging.info('RGB checkpoint restored')

            listfile = open(_TESTLIST_PATH_, 'r')
            testlist = listfile.readlines()
            actnames = []

            gt_labels = []
            results = []

            for item in tqdm(testlist):

                sample = item.split('\n')[0]
                video_name = sample.split(' ')[0]
                video_label = int(sample.split(' ')[1])
                class_name = video_name.split('/')[0]
                gt_labels.append(video_label)
                actnames.append(class_name)

                rgb_sample = load_data(os.path.join(_SAMPLE_ROOT_, video_name))
                #tf.logging.info('RGB data loaded, shape=%s', str(rgb_sample.shape))
                feed_dict[rgb_input] = rgb_sample

                out_logits, out_predictions = sess.run(
                    [model_logits, model_predictions],
                    feed_dict=feed_dict)

                #out_logits = out_logits[0]
                out_predictions = out_predictions[0]

                results.append(out_predictions)

            assert len(results) % _VIDS_PER_CLASS_ == 0
            class_num = len(results) // _VIDS_PER_CLASS_

            for idx in range(0, class_num):

                 preds = results[idx*_VIDS_PER_CLASS_ : (idx+1)*_VIDS_PER_CLASS_]
                 labels = gt_labels[idx*_VIDS_PER_CLASS_ : (idx+1)*_VIDS_PER_CLASS_]

                 top1, top5 = top_k_accuracy(preds, labels, k=(1, 5))
                 row = [actnames[idx*_VIDS_PER_CLASS_],
                   "{:.4f}".format(top1),  "{:.4f}".format(top5)]
                 writer.writerow(row)

    csvFile.close()
示例#20
0
    input_size = 28 * 28
    output_size = 10

elif name_dataset == "diabetes":
    modelname = "natural_5"
    modelname_pgd = "pgd_5"

    batch_size_train = 10
    batch_size_test = 10
    test_size_abs = 77
    hidden_layers = [20, 20]
    hist = False
    hist_str = "_hist" if hist else ""

    dataset = load_data(name_dataset)

    #Normalize data between 0-1
    atts = dataset.columns[0:dataset.shape[1] - 1]
    dataset[atts] = preprocessing.normalize(dataset[atts])

    if os.path.isfile("../data/splits/" + name_dataset + "_trainsplit.pkl"):
        print("Split loading")
        with open("../data/splits/" + name_dataset + "_trainsplit.pkl",
                  'rb') as f:
            train = pickle.load(f)
        with open("../data/splits/" + name_dataset + "_testsplit.pkl",
                  'rb') as f:
            test = pickle.load(f)
    else:
        train, test = train_test_split(dataset,
示例#21
0
def main():
    print('Prepare data')
    train_loader, valid_loader, valid_gallery_loader, test_loader, test_gallery_loader, in_size = load_data(
        args.dataset,
        args.data_path,
        batch_size=args.batch_size,
        prefetch=args.prefetch,
        set_partition=args.set_partition)

    distance = realdistance.HausdorffEditDistance(alpha=args.alpha,
                                                  beta=args.beta,
                                                  tau_n=args.tau_n,
                                                  tau_e=args.tau_e)

    print('Check CUDA')
    if args.cuda and args.ngpu > 1:
        distance = torch.nn.DataParallel(distance,
                                         device_ids=list(range(args.ngpu)))

    if args.cuda:
        distance = distance.cuda()

    print('***Test***')
    test(test_loader, test_gallery_loader, distance, args.cuda)
def run_tpot_idn_optimization(gens,
                              pop_size,
                              max_time_mins,
                              sample,
                              sample_size,
                              export_best_pipeline=True):


    # Initiate logging - overwrite existing log file if exists
    logging.basicConfig(filename="tpot_idn_pipeline.log", 
                        level=logging.INFO, 
                        filemode='w')

    # Load and transform the training data
    X_train, y_train, w_train = load_data(TRAIN_PATH)

    # Load and transform the test data
    X_test, y_test, w_test = load_data(TEST_PATH)

    logging.info("Train and test data loaded")

    orig_index = X_train.index
    # Reduce training set if testing sample
    if sample:
        orig_train_size = X_train.shape[0]
        X_train = (X_train.reset_index()
                          .sample(sample_size, random_state=0)
                          .set_index('hid'))

        logging.info(f"Training samples reduced from {orig_train_size} to {X_train.shape[0]}")

    # Assert consistency before training
    assert X_train.shape[1] == X_test.shape[1]

    # Enforce consistency between features and labels
    y_train = pd.DataFrame(y_train, index=orig_index).loc[X_train.index]
    y_train = np.ravel(y_train.values)

    if max_time_mins is not None:
        max_time_mins = int(max_time_mins)

    # Instantiate tpot classifier
    logging.info("Instantiating classifier")
    tpot = TPOTClassifier(generations=gens,
                          population_size=pop_size,
                          max_time_mins=max_time_mins,
                          scoring='f1',
                          verbosity=2,
                          n_jobs=-1,
#                          memory='auto',
                          random_state=2017,
#                          early_stop=6,
                          subsample=0.5)

    # Fit the classifier
    start_time = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime())
    logging.info(f"Fitting classifier:\t\t{start_time}")
    
    tpot.fit(X_train, y_train, w_train)
    
    end_time = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime())
    logging.info(f"Fitting complete:\t\t{end_time}")
    
    # Export the pipeline
    logging.info("Exporting best pipeline...")
    if export_best_pipeline:
        tpot.export('tpot_idn_pipeline.py')

    # Record f1 score on test set
    logging.info(f"Test f1 score:\t{tpot.score(X_test, y_test)}")
def setUpGlobal(cls):
    a, b, c, d = load_data("./data/datasets/75191")
    cls.basemodels_preds = a
    cls.train_y = b
    cls.test_preds = c
    cls.test_y = d
示例#24
0
def run():
    torch.multiprocessing.freeze_support()
    device = torch.device('cpu')

    weights_path = "C:\\Users\\Hertz\\Documents\\SJSU Coursework\\MS Project_big files\\git\\distracting_feature\\distracting_feature\\epochs\\epoch860"

    weights_path = "C:\\Users\\Hertz\\Documents\\SJSU Coursework\\MS Project_big files\\git\\distracting_feature\\distracting_feature\\epochs\\epoch860"  # got ~20 percent here.

    weights_path = "C:\\Users\\Hertz\\Documents\\SJSU Coursework\\MS Project_big files\\git\\distracting_feature\\distracting_feature\\epochs\\epochbest(73.6)"

    #model_path = "C:\\Users\\Hertz\\Documents\\SJSU Coursework\\MS Project_big files\\git\\distracting_feature\\distracting_feature\\epochs\\epoch860" # got ~70 percent here.

    #image_path = "C:\\Users\\sonam\\Desktop\\MS_Project\\test_model\\RAVEN_1368_test\\image.npy"
    # image_path = os.path.join(RAVEN_folder, file_folder)
    # image_path = str(RAVEN_folder)+"/"+ str(file)
    # image_path = "C:/Users/Hertz/Documents/SJSU Coursework/MS Project_big files/RAVEN-10000-release/RAVEN-10000/center_single/RAVEN_10_train.npz"
    image_path = "C:/Users/Hertz/Documents/SJSU Coursework/MS Project_big files/RAVEN-10000-release/RAVEN-10000/"
    ap = argparse.ArgumentParser()
    #ap.add_argument("type_loss", type=bool)
    #ap.add_argument("image_path", type=str)
    ap.add_argument('--type_loss', type=bool, default=True)
    ap.add_argument('--image_path', type=str, default=image_path)
    ap.add_argument('--regime', type=str, default='all')
    ap.add_argument('--image_type', type=str, default='image')
    ap.add_argument('--batch_size', type=int, default=1)
    ap.add_argument('--numwork', dest='numwork', type=int, default=1)
    args = ap.parse_args()

    # args = config.get_args()
    # args = args[0]
    # weights_path = args.path_weight+"/"+args.load_weight

    pretrained_dict = torch.load(weights_path, map_location=device)
    # print("pretrained_dict:", pretrained_dict)
    r_model = m.Reab3p16(args)
    model_dict = r_model.state_dict(
    )  ## https://pytorch.org/tutorials/recipes/recipes/what_is_state_dict.htmlA state_dict is an integral entity
    pretrained_dict1 = {
    }  ##..if you are interested in saving or loading models from PyTorch
    for k, v in pretrained_dict.items():  ##filter out unnecessary keys k
        if k[:7] == "module.":
            k = k[7:]
        if k in model_dict:  ##only when keys match(like conv2D..and so forth)
            pretrained_dict1[k] = v
            print(k)
    model_dict.update(
        pretrained_dict1)  ##overwrite entries in the existing state dict
    r_model.load_state_dict(model_dict)

    # print("pretrained_dict1:", pretrained_dict1)

    with torch.no_grad():
        r_model.eval()
        accuracy_all = []
        #data_dir = "C:/Users/Hertz/Documents/SJSU Coursework/MS Project_big files/RAVEN-10000-release/RAVEN-10000/"
        data_dir = "C:/Users/Hertz/Documents/SJSU Coursework/MS Project_big files/RAVEN 1000/"
        loader_try = load_data(args, "train", data_dir)

        # print(loader_try)

        # data_split = "train"
        # data_files = [image_path]
        # print("datafiles: ", data_files)

        # df = [data_file for data_file in data_files][:]
        # print("df: ", df)
        loader = torch.utils.data.DataLoader(Dataset(args, loader_try),
                                             batch_size=args.batch_size,
                                             num_workers=args.numwork)

        # checkpoint = torch.load(model_path, map_location=device)
        count = 0
        for x, y, style, me in loader_try:
            count = count + 1
            #print("count", count)
            # print("style:", style)
            x, y = Variable(x), Variable(y)
            pred = r_model(x)
            # print("pred:", pred)
            pred = pred[0].data.max(1)[1]
            # print("pred:", pred)
            # print("y",y)
            correct = pred.eq(y.data).cpu().numpy()
            accuracy = correct.sum() * 100.0 / len(y)
            print("accuracy", accuracy)
            accuracy_all.append(accuracy)
        accuracy_all = sum(accuracy_all) / len(accuracy_all)
        print(accuracy_all)
                        help="Input text file")
    parser.add_argument("-w",
                        "--weights",
                        action="store",
                        required=False,
                        dest="weights",
                        help="Model weights path")
    parser.add_argument("-i",
                        "--input",
                        action="store",
                        required=False,
                        dest="input",
                        help="Input string for complete")
    parser.add_argument("-o",
                        "--out_len",
                        action="store",
                        required=False,
                        dest="out_len",
                        help="Out length")
    args = parser.parse_args()

    _, _, vectorizer = load_data.load_data(args.text, False, False, False, 1)
    model = model.make_text_generator_model(1, vectorizer.vocab_size)
    model.load_weights(args.weights)

    res = generate(model,
                   vectorizer,
                   seed=args.input,
                   length=int(args.out_len))
    print(res)
    parser.add_argument("-v",
                        "--validaion_rate",
                        action="store",
                        required=False,
                        dest="validaion_rate",
                        help="Rate validation data")

    args = parser.parse_args()

    if args.batch_size is not None:
        config.batch_size = int(args.batch_size)
    if args.epochs is not None:
        config.num_epochs = int(args.epochs)
    if args.use_words is not None:
        config.use_words = bool(args.use_words)
    if args.save_path is not None:
        config.save_path = args.save_path
    if args.validaion_rate is not None:
        config.validation_rate = args.validation_rate

    x_train, y_train, vectorizer = load_data.load_data(args.text,
                                                       config.use_words, False,
                                                       False,
                                                       config.batch_size,
                                                       config.seq_length)

    generator = model.make_text_generator_model(
        batch_size=config.batch_size, vocab_size=vectorizer.vocab_size)

    train(generator, x_train, y_train)
示例#27
0
def main(args):

    # Step 1: init data folders
    '''if os.path.exists('save_state/'+args.regime+'/normalization_stats.pkl'):           ##to load raw data and preprocess it 
        print('Loading normalization stats')
        x_mean, x_sd = misc.load_file('save_state/'+args.regime+'/normalization_stats.pkl')
    else:
        x_mean, x_sd = preprocess.save_normalization_stats(args.regime)
        print('x_mean: %.3f, x_sd: %.3f' % (x_mean, x_sd))'''

    val_loader=load_data(args, "val")              ##loading already preprocessed validation/testing data 

    tb=TensorBoard(args.model_dir)                ##The model_dir arguments represents the directory to save model parameters, graph and etc. This can also be used to 
                                                  ##load checkpoints from the directory into a estimator to continue training a previously saved model.

    # Step 2: init neural networks
    print("network is:",args.net)
    if args.net == 'Reab3p16':                ##if want to use model Reab3p16
        model = Reab3p16(args)
    elif args.net=='RN_mlp':                  ##if want to use model WildRelationNet
        model =WildRelationNet()
    if args.gpunum > 1:                        
        model = nn.DataParallel(model, device_ids=range(args.gpunum)) ##The nn package defines a set of Modules, which you can think of as a neural network layer that has produces output from 
                                                                       ##input and may have some trainable weights.
                                                                    ##when more than one gpu, want to save model weights using DataParrallel module prefix
    weights_path = args.path_weight+"/"+args.load_weight               ##saved weigths of model 

    if os.path.exists(weights_path) and args.restore:             ##pretrained weights
        pretrained_dict = torch.load(weights_path)                 ##pretrained_dict is the state dictionary of the pre-trained model available
        model_dict = model.state_dict()                           ## https://pytorch.org/tutorials/recipes/recipes/what_is_state_dict.htmlA state_dict is an integral entity 
        pretrained_dict1 = {}                                      ##..if you are interested in saving or loading models from PyTorch
        for k, v in pretrained_dict.items():                      ##filter out unnecessary keys k
            if k in model_dict:                                   ##only when keys match(like conv2D..and so forth)
                pretrained_dict1[k] = v
                #print(k)                   
        model_dict.update(pretrained_dict1)                        ##overwrite entries in the existing state dict 
        model.load_state_dict(model_dict)                          ##load the new state dict, new weights

        print('load weight')

    style_raven={65:0, 129:1, 257:2, 66:3, 132:4, 36:5, 258:6, 136:7, 264:8, 72:9, 130:10    ##dictionary(key:value pair of      
         , 260:11, 40:12, 34:13, 49:14, 18:15, 20:16, 24:17}

##After setting weights using optimizer for training.

##The standard way in PyTorch to train a model in multiple GPUs is to use nn.DataParallel which copies the model to the GPUs 
##and during training splits the batch among them and combines the individual outputs.
##model.cuda() by default will send your model to the "current device"

#If you need to move a model to GPU via .cuda(), please do so before constructing optimizers for it. Parameters of a model 
#after .cuda() will be different objects with those before the call.

##A very popular technique that is used along with SGD is called Momentum. Instead of using only the gradient of the current 
##step to guide the search, momentum also accumulates the gradient of the past steps to determine the direction to go
    model.cuda()
    optimizer = optim.SGD(model.parameters(), lr=args.lr,momentum=args.mo, weight_decay=5e-4) ##Adam has convergence problems that often SGD + momentum can converge better 
                                                                               ##with longer training time. We often see a lot of papers in 2018 and 2019 were still using SGD
    if args.gpunum>1:
        optimizer = nn.DataParallel(optimizer, device_ids=range(args.gpunum))
                                  ##setting iter-count and epoch to 1 before starting training
    iter_count = 1               ## number of batches of data the algorithm has seen (or simply the number of passes the algorithm has done on the dataset)
    epoch_count = 1              ##number of times a learning algorithm sees the complete dataset 
    #iter_epoch=int(len(train_files) / args.batch_size)
    print(time.strftime('%H:%M:%S', time.localtime(time.time())), 'training')
    style_raven_len = len(style_raven)  ##length of  style raven dict
    
    if args.rl_style=="dqn":     ##calling reinforcemt model for training
        dqn = DQN()                ##if want to use dqn model
    elif args.rl_style=="ddpg":    ##if want to use ddpg model (aiming to use this)
        ram = MemoryBuffer(1000)   
        ddpg = Trainer(style_raven_len*4+2, style_raven_len, 1, ram)        ##creating an instance of Trainer class defined  in rl folder (ddpg.py) why style_raven_len*4+2? 
    alpha_1=0.1

    if args.rl_style=="dqn":
        a = dqn.choose_action([0.5] * 3)  # TODO
    elif args.rl_style=="ddpg":
        action_ = ddpg.get_exploration_action(np.zeros([style_raven_len*4+2]).astype(np.float32),alpha_1) ##calling exploration which returns action? 
    if args.type_loss:loss_fn=nn.BCELoss()                      ##Creates a criterion that measures the Binary Cross Entropy between the target and the output.
    best_acc=0.0                                                ##setting accuracy to 0.0
    while True:                                                ##loop(train)  until
        since=time.time()
        print(action_)                                            
        for i in range(style_raven_len):                
            tb.scalar_summary("action/a"+str(i), action_[i], epoch_count) ##saving summary such as poch counts and actions

        data_files = preprocess.provide_data(args.regime, style_raven_len, action_,style_raven) 

        train_files = [data_file for data_file in data_files if 'train' in data_file]               #creating a list of training files
        print("train_num:", len(train_files))
    
        ##torch.utils.data.DataLoader` supports both map-style and iterable-style datasets with single- or multi-process loading,
        ##customizing loading order and optional automatic batching (collation) and memory pinning
        ##shuffle true because we want independent B training batches from Dataset
        train_loader = torch.utils.data.DataLoader(Dataset(args,train_files), batch_size=args.batch_size, shuffle=True,  
                                                   num_workers=args.numwork)
        model.train()                      ##start training model
        iter_epoch = int(len(train_files) / args.batch_size)         ##setting iteration count for total dataset
        acc_part_train=np.zeros([style_raven_len,2]).astype(np.float32)       ##defining variable for saving part accuracy while training

        mean_loss_train= np.zeros([style_raven_len, 2]).astype(np.float32)     ##defining variable for saving mean loss while training
        loss_train=0
        for x, y,style,me in train_loader:                              
            if x.shape[0]<10:                             ##x.shape[0] will give the number of rows in an array  (10 by 1024 2D array)                 
                print(x.shape[0])
                break                                                            
            x, y ,meta = Variable(x).cuda(), Variable(y).cuda(), Variable(me).cuda()  ##Components are accessible as variable.x,  variable.y,  variable.z
            if args.gpunum > 1:                                                        
                optimizer.module.zero_grad()             ##to set the gradient of the parameters in the model to 0, module beacause DataParallel
            else:
                optimizer.zero_grad()                    ## same as above set the gradient of the parameters to zero
            if args.type_loss:
                pred_train, pred_meta= model(x)              ##applying model to x where x is from training data
            else:
                pred_train = model(x)                        ##x is images y is actual label/category
            loss_ = F.nll_loss(pred_train, y,reduce=False)     ##calculating loss occurred while training
            loss=loss_.mean() if not args.type_loss else loss_.mean()+10*loss_fn(pred_meta,meta)##If your loss is not a scalar value, then you should certainly use either 
            loss.backward()             ##loss.mean() or loss.sum() to convert it to a scalar before calling the backward. Otherwise, it will cause an error
        
        #When you call loss.backward(), all it does is compute gradient of loss w.r.t all the parameters in loss that have 
        ##requires_grad = True and store them in parameter.grad attribute for every parameter.
        ##optimizer.step() updates all the parameters based on parameter.grad
            if args.gpunum > 1:
                optimizer.module.step()      ##module for DataParallel
            else:
                optimizer.step()
            iter_count += 1                ##update iter-count by 1 evrytime
            pred = pred_train.data.max(1)[1]  
            correct = pred.eq(y.data).cpu()       ##compare actual and predicted category
            loss_train+=loss.item()               ##The average of the batch losses will give you an estimate of the “epoch loss” during training.
            for num, style_pers in enumerate(style):
                style_pers = style_pers[:-4].split("/")[-1].split("_")[3:]
                for style_per in style_pers:
                    style_per=int(style_per)
                    if correct[num] == 1:
                        acc_part_train[style_per, 0] += 1
                    acc_part_train[style_per, 1] += 1
                    #mean_pred_train[style_per,0] += pred_train[num,y[num].item()].data.cpu()
                    #mean_pred_train[style_per, 1] += 1
                    mean_loss_train[style_per,0] += loss_[num].item()
                    mean_loss_train[style_per, 1] += 1
            accuracy_total = correct.sum() * 100.0 / len(y)       ####calc accuracy 

            if iter_count %10 == 0:                        ##do this for 10 iterations
                iter_c = iter_count % iter_epoch
                print(time.strftime('%H:%M:%S', time.localtime(time.time())),
                      ('train_epoch:%d,iter_count:%d/%d, loss:%.3f, acc:%.1f') % (
                      epoch_count, iter_c, iter_epoch, loss, accuracy_total))
                tb.scalar_summary("train_loss",loss,iter_count)               ##saving train loss to summary
        loss_train=loss_train/len(train_files)                             ##The average of the batch losses will give you an estimate of the “epoch loss” during training.
        #mean_pred_train=[x[0]/ x[1] for x in mean_pred_train]
        mean_loss_train=[x[0]/ x[1] for x in mean_loss_train]
        acc_part_train = [x[0] / x[1] if x[1]!=0 else 0  for x in acc_part_train]
        print(acc_part_train)
        if epoch_count %args.lr_step ==0:                 ##adjusting learning rate after  30 epochs
            print("change lr")
            adjust_learning_rate(optimizer, epoch_count, args.lr_step,args.gpunum)
        time_elapsed = time.time() - since
        print('train epoch in {:.0f}h {:.0f}m {:.0f}s'.format(
            time_elapsed // 3600, time_elapsed // 60 % 60, time_elapsed % 60))
        #acc_p=np.array([x[0]/x[1] for x in acc_part])
        #print(acc_p)
        with torch.no_grad():
            model.eval()             ##evaluating model 
            accuracy_all = []
            iter_test=0
            acc_part_val = np.zeros([style_raven_len, 2]).astype(np.float32)
            for x, y, style,me in val_loader:             ##using validation data
                iter_test+=1
                x, y = Variable(x).cuda(), Variable(y).cuda()
                pred,_ = model(x)
                pred = pred.data.max(1)[1]
                correct = pred.eq(y.data).cpu().numpy()
                accuracy = correct.sum() * 100.0 / len(y)   ##accuracy is calc basd on how many labels match
                for num, style_pers in enumerate(style):
                    style_pers = style_pers[:-4].split("/")[-1].split("_")[3:]
                    for style_per in style_pers:
                        style_per = int(style_per)
                        if correct[num] == 1:
                            acc_part_val[style_per, 0] += 1
                        acc_part_val[style_per, 1] += 1
                accuracy_all.append(accuracy)                    ##append to accuracy list

                # if iter_test % 10 == 0:
                #
                #     print(time.strftime('%H:%M:%S', time.localtime(time.time())),
                #           ('test_iter:%d, acc:%.1f') % (
                #               iter_test, accuracy))

        accuracy_all = sum(accuracy_all) / len(accuracy_all)              ##total accuracy is calculated 
        acc_part_val = [x[0] / x[1] if x[1]!=0 else 0 for x in acc_part_val ]
        baseline_rl=70                                          ##baseline for accuracy
        reward=np.mean(acc_part_val)*100-baseline_rl          ##calculating reward using val accuracy
        tb.scalar_summary("valreward", reward,epoch_count)        ##saving summary
        action_list=[x for x in a]
        cur_state=np.array(acc_part_val+acc_part_train+action_list+mean_loss_train ##saving all calc in currnt state
                           +[loss_train]+[epoch_count]).astype(np.float32)
        #np.expand_dims(, axis=0)
        if args.rl_style == "dqn":
            a = dqn.choose_action(cur_state)  # TODO
        elif args.rl_style == "ddpg":                              ##passing current state to rl model's get_exploration_action
            a = ddpg.get_exploration_action(cur_state,alpha_1)

        if alpha_1<1:
            alpha_1+=0.005#0.1
        if epoch_count > 1:                                ##saving  last state and current state ,reward in memory  for epoch >1
            if args.rl_style == "dqn":dqn.store_transition(last_state, a, reward , cur_state)
            elif args.rl_style == "ddpg":ram.add(last_state, a, reward, cur_state)    


        if epoch_count > 1:
            if args.rl_style == "dqn":dqn.learn()
            elif args.rl_style == "ddpg":loss_actor, loss_critic=ddpg.optimize()      ##using rl ddpg model's optimize function to for teaching
            print('------------------------------------')
            print('learn q learning')
            print('------------------------------------')
            tb.scalar_summary("loss_actor", loss_actor, epoch_count)
            tb.scalar_summary("loss_critic", loss_critic, epoch_count)


        last_state=cur_state
        time_elapsed = time.time() - since
        print('test epoch in {:.0f}h {:.0f}m {:.0f}s'.format(
            time_elapsed // 3600, time_elapsed // 60 % 60, time_elapsed % 60))
        print('------------------------------------')
        print(('epoch:%d, acc:%.1f') % (epoch_count, accuracy_all))
        print('------------------------------------')
        if accuracy_all>best_acc:                                        ##save the best accuracy obtained from val data as best accuracy for next epoch
            best_acc=max(best_acc,accuracy_all)
            #ddpg.save_models(args.model_dir + '/', epoch_count)
            save_state(model.state_dict(), args.model_dir + "/epochbest")             ##saving the current state
        epoch_count += 1                                                         ##increasing  epoch count by 1
        if epoch_count%20==0:              ##Do this for 20 epochs for complete dataset 
            print("save weights")
            ddpg.save_models(args.model_dir+'/',epoch_count )                  ##saving the model
            save_state(model.state_dict(), args.model_dir+"/epoch"+str(epoch_count))
import sys

sys.path.append("../../")
from data.load_data import (get_country_filepaths,
                            split_features_labels_weights, load_data)
from features.process_features import get_vif, standardize

sys.path.append("../")
from evaluation import calculate_metrics, evaluate_model

COUNTRY = 'idn'
TRAIN_PATH, TEST_PATH, QUESTIONS_PATH = get_country_filepaths(COUNTRY)

# Load and transform the training data
X_train, y_train, w_train = load_data(TRAIN_PATH)

# Load and transform the test data
X_test, y_test, w_test = load_data(TEST_PATH)
orig_index = X_train.index

# Assert consistency before training
assert X_train.shape[1] == X_test.shape[1]

# Enforce consistency between features and labels
y_train = pd.DataFrame(y_train, index=orig_index).loc[X_train.index]
y_train = np.ravel(y_train.values)

# NOTE: Make sure that the class is labeled 'target' in the data file
# tpot_data = pd.read_csv('PATH/TO/DATA/FILE', sep='COLUMN_SEPARATOR', dtype=np.float64)
# features = tpot_data.drop('target', axis=1).values
示例#29
0
import numpy as np

from data.load_data import load_data
from utils.args import args
from utils.logger import logger
from utils.calc_hamming_ranking import calc_hamming_ranking
from dlfh_algo import dlfh_algo
from hash_func import linear_hash


if __name__ == "__main__":
    np.random.seed(args.seed)

    '''load dataset'''
    logger.info('load dataset: {}'.format(args.dataname))
    test_text, test_image, test_label, database_text, database_image, database_label = load_data()

    '''learning procedure'''
    logger.info('start training procedure')
    start_t = time.time()
    db_image_codes, db_text_codes = dlfh_algo(train_labels=database_label)

    '''out-of-sample extension'''
    wx = linear_hash(database_image, db_image_codes)
    wy = linear_hash(database_text, db_text_codes)

    end_t = time.time() - start_t

    '''start encoding'''
    test_image_codes = np.sign(test_image.dot(wx))
    test_text_codes = np.sign(test_text.dot(wy))
def trainer(train_dir_name, eval_dir_name, out_dir_name):
    '''
    Train the model
    :param train_dir_name: path to training set directory
    :param eval_dir_name: path to evaluation set directory
    :param out_dir_name: output path to save model files
    :return: None
    '''
    if not os.path.exists(out_dir_name):
        os.makedirs(out_dir_name)

    train_features, train_texts = load_data(train_dir_name)
    eval_features, eval_texts = load_data(eval_dir_name)
    steps_per_epoch = len(train_texts) / BATCH_SIZE
    print('Image file format is %s' % IMAGE_FILE_FORMAT)
    print('Keras backend file format is %s' % K.image_data_format())
    print('Training images input shape: {}'.format(train_features.shape))
    print('Evaluation images input shape: {}'.format(eval_features.shape))
    print('Training texts shape: {}'.format(len(train_texts)))
    print('Evaluation texts input shape: {}'.format(len(eval_texts)))
    print('Epoch size: {}'.format(EPOCHS))
    print('Batch size: {}'.format(BATCH_SIZE))
    print('Steps per epoch: {}'.format(int(steps_per_epoch)))
    print('Kernel Initializer: {}'.format(KERNEL_INIT))

    with open(os.path.join(out_dir_name, 'config.txt'), 'w') as fh:
        with redirect_stdout(fh):
            print('Image file format is %s' % IMAGE_FILE_FORMAT)
            print('Keras backend file format is %s' % K.image_data_format())
            print('Training images input shape: {}'.format(
                train_features.shape))
            print('Evaluation images input shape: {}'.format(
                eval_features.shape))
            print('Training texts shape: {}'.format(len(train_texts)))
            print('Evaluation texts input shape: {}'.format(len(eval_texts)))
            print('Epoch size: {}'.format(EPOCHS))
            print('Batch size: {}'.format(BATCH_SIZE))
            print('Steps per epoch: {}'.format(int(steps_per_epoch)))
            print('Kernel Initializer: {}'.format(KERNEL_INIT))

    # Prepare tokenizer to create the vocabulary
    tokenizer = Tokenizer(filters='', split=" ", lower=False)
    # Create the vocabulary
    tokenizer.fit_on_texts([load_doc('../data/code.vocab')])

    # Initialize data generators for training and validation
    train_generator = DataGenerator(train_texts,
                                    train_features,
                                    batch_size=BATCH_SIZE,
                                    tokenizer=tokenizer,
                                    shuffle=True,
                                    image_data_format=IMAGE_FILE_FORMAT)
    validation_generator = DataGenerator(eval_texts,
                                         eval_features,
                                         batch_size=BATCH_SIZE,
                                         tokenizer=tokenizer,
                                         shuffle=True,
                                         image_data_format=IMAGE_FILE_FORMAT)

    # Initialize model
    model = CodeGeneratorModel(IMAGE_SIZE,
                               out_dir_name,
                               image_file_format=IMAGE_FILE_FORMAT,
                               kernel_initializer=KERNEL_INIT)
    model.save_model()
    model.summarize()
    model.summarize_image_model()
    model.plot_model()

    if VALIDATE:
        model.fit_generator(generator=train_generator,
                            steps_per_epoch=steps_per_epoch,
                            callbacks=generate_callbacks(out_dir_name),
                            validation_data=validation_generator)
    else:
        model.fit_generator(generator=train_generator,
                            steps_per_epoch=steps_per_epoch,
                            callbacks=generate_callbacks(out_dir_name))