Beispiel #1
0
def analyse_moving():
    global image_file
    sentence = e.get()
    path = './results/moving.png'
    save_result(qq_msgs='./inputs/msgs.txt', path=path)
    image_file = tk.PhotoImage(file=path)
    canvas.itemconfig(image, image=image_file)
Beispiel #2
0
def color_adjust(x, args):
    if args.iter % args.save_intervel == 0:
        save_result(x, args)
    args.iter += 1
    # Input for VGG
    x_vgg = np.asarray(np.reshape(x, args.shape), dtype=np.float32)
    x_vgg_var = Variable(chainer.dataset.concat_examples([x_vgg], args.gpu))

    # Poisson loss
    poisson_loss = F.sum(
        F.square(args.content_laplace -
                 F.convolution_2d(x_vgg_var, W=args.W_laplace, pad=1)) *
        args.inverse_border_mask_var)

    # tv loss
    tv_loss = total_variation(x_vgg_var)

    # Content loss
    content_loss = F.sum(
        F.square((args.bg_var - x_vgg_var) * args.inverse_mask_var))

    loss = args.poisson_weight * poisson_loss + args.tv_weight * tv_loss + args.content_weight * content_loss

    # Backward
    loss.backward()
    # Transfer loss & diff from GPU to CPU
    loss = cuda.to_cpu(loss.data)
    dx = np.squeeze(cuda.to_cpu(x_vgg_var.grad))

    return loss, np.asarray(dx.flatten(), dtype=np.float64)
Beispiel #3
0
def simple_regression(x_train, x_test, y_train, y_test):
    # 线性回归
    # model = LinearRegression()

    # 岭回归
    # model = Ridge(alpha=0.1)

    # lasso 回归
    model = Lasso(alpha=0.1)

    # kernal ridge
    # model = KernelRidge(kernel='rbf', alpha=0.1)
    # model = KernelRidge(kernel='linear', alpha=0.1)
    # model = KernelRidge(kernel='sigmoid', alpha=0.1)
    # model = KernelRidge(kernel='poly', alpha=0.1)
    # model = KernelRidge(kernel='laplacian', alpha=0.1)
    # model = KernelRidge(kernel='cosine', alpha=0.1)

    # 不知道为什么用不了
    # model = KernelRidge(kernel='chi2', alpha=0.1)
    # model = KernelRidge(kernel='additive_chi2', alpha=0.1)

    model.fit(x_train, y_train)
    score = model.score(x_test, y_test)

    print("score", score)
    y_pred = model.predict(x_test)

    print("mean_squared_error", mean_squared_error(y_test, y_pred))

    plot_learning_curve(model, "rate", x_train, y_train)
    result = model.predict(test_df)
    save_result(result)
    print(result)
Beispiel #4
0
def analyse_shuoshuo():
    global image_file
    sentence = e.get()
    path = "results/my_shuoshuo_sentiment.png"
    save_result(qq_msgs="inputs/msgs.txt", path=path)
    image_file = tk.PhotoImage(file=path)
    canvas.itemconfig(image, image=image_file)
Beispiel #5
0
def val_epoch(model,
              ratings,
              negs,
              K,
              use_cuda=True,
              output=None,
              epoch=None,
              processes=1):
    if epoch is None:
        print("Initial evaluation")
    else:
        print("Epoch {} evaluation".format(epoch))

    mlperf_log.ncf_print(key=mlperf_log.EVAL_START, value=epoch)
    start = datetime.now()
    model.eval()
    if processes > 1:
        context = mp.get_context('spawn')
        _eval_one = partial(eval_one, model=model, K=K, use_cuda=use_cuda)
        with context.Pool(processes=processes) as workers:
            hits_ndcg_numpred = workers.starmap(_eval_one, zip(ratings, negs))
        hits, ndcgs, num_preds = zip(*hits_ndcg_numpred)
    else:
        hits, ndcgs, num_preds = [], [], []
        for rating, items in zip(ratings, negs):
            hit, ndcg, num_pred = eval_one(rating,
                                           items,
                                           model,
                                           K,
                                           use_cuda=use_cuda)
            hits.append(hit)
            ndcgs.append(ndcg)
            num_preds.append(num_pred)

    hits = np.array(hits, dtype=np.float32)
    ndcgs = np.array(ndcgs, dtype=np.float32)

    assert len(set(num_preds)) == 1
    num_neg = num_preds[0] - 1  # one true positive, many negatives
    mlperf_log.ncf_print(key=mlperf_log.EVAL_SIZE,
                         value={
                             "epoch": epoch,
                             "value": len(hits) * (1 + num_neg)
                         })
    mlperf_log.ncf_print(key=mlperf_log.EVAL_HP_NUM_USERS, value=len(hits))
    mlperf_log.ncf_print(key=mlperf_log.EVAL_HP_NUM_NEG, value=num_neg)

    end = datetime.now()
    if output is not None:
        result = OrderedDict()
        result['timestamp'] = datetime.now()
        result['duration'] = end - start
        result['epoch'] = epoch
        result['K'] = K
        result['hit_rate'] = np.mean(hits)
        result['NDCG'] = np.mean(ndcgs)
        utils.save_result(result, output)

    return hits, ndcgs
Beispiel #6
0
def predict_result(train_data, test_data):
  X_train = prepare_X_train(train_data)
  X_test = prepare_X_test(test_data)
  y_train = train_data['Survived']
  clf = RandomForestClassifier(n_estimators=200, random_state=241)
  clf.fit(X_train, y_train)
  predicted = clf.predict(X_test)
  save_result(predicted)
Beispiel #7
0
def val_epoch(model, x, y, dup_mask, real_indices, K, samples_per_user, num_user, output=None,
              epoch=None, loss=None, use_cuda=False):

    start = datetime.now()
    log_2 = math.log(2)

    model.eval()

    hits = torch.tensor(0.)
    ndcg = torch.tensor(0.)

    if use_cuda:
        hits = torch.tensor(0., device='cuda')
        ndcg = torch.tensor(0., device='cuda')
    else:
        hits = torch.tensor(0.)
        ndcg = torch.tensor(0.)


    with torch.no_grad():
        for i, (u,n) in enumerate(zip(x,y)):
            if use_cuda: res = model(u.cuda().view(-1), n.cuda().view(-1), sigmoid=True).detach().view(-1,samples_per_user)
            else: res = model(u.cpu().view(-1), n.cpu().view(-1), sigmoid=True).detach().view(-1,samples_per_user)
            # set duplicate results for the same item to -1 before topk
            res[dup_mask[i]] = -1
            out = torch.topk(res,K)[1]
            # topk in pytorch is stable(if not sort)
            # key(item):value(predicetion) pairs are ordered as original key(item) order
            # so we need the first position of real item(stored in real_indices) to check if it is in topk


            if use_cuda: ifzero = (out == real_indices[i].cuda().view(-1,1))
            else: ifzero = (out == real_indices[i].cpu().view(-1,1))

            hits += ifzero.sum()
            ndcg += (log_2 / (torch.nonzero(ifzero)[:,1].view(-1).to(torch.float)+2).log_()).sum()

    mlperf_log.ncf_print(key=mlperf_log.EVAL_SIZE, value={"epoch": epoch, "value": num_user * samples_per_user})
    mlperf_log.ncf_print(key=mlperf_log.EVAL_HP_NUM_USERS, value=num_user)
    mlperf_log.ncf_print(key=mlperf_log.EVAL_HP_NUM_NEG, value=samples_per_user - 1)

    end = datetime.now()

    hits = hits.item()
    ndcg = ndcg.item()

    if output is not None:
        result = OrderedDict()
        result['timestamp'] = datetime.now()
        result['duration'] = end - start
        result['epoch'] = epoch
        result['K'] = K
        result['hit_rate'] = hits/num_user
        result['NDCG'] = ndcg/num_user
        result['loss'] = loss
        utils.save_result(result, output)

    return hits/num_user, ndcg/num_user
Beispiel #8
0
def val_epoch(model,
              ratings,
              negs,
              K,
              use_cuda=True,
              output=None,
              epoch=None,
              processes=1,
              num_users=-1):
    if epoch is None:
        msglogger.info("Initial evaluation")
    else:
        msglogger.info("Epoch {} evaluation".format(epoch))
    start = datetime.now()
    model.eval()

    if num_users > 0:
        ratings = ratings[:num_users]
        negs = negs[:num_users]

    if processes > 1:
        context = mp.get_context('spawn')
        _eval_one = partial(eval_one, model=model, K=K, use_cuda=use_cuda)
        with context.Pool(processes=processes) as workers:
            hits_and_ndcg = workers.starmap(_eval_one, zip(ratings, negs))
        hits, ndcgs = zip(*hits_and_ndcg)
    else:
        hits, ndcgs = [], []
        with tqdm.tqdm(zip(ratings, negs), total=len(ratings)) as t:
            for rating, items in t:
                hit, ndcg = eval_one(rating,
                                     items,
                                     model,
                                     K,
                                     use_cuda=use_cuda)
                hits.append(hit)
                ndcgs.append(ndcg)
                steps_completed = len(hits) + 1
                if steps_completed % 100 == 0:
                    t.set_description('HR@10 = {0:.4f}, NDCG = {1:.4f}'.format(
                        np.mean(hits), np.mean(ndcgs)))

    hits = np.array(hits, dtype=np.float32)
    ndcgs = np.array(ndcgs, dtype=np.float32)

    end = datetime.now()
    if output is not None:
        result = OrderedDict()
        result['timestamp'] = datetime.now()
        result['duration'] = end - start
        result['epoch'] = epoch
        result['K'] = K
        result['hit_rate'] = np.mean(hits)
        result['NDCG'] = np.mean(ndcgs)
        utils.save_result(result, output)

    return hits, ndcgs
Beispiel #9
0
def predict_result(train_data, test_data):
  scaler = StandardScaler()
  X_train = prepare_X_train(train_data, scaler)
  X_test = prepare_X_test(test_data, scaler)
  y_train = train_data['Survived']
  clf = LogisticRegression(C=1000.0, random_state=241)
  clf.fit(X_train, y_train)
  predicted = clf.predict(X_test)
  save_result(predicted, X_test.index)
Beispiel #10
0
def run_evaluation(args, model, data_loaders, model_description, n_choices,
                   layers_types, downsample_layers):

    start = time.time()

    num_samples = utils.get_number_of_samples(args.dataset)

    all_values = {}

    device = 'cuda'

    #setting up random seeds
    utils.setup_torch(args.seed)

    #creating model skeleton based on description
    propagate_weights = []
    for layer in model_description:
        cur_weights = [0 for i in range(n_choices)]
        cur_weights[layers_types.index(layer)] = 1
        propagate_weights.append(cur_weights)

    model.propagate = propagate_weights

    #Create the computationally identical model but without multiple choice blocks (just a single path net)
    #This is needed to correctly measure MACs
    pruned_model = models.SinglePathSupernet(
        num_classes=utils.get_number_of_classes(args.dataset),
        propagate=propagate_weights,
        put_downsampling=downsample_layers)  #.to(device)
    pruned_model.propagate = propagate_weights
    inputs = torch.randn((1, 3, 32, 32))
    total_ops, total_params = profile(pruned_model, (inputs, ), verbose=True)
    all_values['MMACs'] = np.round(total_ops / (1000.0**2), 2)
    all_values['Params'] = int(total_params)

    del pruned_model
    del inputs

    ################################################
    criterion = torch.nn.CrossEntropyLoss()

    #Initialize batch normalization parameters
    utils.bn_update(device, data_loaders['train_for_bn_recalc'], model)

    val_res = utils.evaluate(device, data_loaders['val'], model, criterion,
                             num_samples['val'])
    test_res = utils.evaluate(device, data_loaders['test'], model, criterion,
                              num_samples['test'])

    all_values['val_loss'] = np.round(val_res['loss'], 3)
    all_values['val_acc'] = np.round(val_res['accuracy'], 3)
    all_values['test_loss'] = np.round(test_res['loss'], 3)
    all_values['test_acc'] = np.round(test_res['accuracy'], 3)

    print(all_values, 'time taken: %.2f sec.' % (time.time() - start))

    utils.save_result(all_values, args.dir, model_description)
Beispiel #11
0
def run(ckpt_fpath):
    checkpoint = torch.load(ckpt_fpath)
    """ Load Config """
    config = dict_to_cls(checkpoint['config'])
    """ Build Data Loader """
    if config.corpus == "MSVD":
        corpus = MSVD(config)
    elif config.corpus == "MSR-VTT":
        corpus = MSRVTT(config)
    train_iter, val_iter, test_iter, vocab = \
        corpus.train_data_loader, corpus.val_data_loader, corpus.test_data_loader, corpus.vocab
    print(
        '#vocabs: {} ({}), #words: {} ({}). Trim words which appear less than {} times.'
        .format(vocab.n_vocabs, vocab.n_vocabs_untrimmed, vocab.n_words,
                vocab.n_words_untrimmed, config.loader.min_count))
    """ Build Models """
    decoder = Decoder(rnn_type=config.decoder.rnn_type,
                      num_layers=config.decoder.rnn_num_layers,
                      num_directions=config.decoder.rnn_num_directions,
                      feat_size=config.feat.size,
                      feat_len=config.loader.frame_sample_len,
                      embedding_size=config.vocab.embedding_size,
                      hidden_size=config.decoder.rnn_hidden_size,
                      attn_size=config.decoder.rnn_attn_size,
                      output_size=vocab.n_vocabs,
                      rnn_dropout=config.decoder.rnn_dropout)
    decoder.load_state_dict(checkpoint['decoder'])
    model = CaptionGenerator(decoder, config.loader.max_caption_len, vocab)
    model = model.cuda()
    """ Train Set """
    """
    train_vid2pred = get_predicted_captions(train_iter, model, model.vocab, beam_width=5, beam_alpha=0.)
    train_vid2GTs = get_groundtruth_captions(train_iter, model.vocab)
    train_scores = score(train_vid2pred, train_vid2GTs)
    print("[TRAIN] {}".format(train_scores))
    """
    """ Validation Set """
    """
    val_vid2pred = get_predicted_captions(val_iter, model, model.vocab, beam_width=5, beam_alpha=0.)
    val_vid2GTs = get_groundtruth_captions(val_iter, model.vocab)
    val_scores = score(val_vid2pred, val_vid2GTs)
    print("[VAL] scores: {}".format(val_scores))
    """
    """ Test Set """
    test_vid2pred = get_predicted_captions(test_iter,
                                           model,
                                           model.vocab,
                                           beam_width=5,
                                           beam_alpha=0.)
    test_vid2GTs = get_groundtruth_captions(test_iter, model.vocab)
    test_scores = score(test_vid2pred, test_vid2GTs)
    print("[TEST] {}".format(test_scores))

    test_save_fpath = os.path.join(C.result_dpath,
                                   "{}_{}.csv".format(config.corpus, 'test'))
    save_result(test_vid2pred, test_vid2GTs, test_save_fpath)
def test_model(model, dataset, path_to_results):
    running_loss = 0.0
    for image_number in tqdm(range(len(dataset))):
        path_to_image = dataset.iloc[image_number]['image']
        
        image = Image.open(path_to_image).convert('L') # PIL Image grayscale
        np_image = np.array(image)[..., np.newaxis] # numpy array from PIL Image
        
        path_to_save = os.path.join(path_to_results, os.path.basename(path_to_image))
        save_result(model, np_image, FRAME_SIZE, OVERLAY_SIZE, path_to_save, figsize=(16, 9))
Beispiel #13
0
def val_epoch(model, x, y, dup_mask, real_indices, K, samples_per_user, num_user, output=None,
              epoch=None, distributed=False):

    start = datetime.now()
    log_2 = math.log(2)

    model.eval()

    with torch.no_grad():
        p = []
        for u,n in zip(x,y):
            p.append(model(u, n, sigmoid=True).detach())

        del x
        del y
        temp = torch.cat(p).view(-1,samples_per_user)
        del p
        # set duplicate results for the same item to -1 before topk
        temp[dup_mask] = -1
        out = torch.topk(temp,K)[1]
        # topk in pytorch is stable(if not sort)
        # key(item):value(predicetion) pairs are ordered as original key(item) order
        # so we need the first position of real item(stored in real_indices) to check if it is in topk
        ifzero = (out == real_indices.view(-1,1))
        hits = ifzero.sum()
        ndcg = (log_2 / (torch.nonzero(ifzero)[:,1].view(-1).to(torch.float)+2).log_()).sum()

    LOGGER.log(key=tags.EVAL_SIZE, value={"epoch": epoch, "value": num_user * samples_per_user})
    LOGGER.log(key=tags.EVAL_HP_NUM_USERS, value=num_user)
    LOGGER.log(key=tags.EVAL_HP_NUM_NEG, value=samples_per_user - 1)

    end = datetime.now()

    if distributed:
        torch.distributed.all_reduce(hits, op=torch.distributed.reduce_op.SUM)
        torch.distributed.all_reduce(ndcg, op=torch.distributed.reduce_op.SUM)

    hits = hits.item()
    ndcg = ndcg.item()

    if output is not None:
        result = OrderedDict()
        result['timestamp'] = datetime.now()
        result['duration'] = end - start
        result['epoch'] = epoch
        result['K'] = K
        result['hit_rate'] = hits/num_user
        result['NDCG'] = ndcg/num_user
        utils.save_result(result, output)

    model.train()
    return hits/num_user, ndcg/num_user
Beispiel #14
0
def train(target_distribution: Distribution) -> None:
    """Training Normalizing Flow"""

    target_distribution.save_distribution()

    normalizing_flow = NormalizingFlow(K=NORMALIZING_FLOW_LAYER_NUM)

    z_0, log_q_0 = normalizing_flow.get_placeholder()
    z_k, log_q_k = normalizing_flow.forward(z_0, log_q_0)
    loss = normalizing_flow.calc_loss(z_k, log_q_k, target_distribution)
    trainer = normalizing_flow.get_trainer(loss)
    logger.info('Calculation graph constructed')

    loss_values = []

    with tf.Session() as sess:
        logger.info('Session Start')
        sess.run(tf.global_variables_initializer())
        logger.info('All variables initialized')
        logger.info(f'Training Start (number of iterations: {ITERATION})')

        for iteration in range(ITERATION + 1):
            z_0_batch = NormalDistribution.sample(BATCH_SIZE)
            log_q_0_batch = np.log(NormalDistribution.calc_prob(z_0_batch))
            _, loss_value = sess.run([trainer, loss], {
                z_0: z_0_batch,
                log_q_0: log_q_0_batch
            })
            loss_values.append(loss_value)

            if iteration % 100 == 0:
                iteration_digits = len(str(ITERATION))
                logger.info(
                    f'Iteration:  {iteration:<{iteration_digits}}  Loss:  {loss_value}'
                )

            if iteration % SAVE_FIGURE_INTERVAL == 0:
                z_0_batch_for_visualize = NormalDistribution.sample(
                    NUMBER_OF_SAMPLES_FOR_VISUALIZE)
                log_q_0_batch_for_visualize = np.log(
                    NormalDistribution.calc_prob(z_0_batch_for_visualize))
                z_k_value = sess.run(
                    z_k, {
                        z_0: z_0_batch_for_visualize,
                        log_q_0: log_q_0_batch_for_visualize
                    })
                save_result(z_k_value, iteration, target_distribution.__name__)
                save_loss_values(loss_values, target_distribution.__name__)

        logger.info('Training Finished')

    logger.info('Session Closed')
Beispiel #15
0
def train_model(cfg, model, Xvalid, Yvalid, writer, save_dir):
    mll_max = -np.inf
    best_iter = 0
    accuracy_list = []
    mll_list_que = deque([mll_max, mll_max, mll_max])
    best_model_que = deque([model, model, model])
    for i in tdqm(range(cfg.iterations), ascii=" .oO0", bar_format="{total_time}: {percentage:.0f}%|{bar}{r_bar}"):
        if i == 0:
            for j in range(cfg.sghmc_step):
                model.sghmc_step()
            summary = model.train_hypers(tb=True)
            print("Iteration", i, end='\r')
            mll, sum_mll = model.print_sample_performance(tb=True)
            print(f"MLL: {mll}")
            # mll = struct.unpack('fff', sum_mll)[2]
            # set_trace()
            writer.add_summary(summary, global_step=i)
            writer.add_summary(sum_mll, global_step=i)
        else:
            for j in range(cfg.sghmc_step):
                model.sghmc_step()
            model.train_hypers()
            print("Iteration", i, end='\r')
            mll, _ = model.print_sample_performance(tb=True)
            print(f"MLL: {mll}")

        if np.round(mll - mll_max, decimals=5) > 0:
            # accuracy = measure_accuracy(model)
            mll_max = mll

            print('MLL increased ({:.7f} --> {:.7f}). Updating values ....'.format(mll_list_que[-1], mll_max))
            mll_list_que.append(mll)
            best_model_que.append(model)  # append best model so far
            best_model_que.popleft()  # remove worst model so far
            mll_list_que.popleft()
            best_iter = i

    # save best model
    print(f'################## save best model at {save_dir} ##################')
    model_name = f'{best_iter}_bestmodel_{mll_list_que[-1]}'
    best_model_que[-1].save(save_dir, name=model_name)

    accuracy = measure_accuracy(best_model_que[-1], Xvalid, Yvalid)
    accuracy_list.append(accuracy)
    print("Best Model Test accuracy:", accuracy)

    acc_mll_df = pd.DataFrame(accuracy_list, columns=['accuracy'])
    acc_mll_df['mll'] = mll_list_que[-1]
    save_result(acc_mll_df, save_dir, name='_mll_accuracy')
Beispiel #16
0
def test(model, result_file):
    print('Begin testing')
    model.eval()

    dataset = Text2EmojiTestset()
    testloader = DataLoader(dataset, batch_size=10000,
                            shuffle=False, num_workers=4)
    pred = []
    with torch.no_grad():
        for data in testloader:
            pred += list(predict(model(data)))
    save_result(result_file, pred)

    model.train()
    print('Testing complete')
Beispiel #17
0
def home():
    if request.method=="GET":
        return render_template("form.html")
    else:
        button=request.form['button']
        if button=='Cancel':
            return redirect(url_for('home'))
        elif button=='Results':
            return redirect(url_for('results'))
                          
        name=request.form['username']
        rating=request.form['rating']
        assert name != ""
        utils.save_result(name,rating)
        flash("Name:%s Rating:%s"%(name,rating))
        return redirect(url_for('home'))
Beispiel #18
0
def train(model,
          dataloader,
          epochs,
          optimizer,
          criterion,
          save_output_every=10,
          save_model_every=50,
          only_show=False):
    device = utils.get_device()

    for epoch in tqdm(range(1, epochs + 1)):
        losses_per_epoch = []
        accuracies_per_epoch = []
        # go over all batches
        for step, (input, target) in enumerate(dataloader):
            model.train()
            input, target = input.to(device), target.to(device)

            target = utils.preprocess_target(target)
            pred = model(input)

            optimizer.zero_grad()
            loss = criterion(pred, target)
            loss.backward()
            optimizer.step()

            acc = utils.get_accuracy(pred, target)

            losses_per_epoch.append(loss.item())
            accuracies_per_epoch.append(acc)

        mean_loss = np.mean(losses_per_epoch)
        mean_acc = np.mean(accuracies_per_epoch)

        print(f'{"-"*30} Epoch {epoch} {"-"*30}')
        print('Loss: %.3f   Accuracy: %.3f' % (mean_loss, mean_acc))

        if epoch % save_output_every == 0:
            utils.save_result(epoch,
                              input,
                              pred,
                              target,
                              name='epoch',
                              only_show=only_show)

        if epoch % save_model_every == 0:
            utils.save_model(epoch, model)
Beispiel #19
0
def val_epoch(model,
              ratings,
              negs,
              K,
              ctx,
              output=None,
              epoch=None,
              processes=1):
    thistime = time.time()
    if epoch is None:
        print("Initial evaluation")
    else:
        print("Epoch {} evaluation".format(epoch))
    start = datetime.now()
    # model.eval()

    if processes > 1:
        context = mp.get_context('spawn')
        _eval_one = partial(eval_one, model=model, K=K, ctx=ctx)
        with context.Pool(processes=processes) as workers:
            zip(ratings, negs)
            hits_and_ndcg = workers.starmap(_eval_one, zip(ratings, negs))
        hits, ndcgs = zip(*hits_and_ndcg)
    else:
        hits, ndcgs = [], []
        for rating, items in zip(ratings, negs):
            hit, ndcg = eval_one(rating, items, model, K, ctx=ctx)
            hits.append(hit)
            ndcgs.append(ndcg)
    hits = np.array(hits, dtype=np.float32)
    ndcgs = np.array(ndcgs, dtype=np.float32)

    end = datetime.now()
    if output is not None:
        result = OrderedDict()
        result['timestamp'] = datetime.now()
        result['duration'] = end - start
        result['epoch'] = epoch
        result['K'] = K
        result['hit_rate'] = np.mean(hits)
        result['NDCG'] = np.mean(ndcgs)
        utils.save_result(result, output)
    print("epoch time:")
    print(time.time() - thistime)

    return hits, ndcgs
def main_experiment(env_parameter, agent_parameter, experiment_parameter):
    print "EXPERIMENT START"
    numRuns = experiment_parameter[0]
    numEpisodes = experiment_parameter[1]
    max_steps = experiment_parameter[2]

    average_steps = [0.0 for i in range(numEpisodes)]
    average_acc_reward = [0.0 for i in range(numEpisodes)]
    global numSteps, accumulated_reward

    for i in range(numRuns):
        print "Runs", i, "start..."
        task_env, task_agent = RL_init(env_parameter, agent_parameter)
        for j in range(numEpisodes):
            RL_epispdes(task_env, task_agent, max_steps)
            average_steps[j] += numSteps / (numRuns + 1.0)
            average_acc_reward[j] += accumulated_reward / (numRuns + 1.0)
        if i == numRuns - 1:
            print "test mode"
            test(task_env, task_agent, max_steps)

    utils.save_result(average_steps, "avg_steps.txt")
    utils.save_result(average_acc_reward, "avg_acc_reward.txt")
    np.save("Q.npy", task_agent.Q_value)

    plot_steps = plt.figure(1)
    plt.plot(average_steps)
    plt.ylabel('Steps')  #'# of steps'
    plt.xlabel('Episodes')  #'# of episodes'
    #plot_steps.show()

    plot_reward = plt.figure(2)
    plt.plot(average_acc_reward)
    plt.ylabel('Accumulated Rewards')  #'# of steps'
    plt.xlabel('Episodes')  #'# of episodes'
    #plot_reward.show()

    plt.show()

    print "EXPERIMENT END"

    pass
Beispiel #21
0
def train_model_sghmc(cfg, model, Xvalid, Yvalid, Xtest, Ytest, writer,
                      save_dir):
    mll_max = -np.inf
    best_iter = 0
    best_model = model
    for i in tdqm(range(cfg.train.iterations),
                  ascii=" .oO0",
                  bar_format="{total_time}: {percentage:.0f}%|{bar}{r_bar}"):
        model.sghmc_step()
        if i % cfg.train.train_hyper == 0:
            summary = model.train_hypers()
            writer.add_summary(summary, global_step=i)
        print("Iteration", i, end='\r')
        mll, sum_mll = model.print_sample_performance()
        print(f"MLL: {mll}")
        # set_trace()
        writer.add_summary(sum_mll, global_step=i)

        if np.round(mll - mll_max, decimals=5) > 0:
            print('MLL increased ({:.7f} --> {:.7f}). Updating values ....'.
                  format(mll_max, mll))
            mll_max = mll
            best_model = model
            best_iter = i

    if cfg.save_model:
        # save best model
        print(
            f'################## save best model at {save_dir} ##################'
        )
        model_name = f'{best_iter}_bestmodel_{mll_max}'
        best_model.save(save_dir, name=model_name)

    accuracy = measure_accuracy(best_model, Xvalid, Yvalid, collect=True)
    acc_mll_df = pd.DataFrame([accuracy], columns=['valid_accuracy'])
    acc_mll_df['mll'] = mll_max
    accuracy = measure_accuracy(best_model, Xtest, Ytest)
    acc_mll_df['test_accuracy'] = accuracy
    print(f"Best Model Test accuracy: {accuracy} with MLL {mll_max}")
    # export accuracy
    save_result(acc_mll_df, save_dir, name='_mll_accuracy')
Beispiel #22
0
def color_adjust(x, args):
    if args.iter % args.save_intervel == 0:
        save_result(x, args)
    args.iter += 1
    # Input for VGG
    x_vgg = np.asarray(np.reshape(x, args.shape), dtype=np.float32)
    x_vgg_var = Variable(chainer.dataset.concat_examples([x_vgg], args.gpu))

    # Poisson loss
    poisson_loss = F.mean_squared_error(
        (args.content_laplace + args.border_sum) * args.mask_var,
        F.convolution_2d(x_vgg_var * args.mask_var, W=args.W_laplace, pad=1) *
        args.mask_var)
    poisson_loss *= np.prod(x_vgg_var.shape)

    # tv loss
    tv_loss = total_variation(x_vgg_var)

    # Concent loss
    content_loss = 0
    x_features = extract({'data': x_vgg_var}, args.vgg, args.content_layers)
    x_features = {key: value[0] for key, value in x_features.items()}
    for layer in args.content_layers:
        content_loss += F.mean_squared_error(args.content_features[layer],
                                             x_features[layer])

    # Realism loss
    y = args.realism_cnn(x_vgg_var, dropout=False)
    b, _, w, h = y.shape
    xp = cuda.get_array_module(x_vgg_var.data)
    realism_loss = F.sum(y[:, 0, :, :])

    loss = args.poisson_weight * poisson_loss + args.realism_weight * realism_loss + args.tv_weight * tv_loss + args.content_weight * content_loss

    # Backward
    loss.backward()
    # Transfer loss & diff from GPU to CPU
    loss = cuda.to_cpu(loss.data)
    dx = np.squeeze(cuda.to_cpu(x_vgg_var.grad))

    return loss, np.asarray(dx.flatten(), dtype=np.float64)
Beispiel #23
0
def val_epoch(model, ratings, negs, K, use_cuda=True, output=None, epoch=None):
    start = datetime.now()
    hits, ndcgs = [], []
    for rating, items in zip(ratings, negs):
        hit, ndcg = eval_one(rating, items, model, K, use_cuda=use_cuda)
        hits.append(hit)
        ndcgs.append(ndcg)

    hits = np.array(hits, dtype=np.float32)
    ndcgs = np.array(ndcgs, dtype=np.float32)

    end = datetime.now()
    if output is not None:
        result = OrderedDict()
        result['timestamp'] = datetime.now()
        result['duration'] = end - start
        result['epoch'] = epoch
        result['K'] = K
        result['hit_rate'] = np.mean(hits)
        result['NDCG'] = np.mean(ndcgs)
        utils.save_result(result, output)

    return hits, ndcgs
Beispiel #24
0
        np.reshape(Theta[:hidden_layer_size * (input_layer_size + 1)], (hidden_layer_size, input_layer_size + 1), order='F'))
    Theta2 = np.matrix(
        np.reshape(Theta[hidden_layer_size * (input_layer_size + 1):], (num_labels, hidden_layer_size + 1), order='F'))

    p = fnx.predict(Theta1, Theta2, X)

    precision = 0
    for i in range(len(y)):
        if y[i] == p[i]:
            precision += 1

    print('Training Set Accuracy:', (1.0 * precision) / len(y))

    return Theta1, Theta2


if __name__ == '__main__':
    cuisine_list, ingredients_list, X, y = utl.load_train('number')

    ingredients_count = len(ingredients_list)
    cuisines_count = len(cuisine_list)

    Theta1, Theta2 = train_nn(ingredients_count, ingredients_count//16, cuisines_count, X, y)

    T, ids = utl.load_test(ingredients_list)

    p = fnx.predict(Theta1, Theta2, T)

    utl.save_result('nn', cuisine_list, p, ids)

Beispiel #25
0
def main(para):
        #data = DH.load_data()
        data = para['data']

        MODEL = para['do_what'] #hold_out  submission  cv tune
        CALIB = para['callib']
        BAG = para['bag']

        xgb_para = {'base_score': 0.5,
             'colsample_bylevel': 1,
             'colsample_bytree': 0.3,
             'gamma': 0,
             'learning_rate': 0.04,
             'max_delta_step': 0,
             'max_depth': 8,
             'min_child_weight': 5,
             'missing': None,
             'n_estimators': 2,
             'nthread': 4,
             'objective': 'binary:logistic',
             'reg_alpha': 0,
             'reg_lambda': 700,
             'scale_pos_weight': 15,
             'seed': 0,
             'silent': True,
             'subsample': 0.7}
       

        xgb_para['n_estimators'] = 1500

        clf = xgb.XGBClassifier(**xgb_para)

        if MODEL == 'tune':

            def objective(args):
                print args
                rf_para = {'bootstrap': True,
                 'class_weight': None,
                 'criterion': 'gini',
                 'max_depth': None,
                 'max_features': 'auto',
                 'max_leaf_nodes': None,

                 'min_samples_leaf': 1,
                 'min_samples_split': 2,
                 'min_weight_fraction_leaf': 0.0,

                 'n_estimators': 10,
                 'n_jobs': 1,
                 'oob_score': False,
                 'random_state': None,
                 'verbose': 0,
                 'warm_start': False}
                n_estimators,max_depth, max_features= args
                rf_para['n_estimators'] = int(n_estimators) + 1
                rf_para['max_depth'] = int(max_depth)
                rf_para['max_features'] = max_features
                rf_para['random_state'] = 4
                rf_para['n_jobs'] = 4
                rf_para['class_weight'] = 'balanced'
                clf = RandomForestClassifier(**rf_para)

                clf.fit(data['val_train_x'],data['val_train_y'])
                pred = clf.predict_proba(data['val_test_x'])
                score = roc_auc_score(data['val_test_y'],pred[:,1])*-1.

                rsv = {
                    'loss': score,
                    'status': STATUS_OK,
                    # -- store other results like this
                    'n_estimators': n_estimators,
                    'max_depth':max_depth,
                    }
                return rsv
            space = (
                hp.quniform('n_estimators',100,1000,100),
                hp.quniform('max_depth', 4, 10, 1),
                hp.quniform('max_features', 0.1,1., 0.2)
            )
            trials = Trials()

            best_sln = fmin(objective, space, algo=tpe.suggest, max_evals=40,trials=trials)
            rinfo = trials.results
            df = pd.DataFrame(rinfo)
            df.to_csv('./tune.csv',index=False)
            print best_sln

        if MODEL == 'submission':
            print 'working'
            if CALIB:

                tp = {}
                tp['base_estimator'] = clf
                tp['method'] = 'sigmoid'
                tp['cv'] = 5
                clf = CalibratedClassifierCV(**tp)


            if BAG:
                bagp = {'base_estimator': None,
                         'bootstrap': True,
                         'bootstrap_features': False,
                         'max_features': 1.0,
                         'max_samples': 1.0,
                         'n_estimators': 10,
                         'n_jobs': 1,
                         'oob_score': False,
                         'random_state': None,
                         'verbose': 0,
                         'warm_start': False}
                bagp['random_state'] = 4
                bagp['base_estimator'] = clf
                bagp['n_jobs'] = 4
                clf = BaggingClassifier(**bagp)
            
            print 'fiting'

            clf.fit(data['train_x'],data['train_y'])
            test_x = data['test'].drop('id',axis=1)
            p = clf.predict_proba(test_x)[:,0]
            test = data['test']
            test['score'] = p
            
            utils.save_result('xgb_pre.csv', test[['id','score']])
            logger.info('submission done!')



        if MODEL == 'hold_out':


            if CALIB:
                tp = {}
                tp['base_estimator'] = clf
                tp['method'] = 'sigmoid'
                tp['cv'] = 5
                tp['cv'] = StratifiedKFold(data['val_train_y'], n_folds=5,shuffle=True,\
                 random_state=4)

                clf = CalibratedClassifierCV(**tp)

            if BAG:
                bagp = {'base_estimator': None,
                         'bootstrap': True,
                         'bootstrap_features': False,
                         'max_features': 1.0,
                         'max_samples': 1.0,
                         'n_estimators': 10,
                         'n_jobs': 1,
                         'oob_score': False,
                         'random_state': None,
                         'verbose': 0,
                         'warm_start': False}
                bagp['random_state'] = 4
                bagp['base_estimator'] = clf
                bagp['n_jobs'] = 4
                clf = BaggingClassifier(**bagp)

            if not CALIB:
                clf.fit(data['val_train_x'],data['val_train_y'],verbose=True,
                eval_set=[(data['val_test_x'],data['val_test_y'])],eval_metric='auc',early_stopping_rounds=2100)
                print clf.best_score, clf.best_iteration
                rs = 'best_score %f, best_iteration %d ' %(clf.best_score, clf.best_iteration)
                logger.info(rs)
                logger.info('score %f, model: %s'%(clf.best_score, clf))

            else:
                clf.fit(data['val_train_x'],data['val_train_y'])
                p1 = clf.predict_proba(data['val_test_x'])[:,1]
                score = roc_auc_score(data['val_test_y'],p1)
                logger.info('score %f, model: %s'%(score,clf))
Beispiel #26
0
def main():
    parser = argparse.ArgumentParser(
        description='Modified Poisson image editing')
    parser.add_argument('--poisson_weight',
                        type=float,
                        default=1,
                        help='Weight for poisson loss')
    parser.add_argument('--content_weight',
                        type=float,
                        default=5e-4,
                        help='Weight for content loss')
    parser.add_argument('--tv_weight',
                        type=float,
                        default=1e-3,
                        help='Weight for tv loss')
    parser.add_argument('--n_iteration',
                        type=int,
                        default=3500,
                        help='# of iterations')
    parser.add_argument('--save_intervel',
                        type=int,
                        default=100,
                        help='save result every # of iterations')
    parser.add_argument('--rand_init',
                        type=lambda x: x == 'True',
                        default=True,
                        help='Random init input if True')

    parser.add_argument('--gpu',
                        type=int,
                        default=0,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument(
        '--data_root',
        default='/data1/wuhuikai/benchmark/TransientAttributes/imageCropped',
        help='Root folder for cropped transient attributes dataset')
    parser.add_argument('--load_size',
                        type=int,
                        default=224,
                        help='Scale image to load_size')
    parser.add_argument('--total_instance',
                        type=int,
                        default=175,
                        help='# of instance to run test')
    parser.add_argument('--seed', type=int, default=5, help='Random seed')
    parser.add_argument('--min_ratio',
                        type=float,
                        default=0.2,
                        help='Min ratio for size of random rect')
    parser.add_argument('--max_ratio',
                        type=float,
                        default=0.6,
                        help='Max ratio for size of random rect')
    parser.add_argument('--result_folder',
                        default='transient_attributes_result/modified_result',
                        help='Name for folder storing results')
    parser.add_argument('--result_name',
                        default='loss.txt',
                        help='Name for file saving loss change')
    args = parser.parse_args()

    print('Input arguments:')
    for key, value in vars(args).items():
        print('\t{}: {}'.format(key, value))
    print('')

    args.prefix_name = '_'.join(
        sorted([
            '{}({})'.format(key, value) for key, value in vars(args).items()
            if key in set([
                'poisson_weight', 'realism_weight', 'content_weight',
                'tv_weight', 'n_iteration', 'rand_init'
            ])
        ]))

    # Init image list
    print('Load images from {} ...'.format(args.data_root))
    folders = [
        folder for folder in os.listdir(args.data_root)
        if os.path.isdir(os.path.join(args.data_root, folder))
    ]
    imgs_each_folder = {
        folder: glob.glob(os.path.join(args.data_root, folder, '*'))
        for folder in folders
    }
    print('\t {} images in {} folders in total ...\n'.format(
        np.sum([len(v) for k, v in imgs_each_folder.items()]), len(folders)))

    # Init result folder
    if not os.path.isdir(args.result_folder):
        os.makedirs(args.result_folder)
    print('Result will save to {} ...\n'.format(args.result_folder))

    # Init Constant Variable
    args.W_laplace = Variable(make_kernel(
        3, 3,
        np.asarray([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]],
                   dtype=np.float32)),
                              volatile='auto')
    args.W_laplace.to_gpu()

    loss_change = []
    np.random.seed(args.seed)
    for i in range(args.total_instance):
        folder = np.random.choice(folders)
        print('Processing {}/{}, select folder {} ...'.format(
            i + 1, args.total_instance, folder))
        obj_path, bg_path = np.random.choice(imgs_each_folder[folder],
                                             2,
                                             replace=False)
        print('\tObj: {}, Bg: {} ...'.format(os.path.basename(obj_path),
                                             os.path.basename(bg_path)))

        obj_img = imread(obj_path)
        args.orig_size = obj_img.shape[:2]
        obj_vgg = im_preprocess_vgg(obj_img, args.load_size, dtype=np.float32)
        bg_vgg = im_preprocess_vgg(imread(bg_path),
                                   args.load_size,
                                   dtype=np.float32)
        args.shape = obj_vgg.shape

        # random rect
        w, h = np.asarray(
            np.random.uniform(args.min_ratio, args.max_ratio, 2) *
            args.load_size,
            dtype=np.uint32)
        sx, sy = np.random.randint(0, args.load_size - w), np.random.randint(
            0, args.load_size - h)

        expand_mask = np.zeros((1, args.load_size, args.load_size),
                               dtype=np.float32)
        expand_mask[:, sx:sx + w, sy:sy + h] = 1
        mask = np.zeros_like(expand_mask)
        mask[:, sx + 1:sx + w - 1, sy + 1:sy + h - 1] = 1
        inverse_mask = 1 - mask
        ## vars
        obj_var = Variable(chainer.dataset.concat_examples([obj_vgg],
                                                           args.gpu),
                           volatile='on')
        bg_var = Variable(chainer.dataset.concat_examples([bg_vgg], args.gpu),
                          volatile='auto')
        mask_var = F.broadcast_to(
            Variable(chainer.dataset.concat_examples([mask], args.gpu),
                     volatile='auto'), obj_var.shape)
        inverse_mask_var = F.broadcast_to(
            Variable(chainer.dataset.concat_examples([inverse_mask], args.gpu),
                     volatile='auto'), obj_var.shape)
        inverse_border_mask_var = F.broadcast_to(
            Variable(chainer.dataset.concat_examples([1 - expand_mask + mask],
                                                     args.gpu),
                     volatile='auto'), obj_var.shape)
        ## Laplace
        content_laplace = F.convolution_2d(
            obj_var, W=args.W_laplace, pad=1) * mask_var + F.convolution_2d(
                bg_var, W=args.W_laplace, pad=1) * inverse_mask_var
        content_laplace.volatile = 'off'

        copy_paste_vgg = obj_vgg * mask + bg_vgg * inverse_mask
        ## args
        args.content_laplace = content_laplace
        args.mask = mask
        args.inverse_mask = inverse_mask
        args.inverse_mask_var = inverse_mask_var
        args.inverse_border_mask_var = inverse_border_mask_var
        args.bg_vgg = bg_vgg
        args.bg_var = bg_var
        args.copy_paste_vgg = copy_paste_vgg
        args.im_name = 'folder_{}_obj_{}_bg_{}'.format(
            folder,
            os.path.splitext(os.path.basename(obj_path))[0],
            os.path.splitext(os.path.basename(bg_path))[0])

        args.iter = 0
        x_init = np.asarray(
            np.random.randn(*args.shape) * 0.001,
            dtype=np.float32) if args.rand_init else np.copy(copy_paste_vgg)
        print('\tOptimize start ...')
        res = minimize(color_adjust,
                       x_init,
                       args=(args),
                       method='L-BFGS-B',
                       jac=True,
                       options={
                           'maxiter': args.n_iteration,
                           'disp': True
                       })
        # Cut and paste loss
        args.iter = -1
        f0, _ = color_adjust(copy_paste_vgg, args)
        print('\tOptimize done, loss = {} from {}\n'.format(res.fun, f0))
        loss_change.append((args.im_name, f0, res.fun))

        args.iter = ''
        save_result(res.x, args)

    with open(os.path.join(args.result_folder, args.result_name), 'w') as f:
        for name, f0, fb in loss_change:
            f.write('{} {} {}\n'.format(name, f0, fb))
Beispiel #27
0
                            print_freq=10,
                            verbose=True)
    # sa.find(stop_fun=lambda fitness: fitness == fp.max_possible_fit)
    result_record.append(sa.find())
    result = nn.evaluate_on_all_datasets(sa)
    result_train.append(result[0])
    result_val.append(result[1])
    result_test.append(result[2])
print("takes %d seconds" % (datetime.now() - start_time).seconds)
print("Averaged train result: %f" % np.mean(result_train))
print("Averaged val result: %f" % np.mean(result_val))
print("Averaged test result: %f" % np.mean(result_test))
plt.plot(range(10, 50001, 10), np.mean(result_record, axis=0))
plt.savefig("fig/nn_sa_9995")
plt.show()
save_result(result_record, "nn_sa_9995")

# sa = SimulatedAnnealing(params, neighbor_nn, nn, max_try_per_step=10000, max_iter=4000, temperature_decay=0.997)
# sa.find()
# nn.evaluate_on_all_datasets(sa)

# def crossover(params):
#     first, second = params
#     offspring1 = {}
#     offspring2 = {}
#     for weight_name in first:
#         ran = np.random.random(size=first[weight_name].shape)
#         offspring1[weight_name] = np.where(ran < 0.5, first[weight_name], second[weight_name])
#         offspring2[weight_name] = np.where(ran > 0.5, first[weight_name], second[weight_name])
#     return offspring1, offspring2
#
Beispiel #28
0
y = tf.nn.softmax(tf.matmul(x, W) + b)

y_ = tf.placeholder(tf.float32, [None, cuisine_count])

t = tf.placeholder(tf.float32, [None, ingredients_count])

p = tf.nn.softmax(tf.matmul(t, W) + b)

cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy)
# train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

init = tf.initialize_all_variables()

sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
sess.run(init)

for d in ['/cpu:0', '/gpu:0']:
    with tf.device(d):
        for i in range(100):
            batch_xs, batch_ys = utl.next_batch(xs, ys, 12000)
            sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print(sess.run(accuracy, feed_dict={x: xs, y_: ys}))

cls = sess.run(p, feed_dict={t: ts})
utl.save_result('tf_gd', cuisine_list, np.argmax(cls, axis=1).tolist(), ids, 'number')
Beispiel #29
0
y_ = tf.placeholder(tf.float32, [None, cuisine_count])

t = tf.placeholder(tf.float32, [None, ingredients_count])

p = tf.nn.softmax(tf.matmul(t, W) + b)

cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy)
# train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

init = tf.initialize_all_variables()

sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
sess.run(init)

for d in ['/cpu:0', '/gpu:0']:
    with tf.device(d):
        for i in range(100):
            batch_xs, batch_ys = utl.next_batch(xs, ys, 12000)
            sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print(sess.run(accuracy, feed_dict={x: xs, y_: ys}))

cls = sess.run(p, feed_dict={t: ts})
utl.save_result('tf_gd', cuisine_list,
                np.argmax(cls, axis=1).tolist(), ids, 'number')
Beispiel #30
0
def main():
    parser = argparse.ArgumentParser(
        description='Poisson image editing using RealismCNN')
    parser.add_argument('--poisson_weight',
                        type=float,
                        default=1,
                        help='Weight for poisson loss')
    parser.add_argument('--realism_weight',
                        type=float,
                        default=1e4,
                        help='Weight for realism loss')
    parser.add_argument('--content_weight',
                        type=float,
                        default=1,
                        help='Weight for content loss')
    parser.add_argument('--tv_weight',
                        type=float,
                        default=1e-1,
                        help='Weight for tv loss')
    parser.add_argument('--n_iteration',
                        type=int,
                        default=1000,
                        help='# of iterations')
    parser.add_argument('--save_intervel',
                        type=int,
                        default=100,
                        help='save result every # of iterations')
    parser.add_argument('--rand_init',
                        type=lambda x: x == 'True',
                        default=True,
                        help='Random init input if True')
    parser.add_argument('--content_layers',
                        type=str2list,
                        default='conv4_1',
                        help='Layers for content_loss, sperated by ;')

    parser.add_argument('--gpu',
                        type=int,
                        default=0,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--realism_model_path',
                        default='model/realismCNN_all_iter3.npz',
                        help='Path for pretrained Realism model')
    parser.add_argument('--content_model_path',
                        default='model/VGG_ILSVRC_19_layers.pkl',
                        help='Path for pretrained VGG model')
    parser.add_argument(
        '--data_root',
        default='/data1/wuhuikai/benchmark/Realistic/color_adjustment',
        help='Root folder for color adjustment dataset')
    parser.add_argument('--img_folder',
                        default='pngimages',
                        help='Folder for stroing images')
    parser.add_argument('--list_name',
                        default='list.txt',
                        help='Name for file storing image list')
    parser.add_argument('--load_size',
                        type=int,
                        default=224,
                        help='Scale image to load_size')
    parser.add_argument('--result_folder',
                        default='image_editing_result',
                        help='Name for folder storing results')
    parser.add_argument('--result_name',
                        default='loss.txt',
                        help='Name for file saving loss change')
    args = parser.parse_args()

    args.content_layers = set(args.content_layers)

    print('Input arguments:')
    for key, value in vars(args).items():
        print('\t{}: {}'.format(key, value))
    print('')

    args.prefix_name = '_'.join(
        sorted([
            '{}({})'.format(key, value) for key, value in vars(args).items()
            if key not in set([
                'realism_model_path', 'content_model_path', 'data_root',
                'img_folder', 'list_name', 'result_folder', 'result_name'
            ])
        ]))

    # Init CNN model
    realism_cnn = RealismCNN()
    print('Load pretrained Realism model from {} ...'.format(
        args.realism_model_path))
    serializers.load_npz(args.realism_model_path, realism_cnn)
    print('Load pretrained VGG model from {} ...\n'.format(
        args.content_model_path))
    with open(args.content_model_path, 'rb') as f:
        vgg = pickle.load(f)
    if args.gpu >= 0:
        chainer.cuda.get_device(args.gpu).use()  # Make a specified GPU current
        realism_cnn.to_gpu()  # Copy the model to the GPU
        vgg.to_gpu()

    # Init image list
    im_root = os.path.join(args.data_root, args.img_folder)
    print('Load images from {} according to list {} ...'.format(
        im_root, args.list_name))
    with open(os.path.join(args.data_root, args.list_name)) as f:
        im_list = f.read().strip().split('\n')
    total = len(im_list)
    print('{} images loaded done!\n'.format(total))

    # Init result folder
    if not os.path.isdir(args.result_folder):
        os.makedirs(args.result_folder)
    print('Result will save to {} ...\n'.format(args.result_folder))

    # Init Constant Variable
    W_laplace = Variable(make_kernel(
        3, 3,
        np.asarray([[0, -1, 0], [-1, 4, -1], [0, -1, 0]], dtype=np.float32)),
                         volatile='auto')
    W_laplace.to_gpu()
    args.W_laplace = W_laplace
    W_sum = Variable(make_kernel(
        3, 3, np.asarray([[0, 1, 0], [1, 0, 1], [0, 1, 0]], dtype=np.float32)),
                     volatile='auto')
    W_sum.to_gpu()

    loss_change = []
    for idx, im_name in enumerate(im_list):
        print('Processing {}/{}, name = {} ...'.format(idx + 1, total,
                                                       im_name))
        obj_vgg = im_preprocess_vgg(imread(
            os.path.join(im_root, '{}_obj.png'.format(im_name))),
                                    args.load_size,
                                    dtype=np.float32)
        bg_vgg = im_preprocess_vgg(imread(
            os.path.join(im_root, '{}_bg.png'.format(im_name))),
                                   args.load_size,
                                   dtype=np.float32)
        expand_mask = im_preprocess_vgg(imread(
            os.path.join(im_root, '{}_softmask.png'.format(im_name))),
                                        args.load_size,
                                        sub_mean=False,
                                        dtype=np.uint8,
                                        preserve_range=False)

        args.orig_size = (args.load_size, args.load_size)
        args.shape = bg_vgg.shape
        ## mask
        mask = erosion(np.squeeze(expand_mask), np.ones((3, 3),
                                                        dtype=np.uint8))
        mask = np.asarray(mask[np.newaxis, :, :], dtype=np.float32)
        expand_mask = np.asarray(expand_mask, dtype=np.float32)
        inverse_mask = 1 - mask
        ## vars
        obj_var = Variable(chainer.dataset.concat_examples([obj_vgg],
                                                           args.gpu),
                           volatile='on')
        mask_var = F.broadcast_to(
            Variable(chainer.dataset.concat_examples([mask], args.gpu)),
            obj_var.shape)
        ## Laplace
        content_laplace = F.convolution_2d(obj_var, W=W_laplace, pad=1)
        content_laplace.volatile = 'off'
        # prefilled
        border = bg_vgg * expand_mask * inverse_mask
        border_var = Variable(chainer.dataset.concat_examples([border],
                                                              args.gpu),
                              volatile='on')
        border_sum = F.convolution_2d(border_var, W=W_sum, pad=1)
        border_sum.volatile = 'off'

        print('\tExtracting content image features ...')
        copy_paste_vgg = obj_vgg * mask + bg_vgg * inverse_mask
        copy_paste_var = Variable(chainer.dataset.concat_examples(
            [copy_paste_vgg], args.gpu),
                                  volatile='on')
        content_features = extract({'data': copy_paste_var}, vgg,
                                   args.content_layers)
        content_features = {
            key: value[0]
            for key, value in content_features.items()
        }
        for _, value in content_features.items():
            value.volatile = 'off'

        ## args
        args.vgg = vgg
        args.realism_cnn = realism_cnn
        args.border_sum = border_sum
        args.content_laplace = content_laplace
        args.content_features = content_features
        args.mask = mask
        args.mask_var = mask_var
        args.inverse_mask = inverse_mask
        args.bg_vgg = bg_vgg
        args.copy_paste_vgg = copy_paste_vgg
        args.im_name = im_name

        args.iter = 0
        x_init = np.asarray(
            np.random.randn(*args.shape) * 0.001,
            dtype=np.float32) if args.rand_init else np.copy(copy_paste_vgg)
        print('\tOptimize start ...')
        res = minimize(color_adjust,
                       x_init,
                       args=(args),
                       method='L-BFGS-B',
                       jac=True,
                       options={
                           'maxiter': args.n_iteration,
                           'disp': False
                       })
        # Cut and paste loss
        args.iter = -1
        f0, _ = color_adjust(copy_paste_vgg, args)
        print('\tOptimize done, loss = {} from {}\n'.format(res.fun, f0))
        loss_change.append((im_name, f0, res.fun))

        args.iter = ''
        save_result(res.x, args)

    with open(os.path.join(args.result_folder, args.result_name), 'w') as f:
        for name, f0, fb in loss_change:
            f.write('{} {} {}\n'.format(name, f0, fb))
Beispiel #31
0
    def test_save_result(self, makedirs_mock, savefig_mock):
        makedirs_mock.return_value = None
        savefig_mock.return_value = None

        save_result(self.z_k_value, self.iteration, 'distribution_name')
from sklearn.multiclass import OneVsRestClassifier
from sklearn.linear_model import LogisticRegression
import utils as utl


cuisine_list, ingredients_list, x, y = utl.load_train('number')
classifier = OneVsRestClassifier(LogisticRegression(C=1e6)).fit(x, y)
p = classifier.predict(x)

precision = 0
for i in range(len(y)):
    if y[i] == p[i]:
        precision += 1
accuracy = (1.0 * precision) / len(y)

print('Training Set Accuracy:', accuracy)

t, ids = utl.load_test(ingredients_list)
p = classifier.predict(t)
utl.save_result('sk_lr', cuisine_list, p, ids, 'number')


Beispiel #33
0
        project_name = "itext"
    elif repo_id == "6":
        project_name = "spring"
    elif repo_id == "7":
        project_name = "lucene"
    else:
        print "repository id not exist"

    try:
        db = Database(user, passwd, database)
        cnn = db.connect()
        cursor = cnn.cursor()
        if no_parse != True:
            change_info = get_change_line(cursor, db, start_datetime, end_datetime, repo_id, change_type)
            save_file = change_file_path + change_type + "_" + project_name + ".csv"
            save_result(change_info, save_file)
            change_metric = get_change_metric(cursor, db, previous_datetime, start_datetime, repo_id, change_type)
            changemetric_file = changemetric_file_path + change_type + "_" + project_name + ".csv"
            save_result(change_metric, changemetric_file)
        cnn.close()
        if extensions != None:
            emg = _get_extensions_manager(extensions)
            change_file = change_file_path + change_type + "_" + project_name + ".csv"
            changemetric_file = changemetric_file_path + change_type + "_" + project_name + ".csv"
            emg.run_extensions(
                change_file, understand_file, stat_file, rank_file, changemetric_file, predict_file, change_type
            )
    #        print "Finish. Repository: "+ repo_id +", time: " + start_time+"-" +end_time+", bug: "+str(buggy) + ", Num_hunks: "+str(total_num_hunk)
    except MySQLdb.Error, e:
        print "Error %d: %s" % (e.args[0], e.args[1])
Beispiel #34
0
           torch.mean(torch.FloatTensor(G_losses))))

    ########## test ##########
    G.eval()  # stop train and start test
    z = torch.randn((5*5, 100)).view(-1, 100, 1, 1)
    if GPU_MODE:
        z = Variable(z.cuda(), volatile=True)
    else:
        z = Variable(z, volatile=True)
    random_image = G(z)
    fixed_image = G(fixed_z)
    G.train()  # stop test and start train

    p = DIR + '/Random_results/MNIST_GAN_' + str(epoch + 1) + '.png'
    fixed_p = DIR + '/Fixed_results/MNIST_GAN_' + str(epoch + 1) + '.png'
    utils.save_result(random_image, (epoch+1), save=True, path=p)
    utils.save_result(fixed_image, (epoch+1), save=True, path=fixed_p)
    train_hist['D_losses'].append(torch.mean(torch.FloatTensor(D_losses)))
    train_hist['G_losses'].append(torch.mean(torch.FloatTensor(G_losses)))
    train_hist['per_epoch_times'].append(per_epoch_time)

end_time = time()
total_time = end_time - end_time
print("Avg per epoch time: %.2f, total %d epochs time: %.2f" % (torch.mean(torch.FloatTensor(train_hist['per_epoch_times'])), EPOCH, total_time))
print("Training finish!!!...")

# save parameters
torch.save(G.state_dict(), DIR + "/generator_param.pkl")
torch.save(D.state_dict(), DIR + "/discriminator_param.pkl")

# save history
from sklearn.multiclass import OneVsRestClassifier
from sklearn.linear_model import LogisticRegression
import utils as utl

cuisine_list, ingredients_list, x, y = utl.load_train('number')
classifier = OneVsRestClassifier(LogisticRegression(C=1e6)).fit(x, y)
p = classifier.predict(x)

precision = 0
for i in range(len(y)):
    if y[i] == p[i]:
        precision += 1
accuracy = (1.0 * precision) / len(y)

print('Training Set Accuracy:', accuracy)

t, ids = utl.load_test(ingredients_list)
p = classifier.predict(t)
utl.save_result('sk_lr', cuisine_list, p, ids, 'number')
Beispiel #36
0
                         shuffle=True,
                         random_state=4891)
    for i in range(1, MAX_POWER + 1):
        clf = KNeighborsClassifier(n_neighbors=2**i, n_jobs=1)
        clf = CalibratedClassifierCV(clf, method='isotonic', cv=kf)
        utils.VJUH(X_train,
                   X_test,
                   y_train,
                   clf,
                   MODEL_NAME + str(i),
                   'ensemble',
                   stratify_labels=utils.load_stratify_labels())

    train = []
    test = []
    for i in range(1, MAX_POWER + 1):
        tr, ts, _ = utils.load_data(data_name=MODEL_NAME + str(i),
                                    data_folder='ensemble')
        train.append(tr)
        test.append(ts)
    result = pd.concat(train, axis=1, ignore_index=True)
    utils.save_result(result.values,
                      MODEL_NAME[:-1],
                      data_folder='ensemble',
                      test=False)
    result = pd.concat(test, axis=1, ignore_index=True)
    utils.save_result(result.values,
                      MODEL_NAME[:-1],
                      data_folder='ensemble',
                      test=True)